SQL · Lesson 3 of 8
ORDER BY, LIMIT & DISTINCT
Sorting results, taking the top rows and removing duplicates.
ORDER BY column sorts results — ascending by default, add DESC for largest first. Sort by several columns: ORDER BY city, salary_lpa DESC.
LIMIT n returns only the first n rows; LIMIT n OFFSET m skips m rows first — the basis of pagination. (SQL Server uses TOP, Oracle uses FETCH FIRST.)
SELECT DISTINCT city FROM jobs lists each city once.
Try it yourself
Press Run to see the results.
Runs on a small fictional sample database (companies, jobs) that is rebuilt on every run, so you can't break anything.
Key points
- ORDER BY … DESC for highest first.
- LIMIT / OFFSET for top-N and pages.
- DISTINCT removes duplicate rows.
Quick check
How do you sort from highest to lowest salary?
DESC means descending — largest first.
Practice task
Show the 2 most recently posted jobs (use posted_on).
Interview corner
How would you find the second-highest salary?
One way: SELECT MAX(salary_lpa) FROM jobs WHERE salary_lpa < (SELECT MAX(salary_lpa) FROM jobs). Another: ORDER BY salary_lpa DESC LIMIT 1 OFFSET 1 on the distinct salaries.
Does the order of rows in a table have meaning without ORDER BY?
No. Without ORDER BY the database may return rows in any order, so always sort when order matters.
Further reading
Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.
Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.