Free SQL quiz
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
-- staff-- id name dept salary-- 1 Ana eng 90-- 2 Ben eng 70-- 3 Cara sales 50-- 4 Dev sales NULL-- 5 Eli NULL 60SELECT name FROM staffWHERE salary > 60ORDER BY id;WHERE
1 / 10
Which names does this query return?
0 correct · Keys 1 to 4 to answer
Your score and streak are saved in this browser. No account needed.
Question bank
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 counting1WHEREORDER 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 60SELECT name FROM staffWHERE salary > 60ORDER BY id;- AAna
- BAna, Ben, Dev
- CAna, Ben
- 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 60SELECT name FROM staffWHERE salary IS NOT NULLORDER BY salaryLIMIT 1;- ACara
- BAna
- CDev
- 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 60SELECT COUNT(*) FROM staff;- A4
- BNULL
- C3
- 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 60SELECT COUNT(salary) FROM staff;- A5
- B4
- C270
- 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 60SELECT COUNT(DISTINCT dept) FROM staff;- A2
- B3
- C5
- 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 60SELECT name FROM staffWHERE dept = 'eng' AND salary < 80;- AAna
- BAna, Ben
- CBen
- 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 60SELECT name FROM staffWHERE salary BETWEEN 60 AND 90ORDER BY id;- ABen
- BAna, Ben, Eli
- CBen, Eli
- 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 60SELECT name FROM staffWHERE dept IN ('sales', 'ops')ORDER BY id;- ACara
- BNo rows
- CCara, Dev, Eli
- 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?
- AHAVING
- BORDER BY
- CWHERE
- 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 60SELECT SUM(salary) FROM staff;- A270
- BNULL
- C0
- 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 NULL11Comparing 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 60SELECT COUNT(*) FROM staffWHERE salary = NULL;- A1
- BIt raises an error
- C5
- 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 60SELECT name FROM staffWHERE salary <> 90ORDER BY id;- ABen, Cara, Dev, Eli
- BBen, Cara, Eli
- CBen, Cara
- 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 60SELECT dept, COUNT(*) FROM staffWHERE dept IS NOT NULLGROUP BY deptORDER BY dept;- Aeng 2, sales 1
- BOne row: 4
- Ceng 1, sales 1
- 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 60SELECT AVG(salary) FROM staffWHERE dept = 'sales';- A50
- B25
- CNULL
- 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 60SELECT dept FROM staffGROUP BY deptHAVING SUM(salary) > 100;- Aeng, sales
- Bsales
- Ceng
- 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 GoaSELECT COUNT(*)FROM staff sJOIN dept d ON s.dept = d.code;- A5
- B4
- C6
- 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 GoaSELECT s.name, d.cityFROM staff sLEFT JOIN dept d ON s.dept = d.codeWHERE s.id = 5;- ANo rows
- BEli, Goa
- CEli, NULL
- 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 GoaSELECT d.codeFROM dept dLEFT JOIN staff s ON s.dept = d.codeWHERE s.id IS NULL;- Aops
- Beng
- Csales
- 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 60SELECT name, COALESCE(salary, 0)FROM staffWHERE id = 4;- ADev, NULL
- BDev, 50
- CNo rows
- 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?
- AFROM, WHERE, GROUP BY, SELECT
- BSELECT, FROM, WHERE, GROUP BY
- CWHERE, FROM, SELECT, GROUP BY
- 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 love21NOT 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 GoaSELECT COUNT(*) FROM deptWHERE code NOT IN (SELECT dept FROM staff);- A1
- B0
- C3
- 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 60SELECT COUNT(*), SUM(salary)FROM staffWHERE dept = 'ops';- A0, 0
- BNo rows
- CNULL, NULL
- 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 60SELECT dept FROM staff WHERE dept = 'eng'UNIONSELECT dept FROM staff WHERE dept = 'eng';- A4
- B2
- C1
- 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 60SELECT name, CASE WHEN salary >= 70 THEN 'high' WHEN salary >= 50 THEN 'mid' ELSE 'none' ENDFROM staffWHERE id IN (2, 4)ORDER BY id;- ABen high, Dev mid
- BBen high, Dev none
- CBen mid, Dev none
- 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 60SELECT name FROM staffWHERE salary > (SELECT AVG(salary) FROM staff)ORDER BY id;- AAna, Ben
- BAna
- CAna, Ben, Eli
- 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 60SELECT 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;- AAna
- BAna, Ben
- CAna, Cara
- 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 60SELECT COUNT(*) FROM ( SELECT DISTINCT dept FROM staff) t;- A2
- B4
- C5
- 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 60SELECT COUNT(*) FROM staffWHERE salary > 60 OR salary <= 60;- A5
- B4
- C0
- 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 60SELECT a.nameFROM staff aJOIN staff b ON a.dept = b.dept AND a.salary > b.salary;- AAna
- BBen
- CAna, Cara
- 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?
- ADELETE FROM staff
- BREMOVE TABLE staff
- CTRUNCATE TABLE staff
- 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.
FAQ
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.