MySQL ORDER BY
Sorting query results in ascending or descending order
📊 What is ORDER BY?
The ORDER BY clause sorts query results based on one or more columns. You can sort data in ascending (ASC) or descending (DESC) order, making it easy to organize and analyze your data effectively.
-- Sort users by age
SELECT * FROM users
ORDER BY age ASC;
Sorting Options
Ascending Order
Sort from lowest to highest (default)
Descending Order
Sort from highest to lowest
Multiple Columns
Sort by multiple fields
Column Position
Sort by column number
🔹 Basic ORDER BY Syntax
The ORDER BY clause comes after the FROM and WHERE clauses in a SELECT statement. By default, ORDER BY sorts in ascending order. You can explicitly specify ASC for ascending or DESC for descending order to control how your data is organized in the result set.
-- Ascending order (default)
SELECT name, age FROM users
ORDER BY age;
-- Explicit ascending order
SELECT name, age FROM users
ORDER BY age ASC;
Sample Output:
| name | age |
|---|---|
| Emma | 22 |
| John | 25 |
| Sarah | 30 |
🔹 Descending Order
Use the DESC keyword to sort results from highest to lowest. This is particularly useful when you want to see the largest values first, such as finding the most expensive products, newest dates, or highest scores. Descending order reverses the natural sorting sequence for better data analysis.
-- Sort by age from highest to lowest
SELECT name, age FROM users
ORDER BY age DESC;
-- Sort products by price (highest first)
SELECT product_name, price FROM products
ORDER BY price DESC;
Sample Output:
| name | age |
|---|---|
| Sarah | 30 |
| John | 25 |
| Emma | 22 |
🔹 Sorting by Multiple Columns
You can sort by multiple columns by separating them with commas. The first column is the primary sort, and subsequent columns are used as tiebreakers when values in the previous column are equal. This creates a hierarchical sorting structure for more organized and meaningful data presentation.
-- Sort by country, then by age
SELECT name, country, age FROM users
ORDER BY country ASC, age DESC;
-- Multiple columns with different orders
SELECT product_name, category, price FROM products
ORDER BY category ASC, price DESC;
Sample Output:
| name | country | age |
|---|---|---|
| Emma | Canada | 22 |
| Sarah | USA | 30 |
| John | USA | 25 |
🔹 Sorting Alphabetically
ORDER BY works with text columns to sort alphabetically. Ascending order sorts from A to Z, while descending order sorts from Z to A. This is essential for organizing names, titles, categories, and any text-based data in a logical and readable sequence for better user experience.
-- Sort names alphabetically
SELECT name FROM users
ORDER BY name ASC;
-- Sort in reverse alphabetical order
SELECT product_name FROM products
ORDER BY product_name DESC;
Sample Output:
| name |
|---|
| Emma |
| John |
| Sarah |
🔹 ORDER BY with WHERE Clause
You can combine ORDER BY with WHERE to filter and sort data simultaneously. The WHERE clause filters the rows first, and then ORDER BY sorts the filtered results. This powerful combination allows you to create precise queries that both select specific data and present it in your desired order.
-- Filter and sort
SELECT name, age FROM users
WHERE age > 20
ORDER BY age DESC;
-- Complex filtering with sorting
SELECT product_name, price FROM products
WHERE category = 'Electronics' AND price < 1000
ORDER BY price ASC;
Sample Output:
| name | age |
|---|---|
| Sarah | 30 |
| John | 25 |
| Emma | 22 |