Basic SQL Interview Questions
1. What is SQL?
SQL (Structured Query Language) is a standard language for managing and manipulating
databases. It is used to perform operations such as querying, inserting, updating, and deleting
data.
2. What are the types of SQL commands?
SQL commands are categorized into five 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
DQL (Data Query Language): SELECT
3. What is the difference between SQL and MySQL?
SQL is a query language used to communicate with databases.
MySQL is a relational database management system (RDBMS) that uses SQL as its query
language.
4. What is the difference between DELETE and TRUNCATE?
DELETE: Removes specific rows from a table based on conditions; can be rolled back.
TRUNCATE: Removes all rows from a table without logging individual row deletions; cannot
be rolled back.
5. What is a primary key?
A primary key is a column or a combination of columns that uniquely identifies each row in a
table. It must contain unique and non-null values.
Intermediate SQL Interview Questions
6. What is the difference between WHERE and HAVING clauses?
WHERE is used to filter records before aggregation.
HAVING is used to filter records after aggregation.
7. What are the different types of joins in SQL?
SQL supports several types of joins:
INNER JOIN: Returns records with matching values in both tables.
LEFT JOIN: Returns all records from the left table and matched records from the right table.
RIGHT JOIN: Returns all records from the right table and matched records from the left table.
FULL JOIN: Returns all records from both tables.
CROSS JOIN: Returns the Cartesian product of both tables.
SELF JOIN: Joins a table with itself.
8. What is a foreign key?
A foreign key is a column or set of columns in a table that establishes a relationship between
two tables. It references a primary key in another table.
9. What is normalization, and why is it important?
Normalization is the process of organizing a database to reduce redundancy and improve
efficiency. It is done through normal forms:
1NF: Eliminates duplicate columns and ensures atomicity.
2NF: Ensures all non-key attributes depend on the primary key.
3NF: Removes transitive dependencies.
BCNF: Ensures no partial dependency on candidate keys.
10. What is an index in SQL?
An index is a database object that improves query performance by allowing faster retrieval of
records. Types of indexes include:
Clustered Index: Determines the physical order of data in a table.
Non-Clustered Index: Does not affect the physical order of data but creates a separate structure.
Advanced SQL Interview Questions
11. What is a stored procedure?
A stored procedure is a precompiled collection of SQL statements stored in the database that
can be executed as a single unit.
12. What are window functions in SQL?
Window functions perform calculations across a set of table rows related to the current row.
Common functions include:
RANK(): Assigns a unique rank to rows.
DENSE_RANK(): Similar to RANK() but without gaps.
ROW_NUMBER(): Assigns a sequential row number.
LEAD() & LAG(): Access data from previous or next rows.
13. What is the difference between UNION and UNION ALL?
UNION: Combines result sets and removes duplicates.
UNION ALL: Combines result sets without removing duplicates.
14. What is ACID compliance?
ACID (Atomicity, Consistency, Isolation, Durability) ensures reliable transactions in databases:
Atomicity: Ensures complete execution of transactions.
Consistency: Maintains database integrity.
Isolation: Ensures transactions do not interfere with each other.
Durability: Ensures committed transactions are permanent.
15. What are CTEs (Common Table Expressions)?
A CTE is a temporary result set used within a SQL query. It simplifies complex queries and
improves readability.
WITH EmployeeCTE AS (
SELECT EmployeeID, Name, Salary FROM Employees WHERE Salary > 50000
)
SELECT * FROM EmployeeCTE;
Bonus: Scenario-Based SQL Questions
16. Find the second highest salary from an Employee table.
SELECT MAX(Salary) FROM Employees
WHERE Salary < (SELECT MAX(Salary) FROM Employees);
17. Retrieve duplicate records from a table.
SELECT Name, COUNT(*) FROM Employees
GROUP BY Name HAVING COUNT(*) > 1;
18. Find the nth highest salary using LIMIT.
SELECT Salary FROM Employees
ORDER BY Salary DESC LIMIT 1 OFFSET (N-1);