🚀 New batches open: Advanced Excel • Power BI • SQL • AI for Analytics — Book a free demo
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 5 at the end
  • SQL Server — SELECT TOP 5 at 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

  1. List products cheapest first.
  2. List employees by Salary, highest first.
  3. Show the distinct Status values in Orders.
  4. Show the 3 largest orders.
  5. 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 →