SQL Notes
Learn how to sort query results using the SQL ORDER BY clause, arrange records in ascending or descending order, sort by multiple columns, and improve data presentation.
Raw database output rarely makes sense. You need things organized � alphabetically, numerically, by date, whatever fits your story.
Say you need to:
- Rank students by their grades. (highest first)
- Show products cheapest to most expensive.
- List people by what they earn.
- Show transactions in order, newest first.
Unsorted data is noise. With thousands of records, finding what matters is like a needle in a haystack.
To solve this problem, SQL provides the ORDER BY Clause.
ORDER BY sorts your results. Ascending or descending, single column or many. You pick the story you want to tell.
Let's learn how to tell that story with data.
Why is ORDER BY Important?
Imagine you're running a store with:
10,000 Products
Without sorting:
Laptop
Mouse
Phone
Keyboard
MonitorThe order may appear random.
With ORDER BY:
SELECT *
FROM Products
ORDER BY Price;Now you see the logic � cheapest first.
Why it matters:
- Users find what they want faster
- Reports tell a real story
- Data analysis actually works
- You look professional
Basic ORDER BY Syntax
Here's the pattern:
SELECT ColumnName
FROM TableName
ORDER BY ColumnName;Watch:
SELECT *
FROM Students
ORDER BY Name;This sorts records alphabetically by name.
Understanding the Syntax
Watch:
SELECT *
FROM Students
ORDER BY Age;SELECT
Specifies data to retrieve.
FROM
Specifies the source table.
ORDER BY
Specifies the sorting operation.
Age
Column used for sorting.
The database arranges records according to the values in the specified column.
Default Sorting Order
ORDER BY defaults to ascending � smallest to biggest, A to Z.
Watch:
SELECT *
FROM Students
ORDER BY Age;Result:
18 20 22 25
Ascending =
- Smallest to largest
- A to Z
- Oldest date to newest date
Ascending Order (ASC)
The ASC keyword explicitly specifies ascending order.
Watch:
SELECT *
FROM Students
ORDER BY Age ASC;Get:
| Name | Age |
|---|---|
| Amit | 18 |
| Rahul | 20 |
| Priya | 22 |
ASC is redundant � that's the default anyway.
Descending Order (DESC)
The DESC keyword sorts data in descending order.
Watch:
SELECT *
FROM Students
ORDER BY Age DESC;Get:
| Name | Age |
|---|---|
| Priya | 22 |
| Rahul | 20 |
| Amit | 18 |
Descending =
- Largest to smallest
- Z to A
- Newest date to oldest date
Sorting Text Values
Watch:
SELECT *
FROM Students
ORDER BY Name;Result:
Amit Priya Rahul
Text values are sorted alphabetically.
Sorting Numeric Values
Watch:
SELECT *
FROM Products
ORDER BY Price;Result:
500 1000 2500 5000
Numbers are sorted from smallest to largest.
Sorting Dates
Watch:
SELECT *
FROM Orders
ORDER BY OrderDate;Result:
2025-01-01 2025-01-05 2025-01-10
Older dates appear first.
To display newest dates first:
SELECT *
FROM Orders
ORDER BY OrderDate DESC;Sorting by Multiple Columns
You can sort by multiple columns. Primary sort, then secondary, etc.
Watch:
SELECT *
FROM Students
ORDER BY City, Name;Sorting process:
- Sort by City.
- Within each city, sort by Name.
Get:
| City | Name |
|---|---|
| Delhi | Amit |
| Delhi | Rahul |
| Mumbai | Priya |
Multiple Column Sorting with DESC
Watch:
SELECT *
FROM Employees
ORDER BY Department ASC,
Salary DESC;Explanation:
- Departments sorted alphabetically.
- Employees within each department sorted by salary.
Real-World Example
Let's say you're building an e-commerce site.
Your Products table:
| ProductID | ProductName | Price |
|---|---|---|
| 101 | Laptop | 60000 |
| 102 | Mouse | 500 |
| 103 | Mobile | 25000 |
You sort by price:
SELECT *
FROM Products
ORDER BY Price;Customer sees:
| ProductName | Price |
|---|---|
| Mouse | 500 |
| Mobile | 25000 |
| Laptop | 60000 |
Cheap stuff first. Natural.
ORDER BY with WHERE
Mix WHERE and ORDER BY for surgical sorting.
Watch:
SELECT *
FROM Products
WHERE Price > 1000
ORDER BY Price DESC;Process:
- Filter products.
- Sort remaining records.
This combination is very common in real applications.
ORDER BY with Aliases
Watch:
SELECT
Name AS StudentName
FROM Students
ORDER BY StudentName;Result:
Records are sorted using the alias.
ORDER BY Using Column Position
You can also sort by column number:
Watch:
SELECT Name, Age
FROM Students
ORDER BY 2;Meaning:
Sort by second column (Age)
Although valid, using column names is usually clearer.
Common Errors
Sorting by a Non-Existent Column
Don't do this:
SELECT *
FROM Students
ORDER BY Salary;If Salary isn't in the table:
Unknown Column Error
Misspelling ASC or DESC
This breaks:
ORDER BY Age DESFix it:
ORDER BY Age DESCIncorrect Multiple Column Syntax
Bad syntax:
ORDER BY Age NameCorrect:
ORDER BY Age, NameAssuming Database Returns Sorted Data
Never assume rows come back sorted.
SELECT *
FROM Students;No guaranteed order exists.
Always use ORDER BY when sorting is required.
Best Practices
Always Specify ORDER BY When Needed
Never assume records will appear in a specific order.
Use Indexed Columns
Sorting indexed columns can improve performance.
Sort Only When Necessary
Large sorting operations can increase query execution time.
Use Meaningful Sorting Logic
Choose sorting columns based on business requirements.
Prefer Column Names Over Positions
Good:
ORDER BY AgeLess clear:
ORDER BY 2Common Interview Questions
What is the purpose of ORDER BY?
ORDER BY sorts query results based on one or more columns.
What is the default sorting order?
Ascending (ASC).
How do you sort records in descending order?
ORDER BY ColumnName DESC;Can ORDER BY sort multiple columns?
Yes.
Watch:
ORDER BY City, Name;Summary
The ORDER BY clause is used to arrange query results in a meaningful order. It improves readability, reporting, and user experience by sorting records alphabetically, numerically, or chronologically.
In this lesson, you learned:
- What ORDER BY is
- Why sorting is important
- Ascending order
- Descending order
- Sorting text values
- Sorting numeric values
- Sorting dates
- Multiple-column sorting
- Common mistakes
- Best practices
Understanding ORDER BY is essential because properly organized data makes database applications easier to use and analyze.
Next Step
Continue to the next lesson:
GROUP BY →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for ORDER BY Clause.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL Complete Guide topic.
Search Terms
sql-complete-guide, sql complete guide, sql, complete, guide, filtering, order, order by clause
Related SQL Complete Guide Topics