Structured Query Language (SQL) is the backbone of modern data handling and is a must-know for developers, data analysts, and software engineers. If you’re preparing for tech interviews in 2025–2026, here’s a comprehensive guide to the top 20 SQL interview questions with detailed explanations and real-world examples.
1. What is SQL?
SQL stands for Structured Query Language. It is used to interact with databases—to create, retrieve, update, and delete data.
Example:
SELECT * FROM employees;
This command fetches all records from the “employees” table.
2. Types of SQL Commands
SQL commands are categorized into:
- DDL (CREATE, ALTER, DROP)
- DML (INSERT, UPDATE, DELETE)
- DQL (SELECT)
- DCL (GRANT, REVOKE)
- TCL (COMMIT, ROLLBACK)
Example:
CREATE TABLE students (id INT, name VARCHAR(50));
3. What is a Primary Key?
A primary key uniquely identifies each record and must not contain NULLs.
Example:
CREATE TABLE users (user_id INT PRIMARY KEY, username VARCHAR(50))
4. What is a Foreign Key?
A foreign key links one table to another using the primary key of the second table.
Example:
CREATE TABLE orders (
order_id INT,
user_id INT,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
5. Difference between WHERE and HAVING
WHERE
filters rows before groupingHAVING
filters after grouping
Example:
SELECT department, COUNT(*) FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
6. What is a JOIN? Types?
JOINs combine rows from two or more tables.
Types:
->INNER
->LEFT
->RIGHT
->FULL OUTER
->CROSS, SELF
Example:
SELECT orders.order_id, users.username
FROM orders
INNER JOIN users ON orders.user_id = users.user_id;
7. INNER JOIN vs LEFT JOIN
INNER JOIN
: returns only matching rows.LEFT JOIN
: returns all rows from the left table, and matched rows from the right.
Example:
SELECT * FROM users
LEFT JOIN orders ON users.user_id = orders.user_id;
8. What is a Subquery?
A subquery is a query nested inside another query.
Example:
SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
9. DELETE vs TRUNCATE vs DROP
DELETE
: removes specific rowsTRUNCATE
: removes all rows quicklyDROP
: deletes table structure
Example:
DELETE FROM users WHERE user_id = 1;
TRUNCATE TABLE users;
DROP TABLE users;
10. What is Normalization?
Organizing data to reduce redundancy.
Forms: 1NF, 2NF, 3NF
Example:
Split table storing customer & order details into two linked tables.
11. What is a View?
A virtual table based on a query.
Example:
CREATE VIEW active_users AS
SELECT * FROM users WHERE status = 'active';
12. What is an Index?
Indexes speed up data retrieval.
Example:
CREATE INDEX idx_username ON users(username);
13. UNION vs UNION ALL
UNION
: removes duplicatesUNION ALL
: includes duplicates
Example:
SELECT name FROM employees
UNION
SELECT name FROM managers;
14. What are Constraints?
Constraints are rules applied to table columns to maintain data accuracy, like NOT NULL
, UNIQUE
, CHECK
, and FOREIGN KEY
. They help enforce data integrity.
Types: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT
Example:
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE NOT NULL
);
15. Aggregate Functions
These are functions that perform calculations on multiple rows and return a single result — like SUM()
, AVG()
, COUNT()
, etc. They’re commonly used with GROUP BY
.
Functions: COUNT(), SUM(), AVG(), MAX(), MIN()
Example:
SELECT department, AVG(salary) FROM employees GROUP BY department;
16. What is a Stored Procedure?
A stored procedure is a reusable set of SQL statements saved in the database. It simplifies complex operations and improves performance.
Example:
CREATE PROCEDURE GetEmployees()
BEGIN
SELECT * FROM employees;
END;
17. What is a Trigger?
A trigger is an automatic action that runs in response to events like INSERT
, UPDATE
, or DELETE
on a table. It’s used to enforce rules or update data automatically.
Example:
CREATE TRIGGER before_insert BEFORE INSERT ON users
FOR EACH ROW SET NEW.created_at = NOW();
18. NULL vs 0
NULL
means a missing or unknown value, while 0
is a definite numeric value. They are not the same and behave differently in comparisons.
NULL
: unknown or missing value0
: a numeric value
Example:
SELECT * FROM users WHERE age IS NULL;
19. How to Find Duplicates
Use GROUP BY
and HAVING COUNT(*) > 1
to identify repeated values in a column. It groups similar entries and filters out those that occur more than once.
Example:
SELECT name, COUNT(*) FROM employees
GROUP BY name
HAVING COUNT(*) > 1;
20. Find the Second Highest Salary
Use a subquery with MAX()
to find the highest salary less than the maximum, effectively returning the second highest value.
Example:
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Final Thoughts
SQL is more than just a query language—it’s a fundamental skill for anyone working in data or development. These 20 questions cover the basics and more. Practice them, build your own queries, and you’ll be ready for any SQL round.
At FACE Prep Campus, we don’t just teach SQL theory. We help students apply SQL in real-world projects, crack interview rounds, and become job-ready. Join our UG programs in Computer Science, BCA, or BCom with FinTech and get hands-on with SQL, Python, and more.
Your career in tech starts with strong fundamentals. Learn with FACE Prep.