Crack your SQL interview with these Top 20 SQL Interview Questions and Answers. Includes detailed explanations, examples, and key differences for freshers and experienced candidates.
🔹 1. What is SQL?
Answer:
SQL (Structured Query Language) is a standard language used to manage and manipulate databases.
Example:
SELECT * FROM employees;
🔹 2. What is the difference between DELETE
, TRUNCATE
, and DROP
?
Command | Description | Rollback Possible | Affects Structure |
---|---|---|---|
DELETE | Deletes selected rows | Yes | No |
TRUNCATE | Deletes all rows quickly | No | No |
DROP | Deletes the entire table | No | Yes |
🔹 3. What are different types of SQL statements?
Answer:
- DDL – Data Definition Language (
CREATE
,ALTER
,DROP
) - DML – Data Manipulation Language (
INSERT
,UPDATE
,DELETE
) - DQL – Data Query Language (
SELECT
) - DCL – Data Control Language (
GRANT
,REVOKE
) - TCL – Transaction Control Language (
COMMIT
,ROLLBACK
)
Top 20 SQL Interview Questions and Answers
🔹 4. What is a primary key?
Answer:
A primary key is a column (or set of columns) that uniquely identifies each row in a table.
Example:
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50)
);
🔹 5. What is a foreign key?
Answer:
A foreign key in one table points to a primary key in another table to establish a relationship.
Example:
FOREIGN KEY (student_id) REFERENCES students(student_id)
🔹 6. What is the difference between WHERE
and HAVING
?
Clause | Use Case | Works On |
---|---|---|
WHERE | Filters rows before grouping | Rows |
HAVING | Filters after GROUP BY | Aggregated data |
Example:
SELECT dept, COUNT(*) FROM employees
GROUP BY dept
HAVING COUNT(*) > 5;
Top 20 SQL Interview Questions and Answers
🔹 7. What are JOINS in SQL?
Answer:
JOINS combine rows from two or more tables.
Join Type | Description |
---|---|
INNER JOIN | Matches rows in both tables |
LEFT JOIN | All rows from left + matched from right |
RIGHT JOIN | All rows from right + matched from left |
FULL JOIN | All matched + unmatched rows from both |
Example:
SELECT e.name, d.name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;
🔹 8. What is the difference between UNION
and UNION ALL
?
Keyword | Duplicates | Faster |
---|---|---|
UNION | Removes | No |
UNION ALL | Keeps | Yes |
🔹 9. What is normalization?
Answer:
Normalization is a process to reduce data redundancy and improve data integrity.
Normal Forms:
- 1NF: Atomic columns
- 2NF: No partial dependency
- 3NF: No transitive dependency
Top 20 SQL Interview Questions and Answers
🔹 10. What is a subquery?
Answer:
A subquery is a query nested inside another query.
Example:
SELECT name FROM students
WHERE id IN (SELECT student_id FROM enrollments);
🔹 11. What is the difference between IN
and EXISTS
?
IN
: Checks values in a list or subquery.EXISTS
: Checks if subquery returns at least one row.
🔹 12. What is an index?
Answer:
An index improves query performance by allowing faster search and retrieval.
Example:
CREATE INDEX idx_name ON employees(name);
🔹 13. What is a view?
Answer:
A view is a virtual table based on a SQL query.
Example:
CREATE VIEW active_employees AS
SELECT * FROM employees WHERE status = 'active';
Top 20 SQL Interview Questions and Answers
🔹 14. What is a stored procedure?
Answer:
A stored procedure is a precompiled group of SQL statements.
Example:
CREATE PROCEDURE GetEmployees()
AS
BEGIN
SELECT * FROM employees;
END;
🔹 15. What is the difference between CHAR
and VARCHAR
?
Type | Fixed/Variable | Storage |
---|---|---|
CHAR(n) | Fixed length | Always n bytes |
VARCHAR(n) | Variable length | Up to n bytes |
🔹 16. What are aggregate functions in SQL?
Answer:
Aggregate functions perform calculations on multiple rows.
Function | Purpose |
---|---|
SUM() | Adds values |
AVG() | Average |
COUNT() | Count rows |
MAX() | Maximum value |
MIN() | Minimum value |
🔹 17. What is a constraint?
Answer:
Constraints enforce rules on data columns.
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
🔹 18. What is the difference between BETWEEN
and IN
?
BETWEEN
checks for a range.IN
checks for specific values.
Example:
-- BETWEEN
SELECT * FROM students WHERE age BETWEEN 18 AND 25;
-- IN
SELECT * FROM students WHERE city IN ('Delhi', 'Mumbai');
Top 20 SQL Interview Questions and Answers
🔹 19. What is a transaction in SQL?
Answer:
A transaction is a unit of work that is either fully completed or fully failed.
Commands:
BEGIN TRANSACTION
COMMIT
ROLLBACK
🔹 20. How do you optimize a slow SQL query?
Answer:
- Use indexes
- Avoid
SELECT *
- Use
JOIN
properly - Avoid unnecessary subqueries
- Analyze with
EXPLAIN
orQUERY PLAN
Table of Contents
Top 20 SQL Interview Questions and Answers