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.