Interview prep
SQL interview questions
75 of the most common SQL questions for data and AI interviews — each with a worked answer, the trap to avoid, and a link to learn it properly. Joins, aggregation, window functions, query tuning.
Filter by role
- What do the ACID properties mean, and how does each one protect your data? Easy ·Amazon·Google·Meta
- What aggregate functions does SQL provide, and what are the subtle behaviours of MIN/MAX on non-numeric types? Easy ·Google·Amazon
- What is the difference between COUNT(*), COUNT(column), and COUNT(DISTINCT column)? Easy ·Amazon·Microsoft·Uber
- What does a CROSS JOIN do, and when is it actually useful? Easy ·Google·Amazon
- What is a derived table, and how does it differ from a correlated subquery or a CTE? Easy ·Microsoft·Snowflake
- What is the difference between DISTINCT and GROUP BY for deduplication? Easy ·Amazon·Microsoft
- How does operator precedence work with AND and OR in a WHERE clause? Easy ·Amazon
- How does GROUP BY behave with multiple columns, and what does each combination represent? Easy ·Amazon·Walmart
- Why does SQL require every non-aggregated SELECT column to appear in GROUP BY? Easy ·Amazon·Meta
- What is the difference between WHERE and HAVING in SQL, and when must you use HAVING? Easy ·Amazon·Google·Meta
- When would you use IN versus BETWEEN in a WHERE clause? Easy
- What is the difference between INNER JOIN, LEFT OUTER JOIN, and FULL OUTER JOIN? Easy ·Amazon·Google·Microsoft
- How does the LIKE operator work in SQL, and when is it a performance problem? Easy ·Microsoft·Amazon
- What does NTILE do, and how would you use it to label customers into quartiles by spend? Easy ·Stripe·Shopify
- Why does WHERE column = NULL never return rows in SQL? Easy ·Amazon·Google·Meta
- What is the difference between OLTP and OLAP workloads, and how does that drive database design choices? Easy ·Amazon·Google·Meta
- How does ORDER BY work with multiple columns, and what is the default sort direction? Easy
- What is the difference between PARTITION BY in a window function and GROUP BY in an aggregate query? Easy ·Amazon·Meta·Microsoft
- What is the difference between a primary key and a foreign key, and what guarantees do they provide? Easy ·Amazon·Google·Microsoft
- What is a scalar subquery, where can it appear in a SQL statement, and what happens if it returns more than one row? Easy ·Google·Microsoft
- How do column aliases work in SQL, and where can you reference them? Easy
- Why is SELECT * considered bad practice in production SQL? Easy ·Amazon·Microsoft
- What is the difference between WHERE and HAVING in SQL? Easy ·Amazon·Google·Microsoft
- Why can't you use a window function directly in a WHERE clause, and how do you work around it? Easy ·Amazon·Google·Microsoft
- How can aggregating after a JOIN produce inflated (double-counted) totals, and how do you fix it? Medium ·Airbnb·Stripe·Lyft
- What is an anti-join, how do you implement one in SQL, and which implementation is most reliable? Medium ·Meta·Airbnb·Stripe
- Why does column order matter in a composite index, and what is the left-prefix rule? Medium ·Amazon·Stripe·Shopify
- How do you use conditional aggregation to pivot row data into columns without a PIVOT keyword? Medium ·Meta·Lyft·Shopify
- What is the difference between a correlated and an uncorrelated subquery, and when does the distinction matter for performance? Medium ·Amazon·Google·Microsoft
- What is a covering index and how does it eliminate heap fetches? Medium ·Amazon·Uber·Snowflake
- When should you choose a CTE over a subquery, and does a CTE always offer a performance advantage? Medium ·Amazon·Google·Databricks
- When should you use EXISTS, IN, or a JOIN for a semi-join, and what are the NULL-safety differences? Medium ·Amazon·Meta·Stripe
- Given a query that filters on both a raw column and an aggregate result, how do you structure it for correctness and performance? Medium ·Amazon·Uber·DoorDash
- How do FIRST_VALUE and LAST_VALUE work, and why does LAST_VALUE often return unexpected results? Medium ·Google·Snowflake
- How do you use a FULL OUTER JOIN to detect missing or mismatched rows between two tables? Medium ·Amazon·Microsoft
- Can you GROUP BY a derived expression or a SELECT alias, and how does this differ across databases? Medium ·Google·Amazon
- Can you use HAVING without GROUP BY, and what does it mean? Medium
- How does a B-tree index work, and when does the database choose not to use it? Medium ·Amazon·Google·Meta
- How do you join tables on multiple keys, and why is the key order in a composite index important? Medium ·Amazon·Google
- What happens when a join key contains NULLs? Do NULL values ever match in a JOIN? Medium ·Amazon·Google
- Does the order of tables in a JOIN clause affect query results or performance? Medium ·Amazon·Google·Snowflake
- How do LAG and LEAD work, and how would you use them to compute month-over-month revenue change? Medium ·Meta·Uber·Airbnb
- Why does filtering on a right-table column in the WHERE clause turn a LEFT JOIN into an INNER JOIN? Medium ·Amazon·Airbnb·Stripe
- How do LIMIT and OFFSET work, and what is the problem with deep pagination? Medium ·Amazon·Meta
- How would you calculate a 7-day moving average of daily sales, and what frame clause is needed? Medium ·Stripe·Shopify·Netflix
- How do you chain multiple CTEs in a single query, and what are the scoping and execution rules you need to know? Medium ·Snowflake·Databricks·Amazon
- What are 1NF, 2NF, and 3NF, and when would you intentionally denormalize? Medium ·Amazon·Google·Microsoft
- Why does NOT IN (subquery) return zero rows when the subquery contains a NULL, and how do you fix it? Medium ·Google·Stripe·Airbnb
- Write a query to find the Nth highest salary in a table, where N is a parameter, handling ties correctly. Medium ·Amazon·Google·Microsoft
- How do aggregate functions handle NULL values in SQL? Medium ·Airbnb·Stripe
- How do you use EXPLAIN / EXPLAIN ANALYZE to diagnose a slow query? Medium ·Amazon·Meta·Stripe
- How do you use a recursive CTE to generate a date series or number sequence when your database lacks a built-in generate_series function? Medium ·Amazon·Databricks
- How do you delete duplicate rows from a table using ROW_NUMBER, keeping only one copy per duplicate group? Medium ·Amazon·Microsoft
- What is the difference between ROW_NUMBER, RANK, and DENSE_RANK? When would you choose each? Medium ·Google·Amazon·Meta
- How do you compute a running total (cumulative sum) using a window function, and what frame clause does it use by default? Medium ·Amazon·Stripe·Airbnb
- What makes a predicate sargable, and what are the most common ways to accidentally make a predicate non-sargable? Medium ·Amazon·Microsoft·Oracle
- How do you find the second-highest salary in SQL? Medium ·Amazon·Meta·Google
- When would you use a self-join, and how do you write one? Medium ·Amazon·Meta
- What is a semi-join and how does it differ from an INNER JOIN in terms of output and performance? Medium ·Google·Amazon
- What is the logical processing order of a SQL SELECT statement? Medium ·Google·Amazon·Meta
- What is the difference between a star schema and a snowflake schema, and which should you choose? Medium ·Amazon·Snowflake·Databricks
- What are the risks of placing a correlated subquery in the SELECT list, and what is the preferred rewrite? Medium ·Amazon·Snowflake·Databricks
- What is table partitioning and when does it improve query performance? Medium ·Amazon·Google·Uber
- Write a query to return the top 3 highest-paid employees in each department. Medium ·Google·Amazon·Meta
- What is an anti-join, and when does NOT IN behave differently from NOT EXISTS? Hard ·Google·Amazon·Microsoft
- What does CTE materialization mean, when does it help versus hurt, and how do you control it in PostgreSQL? Hard ·Amazon·Databricks
- What is the gaps-and-islands problem, and how do you solve it with window functions? Hard ·Amazon·Google·LinkedIn
- What are the four SQL isolation levels and what anomalies does each prevent? Hard ·Amazon·Stripe·Google
- What is join fan-out and how does it cause duplicate rows or inflated aggregates? Hard ·Meta·Amazon·Stripe
- How do you safely join two tables in a many-to-many relationship without creating a row explosion? Hard ·Meta·Amazon·Stripe
- How do you write a non-equi join (range join), and what are the performance implications? Hard ·Amazon·Airbnb
- How does a recursive CTE work, and how would you use one to walk an employee-manager hierarchy? Hard ·Amazon·Microsoft·Oracle
- What are ROLLUP, CUBE, and GROUPING SETS, and when would you choose each? Hard ·Microsoft·Oracle·Snowflake
- What is the difference between ROWS and RANGE in a window frame clause, and when does it matter? Hard ·Google·Snowflake·Databricks
- Walk me through how you would systematically diagnose a slow SQL query in production. Hard ·Amazon·Meta·Stripe
No questions tagged for that role yet.