InterviewSkill

SQL Interview Questions

Query concepts for analytics, backend, data engineering, and ML roles.

8 questions
SQL

What is the difference between WHERE and HAVING?medium

Type
conceptual
Topic
having
Frequency
common
Tags
having
Answer

WHERE filters rows before grouping; HAVING filters groups after aggregation.

Explanation

WHERE cannot filter aggregate results because it runs before GROUP BY. HAVING can filter values like COUNT, SUM, and AVG.

Follow-upCan one query use both WHERE and HAVING?

What are the main types of joins?medium

Type
conceptual
Topic
main-types-joins
Frequency
common
Tags
main, types, joins
Answer

INNER, LEFT, RIGHT, FULL OUTER, and CROSS JOIN are common join types.

Explanation

Inner joins return matches, outer joins preserve unmatched rows, and cross joins return every row combination.

Follow-upHow do you find records missing from another table?

What is a window function?hard

Type
conceptual
Topic
window-function
Frequency
common
Tags
window, function
Answer

It calculates across related rows without collapsing them.

Explanation

Window functions use OVER with optional PARTITION BY and ORDER BY for rankings, running totals, moving averages, and row comparisons.

Follow-upWhat is the difference between RANK and ROW_NUMBER?

What is a CTE?medium

Type
conceptual
Topic
cte
Frequency
common
Tags
cte
Answer

A common table expression is a named temporary result inside a query.

Explanation

CTEs make complex SQL easier to read by breaking logic into named steps. Some databases may materialize them, while others optimize them inline.

Follow-upWhen would you prefer a CTE over a subquery?

What is the difference between DELETE, TRUNCATE, and DROP?medium

Type
conceptual
Topic
delete-truncate-drop
Frequency
common
Tags
delete, truncate, drop
Answer

DELETE removes rows, TRUNCATE clears all rows, and DROP removes the table.

Explanation

DELETE can filter rows and often logs row changes. TRUNCATE is faster for clearing a table. DROP removes the schema object and data.

Follow-upWhich operations can be rolled back in your database?

How do you find the second highest salary in SQL?medium

Type
problem-solving
Topic
second-highest-salary
Frequency
common
Tags
ranking, window-function, salary
Answer

Use a ranking window function or a subquery that finds the maximum below the overall maximum.

Explanation

DENSE_RANK is often safest when duplicates matter: rank salaries descending, then filter rank = 2. Clarify whether ties should count as one rank.

Hint: reason from constraints before calculating.
Follow-upHow would your query change if multiple employees share the second highest salary?

How do you find duplicate rows in a table?medium

Type
problem-solving
Topic
duplicate-rows
Frequency
common
Tags
duplicates, group-by, data-cleaning
Answer

Group by the duplicate-defining columns and filter groups with COUNT(*) greater than one.

Explanation

The key is defining what duplicate means. For full-row duplicates, group by all relevant columns. For business duplicates, group by natural keys like email or order id.

Hint: reason from constraints before calculating.
Follow-upHow would you keep only the latest row from each duplicate group?

How does SQL treat NULL in comparisons?medium

Type
conceptual
Topic
null-comparisons
Frequency
common
Tags
null, three-valued-logic, filters
Answer

NULL represents unknown, so comparisons like = NULL do not evaluate to true.

Explanation

Use IS NULL or IS NOT NULL. NULL also affects joins, filters, aggregates, and three-valued logic, so explicit handling prevents subtle bugs.

Follow-upHow do COUNT(*) and COUNT(column) handle NULL differently?