All subjectsDatabases · 44 questions · 15 junior / 22 mid / 7 senior

Databases & SQL quiz and interview questions

SQL is asked in nearly every backend interview and database exam. This quiz covers keys, joins, aggregates, NULL traps, normalization, indexes and transactions — including queries where you have to work out the result.

Databases & SQL quiz44 questions in the bank · 10 per round
Level
Question type
Mode

Databases & SQL: key concepts

Keys
A primary key uniquely identifies each row and can't be NULL. A foreign key points to a primary key in another table and keeps related data consistent.
Joins
INNER JOIN returns only matching rows. LEFT JOIN returns every row from the left table, with NULLs where there's no match.
GROUP BY and HAVING
WHERE filters rows before grouping; HAVING filters groups after GROUP BY, so it can use aggregates like COUNT(*) > 5.
NULL
NULL means unknown. Compare with IS NULL / IS NOT NULL — = NULL is never true. COUNT(column) skips NULLs, COUNT(*) doesn't.
Normalization
1NF: atomic values. 2NF: no partial dependency on part of a composite key. 3NF: no transitive dependency between non-key columns.
Indexes and transactions
Indexes speed up reads but slow down writes. A transaction is ACID: all-or-nothing, consistent, isolated and durable.

All 44 Databases & SQL questions with answers

Try each one first, then open the answer. Making a class test? Use the test maker.

  1. juniorConcepts

    Which is true for a primary key?

    • A. It can contain NULL
    • B. It must be unique and not NULL
    • C. A table can have many primary keys
    • D. It must be a number
    Show answer

    B. It must be unique and not NULL — A primary key identifies each row, so it must be unique and NOT NULL. A table has one primary key (which may span several columns).

  2. juniorConcepts

    What is a foreign key for?

    • A. Speeding up queries
    • B. Linking a row to a row in another table and keeping that link valid
    • C. Encrypting data
    • D. Sorting results
    Show answer

    B. Linking a row to a row in another table and keeping that link valid — Orders.CustomerId references Customers.Id, so the database rejects orders for customers that don't exist.

  3. juniorPredict the output

    The Orders table has amounts 100, NULL and 50. What does this return?

    SELECT COUNT(amount) FROM Orders;
    • A. 3
    • B. 2
    • C. 150
    • D. NULL
    Show answer

    B. 2 — COUNT(column) counts only non-NULL values, so it returns 2. COUNT(*) would return 3.

  4. juniorConcepts

    What is the difference between WHERE and HAVING?

    • A. None
    • B. WHERE filters rows before grouping; HAVING filters groups after GROUP BY
    • C. HAVING is faster
    • D. WHERE works only with numbers
    Show answer

    B. WHERE filters rows before grouping; HAVING filters groups after GROUP BY — Use WHERE for row conditions and HAVING for conditions on aggregates, e.g. HAVING COUNT(*) > 5.

  5. midPredict the output

    Customers: A, B, C. Orders: two for A, one for B, none for C. How many rows does this return?

    SELECT c.name, o.id
    FROM Customers c
    LEFT JOIN Orders o ON o.customer_id = c.id;
    • A. 3
    • B. 4
    • C. 2
    • D. 6
    Show answer

    B. 4 — LEFT JOIN keeps every customer: A appears twice, B once, and C once with a NULL order — 4 rows. An INNER JOIN would return 3.

  6. midConcepts

    A table is in 3NF when it is in 2NF and…

    • A. Has no NULLs
    • B. Has no transitive dependencies (non-key columns depend only on the key)
    • C. Has an index on every column
    • D. Uses only integers
    Show answer

    B. Has no transitive dependencies (non-key columns depend only on the key) — If Employee stores DeptId and DeptName, DeptName depends on DeptId, not the employee key — move it to a Departments table.

  7. midConcepts

    What is the trade-off of adding an index?

    • A. None
    • B. Faster reads, but slower inserts/updates and more storage
    • C. Slower reads and faster writes
    • D. It removes duplicate rows
    Show answer

    B. Faster reads, but slower inserts/updates and more storage — Every insert, update and delete must also update the index. Index the columns you filter and join on, not everything.

  8. juniorFind the bug

    Why does SQL Server reject this query?

    SELECT name, COUNT(*)
    FROM Employees;
    • A. COUNT needs a column name
    • B. name must be in a GROUP BY clause (or inside an aggregate)
    • C. Missing WHERE
    • D. Employees is a reserved word
    Show answer

    B. name must be in a GROUP BY clause (or inside an aggregate) — When you mix aggregates and plain columns, every plain column must be grouped: ... GROUP BY name.

  9. midConcepts

    In ACID, what does atomicity mean?

    • A. Data is encrypted
    • B. A transaction happens completely or not at all
    • C. Queries run in parallel
    • D. Each table has one key
    Show answer

    B. A transaction happens completely or not at all — Moving money between accounts: the debit and credit both commit or both roll back — never half.

  10. midFind the bug

    Some users have no email, but this returns zero rows. Why?

    SELECT * FROM Users WHERE email = NULL;
    • A. Users is empty
    • B. = NULL is never true; use IS NULL
    • C. email must be indexed
    • D. Needs quotes around NULL
    Show answer

    B. = NULL is never true; use IS NULL — Comparing anything to NULL with = gives UNKNOWN, not TRUE. Write WHERE email IS NULL.

More subjects

Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.

Chat on WhatsApp