/writing/recruiting & ats/sql-query-based-interview-questions
§ Hiring Tips·9 min read·September 25, 2026

25 SQL Query-Based Interview Questions and Answers to Know

O
Olibr TeamHiring Tips
§ Contents
25 SQL Query-Based Interview Questions and Answers to Know1. Basic SQL query questionsWhat is the difference between WHERE and HAVING?How do you retrieve unique records using DISTINCT?How do you sort results with ORDER BY?How do you limit the number of rows returned?How do you filter records using multiple conditions?2. Join-based interview questionsWhat is the difference between INNER JOIN and LEFT JOIN?What is a self join and when is it used?How do you find records in one table that don't exist in another?What is the difference between UNION and UNION ALL?How do you join more than two tables in one query?3. Aggregation and grouping questionsHow do you use GROUP BY with aggregate functions?How do you find duplicate records in a table?How do you calculate a running total or cumulative sum?How do you find the second highest salary in a table?How do you count records that meet a specific condition?4. Subquery and nested query questionsWhat is a subquery and how does it differ from a join?What is a correlated subquery?How do you use EXISTS and NOT EXISTS in a query?How do you find the nth highest value using a subquery?How do you use a subquery inside the SELECT clause?5. Window function and advanced query questionsWhat is the difference between RANK, DENSE_RANK, and ROW_NUMBER?How do you use a Common Table Expression?How do you calculate a moving average with window functions?How do you find employees who earn more than their manager?How do you optimize a slow-running SQL query?Getting interview-ready with SQL
25 SQL Query-Based Interview Questions and Answers to Know

25 SQL Query-Based Interview Questions and Answers to Know

Hiring for a data role and dreading the SQL screening round? You're not alone. Most recruiters and hiring managers aren't SQL experts themselves, which makes it hard to tell a strong candidate from someone who memorized a few tutorials. This list of sql query based interview questions gives you a ready reference, whether you're screening candidates directly or prepping a technical panel with the right talking points.

This article answers that gap directly: 25 real, commonly asked SQL questions with clear answers, covering everything from basic SELECT statements to complex joins and window functions. You'll see the kind of questions that separate a candidate who can write a query from one who understands database logic, along with sample answers you can use to judge responses on the spot.

We've organized the questions by difficulty, so you can match them to junior, mid-level, or senior data roles. If you're managing high volumes of technical hires, pairing this list with an AI interview screen like Olibr's video interview scoring can save hours of manual review while keeping your evaluations consistent across every candidate in your pipeline.

1. Basic SQL query questions

Entry-level candidates should nail these without hesitation. These five basic SQL query questions show up in almost every technical screen because they reveal whether someone actually understands query logic or just copies syntax from memory.

A laptop showing a SQL query editor next to a notepad and coffee mug on a desk.

What is the difference between WHERE and HAVING?

Recruiters often see candidates confuse these two clauses. WHERE filters individual rows before any grouping happens, while HAVING filters groups after an aggregate function like COUNT or SUM runs. A candidate who says "they're basically the same" hasn't grasped query execution order, which is a red flag for anything beyond a junior role.

WHERE filters rows, HAVING filters groups, and mixing them up is the fastest way to fail a SQL screen.

How do you retrieve unique records using DISTINCT?

Ask the candidate to write SELECT DISTINCT department FROM employees; and explain what happens under the hood. DISTINCT removes duplicate rows from the result set, but it also forces a sort or hash operation, so strong candidates will mention the performance tradeoff on large tables.

How do you sort results with ORDER BY?

Sorting comes up constantly in real reports. Look for candidates who know that ORDER BY defaults to ascending order, that DESC reverses it, and that you can sort by multiple columns with different directions in the same statement.

How do you limit the number of rows returned?

Every database engine handles this differently, and a good candidate flags that immediately; for Microsoft-specific roles, add a few SQL Server questions and answers to the same round.

Database Syntax
MySQL/PostgreSQL LIMIT 10
SQL Server TOP 10
Oracle FETCH FIRST 10 ROWS ONLY

How do you filter records using multiple conditions?

Combining AND and OR without parentheses is a classic mistake. Watch for candidates who understand operator precedence and wrap conditions in parentheses to avoid returning the wrong result set entirely.

2. Join-based interview questions

Joins trip up more candidates than any other SQL topic, mostly because they sound simple but require precise thinking about matching logic between tables. Mid-level candidates should move through these five without needing a whiteboard.

A comparison graphic showing INNER JOIN matching rows against LEFT JOIN keeping all rows with NULLs for gaps.

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only rows with matches in both tables, while LEFT JOIN keeps every row from the left table and fills in NULLs when there's no match on the right. Candidates who can't explain what happens to unmatched rows haven't really used joins in production.

A candidate who can't predict what a LEFT JOIN does with unmatched rows probably hasn't debugged a real report.

What is a self join and when is it used?

Self joins compare a table to itself, commonly used for finding employees who report to a specific manager within the same employees table.

How do you find records in one table that don't exist in another?

Strong answers mention LEFT JOIN with a NULL check or NOT EXISTS, and explain the performance difference between the two approaches on large datasets.

What is the difference between UNION and UNION ALL?

UNION removes duplicates and sorts internally; UNION ALL keeps everything and runs faster.

How do you join more than two tables in one query?

Candidates should chain JOIN clauses logically, aliasing each table clearly to avoid ambiguous column errors.

3. Aggregation and grouping questions

Grouping questions test whether a candidate can summarize data correctly, not just pull raw rows. These five aggregation questions separate candidates who understand data summarization from those who only know SELECT statements, and they pair well with analytics-oriented SQL questions for data analyst hires.

A whiteboard with a grouped data table, a calculator, and a highlighter on a desk.

How do you use GROUP BY with aggregate functions?

GROUP BY collapses rows sharing a column value so functions like SUM, AVG, or COUNT can run per group. A candidate should know that every non-aggregated column in SELECT must appear in the GROUP BY clause, or the query throws an error in most engines.

How do you find duplicate records in a table?

Listen for GROUP BY on the suspect column paired with HAVING COUNT(*) > 1. This combination is the standard fix for data quality issues in candidate or customer databases.

How do you calculate a running total or cumulative sum?

A window function like SUM(amount) OVER (ORDER BY date) produces a running total without collapsing rows, which is cleaner than old-school self joins.

If a candidate reaches for a self join to calculate a running total, they don't know window functions yet.

How do you find the second highest salary in a table?

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

How do you count records that meet a specific condition?

Conditional counting with SUM(CASE WHEN condition THEN 1 ELSE 0 END) shows a candidate can build flexible reports without writing multiple queries.

4. Subquery and nested query questions

Subqueries test whether a candidate can break a problem into steps instead of forcing everything into one flat statement. These five nested query questions show up constantly in mid-level and senior interviews because real reporting logic rarely fits in a single SELECT.

What is a subquery and how does it differ from a join?

A subquery is a query nested inside another query, often used when you need a calculated value before filtering. Unlike a join, which combines rows side by side, a subquery typically returns a single value or list that the outer query then uses, making the execution logic sequential rather than combined.

What is a correlated subquery?

A correlated subquery references a column from the outer query, so it runs once per outer row instead of once overall, which candidates should flag as a performance concern on large tables.

How do you use EXISTS and NOT EXISTS in a query?

EXISTS checks whether a subquery returns any rows at all, stopping as soon as it finds one match, which usually beats IN on large datasets.

A candidate who reaches for EXISTS instead of IN on big tables already understands query performance.

How do you find the nth highest value using a subquery?

SELECT salary FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;

How do you use a subquery inside the SELECT clause?

A scalar subquery in SELECT returns one value per row, often used to attach a company-wide average next to each employee's own salary.

5. Window function and advanced query questions

Senior candidates should handle window functions and query tuning without much prompting, so consider mixing in 50 advanced MySQL questions on transactions, locking, and indexing. These five advanced query questions reveal whether someone can write efficient, production-ready SQL instead of just correct SQL.

What is the difference between RANK, DENSE_RANK, and ROW_NUMBER?

RANK leaves gaps after ties, DENSE_RANK doesn't, and ROW_NUMBER assigns a unique number regardless of ties. Candidates should pick the right one based on whether tied values matter for the report.

How do you use a Common Table Expression?

A CTE, written with WITH name AS (...), breaks a complex query into readable steps and can reference itself for recursive problems like org charts.

How do you calculate a moving average with window functions?

AVG(amount) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) gives a 7-day moving average without collapsing rows.

How do you find employees who earn more than their manager?

This needs a self join comparing each employee's salary to their manager's salary, a good test of join logic under pressure.

How do you optimize a slow-running SQL query?

Strong answers mention checking the execution plan, adding indexes on filtered or joined columns, and avoiding SELECT *.

A candidate who mentions the execution plan unprompted is already thinking like a senior engineer.

Getting interview-ready with SQL

Running through these 25 questions gives you a solid filter for separating candidates who understand databases from those who only memorized syntax. Strong candidates explain their reasoning out loud, mention performance tradeoffs without being asked, and can adapt a query when you change the requirements mid-interview. That's the real signal, not whether they get every answer perfect on the first try.

Use this list as a starting point, not a script. Mix in a live coding exercise or a schema you actually use in production, and watch how candidates think through edge cases like NULLs, duplicates, or missing joins. That's where the gap between junior and senior talent really shows up.

If you'd rather skip screening resumes one by one, hire pre-vetted SQL developers in India and shortlist candidates who've already been tested on exactly these skills.

For engineers

Find work worth your time.

Live engineering roles across India and the US, matched to your stack. Build a profile recruiters actually discover.

Browse jobsHow it works

O
§ The author

Olibr Team

Reviewed by Raman Gupta, Founder, Olibr

Filed underHiring Tips
Reading time9 min · 1,609 words

PublishedSeptember 25, 2026

CategoryHiring Tips
Enjoyed this piece?Share it with someone who would find it useful.
§ Stay in the loop

Don’t miss the next one.

We publish essays on engineering, hiring, and building teams. Subscribe and we’ll send them when they land.

Unsubscribe anytime · one letter, never more