SQL quiz – 41 questions with answers
Try it interactively
With scoring, random questions, and saved progress – or go straight to the timed exam with certificate.
Beginner (13 questions)
Which clause filters individual rows before grouping?
- HAVING
- WHERE
- ORDER BY
- LIMIT
Show answer
Correct: WHERE
WHERE works at the row level before GROUP BY; HAVING only filters the finished groups.
What does this query return (table kunden with 5 rows, Eva has stadt = NULL)?
SELECT COUNT(stadt) FROM kunden;- 5
- 4
- 1
- 0
Show answer
Correct: 4
COUNT(column) counts only non-NULL values: 4 customers have a city.
What does this query return?
SELECT COUNT(*) FROM kunden;- 4
- 5
- NULL
- Error
Show answer
Correct: 5
COUNT(*) counts all rows, regardless of NULL values: 5.
Which rows does this query return?
SELECT * FROM kunden WHERE stadt = NULL;- Only Eva
- All rows
- No rows
- A syntax error
Show answer
Correct: No rows
stadt = NULL always yields NULL (unknown), never true. For NULL checks there is IS NULL.
What does this query output?
SELECT name FROM kunden
WHERE stadt = 'Berlin'
ORDER BY name DESC;- Ada, Cem
- Cem, Ada
- Ada
- Cem
Show answer
Correct: Cem, Ada
Ada and Cem live in Berlin; DESC sorts in descending order: Cem before Ada.
How many rows does this query return?
SELECT DISTINCT stadt FROM kunden;- 3
- 4
- 5
- 2
Show answer
Correct: 4
Berlin, Hamburg, München, and NULL – DISTINCT treats NULL as its own value. That gives 4 rows.
What does this query return?
SELECT name FROM produkte
ORDER BY preis
LIMIT 2;- Laptop, Stuhl
- Kaffee, Maus
- Maus, Buch SQL
- Kaffee, Laptop
Show answer
Correct: Kaffee, Maus
Sorted ascending, Kaffee (8.50) and Maus (25) come first; LIMIT 2 keeps two rows.
Which names does this pattern find?
SELECT name FROM kunden WHERE name LIKE '_e%';- Ben and Cem
- Ada and Dana
- Only Ben
- Eva and Ben
Show answer
Correct: Ben and Cem
_ stands for exactly one arbitrary character, followed by an e: Ben and Cem. For Eva, the second letter is a v.
What happens with this statement?
DELETE FROM kunden;- Only the first row is deleted
- The table is deleted along with its structure
- All rows of the table are deleted
- A syntax error always appears
Show answer
Correct: All rows of the table are deleted
Without WHERE, DELETE affects all rows. The table structure remains (that would be DROP TABLE).
Which products does this query find?
SELECT name FROM produkte
WHERE preis BETWEEN 25 AND 150;- Maus, Buch SQL
- Maus, Buch SQL, Stuhl
- Buch SQL, Stuhl
- Laptop, Stuhl
Show answer
Correct: Maus, Buch SQL, Stuhl
BETWEEN includes both bounds: Maus (25), Buch SQL (30), and Stuhl (150).
What does this query return?
SELECT kategorie, COUNT(*)
FROM produkte
GROUP BY kategorie
HAVING COUNT(*) > 1;- Elektronik | 2
- Elektronik | 2 and Bücher | 1
- All four categories
- An empty result set
Show answer
Correct: Elektronik | 2
Only Elektronik (Laptop and Maus) has more than one product; HAVING discards the other groups.
What characterizes a PRIMARY KEY?
- It may occur multiple times
- It is unique and not NULL
- It must always be a number
- It is always the name
Show answer
Correct: It is unique and not NULL
A primary key identifies each row uniquely and must not be NULL. The data type is freely selectable (number, text, UUID).
What does this query return?
SELECT 7 % 3;- 2
- 1
- 2.33
- 0
Show answer
Correct: 1
% is the modulo operator and returns the remainder of the division: 7 = 2·3 + 1.
Intermediate (15 questions)
How many rows does this JOIN return? (6 orders, each with a valid customer)
SELECT k.name, b.id
FROM kunden k
JOIN bestellungen b ON b.kunde_id = k.id;- 5
- 6
- 11
- 30
Show answer
Correct: 6
The INNER JOIN returns exactly one row per order: 6. Eva, with no order, drops out.
How many rows does this LEFT JOIN return?
SELECT k.name, b.id
FROM kunden k
LEFT JOIN bestellungen b ON b.kunde_id = k.id;- 5
- 6
- 7
- 11
Show answer
Correct: 7
There are 6 orders (rows with a match) plus Eva without an order as a row with NULL: 7.
What value does bestellungen have in the row for Eva?
SELECT k.name, COUNT(b.id) AS bestellungen
FROM kunden k
LEFT JOIN bestellungen b ON b.kunde_id = k.id
GROUP BY k.id, k.name;- 1
- 0
- NULL
- Eva is missing from the result
Show answer
Correct: 0
For Eva, b.id is NULL, COUNT(b.id) does not count NULL and returns 0 (COUNT(*) would have returned 1 here).
What does this query return?
SELECT 1 WHERE 1 NOT IN (2, NULL);- A row with 1
- No rows
- An error
- A row with NULL
Show answer
Correct: No rows
1 NOT IN (2, NULL) means 1<>2 AND 1<>NULL – the second part is unknown, so the overall result is never true. That is why NOT EXISTS is preferable.
Which products does this query return?
SELECT name FROM produkte
WHERE preis > (SELECT AVG(preis) FROM produkte);- Laptop
- Laptop and Stuhl
- All products
- None
Show answer
Correct: Laptop
The average is 242.50. Only the Laptop (999) is above it, the Stuhl (150) is not.
What does this statement return?
SELECT COALESCE(stadt, 'unknown')
FROM kunden WHERE name = 'Eva';- NULL
- unknown
- An empty string
- No rows
Show answer
Correct: unknown
COALESCE returns the first non-NULL value. Eva's city is NULL, so the fallback value is used.
How many customers have placed orders?
SELECT COUNT(DISTINCT kunde_id) FROM bestellungen;- 6
- 4
- 5
- 3
Show answer
Correct: 4
The 6 orders come from customers 1, 2, 3, and 4 – distinct values: 4.
What does the sum of all ordered quantities return?
SELECT SUM(menge) FROM bestellpositionen;- 9
- 25
- 5
- 19
Show answer
Correct: 25
1+2+1+10+2+1+4+1+3 = 25.
What is the difference between UNION and UNION ALL?
- UNION ALL removes duplicates, UNION does not
- UNION removes duplicates, UNION ALL keeps them (and is faster)
- UNION is only allowed in MySQL
- No difference
Show answer
Correct: UNION removes duplicates, UNION ALL keeps them (and is faster)
UNION deduplicates and removes duplicate rows; UNION ALL simply appends and is therefore faster.
What does ROLLBACK do?
- It saves the transaction permanently
- It discards all changes since BEGIN
- It deletes the table
- It restarts the database
Show answer
Correct: It discards all changes since BEGIN
ROLLBACK undoes all changes of the current transaction – the "A" in ACID (atomicity).
Which column should typically get an index for fast queries like WHERE kunde_id = ??
- Every column of the table
- The foreign key
kunde_idin bestellungen - Only columns of type TEXT
- None – foreign keys are always indexed
Show answer
Correct: The foreign key kunde_id in bestellungen
PostgreSQL and SQLite do not create an index for foreign keys automatically – frequently filtered or joined foreign keys should be indexed manually.
What happens with this INSERT if foreign key checking is active and customer 99 does not exist?
INSERT INTO bestellungen (kunde_id, datum)
VALUES (99, '2024-05-01');- The row is inserted
- A foreign key error occurs
- kunde_id is set to NULL
- Customer 99 is created automatically
Show answer
Correct: A foreign key error occurs
The FOREIGN KEY rejects rows that point to non-existent records. In SQLite this requires PRAGMA foreign_keys = ON.
What does the first row of this query return?
SELECT status, COUNT(*)
FROM bestellungen
GROUP BY status
ORDER BY COUNT(*) DESC, status;- bezahlt | 3
- offen | 2
- storniert | 1
- offen | 3
Show answer
Correct: bezahlt | 3
There are 3 paid, 2 open, and 1 cancelled order; sorted by count in descending order, bezahlt comes first.
Why should you not store monetary amounts in FLOAT/REAL?
- Because FLOAT has no decimal point
- Because of rounding errors in the binary floating-point representation
- Because money is always an integer
- Because FLOAT cannot be sorted
Show answer
Correct: Because of rounding errors in the binary floating-point representation
Floating-point numbers cannot represent decimal fractions like 0.1 exactly. NUMERIC(12,2) or amounts in cents as an integer are exact.
What does NULLIF(a, b) do?
- Returns NULL if a equals b, otherwise a
- Checks whether a is NULL
- Replaces NULL with b
- Always returns NULL
Show answer
Correct: Returns NULL if a equals b, otherwise a
NULLIF(x, 0) in the denominator prevents division by zero, because x / NULL simply yields NULL instead of an error.
Advanced (13 questions)
Which values does DENSE_RANK() OVER (ORDER BY x) return for the values 10, 10, 20?
- 1, 1, 3
- 1, 2, 3
- 1, 1, 2
- 1, 1, 1
Show answer
Correct: 1, 1, 2
DENSE_RANK leaves no gaps: 1, 1, 2. RANK would have returned 1, 1, 3, and ROW_NUMBER 1, 2, 3.
What does the second column return for the rows with x = 1, 2, 3 (in this order)?
WITH t(x) AS (VALUES (1), (2), (3))
SELECT x, SUM(x) OVER (ORDER BY x) AS s
FROM t;- 1, 2, 3
- 1, 3, 6
- 6, 6, 6
- 3, 5, 6
Show answer
Correct: 1, 3, 6
With ORDER BY, window aggregates form running totals by default: 1, 1+2, 1+2+3.
What value does this recursive CTE return?
WITH RECURSIVE t(n) AS (
SELECT 1
UNION ALL
SELECT n * 2 FROM t WHERE n < 20
)
SELECT MAX(n) FROM t;- 16
- 20
- 32
- 64
Show answer
Correct: 32
The sequence 1, 2, 4, 8, 16 still satisfies n < 20, producing 32; at 32 the recursion ends. Maximum: 32.
What does LAG(wert) OVER (ORDER BY datum) do?
- Returns the value of the next row
- Returns the value of the previous row (NULL for the first)
- Sums all values so far
- Returns the smallest value
Show answer
Correct: Returns the value of the previous row (NULL for the first)
LAG looks at the preceding row in the defined window, LEAD at the following one.
Why is an index on datum usually not used for WHERE YEAR(datum) = 2024?
- YEAR is invalid
- The function on the column prevents the index lookup (not sargable)
- Indexes only apply to numbers
- The index is too large
Show answer
Correct: The function on the column prevents the index lookup (not sargable)
The index is sorted by datum, not by YEAR(datum). Better: datum >= '2024-01-01' AND datum < '2025-01-01'.
For which query is an index on (kunde_id, datum) the least helpful?
WHERE kunde_id = 1WHERE kunde_id = 1 AND datum > '2024-01-01'WHERE datum > '2024-01-01'WHERE kunde_id = 1 ORDER BY datum
Show answer
Correct: WHERE datum > '2024-01-01'
Composite indexes apply from the first column onward (leftmost prefix). A filter on datum alone cannot use the ordering by kunde_id.
What is the goal of the second normal form (2NF)?
- Atomic cell values
- No partial dependencies on the composite key
- No transitive dependencies
- As few tables as possible
Show answer
Correct: No partial dependencies on the composite key
2NF requires non-key attributes to depend on the entire key. Atomic values are 1NF, no transitive dependencies are 3NF.
What is ON CONFLICT (id) DO UPDATE (PostgreSQL/SQLite) used for?
- For deleting duplicate rows
- For atomic "insert or update" (UPSERT)
- For locking a table
- For creating an index
Show answer
Correct: For atomic "insert or update" (UPSERT)
On a key conflict, an update is performed instead. MySQL offers ON DUPLICATE KEY UPDATE.
How do you reliably prevent SQL injection?
- Delete quotation marks in inputs
- Parameterized queries (prepared statements)
- Only allow GET requests
- Convert inputs to uppercase
Show answer
Correct: Parameterized queries (prepared statements)
With prepared statements, values are transmitted separately from the SQL text and never interpreted as SQL. Blacklist filters are unreliable.
What distinguishes a window function from GROUP BY?
- Window functions are slower and therefore obsolete
- Window functions keep all rows, GROUP BY collapses them
- GROUP BY does not allow aggregates
- There is no difference
Show answer
Correct: Window functions keep all rows, GROUP BY collapses them
With OVER (...), each original row gets its computed value, without rows merging into groups.
What does Seq Scan indicate in a PostgreSQL plan?
- The table is read completely (no index)
- An index is used optimally
- The query is faulty
- The table is locked
Show answer
Correct: The table is read completely (no index)
A sequential scan reads the entire table. With small tables or barely selective filters this is even efficient – not every Seq Scan is bad.
Which isolation level is the default in PostgreSQL?
- READ UNCOMMITTED
- READ COMMITTED
- REPEATABLE READ
- SERIALIZABLE
Show answer
Correct: READ COMMITTED
PostgreSQL uses READ COMMITTED: each statement sees only already committed data. MySQL (InnoDB), by contrast, starts with REPEATABLE READ.
What is a deadlock?
- A transaction waits for a lock held by a second one that itself is waiting for the first
- A crashed database server
- A syntax error in a view
- An index that is too large
Show answer
Correct: A transaction waits for a lock held by a second one that itself is waiting for the first
With cyclic lock dependencies, the database aborts one transaction. Avoidance: always acquire locks in the same order, keep transactions short.