ORDER BY Clause

From rbachwiki
Jump to navigation Jump to search
  • When sorting in ascending order, values are listed in this order:

1. Blank and special characters 2. Numeric values 3. Character values (uppercase first) 4. NULL values

  • Unless you specify “DESC” for descending, the ORDER BY clause sorts in ascending order by default.
SELECT * 
FROM publisher
ORDER BY name DESC

You can also use the ORDER BY clause with the optional NULLS FIRST or NULLS LAST keywords to change the order for listing NULL values. By default, NULL values are listed last when results are sorted in ascending order and first when they’re sorted in descending order.

SELECT lastname, firstname, state, referred
FROM customers
WHERE  state = 'CA'
ORDER BY referred NULLS FIRST;

Secondary Sort

SELECT lastname, firstname, state, city
FROM customers
ORDER BY state DESC, city;

Reverencing Positions of sort columns in the ORDER BY

SELECT lastname, firstname, state, city
FROM customers
ORDER BY 3 DESC, 4;

Back To Top- Home - Category