SQL Interview Questions and Answers
SQL screens begin with query fluency: the join types and when each is right, GROUP BY with HAVING, subqueries versus CTEs, and window functions such as ROW_NUMBER, RANK and running totals. You are usually given a small schema and asked to write the query, then to explain its cost.
Deeper rounds cover how the database executes a query: indexes (B-tree, composite, covering), reading an execution plan, why a query is slow, transactions, ACID and isolation levels (dirty reads, phantom reads, deadlocks), and normalisation versus denormalisation for real workloads.
Questions
- Select all columns from a table
- Select specific columns
- Filter rows with WHERE
- Sort results with ORDER BY
- Return only unique values with DISTINCT
- Limit the number of rows returned
- Skip rows with OFFSET (pagination)
- Filter with multiple conditions (AND / OR)
- Filter a range with BETWEEN
- Filter a list with IN
- Pattern matching with LIKE
- Handle NULL values (IS NULL / IS NOT NULL)
- Replace NULLs with a default (COALESCE)
- Alias columns and tables (AS)
- Concatenate strings
- Compute values in SELECT (arithmetic)
- Conditional logic with CASE
- Convert case and trim strings
- Extract a substring
- Get the length of a string
- Round and format numbers
- Get the current date/time
- Extract parts of a date
- Filter by date range
- Count rows
- Order NULLs first or last
- Filter with NOT
- Use column aliases in ORDER BY
- Return a constant/computed column
- Filter with comparison operators
- Combine result sets with UNION
- Difference between UNION and UNION ALL
- INTERSECT and EXCEPT
- Cast/convert data types
- Filter using a computed condition
- Return top N per simple sort
- Use aliases with expressions and functions
- Filter rows by string length
- Replace substring values
- Order of SQL clause evaluation
- What is a JOIN?
- What is an INNER JOIN?
- What is a LEFT JOIN?
- What is a RIGHT JOIN?
- What is a FULL OUTER JOIN?
- What is a CROSS JOIN?
- What is a SELF JOIN?
- Difference between INNER JOIN and LEFT JOIN
- Find rows in one table with no match in another
- Join three or more tables
- Join on multiple columns
- Difference between ON and WHERE in a join
- Join a table to itself for hierarchy
- Count related rows per parent
- Why COUNT(child.id) instead of COUNT(*) in a LEFT JOIN?
- Join and aggregate (sum per group)
- Avoid duplicate rows from one-to-many joins
- Join with a filtered (derived) table
- Semi-join with EXISTS vs JOIN
- Anti-join with NOT EXISTS
- Difference between NOT IN and NOT EXISTS with NULLs
- Join using USING clause
- Natural join and why to avoid it
- Join to find matching pairs (e.g. same city)
- Left join with default values
- Join and filter on the right table correctly
- Join two tables and pick the latest related row
- Join with aggregation and HAVING
- Multiple LEFT JOINs
- Emulate FULL OUTER JOIN in MySQL
- Join performance: indexing join keys
- Nested loop vs hash vs merge join
- Join with a range condition
- Deduplicate a join result
- Join to a numbers/calendar table
- Combine data with a UNION vs JOIN — which to use?
- Find common rows between two tables
- Join with conditional aggregation
- Join order and the optimizer
- Join to explode an array/JSON (lateral)
Related
- System design interview questions
- Node.js interview questions
- Java interview questions
- Practice coding problems online
Frequently asked questions
What SQL topics come up most in interviews?
Joins, aggregation with GROUP BY and HAVING, window functions, subqueries and CTEs, indexing and query plans, transactions and isolation levels, and schema design.
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation, so it can use aggregate functions like COUNT or SUM.
How do I make a slow query faster?
Read the execution plan, check that filters and joins can use an index, avoid functions on indexed columns, select only the columns you need and consider a covering or composite index.