Home / SQL Tutorial / Day 5
Day 5 — Sorting and trimming results
Three small tools that turn a wall of rows into something a manager will actually read.
ORDER BY
Sorts the result. ASC (smallest first) is the default; DESC reverses it.
Sorting by more than one column
The second column breaks ties in the first.
DISTINCT — unique values only
Perfect for answering "which cities do we actually sell into?"
Interview favourite
DISTINCT applies to the whole row of the result, not just the first column. SELECT DISTINCT City, Segment gives unique combinations.Top N rows
This is the one bit of syntax that genuinely differs between databases:
- MySQL, PostgreSQL, SQLite —
LIMIT 5at the end - SQL Server —
SELECT TOP 5at the start
Both work in this playground so you can see the difference.
Top N without ORDER BY is meaningless
Without a sort, "top 5" is just whichever 5 rows the database happened to hand back. Always pair them.
Try these yourself
- List products cheapest first.
- List employees by Salary, highest first.
- Show the distinct Status values in Orders.
- Show the 3 largest orders.
- Show the 3 lowest-paid employees.
Want this taught properly?
This tutorial covers the basics. Our SQL course in Noida takes you through window functions, CTEs and stored procedures on a real database, with projects and mentor support.
See the SQL course →