Top 20 SQL Interview Questions for 2025 (With Answers)


SQL is a non-negotiable skill for developers, analysts, and engineers in 2025. Whether you're applying for backend roles or data-driven positions, these top SQL questions will sharpen your edge in interviews.
✅ Basics & Core Syntax
1. What is SQL?
SQL (Structured Query Language) is used to interact with relational databases — for querying, updating, and managing data.
2. Difference between WHERE
and HAVING
?
WHERE
filters rows before aggregationHAVING
filters after aggregation
3. What are the different types of SQL statements?
DDL (Data Definition Language):
CREATE
,ALTER
,DROP
DML (Data Manipulation Language):
INSERT
,UPDATE
,DELETE
DQL:
SELECT
DCL:
GRANT
,REVOKE
TCL:
COMMIT
,ROLLBACK
,SAVEPOINT
4. What is the difference between INNER JOIN
and LEFT JOIN
?
INNER JOIN
: Returns only matching recordsLEFT JOIN
: Returns all from left table + matching from right
5. What is a Primary Key vs Foreign Key?
Primary Key: Uniquely identifies each row
Foreign Key: References primary key in another table (used to maintain relationships)
✅ Queries & Clauses
6. How to find duplicate records in a table?
sqlCopyEditSELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
7. What does GROUP BY
do?
It aggregates rows that have the same values in specified columns.
8. How does DISTINCT
work?
Removes duplicate records from a result set.
9. How to fetch the second highest salary from an Employee table?
sqlCopyEditSELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
10. Difference between DELETE
, TRUNCATE
, and DROP
?
DELETE
: Deletes rows, can be rolled backTRUNCATE
: Deletes all rows, can’t be rolled backDROP
: Removes table schema entirely
✅ Constraints, Indexes & Optimization
11. What are SQL Constraints?
Rules enforced on data: NOT NULL
, UNIQUE
, CHECK
, DEFAULT
, PRIMARY KEY
, FOREIGN KEY
12. What is an Index?
A performance tuning feature — speeds up retrievals but slows down inserts/updates.
13. What is Normalization?
Organizing data to reduce redundancy:
1NF: Atomic values
2NF: No partial dependencies
3NF: No transitive dependencies
14. Difference between UNION
and UNION ALL
?
UNION
: Removes duplicatesUNION ALL
: Keeps all records (faster)
15. What is a View?
A virtual table created using a SQL query. It does not store data itself.
✅ Real-World & Advanced Scenarios
16. What are Stored Procedures?
A block of SQL code that performs a task, saved in the DB and reused.
17. What is a CTE (Common Table Expression)?
Temporary result set used inside a WITH
clause for readable, reusable logic.
18. What are window functions in SQL?
Used to perform calculations across rows related to the current row, without collapsing the result set:
Examples: ROW_NUMBER()
, RANK()
, LEAD()
, LAG()
19. Difference between OLTP and OLAP systems?
OLTP: Transactional systems (real-time)
OLAP: Analytical systems (data analysis/reporting)
20. What is ACID in databases?
Atomicity: All-or-nothing transactions
Consistency: Data stays valid
Isolation: Transactions don’t interfere
Durability: Changes are permanent
🚀 Boost Your SQL Game
📘 Want the complete SQL Masterclass eBook (100+ Questions)?
Join our Telegram: t.me/CoDevsCircle
📱 Check our apps at → Visit
Subscribe to my newsletter
Read articles from Team CoDev's directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
