Master SQL Interviews in 2026
Comprehensive collection of SQL interview questions with detailed answers. Covers everything from basic concepts to advanced topics asked at top tech companies like Google, Netflix, and Uber.
Question Categories
💡 SQL Interview Success Tips
Before the Interview:
- • Practice writing queries by hand
- • Review database design principles
- • Understand indexing and performance
- • Study the company's data architecture
During the Interview:
- • Ask clarifying questions about data
- • Explain your thought process aloud
- • Start with simple solutions, then optimize
- • Test your query with edge cases
Basic SQL Questions
Fundamental SQL concepts for entry-level positions
What is SQL and what does it stand for?
SQL stands for Structured Query Language. It's a programming language designed for managing and manipulating relational databases. SQL allows you to create, read, update, and delete data from databases.
What's the difference between WHERE and HAVING clauses?
WHERE filters rows before grouping, while HAVING filters groups after GROUP BY. WHERE cannot use aggregate functions, but HAVING can. Example: WHERE salary > 50000 vs HAVING COUNT(*) > 5.
Explain the different types of SQL JOINs.
INNER JOIN returns matching rows from both tables. LEFT JOIN returns all rows from left table and matching from right. RIGHT JOIN returns all from right and matching from left. FULL OUTER JOIN returns all rows from both tables.
What is a primary key?
A primary key is a column or combination of columns that uniquely identifies each row in a table. It cannot contain NULL values and must be unique. Each table can have only one primary key.
What's the difference between DELETE, TRUNCATE, and DROP?
DELETE removes specific rows and can be rolled back. TRUNCATE removes all rows quickly but can't be rolled back easily. DROP removes the entire table structure and data permanently.
Intermediate SQL Questions
More complex scenarios for mid-level developers
Write a query to find the second highest salary.
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); or using window functions: SELECT DISTINCT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank FROM employees) WHERE rank = 2;
How do you handle NULL values in SQL?
Use IS NULL or IS NOT NULL for comparisons. COALESCE() returns first non-NULL value. ISNULL() replaces NULL with specified value. NULL in arithmetic operations returns NULL.
Explain the difference between UNION and UNION ALL.
UNION removes duplicate rows and sorts results, making it slower. UNION ALL includes all rows including duplicates and doesn't sort, making it faster. Use UNION ALL when you know there are no duplicates.
What are aggregate functions? Name the common ones.
Aggregate functions perform calculations on multiple rows and return a single value. Common ones: COUNT() - counts rows, SUM() - adds values, AVG() - calculates average, MIN()/MAX() - find minimum/maximum, STRING_AGG() - concatenates strings.
How do you find duplicate records in a table?
SELECT column1, column2, COUNT(*) FROM table_name GROUP BY column1, column2 HAVING COUNT(*) > 1; This groups by the columns that might have duplicates and filters for groups with more than one record.
Advanced SQL Questions
Complex scenarios for senior developers and architects
Explain window functions and their use cases.
Window functions perform calculations across rows related to current row without grouping. Examples: ROW_NUMBER() for pagination, RANK() for ranking with ties, LAG/LEAD for comparing with previous/next rows, running totals with SUM() OVER().
What are CTEs and when would you use them?
Common Table Expressions (CTEs) are temporary named result sets. Use for: breaking complex queries into readable parts, recursive operations, replacing views for single queries. Syntax: WITH cte_name AS (SELECT ...) SELECT ... FROM cte_name.
How do you optimize a slow SQL query?
1) Analyze execution plan 2) Add proper indexes 3) Avoid SELECT * 4) Use WHERE clauses early 5) Limit result sets 6) Use EXISTS instead of IN for subqueries 7) Consider query rewriting 8) Update table statistics.
Explain database normalization and its forms.
Normalization reduces data redundancy. 1NF: eliminate repeating groups. 2NF: remove partial dependencies. 3NF: remove transitive dependencies. BCNF: every determinant is a candidate key. Higher forms exist but rarely used in practice.
Write a recursive CTE to traverse a hierarchy.
WITH RECURSIVE emp_hierarchy AS (SELECT employee_id, name, manager_id, 1 as level FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.employee_id, e.name, e.manager_id, h.level + 1 FROM employees e JOIN emp_hierarchy h ON e.manager_id = h.employee_id) SELECT * FROM emp_hierarchy;
Company-Specific Questions
Real questions from top tech companies
Netflix: Find users who watched more than 5 different genres in the last month.
SELECT user_id, COUNT(DISTINCT genre) as genre_count FROM user_views v JOIN content c ON v.content_id = c.content_id WHERE v.view_date >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH) GROUP BY user_id HAVING COUNT(DISTINCT genre) > 5;
Uber: Calculate the average trip distance for each driver in the last week.
SELECT driver_id, AVG(trip_distance) as avg_distance FROM trips WHERE trip_date >= DATE_SUB(CURRENT_DATE, INTERVAL 1 WEEK) AND trip_status = 'completed' GROUP BY driver_id ORDER BY avg_distance DESC;
Google: Find the day with the highest search volume increase compared to the previous day.
WITH daily_searches AS (SELECT date, COUNT(*) as search_count FROM searches GROUP BY date), search_with_prev AS (SELECT date, search_count, LAG(search_count) OVER (ORDER BY date) as prev_count FROM daily_searches) SELECT date, (search_count - prev_count) as increase FROM search_with_prev ORDER BY increase DESC LIMIT 1;
Ready to Practice These Interview Questions?
Don't just memorize answers - practice writing actual SQL queries with our interactive platform. Build muscle memory and confidence for your next interview.
SQL Fundamentals
Master the basics before tackling interview questions. Start with SELECT, JOINs, and filtering.
Learn SQL Basics →Advanced Topics
Window functions, CTEs, and performance optimization for senior-level interviews.
Advanced SQL Guide →Company Case Studies
Real SQL challenges from Netflix, Uber, Discord, and other top tech companies.
Practice Company Questions →