This guide covers the SQL questions that come up most often in technical interviews for roles ranging from junior data analyst to senior data engineer. Questions are organized by topic, with each answer explaining the why alongside an example.

SELECT & Filtering

How do you find duplicate records in a table?

Use GROUP BY with HAVING COUNT(*) > 1, or ROW_NUMBER() window function partitioned by the column(s) you want to check for duplicates.

SELECT email, COUNT(*) as cnt
FROM users
GROUP BY email
HAVING COUNT(*) > 1;

How do you find the Nth highest salary from an employee table?

Use DISTINCT with ORDER BY and LIMIT/OFFSET (MySQL/PostgreSQL) or a subquery with ROW_NUMBER().

-- SQL Server
SELECT DISTINCT salary
FROM (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank
  FROM employees
) t
WHERE rank = 2; -- 2nd highest

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping and aggregation occurs. HAVING filters groups after the GROUP BY aggregation. Use WHERE for row-level conditions, HAVING for aggregated-group conditions.

-- WHERE: filter before aggregation
SELECT department, COUNT(*)
FROM employees
WHERE hire_date > '2023-01-01'
GROUP BY department;

-- HAVING: filter after aggregation
SELECT department, COUNT(*) as cnt
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

JOINs

JOIN questions are a staple of SQL interviews because they test both understanding of relational theory and practical query-writing ability. See our full guide to SQL JOINs.

How do you find employees who have never placed an order?

Use a LEFT JOIN from employees to orders and filter for NULL in the orders table, or use NOT IN / NOT EXISTS subqueries.

-- LEFT JOIN approach
SELECT e.name
FROM employees e
LEFT JOIN orders o ON e.id = o.employee_id
WHERE o.id IS NULL;

-- NOT EXISTS (often better performance)
SELECT e.name
FROM employees e
WHERE NOT EXISTS (
  SELECT 1 FROM orders o WHERE o.employee_id = e.id
);

What is the difference between INNER JOIN and OUTER JOIN?

INNER JOIN returns only rows with matching values in both tables. OUTER JOIN (LEFT, RIGHT, FULL) returns all rows from one or both tables, with NULLs for non-matching rows.

-- INNER JOIN: only matches
SELECT e.name, d.name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;
-- Result: only employees who have a department

-- LEFT JOIN: all from left table
SELECT e.name, d.name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.id;
-- Result: all employees, NULL for those without department

How do you join three or more tables?

Chain JOINs with ON clauses for each relationship, or use multiple JOINs in a single FROM clause.

SELECT o.order_id, c.customer_name, e.employee_name
FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN employees e ON o.employee_id = e.id
WHERE o.order_date > '2025-01-01';

Aggregation & Window Functions

Window functions (OVER, PARTITION BY) are the divide between junior and mid-level SQL candidates. Interviewers ask these because they separate people who know SELECT from people who understand relational algebra.

How do you calculate a running total in SQL?

Use a window function with ORDER BY in the OVER() clause. SUM(amount) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) gives a running total.

SELECT
  order_date,
  amount,
  SUM(amount) OVER (
    ORDER BY order_date
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  ) AS running_total
FROM orders
ORDER BY order_date;

How do you find the first and last value in a group?

Use FIRST_VALUE() and LAST_VALUE() window functions with appropriate window framing, or use subqueries with ORDER BY and LIMIT.

SELECT
  department,
  employee_name,
  hire_date,
  FIRST_VALUE(employee_name) OVER (
    PARTITION BY department
    ORDER BY hire_date
  ) AS first_hired,
  LAST_VALUE(employee_name) OVER (
    PARTITION BY department
    ORDER BY hire_date
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  ) AS last_hired
FROM employees;

What is the difference between RANK() and DENSE_RANK()?

RANK() skips rank numbers after tied rows (1, 1, 3). DENSE_RANK() does not skip (1, 1, 2). Use DENSE_RANK() when you need consecutive ranking without gaps.

-- RANK(): 1, 1, 3 (skips 2)
SELECT name, score,
  RANK() OVER (ORDER BY score DESC) as rank,
  DENSE_RANK() OVER (ORDER BY score DESC) as dense_rank
FROM students;

Subqueries & CTEs

Common Table Expressions (CTEs) have largely replaced deeply nested subqueries in modern codebases. Understanding both — and knowing when each performs better — is what interviewers are really testing.

What is a CTE and when should you use it?

A Common Table Expression (CTE) is a named temporary result set defined within a query using WITH. Use it to simplify complex joins, improve readability, or reference the same result multiple times in a query.

WITH recent_orders AS (
  SELECT customer_id, MAX(order_date) as last_order
  FROM orders
  GROUP BY customer_id
)
SELECT c.name, r.last_order
FROM customers c
JOIN recent_orders r ON c.id = r.customer_id;

What is the difference between correlated and non-correlated subqueries?

Non-correlated subqueries execute independently of the outer query (run once). Correlated subqueries reference the outer query and execute once per row. Correlated subqueries are generally slower.

-- Correlated: references outer query
SELECT e.name
FROM employees e
WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id);

-- Non-correlated: independent
SELECT name FROM employees
WHERE dept_id IN (SELECT id FROM departments WHERE name = 'Sales');

Data Modification

UPDATE and INSERT questions test whether candidates can write modification queries that affect multiple rows correctly — something that breaks in subtle ways in production.

How do you UPDATE rows based on another table?

Use an UPDATE with JOIN (SQL Server) or UPDATE FROM subquery, or use a correlated subquery.

-- SQL Server
UPDATE e
SET e.salary = e.salary * 1.1
FROM employees e
JOIN departments d ON e.dept_id = d.id
WHERE d.name = 'Engineering';

-- MySQL / Standard
UPDATE employees e
SET salary = salary * 1.1
WHERE dept_id IN (
  SELECT id FROM departments WHERE name = 'Engineering'
);

How do you INSERT or UPDATE (upsert) a row?

Use MERGE (SQL Server/Oracle), INSERT ON CONFLICT (PostgreSQL), or INSERT + UPDATE with conditional logic.

-- SQL Server
MERGE INTO customers AS target
USING (VALUES ('john@example.com', 'John Doe')) AS source(email, name)
ON target.email = source.email
WHEN MATCHED THEN
  UPDATE SET name = source.name
WHEN NOT MATCHED THEN
  INSERT (email, name) VALUES (source.email, source.name);

Schema & DDL

Schema questions test database design fundamentals. Strong candidates understand not just that constraints exist but why each one matters for data integrity and query performance.

What is the difference between PRIMARY KEY and UNIQUE constraint?

A table can have only one PRIMARY KEY (clustered by default) and it cannot contain NULLs. A table can have multiple UNIQUE constraints and each column can contain one NULL value (in most databases).

CREATE TABLE users (
  id INT PRIMARY KEY,           -- one per table, not nullable
  email VARCHAR(255) UNIQUE,    -- many per table, allows NULL
  phone VARCHAR(20) UNIQUE,     -- many per table, allows NULL
  name VARCHAR(100)             -- no constraint
);

What is the difference between CLUSTERED and NONCLUSTERED indexes?

CLUSTERED indexes sort and store data rows in the table in order (one per table). NONCLUSTERED indexes are separate structures pointing to data rows (multiple per table).

-- Clustered index (data stored in sorted order)
CREATE CLUSTERED INDEX idx_name ON employees(dept_id);

-- Nonclustered index (separate pointer structure)
CREATE NONCLUSTERED INDEX idx_name ON employees(dept_id, hire_date);

Frequently Asked Questions

Quick-reference answers to the most common follow-up questions.

What is the difference between DELETE and TRUNCATE in SQL?
DELETE is a DML command that removes rows one by one and logs each deletion, making it slower on large tables but allowing WHERE clauses and rollbacks. TRUNCATE is a DDL command that deallocates data pages, making it faster for large datasets but not allowing WHERE clauses and not being fully logged (making it harder to rollback in some databases).
What is the difference between UNION and UNION ALL?
UNION removes duplicate rows from the combined result set, while UNION ALL retains all rows including duplicates. Since UNION performs a DISTINCT operation, it is slower than UNION ALL, especially on large datasets.
What is a window function in SQL?
Window functions perform calculations across a set of table rows that are somehow related to the current row. Unlike aggregate functions that group rows, window functions keep rows separate and add an extra column with the calculated value. Examples include ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), and LAG().
What is the difference between RANK(), DENSE_RANK(), and ROW_NUMBER()?
All three assign ranking numbers to rows within a partition. ROW_NUMBER() always assigns unique sequential numbers (no ties). RANK() assigns the same rank to tied values but skips subsequent ranks (1, 2, 2, 4). DENSE_RANK() also assigns the same rank to tied values but does not skip ranks (1, 2, 2, 3).
What is a subquery and when should you use it?
A subquery is a query nested inside another query. Use it when you need to filter or compute values based on the result of another query. CTEs are often a cleaner alternative for multi-step queries. Correlated subqueries reference columns from the outer query and are evaluated once per row.