Top 50 SQL Interview Questions
Joins, window functions, indexing, and query optimization — the SQL topics that appear in almost every data and backend interview.
Share
Why SQL still matters for every developer
Whether you are a backend engineer, data analyst, or full-stack developer, SQL appears in technical screens constantly. Interviewers test your ability to write correct queries under pressure, explain query plans, and design schemas that scale.
Practice each question by writing the query from scratch, then explaining your approach aloud in 90 seconds.
Fundamentals and filtering (10 questions)
Start here — these are warm-up questions that still trip people up:
- What is the difference between WHERE and HAVING?
- Explain INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN with examples.
- What is the difference between UNION and UNION ALL?
- How do DISTINCT and GROUP BY differ?
- What are aggregate functions? Name the common ones.
- How do you find duplicate rows in a table?
- What is the difference between DELETE, TRUNCATE, and DROP?
- How do NULL values behave in comparisons and aggregate functions?
- What is COALESCE and when do you use it?
- Write a query to find the second highest salary.
Joins and subqueries (10 questions)
Joins are the heart of SQL interviews:
- Self-join — give a real-world example (employee-manager hierarchy).
- Correlated subquery vs non-correlated subquery.
- EXISTS vs IN — when is each more efficient?
- How do you find records in Table A that have no match in Table B?
- What is a CROSS JOIN and when is it useful?
- Write a query to return customers who placed orders in the last 30 days.
- How do you join more than two tables cleanly?
- Subquery in SELECT vs subquery in FROM (derived table).
- What is the difference between ANY, ALL, and EXISTS?
- Find employees who earn more than the average salary in their department.
Window functions and analytics (10 questions)
Modern SQL interviews heavily test window functions:
- What is a window function and how does it differ from GROUP BY?
- ROW_NUMBER vs RANK vs DENSE_RANK — when to use each?
- How do you compute a running total with SUM() OVER?
- LEAD and LAG — give an example for month-over-month comparison.
- PARTITION BY — how does it define window boundaries?
- Write a query to find the top 3 products per category by revenue.
- NTILE — how do you split data into quartiles?
- FIRST_VALUE and LAST_VALUE use cases.
- How do you calculate a 7-day moving average?
- Find consecutive login days for a user (gaps and islands pattern).
Indexing and query optimization (10 questions)
Backend roles expect you to explain performance, not just write queries:
- What is a database index and how does a B-tree index work?
- When should you add an index? When should you avoid one?
- What is a covering index?
- Explain clustered vs non-clustered indexes (or heap vs clustered in PostgreSQL).
- What causes a full table scan? How do you detect it?
- How do you read an EXPLAIN / EXPLAIN ANALYZE plan?
- What is selectivity and why does it matter for index choice?
- How do composite indexes work? What is the leftmost prefix rule?
- What is index fragmentation and how do you address it?
- How do you optimize a slow JOIN query?
Schema design and advanced topics (10 questions)
Senior and backend-heavy interviews go deeper:
- Normalization — explain 1NF, 2NF, 3NF with a simple example.
- When would you denormalize a schema?
- Primary key vs unique key vs foreign key.
- What are transactions? Explain ACID properties.
- Isolation levels — what anomalies does each prevent?
- What is a deadlock in a database? How do you prevent it?
- How do you model a many-to-many relationship?
- Slowly Changing Dimensions (SCD Type 1 vs Type 2) — brief overview.
- What is the difference between OLTP and OLAP workloads?
- How do you handle pagination efficiently on a large table (OFFSET vs keyset)?
Practice strategy
Do 10 questions per day on a whiteboard or SQL fiddle. Focus on window functions and query plans — they differentiate mid-level from senior candidates. Run timed SQL challenges alongside coding practice on InterviewVeda.
Related articles
Complete DSA Roadmap for Interview Preparation
A 12-week structured plan covering arrays, trees, graphs, DP, and system design basics for product company interviews.
Kavya Reddy
Senior SDE Mentor
Top 50 DSA Questions for Amazon Interviews
High-frequency patterns from Amazon OA and onsite loops — arrays, trees, graphs, and greedy.
Kavya Reddy
Senior SDE Mentor
Top 100 Java Interview Questions for 2026
A curated list of core Java, OOP, collections, concurrency, and JVM questions asked in product and service company interviews.
Kavya Reddy
Senior SDE Mentor
Practice what you learned with AI mock interviews and resume tools.
Get started free