Skip to content

Data · updated 26 November 2026

SQL is the language of relational databases. These scenarios test query design, indexing, transactions and correctness.

Top 10 SQL scenario questions

Try each one yourself first, then open the model answer.

1. A query that was fast now takes a minute as the table grew. Where do you start?

Show answer

Read the execution plan with EXPLAIN, look for full scans on large tables, add or adjust indexes for the filter and join columns, and check that statistics are current.

2. An index exists on a date column but WHERE YEAR(created_at) = 2026 ignores it. Why?

Show answer

Applying a function to the column usually prevents index use. Filter with a range: created_at >= '2026-01-01' AND created_at < '2027-01-01'.

3. A LEFT JOIN returns only matched rows after you added a condition on the right table. Why?

Show answer

A WHERE condition on the right table removes rows where it is NULL. Put that condition in the ON clause.

4. Two users buy the last item at the same time and stock goes negative. How do you prevent it?

Show answer

Use a transaction with a conditional update such as SET stock = stock - 1 WHERE id = ? AND stock > 0 and check the affected rows, or lock the row with SELECT ... FOR UPDATE.

5. NOT IN with a subquery returns no rows at all. What is the likely cause?

Show answer

The subquery returns a NULL, which makes the comparison unknown. Use NOT EXISTS or filter out NULL values.

6. You need the most recent order for each customer. How would you write it?

Show answer

Use a window function: ROW_NUMBER partitioned by customer and ordered by order date descending, then keep row number 1.

7. Totals are inflated after joining orders to both items and payments. Why?

Show answer

Joining two one-to-many tables multiplies rows. Aggregate each side in a subquery or CTE first, then join the results.

8. Deep pagination with a large OFFSET is slow. What is the alternative?

Show answer

Use keyset pagination: filter by the last seen value of an indexed, unique ordering and apply LIMIT.

9. Two transactions keep deadlocking. How do you reduce it?

Show answer

Access tables and rows in a consistent order, keep transactions short, index the columns used to find rows, and retry the transaction when a deadlock is reported.

10. You must add a column to a very large table in production. How do you stay safe?

Show answer

Check how your database version applies the change, test on a copy, prefer online or non-blocking methods, backfill in batches and have a rollback plan.

Ready to build something great?

Book your free 30-minute consultation today. No obligation, just honest advice.