Basic SQL Interview Questions & Answers
1. What is SQL?
SQL (Structured Query Language) is used to communicate with
relational databases. It is used to:
Retrieve data (SELECT)
Insert data (INSERT)
Update data (UPDATE)
Delete data (DELETE)
Create/modify tables (DDL)
2. What are the different types of SQL commands?
Type Commands
DDL (Data Definition CREATE, ALTER, DROP,
Language) TRUNCATE
DML (Data Manipulation
INSERT, UPDATE, DELETE
Language)
DQL (Data Query Language) SELECT
DCL (Data Control Language) GRANT, REVOKE
TCL (Transaction Control COMMIT, ROLLBACK,
Language) SAVEPOINT
3. What is the difference between WHERE and HAVING?
WHERE is used to filter rows before grouping.
HAVING is used to filter groups after aggregation.
SELECT department, COUNT(*)
FROM employees
WHERE salary > 30000
GROUP BY department
HAVING COUNT(*) > 5;
4. What are Primary Key and Foreign Key?
Primary Key: Uniquely identifies each record in a table. Cannot be
NULL.
Foreign Key: A field in one table that refers to the Primary Key in
another.
5. What is the difference between DELETE, TRUNCATE, and DROP?
Comman
Description
d
Removes rows (can use WHERE). Can be
DELETE
rolled back.
TRUNCAT Removes all rows. Cannot be rolled back.
E Faster.
DRO Deletes the entire table and
P structure.
6. What is a JOIN? What are its types?
A JOIN is used to combine rows from two or more tables.
Type Description
INNER JOIN Returns matching rows
All rows from left + matching rows
LEFT JOIN
from right
RIGHT All rows from right + matching rows
JOIN from left
FULL JOIN All rows from both sides
SELF JOIN A table joins with itself
CROSS
Cartesian product (all combinations)
JOIN
Ex-
employees.name, departments.dept_name
FROM employees
INNER JOIN departments
ON employees.dept_id = departments.id;
7. What is normalization?
Normalization is the process of organizing data to:
Reduce data redundancy
Improve data integrity
Normal forms:
1NF: Atomic values only
2NF: No partial dependency (only full primary key dependency)
3NF: No transitive dependency