SQL & Databases interview questions

Joins, GROUP BY and HAVING, classic query puzzles, indexes, transactions, normalisation and slow-query debugging.

Readiness: 0 of 30 nailed

How to use this page: 1. Read a question2. Type your answer3. Check with AI4. Open the model answer5. Timed drill when you’re 70% ready

Querying

FoundationConcept

Explain INNER, LEFT and RIGHT JOIN.

FoundationConcept

What is the difference between WHERE and HAVING?

JuniorPredict the output

A table has 10 rows; 3 have discount = NULL, 7 have discount = 0. How many rows does this return?

SELECT * FROM orders WHERE discount <> 5;
JuniorConcept

In what order does SQL actually execute a SELECT query?

Mid-levelConcept

What are window functions? How are they different from GROUP BY?

FoundationPredict the output

A users table has 10 rows; 4 have phone = NULL. What do these return?

SELECT COUNT(*), COUNT(phone) FROM users;
FoundationConcept

UNION vs UNION ALL?

JuniorConcept

Subquery vs JOIN vs EXISTS: when do you use each?

Mid-levelIt’s broken: why?

Why does this return no rows even though some customers never ordered?

SELECT * FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders);
-- fix:
SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
JuniorConcept

What is a CTE (WITH clause), and what is a recursive CTE used for?

Classic puzzles

JuniorWrite it by hand

Find the second-highest salary.

JuniorWrite it by hand

Find duplicate emails in a users table, then delete duplicates keeping the lowest id.

Mid-levelWrite it by hand

Get the highest-paid employee in each department.

JuniorWrite it by hand

Find employees who earn more than their manager (same table has manager_id).

Mid-levelWrite it by hand

Find users who logged in on 3 consecutive days.

Performance

JuniorConcept

What is an index, and why not index every column?

Mid-levelReal-world scenario

A query that was fast now takes 8 seconds. How do you investigate?

Mid-levelConcept

You have an index on (country, city, created_at). Which queries can use it?

Mid-levelConcept

How do you read an EXPLAIN plan? What are the red flags?

Mid-levelWrite it by hand

Why is LIMIT 20 OFFSET 200000 slow, and what’s the alternative?

Transactions

Mid-levelConcept

What is a transaction and what does ACID mean?

SeniorConcept

Explain isolation levels and the anomalies they prevent.

Mid-levelIt’s broken: why?

Your app logs Deadlock found when trying to get lock. What does it mean and how do you fix it?

Mid-levelReal-world scenario

Two customers buy the last item at the same moment. How do you prevent overselling?

Design

JuniorConcept

What is normalisation? When would you denormalise?

Mid-levelReal-world scenario

SQL or NoSQL for a new project? How do you decide?

Mid-levelReal-world scenario

When would you store JSON in a relational database column?

Basics

FoundationConcept

What are DDL, DML, DCL and TCL? DELETE vs TRUNCATE vs DROP?

JuniorConcept

What are views, stored procedures and triggers? When would you use or avoid them?

Scaling

SeniorConcept

What is replication lag, and what bug can it cause?

Also prepare