SQLSQL

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?

  1. HAVING
  2. WHERE
  3. ORDER BY
  4. 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;
  1. 5
  2. 4
  3. 1
  4. 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;
  1. 4
  2. 5
  3. NULL
  4. 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;
  1. Only Eva
  2. All rows
  3. No rows
  4. 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;
  1. Ada, Cem
  2. Cem, Ada
  3. Ada
  4. 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;
  1. 3
  2. 4
  3. 5
  4. 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;
  1. Laptop, Stuhl
  2. Kaffee, Maus
  3. Maus, Buch SQL
  4. 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%';
  1. Ben and Cem
  2. Ada and Dana
  3. Only Ben
  4. 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;
  1. Only the first row is deleted
  2. The table is deleted along with its structure
  3. All rows of the table are deleted
  4. 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;
  1. Maus, Buch SQL
  2. Maus, Buch SQL, Stuhl
  3. Buch SQL, Stuhl
  4. 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;
  1. Elektronik | 2
  2. Elektronik | 2 and Bücher | 1
  3. All four categories
  4. 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?

  1. It may occur multiple times
  2. It is unique and not NULL
  3. It must always be a number
  4. 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;
  1. 2
  2. 1
  3. 2.33
  4. 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;
  1. 5
  2. 6
  3. 11
  4. 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;
  1. 5
  2. 6
  3. 7
  4. 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. 1
  2. 0
  3. NULL
  4. 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);
  1. A row with 1
  2. No rows
  3. An error
  4. 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);
  1. Laptop
  2. Laptop and Stuhl
  3. All products
  4. 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';
  1. NULL
  2. unknown
  3. An empty string
  4. 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;
  1. 6
  2. 4
  3. 5
  4. 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;
  1. 9
  2. 25
  3. 5
  4. 19
Show answer

Correct: 25

1+2+1+10+2+1+4+1+3 = 25.

What is the difference between UNION and UNION ALL?

  1. UNION ALL removes duplicates, UNION does not
  2. UNION removes duplicates, UNION ALL keeps them (and is faster)
  3. UNION is only allowed in MySQL
  4. 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?

  1. It saves the transaction permanently
  2. It discards all changes since BEGIN
  3. It deletes the table
  4. 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 = ??

  1. Every column of the table
  2. The foreign key kunde_id in bestellungen
  3. Only columns of type TEXT
  4. 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');
  1. The row is inserted
  2. A foreign key error occurs
  3. kunde_id is set to NULL
  4. 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;
  1. bezahlt | 3
  2. offen | 2
  3. storniert | 1
  4. 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?

  1. Because FLOAT has no decimal point
  2. Because of rounding errors in the binary floating-point representation
  3. Because money is always an integer
  4. 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?

  1. Returns NULL if a equals b, otherwise a
  2. Checks whether a is NULL
  3. Replaces NULL with b
  4. 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, 1, 3
  2. 1, 2, 3
  3. 1, 1, 2
  4. 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. 1, 2, 3
  2. 1, 3, 6
  3. 6, 6, 6
  4. 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;
  1. 16
  2. 20
  3. 32
  4. 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?

  1. Returns the value of the next row
  2. Returns the value of the previous row (NULL for the first)
  3. Sums all values so far
  4. 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?

  1. YEAR is invalid
  2. The function on the column prevents the index lookup (not sargable)
  3. Indexes only apply to numbers
  4. 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?

  1. WHERE kunde_id = 1
  2. WHERE kunde_id = 1 AND datum > '2024-01-01'
  3. WHERE datum > '2024-01-01'
  4. 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)?

  1. Atomic cell values
  2. No partial dependencies on the composite key
  3. No transitive dependencies
  4. 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?

  1. For deleting duplicate rows
  2. For atomic "insert or update" (UPSERT)
  3. For locking a table
  4. 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?

  1. Delete quotation marks in inputs
  2. Parameterized queries (prepared statements)
  3. Only allow GET requests
  4. 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?

  1. Window functions are slower and therefore obsolete
  2. Window functions keep all rows, GROUP BY collapses them
  3. GROUP BY does not allow aggregates
  4. 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?

  1. The table is read completely (no index)
  2. An index is used optimally
  3. The query is faulty
  4. 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?

  1. READ UNCOMMITTED
  2. READ COMMITTED
  3. REPEATABLE READ
  4. 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?

  1. A transaction waits for a lock held by a second one that itself is waiting for the first
  2. A crashed database server
  3. A syntax error in a view
  4. 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.