SQL Interview Questions for Data Analysts: GROUP BY, HAVING and JOINs

LAB 07 The Interview Lab · Data Analyst

Crack the SQL round for data analyst jobs

Understand the SQL questions every analyst interview asks, from WHERE and GROUP BY to JOINs and NULLs, and explain your queries with confidence.

⏱ 12 min🎯 Freshers & career switchers🧪 4 hands-on drills
Your lab progress0 of 4 drills done
Spot itDecodeBuildTest

SQL (Structured Query Language) is the language you use to ask a database questions. Almost every data analyst interview has a SQL round, whether it is a campus placement, an online test or a live screen-share. Interviewers use it to check if you can turn a business question into correct data.

In this lab you will learn the core ideas behind the most common SQL questions: filtering, grouping, joining tables and handling missing values. You will also practise explaining a query out loud, which is often what decides the round.

The big idea: SQL runs in a different order

You write SELECT first, but the database works in three stages.

Step 1
FRAME 1
Rows
FROM, JOIN and WHERE pick tables and filter single rows.
Step 2
FRAME 2
Groups
GROUP BY makes groups and HAVING filters those groups.
Step 3
FRAME 3
Result
SELECT picks columns, then ORDER BY and LIMIT shape the output.
WHERE filters rows. HAVING filters groups.
Drill 1 · Spot it

Spot the difference

Question: “What is the difference between WHERE and HAVING?” Switch between the two answers.

Both WHERE and HAVING are used to filter data, so they are mostly the same. I use HAVING when the query is big. I think HAVING is also faster. GROUP BY is used to arrange the data in order. I have used SQL in my college project, so I know all these commands well and I can write any query.
✕ Says WHERE and HAVING are the same, which is wrong
✕ Confuses GROUP BY with sorting (that is ORDER BY)
✕ No example to prove real understanding
Drill 2 · Decode

Quick concept check

Tap each card to flip it and see the correct answer.

Key concepts, explained simply

Each idea starts with an everyday picture, then the correct SQL meaning.

SELECT and WHERELike asking a librarian for only the books you need. SELECT chooses the columns. WHERE keeps only rows that meet a condition, like city = ‘Pune’. You can combine filters with AND, OR, IN, BETWEEN and LIKE.
GROUP BY and aggregatesLike sorting exam papers into piles by class, then counting each pile. GROUP BY makes the groups; COUNT, SUM, AVG, MIN and MAX give one value per group. Columns in SELECT that are not aggregated should be in GROUP BY.
JOIN typesLike matching a class list with a fee list using roll numbers. INNER JOIN keeps matches only. LEFT JOIN keeps all rows from the left table, RIGHT JOIN from the right. FULL OUTER JOIN keeps both (MySQL does not support it directly).
Order of executionSQL is written SELECT first but runs FROM first: FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, then LIMIT. That is why an alias made in SELECT usually cannot be used in WHERE, but works in ORDER BY.
Window functionsLike giving ranks in a class without merging the students into one row. ROW_NUMBER, RANK and DENSE_RANK add a position to each row. With a tie, RANK skips numbers (1, 1, 3) but DENSE_RANK does not (1, 1, 2).
Subqueries and CTEsLike solving a big sum in small steps on rough paper. A subquery is a query inside another query. A CTE (WITH name AS …) gives that step a name, so long queries are easier to read and check.
-- Top 3 cities by delivered revenue since Jan 2025 (100+ orders)
SELECT c.city,
       COUNT(*)      AS orders,
       SUM(o.amount) AS revenue
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.status = 'Delivered'
  AND o.order_date >= '2025-01-01'
GROUP BY c.city
HAVING COUNT(*) >= 100
ORDER BY revenue DESC
LIMIT 3;
💡 Say your plan out loud before typing: “First I will join, then filter, then group.” Also note that LIMIT works in MySQL and PostgreSQL, while SQL Server uses TOP.
Drill 3 · Build

Explain a query you wrote

Fill the blanks. Your answer writes itself on the right.

Your project answer
Start typing on the left and your answer will appear here.
⏱ ~0 seconds spoken0 words

Model answers to three classic SQL questions

These come up again and again. Learn the idea, not just the query.

“I would use a subquery: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). It finds the top salary, then the highest one below it. For the Nth highest salary, I prefer DENSE_RANK() OVER (ORDER BY salary DESC) and pick rank N, because it handles ties correctly.”

Four SQL mistakes that cost you the round

Using WHERE with aggregatesWHERE COUNT(*) > 5 gives an error. Filter groups with HAVING instead.
Breaking a LEFT JOIN by accidentA WHERE filter on the right table removes the NULL rows, so it behaves like an INNER JOIN. Put that condition in the ON clause to keep all left rows.
Writing = NULLAlways use IS NULL. Also remember AVG and other aggregates ignore NULL values, which can change your answer.
Typing in silenceInterviewers want your thinking. Ask about the tables, say your plan, then write. Check edge cases like ties and duplicates.
Drill 4 · Test

Test yourself

Question 1 of 5

0:00

Rehearse before the real thing

Pick one sample question, press start and explain your query out loud. The ring turns amber at 60 seconds and red at 90.

Interview Lab · 07
SQL Interview Must-Knows
  1. 1WHERE rows, HAVING groups
  2. 2Know INNER vs LEFT JOIN
  3. 3Use IS NULL, never = NULL
  4. ★Say your plan before you type
worldremotejobs.com@keshav_consults

Screenshot it. Save it. Share it.

Keep this card on your phone and look at it five minutes before your interview.

Share on WhatsApp
Next in the lab
Lab 08 · Why are you leaving your current job?
Open lab →

Quick answers

Should I learn MySQL or PostgreSQL for analyst jobs?

Either is fine. Most interview questions use standard SQL that works in both. Learn one well and note small differences, like LIMIT in MySQL and PostgreSQL versus TOP in SQL Server.

What do fresher SQL rounds usually cover?

Mostly SELECT, WHERE, GROUP BY, HAVING, JOINs and aggregate functions. Many companies also add a window function or CTE question, so practise those too.

Do I really need window functions?

For many analyst roles, yes, at a basic level. Know ROW_NUMBER, RANK and DENSE_RANK, and running totals with SUM() OVER (ORDER BY date).

What if I cannot run the query in the interview?

Many first rounds use a shared doc or simple editor. Write clean, indented SQL, explain each step and walk through a tiny sample of data to show the output.

More remote jobs

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *