SQL Interview Questions for Data Analysts: 8 With a Trap

By ··9 min read

Short answer

SQL interview questions for data analysts rarely test syntax. They test whether you check your result, because a query can run without errors and still return the wrong number. The eight questions below cover WHERE and HAVING, joins, ranking, duplicates, NOT IN, COUNT, running totals and retention. Each one comes with the trap inside it and the check that catches it.

Most SQL tutorials teach syntax, and the syntax is the easy part. In an analyst interview, the person across the table watches what you do after the query runs. On a table with millions of rows, a query that returns without errors can still return the wrong number, so a quick check is worth more than a clever answer. To practise that habit, we go through eight common questions below. Each one has a trap, and each trap has a check. All examples use PostgreSQL.

1. What is the difference between WHERE and HAVING?

To filter rows before they are grouped, we use WHERE. To filter groups after the totals are calculated, we use HAVING. Since WHERE runs first, it shrinks the data before the grouping step starts. This means a condition that can be checked on a single row should go in WHERE. A shop manager does the same thing when she removes cancelled receipts first and only then compares the branches against a revenue target.

SELECT region, SUM(revenue) AS total_revenue
FROM orders
WHERE status = 'paid'          -- row filter
GROUP BY region
HAVING SUM(revenue) > 100000;  -- group filter

2. INNER JOIN vs LEFT JOIN: when does the result change?

INNER JOIN keeps only the rows that match on both sides. LEFT JOIN keeps every row from the left table, and it fills the right side with NULL when nothing matches. The two give the same result only if every left row has a match. Interviewers often follow up with a filter on the right table, because that is where the two joins quietly turn into one. We walk through this case in LEFT JOIN vs INNER JOIN: why your row count changes.

3. Find the second-highest salary

This is a ranking problem, and it depends on how ties count. If two people share the top salary, the second-highest salary can mean the next different value, or it can mean the second row. In most interviews it means the next different value, so we use DENSE_RANK.

WITH r AS (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
  FROM employees
)
SELECT MAX(salary) AS second_highest
FROM r
WHERE rnk = 2;

Wrapping the result in MAX returns one row with NULL when there is no second salary. That is the honest answer, and it avoids an empty result that the caller might misread.

4. Remove duplicates and keep the latest row

A user table often holds the same email several times, because every update inserted a new row. To keep one row per email, we number the rows inside each email and keep number 1.

WITH d AS (
  SELECT *, ROW_NUMBER() OVER (
    PARTITION BY email ORDER BY updated_at DESC, id DESC) AS rn
  FROM users
)
SELECT * FROM d WHERE rn = 1;

5. NOT IN vs NOT EXISTS

Both can answer "which customers have no orders", but they treat NULL differently. In NOT IN, one NULL in the list makes every comparison unknown, so no row comes back.

-- WRONG: returns 0 rows if any orders.customer_id is NULL
SELECT * FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders);

-- safe version
SELECT * FROM customers c
WHERE NOT EXISTS (
  SELECT 1 FROM orders o WHERE o.customer_id = c.id);

6. COUNT(*), COUNT(column) and COUNT(DISTINCT column)

  • COUNT(*) counts every row.
  • COUNT(column) counts the rows where that column is not NULL.
  • COUNT(DISTINCT column) counts the different values that are not NULL.

7. Calculate a running total

A running total adds each day to everything before it. It is a window function with a frame, and the frame decides which rows are included.

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

8. Calculate day-7 retention

Retention answers one question. Of the people who signed up, how many came back on day 7? The denominator is therefore every signup, and the query has to start from the users table.

SELECT COUNT(DISTINCT a.user_id) * 1.0
       / COUNT(DISTINCT u.user_id) AS d7_retention
FROM users u
LEFT JOIN activity a
  ON a.user_id = u.user_id
 AND a.activity_date = u.signup_date + INTERVAL '7 days';

The habit that matters more than the answer

After every query, say how many rows you expected and compare that with what you got. For example, "I expected one row per customer, and I got one per order, so I need to check the join". That sentence shows the skill the job needs. It also gives the interviewer something to follow, even when the query itself has a bug.

Frequently asked questions

What SQL topics are asked most in data analyst interviews?

Joins, GROUP BY with HAVING, window functions, subqueries or CTEs and NULL handling appear in almost every published question list for analyst roles.

Which SQL dialect should I prepare for?

Learn standard SQL first, then ask which database the company uses. PostgreSQL, MySQL, SQL Server and BigQuery differ mostly in date functions and a few keywords.

How do I practise for a SQL interview?

Practise on data you can inspect, write the expected row count before you run each query, and explain the result out loud. Reading answers without running queries does not build this skill.

References

Run these queries yourself.

Predict the result, run it, and check the row count. That habit is what Practice trains.