๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณ
Limited-Time Offer!Get 20% OFF on all live courses
Enroll Now
PrakalpanaLive online tech training
Interview Prepโฑ๏ธ 14 min read๐Ÿ“… Jul 10

30 SQL Interview Questions for Data Analysts (2026), With Answers

VN
Vikram Nairโ€ขSenior Data Analyst
๐Ÿ“‘ Contents (23 sections)

๐Ÿ“ŒWhy SQL Decides Your Data Analyst Interview

Almost every Data Analyst interview has a SQL round, and it's where most candidates get rejected. The good news: the questions are predictable. Master the patterns below and you'll walk in confident.

These are the real questions asked at companies like Swiggy, Razorpay, Meesho, Amazon, Flipkart and top service companies, grouped from basics to window functions.

๐Ÿ“ŒBasics: Filtering & Sorting

Q1. What is the difference between WHERE and HAVING?

WHERE filters rows before grouping; HAVING filters groups after GROUP BY (and can use aggregate functions).

Q2. Difference between DISTINCT and GROUP BY?

Both remove duplicates. GROUP BY is used when you also need aggregations (COUNT, SUM). DISTINCT just returns unique rows.

Q3. Find the second-highest salary.

SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

Or, more robustly, with a window function (see Q22).

๐Ÿ“ŒJoins

Q4. Explain INNER, LEFT, RIGHT and FULL JOIN.

  • INNER, only matching rows in both tables
  • LEFT, all rows from the left table + matches
  • RIGHT, all rows from the right table + matches
  • FULL, all rows from both, matched where possible
  • Q5. What is a self join? Give a use case.

    A table joined to itself, e.g. finding employees and their managers from the same employees table.

    Q6. Customers who never placed an order.

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

    ๐Ÿ“ŒAggregations & GROUP BY

    Q7. Total revenue per month.

    SELECT DATE_TRUNC('month', order_date) AS month,
    SUM(amount) AS revenue
    FROM orders
    GROUP BY 1
    ORDER BY 1;

    Q8. Top 3 products by sales.

    SELECT product_id, SUM(amount) AS sales
    FROM order_items
    GROUP BY product_id
    ORDER BY sales DESC
    LIMIT 3;

    Q9. What does COUNT(*) vs COUNT(column) do differently?

    COUNT(*) counts all rows; COUNT(column) ignores NULLs in that column.

    ๐Ÿ“ŒSubqueries & CTEs

    Q10. What is a CTE and why use it?

    A Common Table Expression (WITH) is a named temporary result set that makes complex queries readable and lets you reference it multiple times.

    Q11. Categories with above-average order value.

    WITH cat_avg AS (
    SELECT category, AVG(amount) AS avg_amt
    FROM orders GROUP BY category
    )
    SELECT * FROM cat_avg
    WHERE avg_amt > (SELECT AVG(amount) FROM orders);

    Q12. Correlated vs non-correlated subquery?

    A correlated subquery references the outer query and runs per row; a non-correlated one runs once independently.

    ๐Ÿ“ŒWindow Functions (The Difference-Maker)

    Q22. Second-highest salary per department.

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

    Q23. Difference between ROW_NUMBER, RANK and DENSE_RANK?

  • ROW_NUMBER, unique sequential number, no ties
  • RANK, same rank for ties, skips next numbers
  • DENSE_RANK, same rank for ties, no gaps
  • Q24. Running total of daily sales.

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

    Q25. Month-over-month growth with LAG.

    SELECT month, revenue,
    revenue - LAG(revenue) OVER (ORDER BY month) AS mom_change
    FROM monthly_revenue;

    ๐Ÿ“ŒHow to Prepare (and Actually Pass)

    Reading answers isn't enough, you must write queries by hand. Practice on real schemas, explain your logic out loud, and rebuild each pattern from scratch. Analytical questions (cohorts, funnels, retention) increasingly use window functions, so get very comfortable with them.

    Our SQL for Data Analytics course drills exactly these patterns with real analytical case studies and mock interviews. For the complete analyst skill set, SQL plus Excel, statistics, Power BI and Tableau, see the Data Analytics course.

    Master these 30 patterns and the SQL round becomes the easiest part of your interview.

    VN

    Written by

    Vikram Nair

    Senior Data Analyst

    ๐Ÿš€ Master Interview Prep

    Live online + 1-on-1 SQL for Data Analytics training ยท Join 5000+ developers