Black Friday Sale20% off every plan — this weekend onlyEnds in04days11hrs51min50secSee plans

SQL interview questions for data analysts (with answers)

SQL Updated 3 min read By LogicByte team

Short answer

Data analyst SQL rounds mostly test five things: filtering with WHERE, grouping with GROUP BY and HAVING, joins, subqueries or CTEs, and window functions such as ROW_NUMBER and RANK. Below are the questions asked most often, each with a short answer and a query you can adapt.

On this page
  1. Basics they check in the first five minutes
  2. Joins
  3. Aggregation and window functions
  4. How to prepare

Basics they check in the first five minutes

1. What is the difference between WHERE and HAVING?

WHERE filters rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates like COUNT or SUM.

SELECT department, COUNT(*) AS staff
FROM employees
WHERE status = 'active'
GROUP BY department
HAVING COUNT(*) > 10;

2. What is the order in which SQL runs a query?

FROM and JOIN, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY and finally LIMIT. This is why you cannot use a SELECT alias inside WHERE in most databases.

3. What is the difference between DELETE, TRUNCATE and DROP?

DELETE removes chosen rows and can use WHERE. TRUNCATE removes all rows quickly but keeps the table. DROP removes the table itself.

4. COUNT(*) vs COUNT(column)?

COUNT(*) counts every row. COUNT(column) skips rows where that column is NULL. COUNT(DISTINCT column) counts unique non-null values.

Joins

5. Explain INNER JOIN and LEFT JOIN

INNER JOIN keeps only rows that match in both tables. LEFT JOIN keeps every row from the left table and fills NULL where the right table has no match.

6. How do you find customers who never placed an order?

SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;

7. What is a self join?

A table joined to itself, for example to show each employee next to their manager when both live in the same employees table.

Aggregation and window functions

8. Find the second-highest salary

SELECT salary
FROM (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
  FROM employees
) ranked
WHERE rnk = 2;

See the full guide to the second-highest salary question for four ways to solve it.

9. RANK vs DENSE_RANK vs ROW_NUMBER

For salaries 100, 100, 90: ROW_NUMBER gives 1, 2, 3; RANK gives 1, 1, 3; DENSE_RANK gives 1, 1, 2.

10. Running total of sales by date

SELECT order_date, amount,
       SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM sales;

11. Find duplicate emails

SELECT email, COUNT(*) AS copies
FROM users
GROUP BY email
HAVING COUNT(*) > 1;

12. Top 3 products by revenue in each category

WITH ranked AS (
  SELECT category, product, SUM(revenue) AS revenue,
         ROW_NUMBER() OVER (PARTITION BY category ORDER BY SUM(revenue) DESC) AS rn
  FROM sales
  GROUP BY category, product
)
SELECT category, product, revenue FROM ranked WHERE rn <= 3;

How to prepare

  • Write every query yourself before reading the answer.
  • Say your logic out loud: interviewers score how you think, not only the final query.
  • Practise under a timer. Most SQL rounds give 20–40 minutes for 3–5 questions.

Try it yourself

5 questions from the LogicByte practice bank. Pick an answer to check it.

  1. 1What does DISTINCT do in a SELECT query?

    Show answer

    Answer: B — Removes duplicate result rows

    DISTINCT removes duplicate rows from the query result; it does not delete stored data.

  2. 2Which query returns only unique department names?

    SELECT DISTINCT department
    FROM Employees;
    Show answer

    Answer: D — SELECT DISTINCT department FROM Employees;

    DISTINCT is placed immediately after SELECT and before the selected column list.

  3. 3What is the result of SELECT DISTINCT city FROM Customers when cities are London, Paris, London?

    Show answer

    Answer: C — London, Paris

    The result contains each distinct city once.

  4. 4Does SELECT DISTINCT name, city return unique names only?

    Show answer

    Answer: B — No, uniqueness applies to the combination of name and city

    DISTINCT applies to the entire selected row combination, not each column independently.

  5. 5Which query finds unique product categories?

    SELECT DISTINCT category
    FROM Products;
    Show answer

    Answer: A — SELECT category UNIQUE FROM Products;

    The query returns each category value once.

Take the free 10-question SQL test

Frequently asked questions

Which SQL topics are asked most in data analyst interviews?

Joins, GROUP BY with HAVING, subqueries or CTEs, and window functions like ROW_NUMBER, RANK and running totals. Questions on NULL handling and duplicates are also common.

Is SQL enough to get a data analyst job?

SQL is usually the most tested skill, but most roles also expect Excel and a BI tool such as Power BI or Tableau. Python helps for larger datasets.

How many SQL questions should I practise before an interview?

Quality matters more than count. Make sure you can solve each common pattern (joins, grouping, ranking, duplicates, date filters) without looking it up, under a timer.