Let us learn "ORDER BY … USING" clause in PostgreSQL

Niladri.Biswas
Posted by in PostgreSQL category on for Beginner level | Points: 250 | Views : 3695 red flag

In this article we will explore "ORDER BY … USING" clause in PostgreSQL

Introduction

ORDER BY ... USING is an interesting look under the hood of PostgreSQL.It is a short hand form of applying the "Ascending" or "Descending" clause in conjunction with the "Order By" clause.

Straight to example

Let us create a table and populate some values to it as shown under

CREATE TABLE MyTestTable
(
  Id serial NOT NULL,
  "Number" integer
)
Insert Into MyTestTable("Number") values (10),(20),(30),(0),(-10);

If we now query as

SELECT * 
FROM MyTestTable 
ORDER BY Number USING >

The result will be

30=>20=>10=>0=>-10

If we now query as

SELECT * 
FROM MyTestTable 
ORDER BY Number USING <

The result will be

-10=>0=>10=>20=>30

An ordering operator must be a less-than or greater-than member of some B-tree operator family.ASC is usually equivalent to USING *gt;and DESC is usually equivalent to USING >.

Conclusion

In this article we have learnt "ORDER BY … USING" clause in PostgreSQL.Hope this will be helpful.Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)