SQL WHERE, ORDER BY and LIMIT Explained
Almost every query starts by choosing rows and putting them in order. WHERE keeps the rows that match a condition, ORDER BY sorts what is left, and LIMIT keeps only the first few. Getting these three right is the foundation for every harder interview question.
Examples run in MySQL 8 and PostgreSQL · Updated
In short
WHEREruns before grouping and sorting, row by row.- A comparison with
NULLis never true; useIS NULL/IS NOT NULL. ORDER BYneeds a tie-breaker when values can repeat.LIMITwithoutORDER BYreturns arbitrary rows.
How the database reads your query
SQL is written in the order SELECT … FROM … WHERE … ORDER BY … LIMIT, but it runs in a different order: the table in FROM is read first, WHERE removes rows, then SELECT picks columns, ORDER BY sorts and LIMIT cuts the list.
That order explains a classic surprise: you cannot use a column alias from SELECT inside WHERE, because WHERE runs first. You can use it in ORDER BY, which runs last.
Conditions and NULL
Combine conditions with AND and OR, and add parentheses whenever you mix them: a OR b AND c means a OR (b AND c). Use IN (…) for lists and BETWEEN x AND y for inclusive ranges.
NULL means "unknown", so dept_id = NULL is unknown for every row and returns nothing. Always write dept_id IS NULL.
The example tables
Every example on this page uses these tables. They are small on purpose, so you can check each result by eye.
employees
| emp_id | name | dept_id | salary | manager_id | hire_date |
|---|---|---|---|---|---|
| 1 | Aarav | 10 | 95000 | NULL | 2021-03-01 |
| 2 | Diya | 10 | 88000 | 1 | 2022-06-15 |
| 3 | Kabir | 10 | 72000 | 1 | 2023-01-10 |
| 4 | Meera | 20 | 64000 | NULL | 2020-11-20 |
| 5 | Rohan | 20 | 58000 | 4 | 2024-02-05 |
| 6 | Isha | 30 | 61000 | NULL | 2022-09-12 |
| 7 | Arjun | 20 | 64000 | 4 | 2023-07-30 |
| 8 | Zoya | NULL | 52000 | NULL | 2024-05-01 |
Examples
Filter and sort with a tie-breaker
Employees earning at least 64,000, highest first. Meera and Arjun both earn 64,000, so name decides their order.
SELECT name, salary
FROM employees
WHERE salary >= 64000
ORDER BY salary DESC, name;| name | salary |
|---|---|
| Aarav | 95000 |
| Diya | 88000 |
| Kabir | 72000 |
| Arjun | 64000 |
| Meera | 64000 |
Top 3 with LIMIT
Sort first, then keep three rows. Without the ORDER BY you would get three arbitrary rows.
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;| name | salary |
|---|---|
| Aarav | 95000 |
| Diya | 88000 |
| Kabir | 72000 |
Finding NULLs
Zoya has no department. WHERE dept_id = NULL would return nothing; IS NULL finds her.
SELECT name
FROM employees
WHERE dept_id IS NULL;| name |
|---|
| Zoya |
Common mistakes
- Using = NULL.
= NULLand<> NULLnever match. UseIS NULLandIS NOT NULL. - No tie-breaker. When two rows share the sort value, their order is not guaranteed. Interview checkers often expect a second sort column such as
nameor an id. - LIMIT without ORDER BY. "Top 3" only means something after sorting. Always pair
LIMITwithORDER BY.
Practise WHERE, ORDER BY & LIMIT
Interview questions that use this concept, easiest first. Each one has the step-by-step visualizer and answer checking.
Top Paid Engineers
Show the name and salary of employees in 'Engineering' who earn more than 70000, highest first, top 3.
Multilingual Language Speakers
Identify users who speak at least two distinct target languages ('English', 'Spanish', 'German') from skill profile records.
Users With Zero Searches
Find registered account profiles that have never submitted a search query using an outer join.
Play Store: Top Rated App Categories
Find categories with at least 3 apps rated 4.0 or higher, returning the category, app count, and average rating rounded to 2 decimals.
Google Maps: High-Impact Local Guides
Find verified reviewers who submitted at least 2 photo reviews in San Francisco or Mountain View.
Google Drive: External Sensitive File Shares
Audit sensitive files shared with non-Google email addresses to identify potential data exposure risks.
Want a full set? Work through the SQL Basics study plan.
Questions people ask
- Does WHERE run before ORDER BY?
- Yes. WHERE filters rows first; ORDER BY sorts only the rows that remain, and LIMIT is applied last.
- Is LIMIT the same in MySQL and PostgreSQL?
- Yes, LIMIT n works in both. MySQL also accepts LIMIT offset, count; PostgreSQL uses LIMIT count OFFSET offset, which MySQL supports too.