Top 50 SQL Interview Questions (2025)
![]() |
Top 50 SQL Interview Questions |
1. What is SQL?
2. Differentiate between SQL and NoSQL databases.
3. What are the different types of SQL commands?
4. Explain the difference between WHERE and HAVING clauses.
WHERE
filters rows before grouping (used with SELECT,
UPDATE).
HAVING
filters groups after aggregation (used with GROUP BY),
e.g., filtering aggregated results like sums or counts.
5. Write a SQL query to find the second highest salary in a table.
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
SELECT salary FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as
rnk
FROM employees) t
WHERE rnk = 2;
6. What is a JOIN? Explain different types of JOINs.
7. How do you optimize slow-performing SQL queries?
8. What is a primary key? What is a foreign key?
9. What are indexes? Explain clustered and non-clustered indexes.
10. Write a SQL query to fetch the top 5 records from a table.
SELECT * FROM table_name
ORDER BY some_column DESC
LIMIT 5;
SELECT TOP 5 * FROM table_name
ORDER BY some_column DESC;
11. What is a subquery? Give an example.
Example:
SELECT name FROM employees
WHERE department_id = (SELECT id FROM departments WHERE name =
'Sales');
12. Explain the concept of normalization.
Normalization is organizing data to minimize redundancy by dividing tables and defining relationships using keys. It improves data integrity and reduces update anomalies. Common normal forms: 1NF, 2NF, 3NF.
13. What is denormalization? When is it used?
Denormalization is combining tables to reduce joins, improving read performance at the cost of redundancy. Used in data warehousing or OLAP scenarios requiring fast query responses.
14. Describe transactions and their properties (ACID).
A transaction is a set of SQL operations treated as a single unit. ACID properties:
⦁ Atomicity: all or nothing execution
⦁ Consistency: database moves from one valid state to another
⦁ Isolation: concurrent transactions don’t interfere
⦁ Durability: changes persist after commit
15. What is a stored procedure?
A stored procedure is a precompiled SQL program stored in the database, which can accept parameters and perform complex operations efficiently, improving performance and reusability.
16. How do you handle NULL values in SQL?
Use IS NULL
or IS NOT NULL
to check NULLs.
Functions like COALESCE()
or IFNULL()
replace
NULLs with specified values in queries.
17. Explain the difference between UNION and UNION ALL.
⦁ UNION combines results of two queries and removes duplicates.
⦁ UNION ALL combines results including duplicates, faster than UNION.
18. What are views? How are they useful?
A view is a virtual table based on a SELECT query. It simplifies complex queries, provides security by restricting access, and allows data abstraction.
19. What is a trigger? Give use cases.
Triggers are special procedures that automatically execute in response to certain events on a table (e.g., INSERT, UPDATE). Use cases: auditing changes, enforcing business rules, cascading changes.
20. How do you perform aggregate functions in SQL?
Aggregate functions process multiple rows to return a single value, e.g.,
COUNT(), SUM(), AVG(), MIN(),
and MAX()
. Often
used with GROUP BY
to group results.
21. What is data partitioning?
Splitting large tables into smaller, manageable pieces (partitions) based on a key like date or region, improving query performance and maintenance.
22. How do you find duplicates in a table?
SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING COUNT(*) > 1;
23. What is the difference between DELETE and TRUNCATE?
24. Explain window functions with examples.
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS
salary_rank
FROM employees;
25. What is the difference between correlated and non-correlated subqueries?
26. How do you enforce data integrity?
27. What are CTEs (Common Table Expressions)?
WITH cte AS (SELECT * FROM employees WHERE salary > 5000)
SELECT * FROM cte;
28. Explain EXISTS and NOT EXISTS operators.
29. How do SQL constraints work?
30. What is an execution plan? How do you use it?
31. Describe how to handle errors in SQL.
TRY...CATCH
blocks (in SQL Server) or exception
handling constructs provided by the database to catch and manage runtime
errors, ensuring graceful failure or rollback.
32. What are temporary tables?
#
(local) or
##
(global) in SQL Server.
33. Explain the difference between CHAR and VARCHAR.
CHAR
is fixed-length and pads unused spaces, faster for
fixed-size data.
VARCHAR
is variable-length, saves space for variable data
but may be slightly slower.
34. How do you perform pagination in SQL?
LIMIT
and OFFSET
(MySQL/PostgreSQL):
SELECT * FROM table_name ORDER BY id LIMIT 10 OFFSET 20;
SELECT * FROM table_name ORDER BY id OFFSET 20 ROWS FETCH NEXT 10
ROWS ONLY;
35. What is a composite key?
36. How do you convert data types in SQL?
CAST()
or CONVERT()
functions, e.g.,
SELECT CAST(column_name AS INT) FROM table_name;
37. Explain locking and isolation levels in SQL.
38. How do you write recursive queries?
WITH
clause:WITH RECURSIVE cte AS (
SELECT id, parent_id FROM table WHERE parent_id IS
NULL
UNION ALL
SELECT t.id, t.parent_id FROM table t INNER JOIN cte ON t.parent_id =
cte.id
)
SELECT * FROM cte;
39. What are the advantages of using prepared statements?
40. How to debug SQL queries?
41. Differentiate between OLTP and OLAP databases.
42. What is schema in SQL?
43. How do you implement many-to-many relationships in SQL?
44. What is query optimization?
45. How do you handle large datasets in SQL?
46. Explain the difference between CROSS JOIN and INNER JOIN.
47. What is a materialized view?
48. How do you backup and restore a database?
49. Explain how indexing can degrade performance.
50. Can you write a query to find employees with no managers?
SELECT * FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM employees m WHERE m.id = e.manager_id);