Difference between revisions of "ORDER BY Clause"
Jump to navigation
Jump to search
(Created page with " *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 * Un...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
FROM publisher | FROM publisher | ||
ORDER BY name DESC | 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; | |||
[[#Rules for Dates|Back To Top]]-[[Main_Page| Home]] - [[Oracle_SQL|Category]] | [[#Rules for Dates|Back To Top]]-[[Main_Page| Home]] - [[Oracle_SQL|Category]] |
Latest revision as of 20:15, 24 October 2017
- 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;