This lesson on ORDER BY is hands-on and example-driven. You will be able to structure SQL queries to sort result sets based on specific column values. You can control the sort direction (ascending or descending) and apply sorting across multiple columns simultaneously. This ensures data is presented exactly how you need it for analysis or reporting.
What You'll Be Able To Do
- Write a basic query to sort results using a single column.
- Specify the sort direction explicitly using DESC.
- Demonstrate how ORDER BY handles both numeric and text data types.
- Construct a query that sorts data based on two or more columns.
- Combine ascending and descending orders within a multi-column sort.
Topics Covered in ORDER BY
- Introduction (0:00 - 0:15) — The ORDER BY keyword is introduced as a powerful tool for sorting data results.
- Basic Sorting (0:15 - 0:30) — A simple query demonstrates sorting the products table by the price column.
- Default Order (0:30 - 0:45) — By default, ORDER BY sorts data in ascending order (ASC).
- Descending Order (0:45 - 1:00) — The DESC keyword is used to sort results from highest to lowest values.
- Text Sorting (1:00 - 1:15) — ORDER BY works on text values, sorting them alphabetically.
- Multi-Column Sort (1:15 - 1:35) — Data can be sorted by a primary column and then a secondary column within those groups.
- Mixed Directions (1:35 - 1:50) — It is possible to combine ASC and DESC within a single multi-column sort list.
- Conclusion (1:50 - 2:00) — The lesson concludes by summarizing the flexibility of the ORDER BY clause for data organization.
SQL Cheat Sheet
-
SELECT * FROM <table> ORDER BY <col>;— Sorts the result set by column in ascending order (default)SELECT * FROM products ORDER BY price; -
DESC— Keyword used to specify descending sort order (Z-A, 9-0)SELECT * FROM products ORDER BY price DESC; -
ASC— Keyword used to explicitly specify ascending sort order (A-Z, 0-9)SELECT * FROM products ORDER BY price ASC; -
ORDER BY <col1>, <col2>;— Sorts first by col1, then secondarily by col2 within col1 groupsSELECT * FROM customers ORDER BY country, customer name; -
ORDER BY <col> DESC;— Sorts text values alphabetically in reverse orderSELECT * FROM products ORDER BY product name DESC; -
ORDER BY <col1> ASC, <col2> DESC;— Applies mixed sort directions across multiple columnsSELECT * FROM customers ORDER BY country ASC, customer name DESC;
Comparison Table
| Sort Direction | ASC (Default) | DESC |
|---|---|---|
| Numeric Sort | Low to High (0-9). | High to Low (9-0). |
| Text Sort | Alphabetical (A-Z). | Reverse Alphabetical (Z-A). |
| Keyword Required | Optional; it is the default. | Required to reverse order. |
Common Pitfalls
- Mistake: Assuming ORDER BY sorts descending by default. Avoid: Use the DESC keyword to guarantee descending order.
- Mistake: Expecting DESC to apply to all columns in a multi-sort. Avoid: DESC only applies to the column immediately preceding it.
- Mistake: Sorting by multiple columns and forgetting the primary sort order. Avoid: The first column listed dictates the primary sort grouping.
FAQs
- What is the default sort order if I don't specify ASC or DESC? The default sort order is ascending (ASC), meaning lowest values or A-Z come first.
- How do I sort by the highest prices first? Use the DESC keyword immediately following the column name, such as ORDER BY price DESC.
- Can I sort by both country and customer name? Yes, list them separated by commas; the first column determines the primary sort.
- Can I mix ascending and descending orders in one query? Yes, specify ASC or DESC individually after each column name in the list.