SQL Interview Questions & Answers
All 200 unique questions in one place — 59 easy, 99 medium, 42 hard — grouped by topic.
Practise by topic
Practise the way your interview works
What are DDL, DML, DCL and TCL commands?
Show answer
What is the difference between DELETE, TRUNCATE and DROP?
Show answer
What is the difference between WHERE and HAVING?
Show answer
What is the logical order of execution of a SELECT query?
Show answer
What is NULL in SQL and how do you compare it?
Show answer
What are the common SQL data types?
Show answer
What is the difference between CHAR and VARCHAR?
Show answer
Write a query to get employees from Delhi with salary above 50000, highest first.
Show answer
Write SQL to create a students table with a primary key and constraints.
Show answer
How do you insert, update and delete a row?
Show answer
Why should we hire you for a SQL or data role as a fresher?
Show answer
What does DISTINCT do and how is it different from GROUP BY?
Show answer
What is the difference between UNION and UNION ALL?
Show answer
What is the CASE expression and where can it be used?
Show answer
How does LIKE work and what are wildcards?
Show answer
Find all customers whose email is missing or empty.
Show answer
Write a query to pivot order counts by status into columns.
Show answer
How do you get the top 3 rows in different databases?
Show answer
Your query WHERE manager_id <> 5 is not returning employees who have no manager. Why?
Show answer
Someone ran an UPDATE without WHERE on production. What would you do?
Show answer
A search on name LIKE '%sharma%' is very slow on a 20 million row table. What can you do?
Show answer
Tell me about a time you automated a manual data task with SQL.
Show answer
A report sums amounts stored in a FLOAT column and finance says totals are off by a few paise. Why?
Show answer
How do SQL dialects differ and how do you write portable SQL?
Show answer
What is a join and what types of joins exist?
Show answer
What is the difference between INNER JOIN and LEFT JOIN?
Show answer
What is a self join? Give an example.
Show answer
What is a CROSS JOIN and when is it useful?
Show answer
Write a query to list orders with customer names.
Show answer
Find customers who have never placed an order.
Show answer
How do you keep improving your SQL skills?
Show answer
What happens when you join on a column that has NULL values?
Show answer
What is the difference between a join and a union?
Show answer
What is the difference between putting a condition in ON versus WHERE in a LEFT JOIN?
Show answer
What are equi joins, non-equi joins and natural joins?
Show answer
What are anti joins and semi joins?
Show answer
Write a query to show each employee with their manager's name.
Show answer
Join three tables to get total sales by product category.
Show answer
Match each employee's salary to a grade using a range table.
Show answer
After joining orders with payments, revenue doubled. What went wrong?
Show answer
A LEFT JOIN is returning fewer rows than the left table. Why could that happen?
Show answer
You need customers who bought both product A and product B. How would you write it?
Show answer
Describe a time you found and fixed a wrong number in a report.
Show answer
What is the difference between FULL OUTER JOIN and UNION of LEFT and RIGHT joins?
Show answer
Compare this month's and last month's sales per product using a full outer join.
Show answer
What physical join algorithms do databases use?
Show answer
Show all dates in a month with sales, including days with zero sales.
Show answer
A join between two large tables takes minutes. How would you speed it up?
Show answer
Two systems store customer IDs differently, one as INT and one as VARCHAR with leading zeros. How do you join them?
Show answer
What are aggregate functions in SQL?
Show answer
What is the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column)?
Show answer
What is GROUP BY and what is the rule for selected columns?
Show answer
How does AVG treat NULL values?
Show answer
Find the number of employees and average salary in each department.
Show answer
Find cities with more than 100 customers.
Show answer
What is the difference between MIN/MAX on numbers and on strings or dates?
Show answer
Can you use a column alias in GROUP BY or HAVING?
Show answer
What are ROLLUP, CUBE and GROUPING SETS?
Show answer
What is the difference between SUM with CASE and filtering with WHERE?
Show answer
How do you concatenate values within a group?
Show answer
Find duplicate email addresses in a users table.
Show answer
Write a query for monthly revenue for the current year.
Show answer
Produce region subtotals and a grand total in one query.
Show answer
Your GROUP BY query returns an error that a column is not in the group by clause. How do you fix it?
Show answer
Average order value looks too low in a report. What could be wrong?
Show answer
Management wants the percentage of each category in total sales. How do you write it?
Show answer
Tell me about a time you had to deliver a report under a tight deadline.
Show answer
A COUNT(DISTINCT) query on a huge events table is very slow. What options do you have?
Show answer
How do databases execute GROUP BY internally?
Show answer
Calculate the median salary per department.
Show answer
A daily aggregation over 2 billion rows is too slow for a dashboard. What would you propose?
Show answer
How do you handle a situation where business users dispute your numbers?
Show answer
Finance asks for the count of customers whose total spend crossed 1 lakh in each city. How do you write it?
Show answer
What is a subquery and where can it be used?
Show answer
What is the difference between a correlated and a non-correlated subquery?
Show answer
What is a CTE and why use it?
Show answer
Find employees earning more than the company average.
Show answer
Find the second highest salary.
Show answer
How do you approach a SQL problem you have never seen before in an interview?
Show answer
What is the difference between IN and EXISTS?
Show answer
Why is NOT IN dangerous when the subquery contains NULL?
Show answer
What is the difference between a CTE, a subquery, a temp table and a view?
Show answer
Are CTEs materialised or inlined?
Show answer
Find employees who earn more than their department average.
Show answer
Rewrite a nested query using CTEs for readability.
Show answer
Find departments where every employee earns above 40000.
Show answer
Your NOT IN query returns zero rows even though you expect results. What do you check?
Show answer
A query with a correlated subquery in SELECT runs for every row and is slow. How do you improve it?
Show answer
You need to delete duplicate rows but keep one copy of each. How do you do it?
Show answer
An interviewer asks for the Nth highest salary in a reusable way. How do you answer?
Show answer
Describe a time you reviewed someone else's SQL and suggested improvements.
Show answer
What is a recursive CTE and how does it work?
Show answer
What is a lateral join or CROSS APPLY?
Show answer
Write a recursive CTE to show an employee hierarchy with levels.
Show answer
Get the latest 3 orders for every customer using a lateral join.
Show answer
A long report query has eight nested subqueries and nobody can maintain it. How would you refactor it?
Show answer
A recursive CTE on a category tree runs forever. What is the likely cause and fix?
Show answer
Tell me about a time you set SQL coding standards for a team.
Show answer
What are window functions in SQL?
Show answer
What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
Show answer
What do PARTITION BY and ORDER BY do inside OVER?
Show answer
What is the difference between GROUP BY and window functions?
Show answer
Rank employees by salary within each department.
Show answer
Calculate a running total of daily sales.
Show answer
Describe your SQL project from college or internship.
Show answer
What are LAG and LEAD functions?
Show answer
What is a window frame and what is the default frame?
Show answer
What is the difference between ROWS and RANGE frames?
Show answer
What are FIRST_VALUE, LAST_VALUE and NTH_VALUE?
Show answer
What do NTILE, PERCENT_RANK and CUME_DIST do?
Show answer
Get the top 2 highest paid employees in each department.
Show answer
Calculate month-over-month revenue growth percentage.
Show answer
Calculate a 7-day moving average of daily active users.
Show answer
Your running total jumps by large amounts on some dates instead of row by row. Why?
Show answer
You need each customer's latest address from a history table. How do you query it?
Show answer
An interviewer asks to show each employee's salary next to their department's average. How do you do it?
Show answer
Tell me about a time you used advanced SQL to solve a business problem.
Show answer
Can you use window functions with aggregate functions in the same query?
Show answer
Assign each customer to a spending quartile.
Show answer
Why can't you use a window function in WHERE, and how do you filter on it?
Show answer
What are the performance considerations of window functions?
Show answer
Find users who logged in on 3 or more consecutive days.
Show answer
Calculate the time each user spent between consecutive events.
Show answer
A retention report needs the percentage of users active in each month after signup. How would you build it in SQL?
Show answer
What is a primary key and a foreign key?
Show answer
What is the difference between a primary key and a unique key?
Show answer
What is normalisation and why is it needed?
Show answer
Explain 1NF, 2NF and 3NF with simple examples.
Show answer
Design tables for students, courses and enrolments.
Show answer
What is a composite key?
Show answer
How do you document a database you designed so others can use it?
Show answer
What are constraints in SQL?
Show answer
What is denormalisation and when is it used?
Show answer
What is the difference between OLTP and OLAP databases?
Show answer
What is a star schema and a snowflake schema?
Show answer
What are surrogate keys and natural keys?
Show answer
What are referential actions such as ON DELETE CASCADE?
Show answer
Add a foreign key and a check constraint to an existing table.
Show answer
A table stores phone numbers as 'ph1, ph2, ph3' in one column. What is wrong and how do you fix it?
Show answer
How would you design a database for an online food delivery app at a high level?
Show answer
A reporting team wants to query the production OLTP database directly. What are the risks and alternatives?
Show answer
Tell me about a database design decision you made and its outcome.
Show answer
What are slowly changing dimensions in data warehousing?
Show answer
What is BCNF and how is it different from 3NF?
Show answer
Product prices change often, but old orders must keep the price at the time of purchase. How do you design it?
Show answer
Your team debates using UUIDs or auto-increment integers as primary keys. What do you advise?
Show answer
How do you handle disagreement with developers about schema changes?
Show answer
An e-commerce app needs products with very different attributes, such as phones and shirts. How do you model them?
Show answer
What is an index in a database?
Show answer
What is the difference between clustered and non-clustered indexes?
Show answer
What is a composite index and why does column order matter?
Show answer
What is an execution plan?
Show answer
Create an index to speed up searching orders by customer and date.
Show answer
What is the difference between a unique index and a normal index?
Show answer
What is the difference between an index scan and an index seek?
Show answer
What is a covering index?
Show answer
What is a SARGable query and why does it matter?
Show answer
When should you not create an index?
Show answer
What are database statistics and why do they matter?
Show answer
Rewrite a non-SARGable date filter to use an index.
Show answer
How do you read an EXPLAIN ANALYZE output in PostgreSQL?
Show answer
A query that ran in 1 second now takes 40 seconds after a data load. What do you check?
Show answer
You added an index but the query still does a full table scan. Why might that be?
Show answer
A query uses SELECT * on a table with 80 columns. Why is that a problem?
Show answer
An interviewer asks how you would find why a query is slow. What steps do you describe?
Show answer
Tell me about a time you significantly improved query performance.
Show answer
What is table partitioning and how does it help performance?
Show answer
What is parameter sniffing and how do you handle it?
Show answer
Implement keyset pagination instead of OFFSET for large tables.
Show answer
Inserts into a heavily used table have become slow. What could be causing it?
Show answer
The dashboard queries hit the production database and slow down the app at month end. What do you propose?
Show answer
A query with many OR conditions across different columns is slow. How can you optimise it?
Show answer
How do you balance performance tuning with delivery deadlines?
Show answer
What is a transaction in SQL?
Show answer
What are ACID properties?
Show answer
What is a view and why use it?
Show answer
What is a stored procedure?
Show answer
Write a transaction to transfer money between two accounts.
Show answer
How do you make sure your SQL changes do not break anything?
Show answer
What is the difference between a stored procedure and a function?
Show answer
What are transaction isolation levels?
Show answer
What are dirty reads, non-repeatable reads and phantom reads?
Show answer
What is a deadlock and how do databases handle it?
Show answer
What is a trigger and when should it be used?
Show answer
What is a materialised view?
Show answer
Write an upsert that inserts a row or updates it if it exists.
Show answer
Create a simple stored procedure that returns orders for a customer.
Show answer
How do you prevent SQL injection in applications?
Show answer
Two users book the last seat at the same time and both succeed. How do you fix it?
Show answer
You need to update 50 million rows in production. How do you do it safely?
Show answer
A business user needs a dashboard on a complex 6-table join that runs slowly. Would you use a view or a materialised view?
Show answer
Tell me about a production incident involving a database and how you handled it.
Show answer
How do you explain a technical database issue to a non-technical manager?
Show answer
What is MVCC and how does it improve concurrency?
Show answer
What is the difference between optimistic and pessimistic locking?
Show answer
Create an audit trigger that logs salary changes.
Show answer
Your application logs frequent deadlock errors at peak hours. How do you investigate and reduce them?
Show answer
A long-running report transaction is causing table bloat and blocking. What do you do?
Show answer
How would you design data archiving for a table growing by 10 million rows a month?
Show answer
Frequently asked questions
What jobs can I get after learning SQL?
SQL opens roles such as Data Analyst, MIS Analyst, SQL Developer, BI Developer, Database Administrator and Data Engineer, and it is a required skill for backend developers and many testers. In India, IT services companies, banks, fintech, e-commerce and analytics firms test SQL in most data-related interviews. Combining SQL with Excel, a BI tool like Power BI or Tableau, or Python greatly increases your job options.
Who can learn SQL and is coding experience needed?
Anyone comfortable with computers and basic logic can learn SQL. It is popular with B.Tech, BCA, MCA, B.Com, BBA, B.Sc and MBA students, and with working professionals in operations, finance, support and testing who want to move into data roles. No prior programming is needed, because SQL is declarative and reads close to English, though logical thinking and practice are essential.
How long does it take to learn SQL for interviews?
Most learners can write basic queries within two weeks of daily practice. Becoming interview-ready, including joins, aggregation, subqueries, CTEs and window functions, typically takes six to eight weeks with one to two hours a day. Solving 150 to 200 practice problems, building a small project and reviewing execution plans makes a strong difference in technical rounds.
Which database should I learn first: MySQL, PostgreSQL or SQL Server?
Core SQL is the same across all of them, so start with any one. MySQL is widely used in web applications and is simple to install, PostgreSQL is feature-rich and popular in startups and data teams, and SQL Server is common in enterprises and banks. Learn one deeply, then note dialect differences such as LIMIT versus TOP, date functions and upsert syntax.
Is an SQL certification useful?
Certifications from database vendors or cloud platforms can strengthen your resume, especially for DBA and data engineering roles in IT services companies. However, most interviewers test SQL live through coding problems, so practical skill matters most. A portfolio of solved problems, a project with real queries on a public dataset, and clear explanations of your logic usually create a stronger impression than a certificate alone.
Is SQL still relevant with AI tools that write queries?
Yes. AI assistants can draft queries, but professionals must check whether joins duplicate rows, whether NULLs and filters are handled correctly, and whether queries perform well on large data. Companies still test SQL heavily in interviews because wrong numbers in reports have real business impact. Strong SQL fundamentals also help you use AI tools faster and more safely.