How to Solve an SQL Interview Problem

SQL Interview Problems: 12 Classic Questions Solved · Start Here · 7 min read · Updated

The six-step method

  1. Start by saying the question again in your own words.
  2. Ask what should happen with ties, NULL values and empty results.
  3. Write down the output columns and what one row means.
  4. Write the simplest correct query first and run it.
  5. Test it on the tie, the NULL and the case with no match.
  6. Explain what the query does and what you would improve.

Most SQL interview problems are not hard because of the syntax. They are hard because you have a few minutes, someone is watching, and the question is short and a little vague. This course gives you two things: a method that keeps you calm, and the twelve problem types that appear again and again in interviews for data, backend and analyst roles. If you are new to SQL, take the free SQL for Beginners course on this site first. Here we assume you can already write a SELECT with WHERE, GROUP BY and a simple JOIN.

The practice database

Every lesson uses the same small database, so you can see the whole data in your head. It has employees with managers, departments, sales, daily logins and a contacts list that contains duplicates. The names and numbers are invented. Run the script once in any SQLite tool, such as the sqlite3 command or DB Browser for SQLite, and keep the file open while you read. Every output shown in this course was produced by running the query on exactly this data.

CREATE TABLE departments (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept_id INTEGER,
  manager_id INTEGER,
  salary INTEGER,
  hired DATE,
  FOREIGN KEY (dept_id) REFERENCES departments(id),
  FOREIGN KEY (manager_id) REFERENCES employees(id)
);
CREATE TABLE sales (
  id INTEGER PRIMARY KEY,
  employee_id INTEGER NOT NULL,
  sale_date DATE NOT NULL,
  amount INTEGER NOT NULL,
  FOREIGN KEY (employee_id) REFERENCES employees(id)
);
CREATE TABLE logins (
  user_id INTEGER NOT NULL,
  login_date DATE NOT NULL
);
CREATE TABLE contacts (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL
);

INSERT INTO departments (id, name) VALUES
 (1, 'Engineering'), (2, 'Sales'), (3, 'HR'), (4, 'Finance'), (5, 'Legal');

INSERT INTO employees (id, name, dept_id, manager_id, salary, hired) VALUES
 (1, 'Meera Joshi', 1, NULL, 150000, '2019-03-04'),
 (2, 'Arjun Rao', 1, 1, 120000, '2020-06-15'),
 (3, 'Sara Khan', 1, 1, 120000, '2021-01-10'),
 (4, 'Liam Brown', 1, 2, 95000, '2022-08-01'),
 (5, 'Nia Okoye', 1, 2, 130000, '2022-11-21'),
 (6, 'Omar Ali', 2, 1, 110000, '2020-02-20'),
 (7, 'Priya Nair', 2, 6, 70000, '2021-09-09'),
 (8, 'Tom Hill', 2, 6, 70000, '2022-03-14'),
 (9, 'Zoe Park', 2, 6, 65000, '2023-05-02'),
 (10, 'Ravi Menon', 3, 1, 90000, '2019-11-11'),
 (11, 'Ella Stone', 3, 10, 55000, '2023-01-23'),
 (12, 'Dev Patel', 4, 1, 100000, '2020-10-05'),
 (13, 'Chloe Wu', 4, 12, 72000, '2022-12-12'),
 (14, 'Jack Reed', 4, 12, NULL, '2024-02-01');

INSERT INTO sales (id, employee_id, sale_date, amount) VALUES
 (1, 6, '2024-01-05', 1200), (2, 7, '2024-01-18', 800), (3, 8, '2024-01-25', 500),
 (4, 6, '2024-02-02', 1500), (5, 7, '2024-02-14', 900), (6, 8, '2024-02-20', 700), (7, 6, '2024-02-27', 400),
 (8, 7, '2024-03-03', 1100), (9, 8, '2024-03-12', 600), (10, 6, '2024-03-19', 2000), (11, 7, '2024-03-28', 300),
 (12, 6, '2024-04-04', 1800), (13, 8, '2024-04-15', 1000), (14, 7, '2024-04-22', 500);

INSERT INTO logins (user_id, login_date) VALUES
 (1, '2024-03-01'), (1, '2024-03-02'), (1, '2024-03-03'), (1, '2024-03-05'),
 (1, '2024-03-06'), (1, '2024-03-07'), (1, '2024-03-08'), (1, '2024-03-10'),
 (2, '2024-03-01'), (2, '2024-03-02'), (2, '2024-03-04'), (3, '2024-03-05');

INSERT INTO contacts (id, name, email) VALUES
 (1, 'Asha', 'asha@example.com'), (2, 'Ben', 'ben@example.com'), (3, 'Asha R', 'asha@example.com'),
 (4, 'Cara', 'cara@example.com'), (5, 'Ben B', 'ben@example.com'), (6, 'Asha R2', 'asha@example.com');

The six-step method

  1. Repeat the question in your own words. Out loud, in one sentence. This catches a misunderstanding in ten seconds instead of ten minutes.
  2. Ask about the edge cases before you write. What should happen with ties? With NULL values? When nothing matches? Interviewers like this question, because it is what you would ask at work.
  3. Write down the output. Which columns, in which order, one row per what? "One row per department" tells you to use GROUP BY or a window function with a partition.
  4. Pick the tables and the join. Name the tables you need and how they connect before you type any SQL.
  5. Write the simplest correct version first. A plain subquery that works beats a clever window function that you cannot finish. You can improve it afterwards.
  6. Test it against the traps. Run it on the tie, the NULL and the empty case, and say what you see.

The order SQL really runs in

Many wrong answers come from forgetting that SQL does not run in the order you write it. The database first reads FROM and JOIN, then filters rows with WHERE, then groups with GROUP BY, then filters groups with HAVING, then calculates the SELECT list (including window functions), and last it applies ORDER BY and LIMIT. This is why you cannot use a column alias from SELECT inside WHERE, why a window function result cannot be filtered in the same query level, and why HAVING exists at all. When a query behaves strangely, walk through this order slowly.

Say your thinking, not only your code

Interviewers cannot read your mind. When you pause to think, say what you are weighing: "I could use a subquery here, or a window function. The window function is cleaner if there are ties." If you get stuck, say what you do know, then solve a smaller version, for example one department instead of all of them. People are often hired for how they handle being stuck, not for never being stuck.

Small things that save you

  • Write keywords in capitals and put one clause per line. A neat query is easier for you to debug and for the interviewer to read.
  • Use short table aliases such as e for employees, and always write e.name, not just name, when two tables are involved.
  • Run the query often, in small pieces. First the subquery alone, then the whole.
  • If you are not allowed to run anything, trace a tiny example by hand and write the result in a comment.
  • Engines differ a little. LIMIT 1 is MySQL, PostgreSQL and SQLite. SQL Server uses TOP 1, and Oracle uses FETCH FIRST 1 ROWS ONLY. Say which one you are writing for, and it shows you know the difference.

What to expect in this course

Each lesson takes one classic problem family, shows a first answer, then the version that survives ties and NULL values, then the common mistake. The families are: the Nth highest value, duplicates, the top rows in each group, rows that are missing, employees and their managers, running totals, month-over-month change, streaks, turning rows into columns, NULL traps, speed, and a mock round. None of this promises a job. It makes you faster and calmer, which is what practice can honestly do.

Your task

  1. Create the practice database from the script above and run SELECT * FROM employees;.
  2. Write the output you would want for this question, as column names only: "For each department, show the department name and the number of employees."
  3. Say out loud, or write, which tables you need and how they connect. Then write the query, and run it.
  4. Find one trap in the data: a NULL, an empty department, a tie. Write one sentence on how it could break a careless query.

How to check it worked

  • SELECT COUNT(*) FROM employees; returns 14.
  • Your department count query lists five departments, and Legal shows 0 if you used LEFT JOIN with COUNT(e.id). Lesson 5 explains why this matters.
  • You can name the six steps without looking.

Frequently asked questions

Do I need to memorise window functions?

You need to understand ROW_NUMBER, RANK, DENSE_RANK, LAG and running totals, because interviews ask for them often. Understanding the idea is more important than remembering every keyword.

What if I cannot finish the query?

Say what you have, solve a smaller version, and explain the next step. A clear plan with a half-finished query is better than silence.

Which SQL database should I practise on?

Any one is fine. This course uses SQLite because it is free and has no setup. Window functions and common table expressions work in PostgreSQL, MySQL 8 and SQL Server with small syntax changes.