Data Skill Hub

H1-Top 25 SQL Interview Questions for Data Analysts with Answers

Top 25 SQL Interview Questions for Data Analysts with Answers

Quick Answer: The most common SQL interview questions for data analysts cover SELECT basics, WHERE vs HAVING, JOINs, GROUP BY, subqueries, CTEs and window functions. Interviewers also ask practical problems like finding the second-highest salary, spotting duplicates and calculating running totals. Master these 25 questions with regular hands-on practice, and you will be ready for most entry-level and mid-level analyst SQL rounds.

Most people don’t fail a SQL round because they don’t know SQL. They fail because they freeze when the interviewer asks a simple question in a slightly different way. That is why practising SQL interview questions before the day matters.

This guide covers 25 of the most asked questions for data analyst roles, from basic commands to joins, CTEs and window functions. Every answer is short and direct, so you can revise quickly. Read them, try the queries yourself, and walk in ready. Whether you are a fresher or switching careers, this is the kind of practice that actually helps. Let’s get started!

Why Is SQL Important for Data Analysts?

Why Is SQL Important for Data Analysts

Every data analyst job starts with data, and most data sits inside databases. SQL is the language that lets you pull it out. That is why SQL interview questions show up in almost every analyst hiring round, even for freshers.

  • Pulls data directly: You fetch exactly the rows you need without waiting on anyone.
  • Saves cleaning time: Filtering, sorting and removing duplicates is faster in a query than in a spreadsheet.
  • Handles big data: SQL works smoothly on millions of rows where Excel starts to hang. Here is a detailed comparison of Power BI vs Excel if you want to know when to use which. 
  • Feeds your dashboards: Power BI and Tableau reports often run on SQL queries underneath.
  • Works across tools: MySQL, SQL Server and PostgreSQL share most of the same core syntax.
  • Sets you apart: Many candidates list SQL, but few can write a clean join under pressure.

Read More: How to Become a Data Analyst: A Complete Step-by-Step Roadmap

Top 25 SQL Interview Questions for Data Analysts

Here are the 25 SQL interview questions that interviewers ask most, grouped by topic and answered directly in plain, simple words.

Basic SQL Interview Questions

  1. What is SQL, and why is it used in data analytics?

SQL stands for Structured Query Language. It is used to store, fetch, update and manage data in relational databases. In data analytics, analysts use SQL to pull the exact data they need, filter it, join tables and summarise numbers before building reports or dashboards. Without it, you depend on someone else for data.

  1. What is the difference between SQL and MySQL?

SQL is the language used to talk to databases. MySQL is database software that uses SQL to store and manage data. Think of SQL as the language and MySQL as one of the tools that understands it. Others include SQL Server, PostgreSQL and Oracle.

  1. What are the different types of SQL commands?

SQL commands fall into five groups:

  • DDL (CREATE, ALTER, DROP): defines table structure
  • DML (INSERT, UPDATE, DELETE): changes the data
  • DQL (SELECT): fetches data
  • DCL (GRANT, REVOKE): controls access
  • TCL (COMMIT, ROLLBACK): manages transactions
  1. What is the difference between WHERE and HAVING?

This one comes up in almost every set of SQL interview questions. WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY has run. So use WHERE for conditions on raw columns and HAVING for conditions on aggregates like SUM or COUNT. You cannot use an aggregate function inside WHERE.

SQL Code:

SELECT department, AVG(salary) AS avg_salary

FROM employees

WHERE status = ‘Active‘

GROUP BY department

HAVING AVG(salary) > 50000;

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

DELETE removes specific rows using WHERE and can be rolled back inside a transaction. TRUNCATE removes all rows at once but keeps the table structure. DROP deletes the whole table, structure and data together. Use DROP with care, because there is no easy way back.

  1. What is the difference between PRIMARY KEY and FOREIGN KEY?

A primary key uniquely identifies each row in a table. It cannot be NULL or repeated. A foreign key is a column that points to the primary key of another table, linking the two. For example, customer_id is the primary key in customers and a foreign key in orders.

  1. What is the difference between NULL, zero and an empty string in SQL?

NULL means the value is unknown or missing. Zero is an actual number. An empty string is a text value with no characters. They are not the same, so you check NULL with IS NULL, not with = NULL. For example, a missing phone number is NULL, not zero.

  1. What is the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column)?
  • COUNT(*) counts all rows, including NULLs.
  • COUNT(column) counts only the non-NULL values in that column.
  • COUNT(DISTINCT column) counts unique non-NULL values.

For example, in a column with 10 rows, 2 NULLs and 5 unique values, the results are 10, 8 and 5.

  1. What is the difference between ORDER BY and GROUP BY?

ORDER BY sorts the final result in ascending or descending order. GROUP BY collects rows with the same value into groups so you can run aggregate functions on each group. One decides the sequence of rows, the other decides how rows are summarised.

  1. What is the difference between UNION and UNION ALL?

UNION combines the results of two queries and removes duplicate rows. UNION ALL combines them and keeps every row, duplicates included. Because it skips the duplicate check, UNION ALL is faster. Both need the same number of columns with compatible data types.

SQL JOIN Interview Questions

  1. What are SQL JOINs, and what are their different types?

A JOIN combines rows from two or more tables using a related column. The main types are:

  • INNER JOIN: only matching rows from both tables
  • LEFT JOIN: all rows from the left table, plus matches from the right
  • RIGHT JOIN: all rows from the right table, plus matches from the left
  • FULL JOIN: all rows from both tables
  • CROSS JOIN: every possible combination of rows
  1. What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only rows that have a match in both tables. LEFT JOIN returns every row from the left table and fills NULL where the right table has no match. Use LEFT JOIN when you cannot afford to lose rows from your main table.

SQL Code:

SELECT c.name, o.order_id

FROM customers c

LEFT JOIN orders o ON c.customer_id = o.customer_id;

This lists all customers, even those who never placed an order.

  1. What is a SELF JOIN in SQL?

A SELF JOIN joins a table to itself using aliases. It is useful when rows in the same table relate to each other, like employees and their managers. Without aliases, the database cannot tell the two copies apart.

SQL Code:

SELECT e.name AS employee, m.name AS manager

FROM employees e

JOIN employees m ON e.manager_id = m.emp_id;

  1. What is a CROSS JOIN in SQL?

A CROSS JOIN returns every possible pair of rows from two tables, with no matching condition. If one table has 5 rows and the other has 4, you get 20 rows. Analysts use it to build combinations, like every product against every month.

SQL Aggregation and Functions Interview Questions

  1. What are aggregate functions in SQL?

Aggregate functions run a calculation on a set of rows and return a single value. The common ones are COUNT, SUM, AVG, MIN and MAX. They are usually paired with GROUP BY, and they ignore NULL values, except COUNT(*).

SQL Code:

SELECT department, SUM(salary) AS total_salary

FROM employees

GROUP BY department;

  1. What is GROUP BY in SQL?

GROUP BY groups rows that share the same value in one or more columns, so you can calculate totals for each group. For instance, grouping sales by region gives you total revenue per region. Every non-aggregated column in SELECT must appear in GROUP BY.

SQL Code:

SELECT region, SUM(revenue) AS total_revenue

FROM sales

GROUP BY region;

  1. What is a subquery in SQL?

A subquery is a query written inside another query. The inner query runs first and passes its result to the outer query. It can sit in the WHERE, FROM or SELECT part. For example, this finds employees earning above the company average:

SQL Code:

SELECT name, salary

FROM employees

WHERE salary > (SELECT AVG(salary) FROM employees);

  1. What is the CASE statement in SQL?

The CASE statement adds if-then-else logic inside a query. It checks conditions one by one and returns the value of the first one that is true. Analysts use it to create labels or buckets, like marking salaries as High, Medium or Low.

SQL Code:

SELECT name, salary,

  CASE

    WHEN salary >= 80000 THEN ‘High‘

    WHEN salary >= 40000 THEN ‘Medium‘

    ELSE ‘Low‘

  END AS salary_band

FROM employees;

Advanced SQL Interview Questions

  1. What is a Common Table Expression (CTE) in SQL?

A CTE is a temporary named result you define with WITH and use right after, in the same query. It makes long queries easier to read and lets you reuse the same logic more than once in that statement.

SQL Code:

WITH dept_avg AS (

  SELECT department, AVG(salary) AS avg_salary

  FROM employees

  GROUP BY department

)

SELECT * FROM dept_avg

WHERE avg_salary > 50000;

  1. What are window functions in SQL?

Window functions run a calculation across a set of rows related to the current row, without collapsing them into one. They use the OVER clause with PARTITION BY and ORDER BY. Common ones are ROW_NUMBER, RANK, LAG and SUM() OVER. Unlike GROUP BY, you keep every row.

SQL Code:

SELECT name, department, salary,

  AVG(salary) OVER (PARTITION BY department) AS dept_avg

FROM employees;

  1. What is the difference between ROW_NUMBER(), RANK() and DENSE_RANK()?
  • ROW_NUMBER(): gives a unique number to every row, even for ties
  • RANK(): gives the same rank to ties, then skips numbers (1, 2, 2, 4)
  • DENSE_RANK(): gives the same rank to ties, with no skipping (1, 2, 2, 3)

Tie handling is a favourite follow-up in advanced SQL interview questions, so be ready to explain what happens when two salaries are equal.

  1. How do you find the second-highest salary in SQL?

Use DENSE_RANK so that tied salaries do not break the result.

SQL Code:

SELECT DISTINCT salary

FROM (

  SELECT salary,

         DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk

  FROM employees

) t

WHERE rnk = 2;

Without window functions, this also works:

SQL Code:

SELECT MAX(salary)

FROM employees

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

  1. How do you find duplicate records in SQL?

Group by the column that should be unique, then use HAVING to keep only the groups that appear more than once. To see the full duplicate rows, join this result back to the original table.

SQL Code:

SELECT email, COUNT(*) AS total

FROM customers

GROUP BY email

HAVING COUNT(*) > 1;

  1. How do you calculate a running total in SQL?

Use a window SUM with ORDER BY. It adds up the amounts row by row in date order. Add PARTITION BY if you want the total to restart for each customer or month.

SQL Code:

SELECT order_date, amount,

  SUM(amount) OVER (

    ORDER BY order_date

    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

  ) AS running_total

FROM orders;

The ROWS clause keeps the total strictly row by row when two orders share the same date.

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

LEFT JOIN keeps all customers, and those without orders get NULL on the order side. Filtering on IS NULL leaves only them. NOT EXISTS gives the same result.

SQL Code:

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;

Common Mistakes to Avoid During SQL Interviews

Common Mistakes to Avoid During SQL Interviews

Knowing the answers is only half the battle. Small slips can still cost you real marks in SQL interview questions.

  • Starting without understanding the table: Ask about columns, data types and NULLs first. One wrong assumption can ruin a correct query.
  • Using = NULL: Comparing with = NULL never works. Always use IS NULL or IS NOT NULL when checking for missing values.
  • Mixing up WHERE and HAVING: Filter rows with WHERE and filter aggregated results with HAVING. Mixing them gives errors or wrong numbers.
  • Forgetting the JOIN condition: A missing ON clause creates a cross join and floods your result with rows that mean nothing.
  • Writing SELECT * everywhere: Pick only the columns you need. It reads cleaner and shows you think about performance.
  • Ignoring duplicates and ties: Repeated records and equal salaries change the answer. Mention how you would handle them before the interviewer asks.
  • Staying silent while solving: Explain your thinking out loud. Interviewers want to hear your logic, not only see the final query.

Fix these habits early, and your next round of SQL interview questions will feel far easier. Practising on real data helps the most.

Conclusion

Practising these SQL interview questions a few times a week is enough to build real confidence. Do not just read the answers. Type the queries, break them, and fix them. That is how the logic sticks. If you want guided practice, Data Skill Hub offers live SQL training in Pune, taught by Microsoft Certified Trainer Mohammad Shabbir with real business datasets. Start small, stay regular, book a free demo session, and walk into your next interview fully prepared.

Upgrade Your Excel Skills with Data Skill Hub

Master advanced Excel formulas, Pivot Tables, Power Query, macros, and dashboards with practical, hands-on training at Data Skill Hub. Join our Advanced Excel Course in Pune to improve your reporting skills, automate repetitive tasks, and gain confidence working with real business data.

Frequently Asked Questions

It depends on the company, but a typical round often has around 5 to 10 SQL questions, usually mixing basic concepts, joins and one or two practical query problems.

SQL is a must, but not enough alone. Most employers also expect Excel, a BI tool like Power BI or Tableau, and basic statistics. Python is a useful bonus.

Start with SELECT, WHERE, GROUP BY and joins. Then practise CTEs and window functions on a real dataset. Write queries daily instead of only reading answers.

Joins, GROUP BY with HAVING, subqueries, CTEs and window functions come up the most. Questions on duplicates, NULL handling and ranking are also very common.

With regular practice, most beginners become interview-ready in about four to eight weeks. Spend that time solving real problems and revising SQL interview questions like the ones above.

Leave a Comment

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

Scroll to Top