Data Analyst Interview
Menu
Browse in your language. All mock interviews, preparation sessions and feedback are in English only.
Learning guide · SQL

SQL for data analysis.
Query with confidence.

Learn the query, understand the result and check that it answers the question. Practise filtering, joins, aggregation and window functions in a live sample database.

By Prakhar Shrivastava · Educational resource · Practice examples use synthetic data

Begin with the grain of the data

SQL lets you select, combine and summarise records in relational tables. Before writing a query, describe what one row represents: an order, a customer, an event or a daily total. Then define what one row in the answer should represent.

The practice database on this page contains four synthetic tables: customers, orders, products and employees. It runs SQLite in your browser. Dates, names and amounts are invented learning data. Syntax for date functions and some other operations differs across database systems.

Use the examples below as a starting point, then change the filters or grouping and predict the result. Reset editor restores Basic SELECT and clears the output. Reload the page to restore the sample database.

Build skills in a useful order

Select and filter

Choose columns, use WHERE, handle NULL explicitly and sort the result.

Join tables

Define the join keys and the expected relationship. Check whether rows multiply.

Group and calculate

Use GROUP BY for one row per group. Reconcile counts and totals.

Rank and compare

Use window functions for calculations that preserve individual rows.

Select rows with an explicit rule

SELECT customer_name, city, total_orders
FROM customers
WHERE city = 'Mumbai'
ORDER BY total_orders DESC, id;

This returns three customers in the sample database: Rahul Sharma, Pooja Sharma and Neha Singh, ordered by their stored total_orders value. That field is a synthetic customer attribute; it is not calculated from the short orders table on this page.

Use IS NULL to test for missing values. A comparison such as column = NULL does not behave like an ordinary equality test. Use an explicit sort order when the order of the output matters.

Join without accidentally multiplying totals

SELECT c.id, c.customer_name,
       COUNT(o.order_id) AS order_count,
       COALESCE(SUM(o.order_amount), 0) AS order_value
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.customer_name
ORDER BY order_value DESC, c.id;

The left join keeps customers without orders. Counting o.order_id gives zero for their unmatched join row, while COUNT(*) would count that row. COALESCE turns a missing sum into zero for this reporting purpose.

This query includes every sample order, including the returned order. If the question asks for delivered orders only while retaining customers with none, add that status condition to the join condition. Putting it in WHERE would remove the unmatched customers.

The result has ten customers and a total order value of 78,600. If a later join increases the total unexpectedly, check key uniqueness and join cardinality before trusting the report.

Group records and filter groups

SELECT category, COUNT(*) AS order_count,
       SUM(order_amount) AS delivered_value
FROM orders
WHERE status = 'delivered'
GROUP BY category
HAVING COUNT(*) >= 2
ORDER BY delivered_value DESC;

WHERE filters individual rows before grouping. HAVING filters groups after aggregation. Here every category has at least two delivered orders, and their totals reconcile to 76,500.

For rates, define the denominator before dividing. In SQLite, use a decimal value such as 1.0 * numerator / NULLIF(denominator, 0) to avoid integer division and handle a zero denominator explicitly. A NULL result is not automatically the same as a zero rate.

Use a CTE to make stages readable

WITH customer_totals AS (
    SELECT customer_id, SUM(order_amount) AS total_value
    FROM orders
    GROUP BY customer_id
)
SELECT customer_id, total_value
FROM customer_totals
WHERE total_value > 10000
ORDER BY total_value DESC, customer_id;

A common table expression names an intermediate result. This example separates per-customer aggregation from the final threshold. It returns five customers in the sample data. CTEs can improve readability; they are not automatically faster than equivalent alternatives.

Rank while keeping each employee row

SELECT emp_name, department, salary,
       DENSE_RANK() OVER (
           PARTITION BY department
           ORDER BY salary DESC
       ) AS salary_rank
FROM employees
ORDER BY department, salary_rank, emp_id;

The window calculation ranks employees within each department without collapsing their rows. DENSE_RANK gives equal values the same rank and leaves no gaps. RANK leaves gaps after ties; ROW_NUMBER assigns a different number to every row.

Choose the function that matches the question. “Three employees” and “employees with the three highest distinct salaries” can produce different answers. Add a deterministic tie-breaker when assigning row numbers. See the SQLite window-function reference for details.

Run a query in the practice database

Select an example, edit the query and choose Run Query. Inspect the schema buttons when you need a column name. All four tables use synthetic data.

SQL Practice Compiler ● Live
SQL Editor
Tables: customers · orders · products · employees
Try:
Query Results
Click “Run Query” to see results
Database Schema — click to inspect
customers(id, customer_name, city, email, total_orders, join_date, segment)

Check your understanding

How do I find the second-highest distinct salary?

Find the maximum salary below the overall maximum. If fewer than two distinct salaries exist, the result is NULL.

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

The expected result here is 95000.

Why can a join increase a revenue total?

One input row can match several rows on the other side. The join then repeats that input value. Check the expected relationship and duplicate keys. DISTINCT is not a general repair: it can remove legitimate records.

What should I explain before running a query?

Name the input grain, output grain, join relationship, filtering rule and treatment of missing values. Then describe a check, such as reconciling totals or testing a small example by hand.

Continue with a business question

Revenue investigation

Use SQL to separate traffic, conversion and order-value changes.

Read the guide →

Python analysis

Apply the same grain and validation checks to pandas.

Read the guide →

For more exercises, visit the SQL question guide. These are practice resources, not verified questions from a named employer.

✉ WhatsApp