SQLQuokka
Start practicing
QuestionsLearnSandboxFeaturesRoadmapFAQ
SQL Concept

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

  • WHERE runs before grouping and sorting, row by row.
  • A comparison with NULL is never true; use IS NULL / IS NOT NULL.
  • ORDER BY needs a tie-breaker when values can repeat.
  • LIMIT without ORDER BY returns 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_idnamedept_idsalarymanager_idhire_date
1Aarav1095000NULL2021-03-01
2Diya108800012022-06-15
3Kabir107200012023-01-10
4Meera2064000NULL2020-11-20
5Rohan205800042024-02-05
6Isha3061000NULL2022-09-12
7Arjun206400042023-07-30
8ZoyaNULL52000NULL2024-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;
Result
namesalary
Aarav95000
Diya88000
Kabir72000
Arjun64000
Meera64000

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;
Result
namesalary
Aarav95000
Diya88000
Kabir72000

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;
Result
name
Zoya

Common mistakes

  • Using = NULL. = NULL and <> NULL never match. Use IS NULL and IS 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 name or an id.
  • LIMIT without ORDER BY. "Top 3" only means something after sorting. Always pair LIMIT with ORDER 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.

  1. Top Paid Engineers

    Show the name and salary of employees in 'Engineering' who earn more than 70000, highest first, top 3.

    Easy✓ Solved
  2. Multilingual Language Speakers

    Identify users who speak at least two distinct target languages ('English', 'Spanish', 'German') from skill profile records.

    Easy✓ Solved
  3. Users With Zero Searches

    Find registered account profiles that have never submitted a search query using an outer join.

    Easy✓ Solved
  4. 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.

    Easy✓ Solved
  5. Google Maps: High-Impact Local Guides

    Find verified reviewers who submitted at least 2 photo reviews in San Francisco or Mountain View.

    Easy✓ Solved
  6. Google Drive: External Sensitive File Shares

    Audit sensitive files shared with non-Google email addresses to identify potential data exposure risks.

    Easy✓ Solved

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.