SQL Practice Quiz.

Thirty SQL questions in three levels. Read the query, pick what it returns, and see at once whether you were right, and why.

  • 30 questions
  • 3 levels
  • No sign-in
  • Checked on SQLite
0 day streakBest –
SQL
-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE salary > 60ORDER BY id;

1 / 10

Which names does this query return?

0 correct

Your score and streak are saved in this browser. No account needed.

All 30 SQL questions and answers

Prefer to read? Open any question to see the code, the answer and the reason behind it.

Basics

SELECT, WHERE and counting
1WHEREORDER BY id;

Which names does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE salary > 60ORDER BY id;
  1. AAna
  2. BAna, Ben, Dev
  3. CAna, Ben
  4. DAna, Ben, Eli

Answer: C. Only 90 and 70 are greater than 60. Eli has exactly 60, which is not greater. Dev has no salary at all, so the comparison is not true for that row.

2ORDER BY and LIMITLIMIT 1;

Which name does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE salary IS NOT NULLORDER BY salaryLIMIT 1;
  1. ACara
  2. BAna
  3. CDev
  4. DEli

Answer: A. ORDER BY sorts from lowest to highest unless you add DESC. LIMIT 1 keeps the first row, which is the lowest salary: Cara with 50.

3COUNT(*)SELECT COUNT(*) FROM staff;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT COUNT(*) FROM staff;
  1. A4
  2. BNULL
  3. C3
  4. D5

Answer: D. COUNT(*) counts rows, whatever is in them. The table has five rows, so the answer is 5 even though some columns are NULL.

4COUNT(column)SELECT COUNT(salary) FROM staff;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT COUNT(salary) FROM staff;
  1. A5
  2. B4
  3. C270
  4. DNULL

Answer: B. COUNT(column) counts only the rows where that column is not NULL. Dev has no salary, so four rows are counted.

5DISTINCTSELECT COUNT(DISTINCT dept) FROM staff;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT COUNT(DISTINCT dept) FROM staff;
  1. A2
  2. B3
  3. C5
  4. D4

Answer: A. DISTINCT removes repeats, leaving eng and sales. The NULL in Eli's row is not counted, because COUNT of a column skips NULL.

6ANDWHERE dept = 'eng' AND salary < 80;

Which names does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE dept = 'eng' AND salary < 80;
  1. AAna
  2. BAna, Ben
  3. CBen
  4. DBen, Cara

Answer: C. AND needs both conditions to be true. Ana and Ben are in eng, but only Ben earns less than 80.

7BETWEENORDER BY id;

Which names does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE salary BETWEEN 60 AND 90ORDER BY id;
  1. ABen
  2. BAna, Ben, Eli
  3. CBen, Eli
  4. DAna, Ben

Answer: B. BETWEEN includes both ends. 60 and 90 themselves match, so Eli (60) and Ana (90) are returned along with Ben (70).

8INORDER BY id;

Which names does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE dept IN ('sales', 'ops')ORDER BY id;
  1. ACara
  2. BNo rows
  3. CCara, Dev, Eli
  4. DCara, Dev

Answer: D. IN is true when the value equals any item in the list. Cara and Dev are in sales. Nobody is in ops, and Eli has no department, so neither adds a row.

9WHERE and HAVINGWhich clause filters rows before they are grouped?

Which clause filters rows before they are grouped?

  1. AHAVING
  2. BORDER BY
  3. CWHERE
  4. DGROUP BY

Answer: C. WHERE removes rows first. GROUP BY then forms groups from what is left, and HAVING filters those groups. ORDER BY only sorts the final result.

10SUM and NULLSELECT SUM(salary) FROM staff;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT SUM(salary) FROM staff;
  1. A270
  2. BNULL
  3. C0
  4. DIt raises an error

Answer: A. Aggregate functions skip NULL values. SUM adds 90, 70, 50 and 60 and ignores Dev's missing salary.

Core

Grouping, joins and NULL
11Comparing with NULLWHERE salary = NULL;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT COUNT(*) FROM staffWHERE salary = NULL;
  1. A1
  2. BIt raises an error
  3. C5
  4. D0

Answer: D. Nothing is ever equal to NULL, not even NULL. The comparison gives "unknown", and WHERE keeps only rows where the condition is true. Write salary IS NULL instead.

12Not equal and NULLORDER BY id;

Which names does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE salary <> 90ORDER BY id;
  1. ABen, Cara, Dev, Eli
  2. BBen, Cara, Eli
  3. CBen, Cara
  4. DDev

Answer: B. Dev is left out. NULL <> 90 is unknown, not true, so the row does not pass. To include it, add OR salary IS NULL.

13GROUP BYORDER BY dept;

Which rows does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT dept, COUNT(*) FROM staffWHERE dept IS NOT NULLGROUP BY deptORDER BY dept;
  1. Aeng 2, sales 1
  2. BOne row: 4
  3. Ceng 1, sales 1
  4. Deng 2, sales 2

Answer: D. GROUP BY makes one row per department. COUNT(*) counts rows, so Dev is counted in sales even without a salary.

14AVG and NULLWHERE dept = 'sales';

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT AVG(salary) FROM staffWHERE dept = 'sales';
  1. A50
  2. B25
  3. CNULL
  4. D0

Answer: A. AVG ignores NULL rather than treating it as zero. Only Cara's 50 is averaged, so the result is 50, not (50 + 0) / 2.

15HAVINGHAVING SUM(salary) > 100;

Which departments does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT dept FROM staffGROUP BY deptHAVING SUM(salary) > 100;
  1. Aeng, sales
  2. Bsales
  3. Ceng
  4. DNo rows

Answer: C. HAVING filters groups after they are formed. eng totals 160. sales totals 50 and the group with no department totals 60, so both are dropped.

16INNER JOINJOIN dept d ON s.dept = d.code;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60-- dept-- code   city-- eng    Pune-- sales  Delhi-- ops    Goa​SELECT COUNT(*)FROM staff sJOIN dept d ON s.dept = d.code;
  1. A5
  2. B4
  3. C6
  4. D3

Answer: B. An inner join keeps only rows that match on both sides. Eli has no department, so that row finds no match and is dropped. ops has no staff and adds nothing.

17LEFT JOINWHERE s.id = 5;

Which rows does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60-- dept-- code   city-- eng    Pune-- sales  Delhi-- ops    Goa​SELECT s.name, d.cityFROM staff sLEFT JOIN dept d ON s.dept = d.codeWHERE s.id = 5;
  1. ANo rows
  2. BEli, Goa
  3. CEli, NULL
  4. DIt raises an error

Answer: C. A left join keeps every row from the left table. When there is no match on the right, the right-hand columns are filled with NULL.

18Finding rows with no matchWHERE s.id IS NULL;

Which department does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60-- dept-- code   city-- eng    Pune-- sales  Delhi-- ops    Goa​SELECT d.codeFROM dept dLEFT JOIN staff s ON s.dept = d.codeWHERE s.id IS NULL;
  1. Aops
  2. Beng
  3. Csales
  4. DNo rows

Answer: A. The left join keeps all departments. Where a department has no staff, the staff columns are NULL, and the WHERE keeps exactly those. This is the usual way to find rows with no match.

19COALESCEWHERE id = 4;

Which rows does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name, COALESCE(salary, 0)FROM staffWHERE id = 4;
  1. ADev, NULL
  2. BDev, 50
  3. CNo rows
  4. DDev, 0

Answer: D. COALESCE returns its first argument that is not NULL. Dev's salary is NULL, so the fallback 0 is used.

20Order of evaluationIn which order does the database logically work through these clauses?

In which order does the database logically work through these clauses?

  1. AFROM, WHERE, GROUP BY, SELECT
  2. BSELECT, FROM, WHERE, GROUP BY
  3. CWHERE, FROM, SELECT, GROUP BY
  4. DGROUP BY, FROM, WHERE, SELECT

Answer: A. The tables are read first (FROM), rows are filtered (WHERE), groups are formed (GROUP BY), and only then is the SELECT list worked out. This is why an alias from SELECT is not available in WHERE in standard SQL.

Tricky

The ones interviews love
21NOT IN and NULLWHERE code NOT IN (SELECT dept FROM staff);

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60-- dept-- code   city-- eng    Pune-- sales  Delhi-- ops    Goa​SELECT COUNT(*) FROM deptWHERE code NOT IN (SELECT dept FROM staff);
  1. A1
  2. B0
  3. C3
  4. DIt raises an error

Answer: B. You would expect ops, but the subquery returns a NULL (Eli). ops NOT IN (..., NULL) is unknown, because ops might equal the unknown value, so no row passes. Use NOT EXISTS, or filter the NULL out.

22Aggregates on no rowsWHERE dept = 'ops';

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT COUNT(*), SUM(salary)FROM staffWHERE dept = 'ops';
  1. A0, 0
  2. BNo rows
  3. CNULL, NULL
  4. D0, NULL

Answer: D. An aggregate without GROUP BY always returns one row. COUNT of nothing is 0, but SUM of nothing is NULL, not 0. Wrap it in COALESCE(SUM(salary), 0) when you need a number.

23UNIONSELECT dept FROM staff WHERE dept = 'eng';

How many rows does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT dept FROM staff WHERE dept = 'eng'UNIONSELECT dept FROM staff WHERE dept = 'eng';
  1. A4
  2. B2
  3. C1
  4. D0

Answer: C. UNION removes duplicate rows from the combined result, so the four "eng" rows become one. UNION ALL keeps every row and would return four.

24CASEORDER BY id;

Which rows does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name,  CASE WHEN salary >= 70 THEN 'high'       WHEN salary >= 50 THEN 'mid'       ELSE 'none' ENDFROM staffWHERE id IN (2, 4)ORDER BY id;
  1. ABen high, Dev mid
  2. BBen high, Dev none
  3. CBen mid, Dev none
  4. DBen high, Dev NULL

Answer: B. CASE checks the branches in order and stops at the first true one. For Dev, both comparisons with NULL are unknown, so the ELSE branch is used.

25SubqueriesORDER BY id;

Which names does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM staffWHERE salary > (SELECT AVG(salary) FROM staff)ORDER BY id;
  1. AAna, Ben
  2. BAna
  3. CAna, Ben, Eli
  4. DAna, Ben, Cara, Eli

Answer: A. The subquery runs first. AVG ignores Dev's NULL, so the average is 270 / 4 = 67.5. Only Ana (90) and Ben (70) are above it.

26Window functionsORDER BY name;

Which names does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT name FROM (  SELECT name, ROW_NUMBER() OVER (    PARTITION BY dept ORDER BY salary DESC) AS rn  FROM staff  WHERE dept IS NOT NULL AND salary IS NOT NULL) tWHERE rn = 1ORDER BY name;
  1. AAna
  2. BAna, Ben
  3. CAna, Cara
  4. DBen, Cara

Answer: C. PARTITION BY restarts the numbering for each department, and ORDER BY salary DESC gives number 1 to the highest earner in each. That is Ana in eng and Cara in sales.

27DISTINCT and NULL) t;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT COUNT(*) FROM (  SELECT DISTINCT dept FROM staff) t;
  1. A2
  2. B4
  3. C5
  4. D3

Answer: D. DISTINCT treats NULL as one value of its own, so the inner query returns three rows: eng, sales and NULL. COUNT(*) counts rows, so the NULL row is included. COUNT(DISTINCT dept) would give 2.

28Three-valued logicWHERE salary > 60 OR salary <= 60;

What does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT COUNT(*) FROM staffWHERE salary > 60 OR salary <= 60;
  1. A5
  2. B4
  3. C0
  4. D3

Answer: B. It looks as if every row must match one side or the other. For Dev both comparisons are unknown, and unknown OR unknown is still unknown, so that row is left out.

29Self join ON a.dept = b.dept AND a.salary > b.salary;

Which name does this query return?

-- staff-- id  name  dept   salary-- 1   Ana   eng    90-- 2   Ben   eng    70-- 3   Cara  sales  50-- 4   Dev   sales  NULL-- 5   Eli   NULL   60​SELECT a.nameFROM staff aJOIN staff b  ON a.dept = b.dept AND a.salary > b.salary;
  1. AAna
  2. BBen
  3. CAna, Cara
  4. DNo rows

Answer: A. The table is joined to itself to compare people in the same department. Ana earns more than Ben. Cara is not returned: comparing 50 with Dev's NULL is unknown.

30DELETE, TRUNCATE, DROPWhich statement removes the table itself, not only its rows?

Which statement removes the table itself, not only its rows?

  1. ADELETE FROM staff
  2. BREMOVE TABLE staff
  3. CTRUNCATE TABLE staff
  4. DDROP TABLE staff

Answer: D. DROP TABLE deletes the table and its definition. DELETE removes rows (all of them without a WHERE) and TRUNCATE empties the table quickly, but both leave the table in place. REMOVE TABLE is not SQL.

Questions people ask

Is this SQL quiz good for interview practice?

Yes. NULL handling, the difference between WHERE and HAVING, inner and left joins, NOT IN with NULL and window functions are the questions SQL interviews return to. The Tricky level is built from those.

Which database do the answers apply to?

Every query was run on SQLite and the marked answer matches the result. The questions use only standard behaviour, so PostgreSQL and MySQL return the same rows. (SQL Server writes TOP instead of LIMIT.)

Is my score saved?

Yes, in your browser on this device. Your best score for each level, your day streak and the question you stopped at are all there when you come back. Nothing is sent to a server and you do not need an account.

How do I get more SQL questions?

Open the AI Quiz Generator on Rune Learn, type a topic such as "SQL joins" or "window functions", and it writes a fresh quiz with explanations.