Introduction
SQL (Structured Query Language) is the backbone of database management, making it a crucial skill for data analysts, backend developers, and database administrators. Whether you’re a beginner or an experienced professional, SQL interviews test your ability to write efficient queries, optimize databases, and solve real-world problems.
To help you prepare, we’ve compiled the most common SQL interview questions that recruiters ask. This guide covers:
✅ Basic SQL Questions (Syntax, Queries, Constraints)
✅ Intermediate SQL Questions (JOINs, Subqueries, Aggregations)
✅ Advanced SQL Questions (Indexing, Optimization, Transactions)
Let’s dive in and make sure you’re fully prepared!
Basic SQL Interview Questions
1. What is SQL?
SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases. It allows users to create, read, update, and delete data efficiently.
2. What are the different types of SQL commands?
SQL commands are categorized into four types:
- DDL (Data Definition Language):
CREATE
,ALTER
,DROP
,TRUNCATE
- DML (Data Manipulation Language):
SELECT
,INSERT
,UPDATE
,DELETE
- DCL (Data Control Language):
GRANT
,REVOKE
- TCL (Transaction Control Language):
COMMIT
,ROLLBACK
,SAVEPOINT
3. What is a Primary Key?
A Primary Key is a unique identifier for a row in a table. It cannot be NULL and must contain unique values.
4. What is a Foreign Key?
A Foreign Key is a column that links two tables by referencing the Primary Key of another table. It ensures referential integrity.
5. What is the difference between DELETE, TRUNCATE, and DROP?
Command | Description | Rollback Possible? | Speed |
---|---|---|---|
DELETE | Removes rows one by one | ✅ (with transaction) | Slow |
TRUNCATE | Removes all rows at once | ❌ | Fast |
DROP | Deletes the entire table | ❌ | Fastest |
Intermediate SQL Interview Questions
6. What are SQL JOINs? Explain types.
JOINs combine rows from two or more tables based on related columns.
JOIN Type | Description |
---|---|
INNER JOIN | Returns only matching rows |
LEFT JOIN | Returns all rows from the left table + matched rows from the right |
RIGHT JOIN | Returns all rows from the right table + matched rows from the left |
FULL JOIN | Returns all rows when there’s a match in either table |
7. What is the difference between WHERE and HAVING?
- WHERE filters rows before grouping.
- HAVING filters groups after the
GROUP BY
clause.
8. What is a Subquery?
A Subquery is a query nested inside another query (SELECT, INSERT, UPDATE, DELETE).
Example:
sql
Copy
SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
9. What is a Self Join?
A Self Join is when a table is joined with itself (useful for hierarchical data).
Example:
sql
Copy
SELECT A.name, B.name AS manager FROM employees A, employees B WHERE A.manager_id = B.id;
10. What is the difference between UNION and UNION ALL?
- UNION: Combines results and removes duplicates.
- UNION ALL: Combines results without removing duplicates (faster).
Advanced SQL Interview Questions
11. What are Indexes? How do they improve performance?
An Index is a database structure that speeds up data retrieval (like a book index). It helps in fast searching but slows down INSERT/UPDATE/DELETE
operations.
12. What is Normalization? Explain 1NF, 2NF, 3NF.
Normalization reduces data redundancy by organizing tables efficiently.
Normal Form | Rule |
---|---|
1NF | No repeating groups, atomic values |
2NF | No partial dependency (all non-key columns depend on the full PK) |
3NF | No transitive dependency (non-key columns don’t depend on other non-key columns) |
13. What is a Stored Procedure?
A Stored Procedure is a precompiled SQL query stored in the database for reuse.
Example:
sql
Copy
CREATE PROCEDURE GetEmployees AS SELECT * FROM employees;
14. What is a Trigger?
A Trigger is an automated action that executes when a specific event (INSERT
, UPDATE
, DELETE
) occurs.
15. How do you optimize a slow SQL query?
✔ Use INDEXES on frequently queried columns.
✔ Avoid SELECT *
(fetch only needed columns).
✔ Optimize JOINs and WHERE clauses.
✔ Use EXPLAIN to analyze query execution.
Final Tips to Crack SQL Interviews
🔹 Practice on real databases (MySQL, PostgreSQL, SQL Server).
🔹 Solve problems on LeetCode & HackerRank.
🔹 Understand query execution plans.
🔹 Be ready for scenario-based questions (e.g., “How would you find the second-highest salary?”).
Conclusion
Mastering these common SQL interview questions will boost your confidence and help you ace technical rounds. Whether you’re a fresher or an experienced candidate, SQL skills are highly valued in tech roles.
📢 Found this helpful? Share it with your peers preparing for SQL interviews!