SQL ORDER BY
The SQL ORDER BY
command allows you to order a given result set by one or more columns.
The default sorting order order is ascending [1, 2, 3]
, but can be made descending [3, 2, 1]
with the DESC
keyword.
-- Works in PostgreSQL, MySQL, SQL Server, and Oracle
SELECT
*
FROM
rent_payments
ORDER BY
dollar_amount DESC;
rent_payments
table
building_id | payment_date | dollar_amount |
---|---|---|
1 | 3/1/2018 | 2000 |
1 | 3/2/2018 | 1500 |
2 | 3/1/2018 | 1800 |
2 | 3/3/2018 | 1900 |
1 | 4/2/2018 | 2000 |
1 | 4/2/2018 | 1500 |
2 | 4/1/2018 | 1800 |
2 | 4/2/2018 | 1900 |
Query results
building_id | payment_date | dollar_amount |
---|---|---|
1 | 3/1/2018 | 2000 |
1 | 4/2/2018 | 2000 |
2 | 3/3/2018 | 1900 |
2 | 4/2/2018 | 1900 |
2 | 3/1/2018 | 1800 |
2 | 4/1/2018 | 1800 |
1 | 3/2/2018 | 1500 |
1 | 4/2/2018 | 1500 |