25 Data Analyst SQL Interview Questions and Answers

·

·

, ,

Preparing for a data analyst interview in India? Start with the SQL tasks you’ll use on the job, such as filtering orders, joining customer data, calculating sales totals, and handling missing values.

These 25 data analyst SQL interview questions cover the topics you’ll practise for fresher and junior analyst roles in India. Start with filtering and joins, then work through sales totals, duplicate records, missing values, and window functions. Each section includes answers and SQL examples you can use to test yourself.

Beginner Data Analyst SQL Interview Questions

1. What is SQL?

SQL stands for Structured Query Language. It is used to query and manage data in relational databases. Data analysts mainly use it to retrieve, combine, and summarise data to answer business questions.

2. How is SQL different from Excel?

Excel is useful for exploring data in a spreadsheet, building pivot tables, and creating quick charts. SQL is useful when your data lives in a database, especially if you need to combine several tables or run the same query regularly.

For example, you might use SQL to pull monthly sales by city, then use Excel or Power BI to visualise the results.

3. What does SELECT do?

SELECT specifies which columns or expressions to return in the query result.

SELECT customer_id, customer_name, city
FROM customers;

This query returns the ID, name, and city for each customer.

4. What does WHERE do?

WHERE filters individual rows based on a condition.

SELECT order_id, customer_id, order_status
FROM orders
WHERE order_status = 'Delivered';

This query returns delivered orders.

5. What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping. HAVING filters the groups after aggregation.

SELECT city, COUNT(*) AS delivered_orders
FROM orders
WHERE order_status = 'Delivered'
GROUP BY city
HAVING COUNT(*) > 100;

Here, WHERE keeps delivered orders. HAVING then keeps cities with more than 100 delivered orders.

6. What does ORDER BY do?

ORDER BY sorts the query result. Use ASC for ascending order and DESC for descending order.

SELECT product_name, sales_amount
FROM sales
ORDER BY sales_amount DESC;

The sales records with the highest amounts appear first.

7. What is DISTINCT used for?

DISTINCT returns unique values or unique combinations of values across the selected columns.

SELECT DISTINCT city
FROM customers;

Each distinct city value appears once in the result.

8. What is a NULL value?

NULL represents a missing, unknown, or inapplicable value. Zero and an empty string are separate values in most SQL databases. Use IS NULL to check for missing values.

SELECT customer_id, customer_name
FROM customers
WHERE email IS NULL;

This query finds customers whose email field contains NULL.

9. What does LIMIT do?

LIMIT sets the maximum number of rows a query returns. Combine it with ORDER BY when you need a specific subset, such as the latest 10 orders.

SELECT order_id, order_date, order_amount
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10;

The order ID breaks ties when multiple orders share the same date. MySQL and PostgreSQL support LIMIT. In SQL Server, you can use TOP (10).

10. What is the difference between COUNT(*) and COUNT(column)?

COUNT(*) counts every row in the result before aggregation. COUNT(column) counts only rows where the specified column contains a non-`NULL` value.

SELECT
    COUNT(*) AS total_orders,
    COUNT(discount_code) AS orders_with_discount_code
FROM orders;

If there are 100 orders and 25 have a non-`NULL` discount code, the query returns 100 and 25.

SQL Joins and Keys Interview Questions

11. What is a JOIN in SQL?

A JOIN combines rows from two tables using a condition. For example, you can match orders.customer_id with customers.customer_id to add the customer’s name to each order.

12. What is the difference between INNER JOIN and LEFT JOIN?

An INNER JOIN returns rows that meet the join condition. A LEFT JOIN keeps every row from the left table and fills columns from the right table with NULL when there is no match.

SELECT c.customer_id, c.customer_name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
    ON c.customer_id = o.customer_id;

Customers with no orders still appear. A customer with three orders appears three times, once per order.

13. When should you use a LEFT JOIN?

Use a LEFT JOIN when your report needs to include every row from the main table. For example, if your manager asks for an order count for every customer, you need to include customers who have placed zero orders.

SELECT
    c.customer_id,
    c.customer_name,
    COUNT(o.order_id) AS total_orders
FROM customers AS c
LEFT JOIN orders AS o
    ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name;

COUNT(o.order_id) returns zero for customers with no matching orders because their order ID is NULL in the joined result.

14. What is a self-join?

A self-join joins a table to itself. One common example is an employees table where each employee’s manager_id refers to another row in the same table.

SELECT
    e.employee_name AS employee,
    m.employee_name AS manager
FROM employees AS e
LEFT JOIN employees AS m
    ON e.manager_id = m.employee_id;

The aliases e and m let you refer to the same table in two roles.

15. What is a primary key?

A primary key is a column or combination of columns that uniquely identifies each row in a table. Primary key values must be unique and cannot contain NULL.

For example, customer_id can serve as the primary key of the customers table.

16. What is a foreign key?

A foreign key is a constraint that links a column or set of columns to a primary key or another eligible unique key.

For example, orders.customer_id can reference customers.customer_id. When the constraint is enforced, a non-`NULL` customer ID in the orders table must match an existing customer.

SQL Grouping and Aggregation Interview Questions

17. What does GROUP BY do?

GROUP BY combines rows that share the same value in one or more columns. You can then calculate a total, average, or count for each group.

SELECT category, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY category;

This query returns one row per product category with its total sales.

18. What are aggregate functions?

Aggregate functions summarise multiple rows into a single value. When you use them with GROUP BY, they return a summary for each group.

FunctionWhat it does
COUNT(*)Counts all rows
COUNT(column)Counts rows with a non-`NULL` value in the column
SUM()Adds numeric values
AVG()Calculates the arithmetic mean
MIN()Returns the smallest value
MAX()Returns the largest value

19. How do you calculate total sales by city?

Group the sales records by city and add up each group’s sales amounts. Sort the results to see the highest-selling cities first.

SELECT
    city,
    SUM(sales_amount) AS total_sales
FROM sales
GROUP BY city
ORDER BY total_sales DESC;

20. How do you find the top five products by revenue?

Group sales by product, calculate each product’s total revenue, then sort from highest to lowest.

SELECT
    product_id,
    product_name,
    SUM(sales_amount) AS total_revenue
FROM sales
GROUP BY product_id, product_name
ORDER BY total_revenue DESC, product_id
LIMIT 5;

Grouping by product ID as well as name keeps products with the same name separate. The product ID also breaks revenue ties so the query returns a consistent top five.

Practical Data Analyst SQL Interview Questions

21. How do you find duplicate email addresses?

First, define what counts as a duplicate. If the rule is that each non-missing email should appear only once, group by email and look for groups with more than one row.

SELECT
    email,
    COUNT(*) AS occurrences
FROM customers
WHERE email IS NOT NULL
GROUP BY email
HAVING COUNT(*) > 1;

The result shows each repeated email and how many times it appears.

22. How do you remove duplicates from a query result?

Use ROW_NUMBER() to assign a number to each row within a group. If the business rule is to keep the most recently created customer record for each email, sort each group by creation date and keep the first row.

WITH ranked_customers AS (
    SELECT
        customer_id,
        customer_name,
        email,
        created_at,
        ROW_NUMBER() OVER (
            PARTITION BY email
            ORDER BY
                CASE WHEN created_at IS NULL THEN 1 ELSE 0 END,
                created_at DESC,
                customer_id DESC
        ) AS row_num
    FROM customers
    WHERE email IS NOT NULL
)
SELECT customer_id, customer_name, email, created_at
FROM ranked_customers
WHERE row_num = 1

UNION ALL

SELECT customer_id, customer_name, email, created_at
FROM customers
WHERE email IS NULL;

The query keeps one record per known email and preserves every record with a missing email. Records with a known creation date take priority, and customer ID breaks date ties. If asked to delete duplicate records from the database, explain which rows you would preview and how you would check their links to tables such as orders before deleting anything.

23. How do you handle NULL values in SQL?

Start by asking what the missing value means. In a report, you might display a missing city as Unknown using COALESCE().

SELECT
    customer_id,
    COALESCE(city, 'Unknown') AS city
FROM customers;

The label appears in the query result, and the source data stays intact. For a numeric field such as order_amount, replacing NULL with zero can change the average, so check the business meaning first.

24. What is a subquery?

A subquery is a query nested inside another query. In this example, the inner query finds customer IDs associated with individual orders above ₹10,000. The outer query returns the corresponding customer names.

SELECT customer_id, customer_name
FROM customers
WHERE customer_id IN (
    SELECT customer_id
    FROM orders
    WHERE order_amount > 10000
);

If the interviewer asks for customers whose combined spending exceeds ₹10,000, group the orders by customer and apply HAVING SUM(order_amount) > 10000.

25. What is a window function?

A window function calculates a value across related rows while keeping each row in the result. Common uses include ranking orders, calculating running totals, and comparing a row with the previous row.

SELECT
    order_id,
    customer_id,
    order_amount,
    RANK() OVER (
        ORDER BY order_amount DESC
    ) AS order_rank
FROM orders
WHERE order_amount IS NOT NULL
ORDER BY order_rank, order_id;

This query ranks orders by amount. Equal amounts receive the same rank, and the next rank skips a position. For example, the ranks could be 1, 2, 2, 4.

How to Prepare for a Data Analyst SQL Interview in 7 Days

“For more on preparing for analyst interviews, read how OneLeap supports interview and portfolio preparation.”

DayWhat to practiseSample task
Day 1SELECT, WHERE, ORDER BY, and LIMITFind the 10 most recent delivered orders.
Day 2GROUP BY, COUNT, SUM, AVG, and HAVINGFind cities with more than 100 orders.
Day 3INNER JOIN, LEFT JOIN, primary keys, and foreign keysList every customer with their order count, including customers with zero orders.
Day 4Duplicate detection and NULL handlingFind repeated email addresses and label missing cities as Unknown in a report.
Day 5Subqueries and CASE WHENFind customers with an order above ₹10,000 and group orders into value bands.
Day 6ROW_NUMBER() and RANK()Find each customer’s latest order and rank orders by amount.
Day 7Timed interview practiceSolve 10–15 queries and explain your reasoning aloud.

When you explain a query, say what one row in your result represents. For example, a city sales report should return one row per city. That gives you a concrete way to check whether your joins and grouping match the business question.

Frequently Asked Questions

How much SQL should a junior data analyst know?

You should be comfortable selecting and filtering rows, joining tables, grouping data, and writing basic subqueries. Practise handling missing values and duplicates, and learn how to use ROW_NUMBER() to find the latest record within a group.

Is SQL enough to become a data analyst?

SQL helps you retrieve and prepare data. Data analyst roles can also involve Excel, dashboards, basic statistics, and explaining findings to business teams. Check the job description to see whether the role expects Power BI, Tableau, or Python.

What SQL questions are asked in interviews for data analyst freshers?

Common examples include explaining WHERE versus HAVING, joining customer and order tables, finding duplicate values, and calculating total sales by category. You may also be given a small dataset and asked to turn a business question into a query.

How can I practise SQL for interviews?

Use a sample database with customers, orders, and products. Start with a business question such as “Which customers have placed no orders in the past 90 days?” Decide what each row in the result should represent, write the query, and check it against a few records you can verify by hand.


Leave a Reply

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