SQL Interview Questions and Answers
Question 1: What is SQL?
SQL (Structured Query Language) is a standard language for accessing and manipulating
databases.
Question 2: What are the different types of SQL commands?
The different types of SQL commands are:
- DDL (Data Definition Language): CREATE, ALTER, DROP
- DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
- DCL (Data Control Language): GRANT, REVOKE
- TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
Question 3: What is the difference between WHERE and HAVING clauses?
WHERE is used to filter rows before grouping, while HAVING is used to filter groups after
grouping.
Question 4: What is a primary key?
A primary key is a column or a set of columns that uniquely identifies each row in a table. It
cannot contain NULL values.
Question 5: What is a foreign key?
A foreign key is a column or a set of columns in one table that refers to the primary key in
another table. It establishes a relationship between the two tables.
Question 6: What are the different types of joins in SQL?
The different types of joins are:
- INNER JOIN
- LEFT JOIN (LEFT OUTER JOIN)
- RIGHT JOIN (RIGHT OUTER JOIN)
- FULL JOIN (FULL OUTER JOIN)
- CROSS JOIN
Question 7: What is normalization?
Normalization is the process of organizing data in a database to eliminate redundancy and
improve data integrity. It involves dividing tables into smaller tables and defining
relationships.
Question 8: What is denormalization?
Denormalization is the process of combining normalized tables to improve read
performance by adding redundancy.
Question 9: What is an index in SQL?
An index is a database object that improves the speed of data retrieval operations on a table
at the cost of additional storage and maintenance.
Question 10: What is the difference between DELETE and TRUNCATE?
DELETE removes specific rows based on a condition and can be rolled back. TRUNCATE
removes all rows from a table and cannot be rolled back.
Question 11: What is a view?
A view is a virtual table based on the result of an SQL query. It does not store data itself but
provides a way to represent data from one or more tables.
Question 12: What is a subquery?
A subquery is a query nested inside another query. It can be used in SELECT, INSERT,
UPDATE, or DELETE statements.
Question 13: What is the difference between correlated and non-correlated
subqueries?
A correlated subquery depends on the outer query for its values, while a non-correlated
subquery is independent and can be executed alone.
Question 14: What are aggregate functions in SQL?
Aggregate functions perform calculations on a set of values and return a single value.
Examples include COUNT, SUM, AVG, MAX, and MIN.
Question 15: What is the purpose of the GROUP BY clause?
GROUP BY is used to group rows that have the same values in specified columns. It is often
used with aggregate functions.
Question 16: How do you find duplicate records in a table?
To find duplicates:
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
Question 17: How do you fetch the second highest salary from a table?
Using a subquery:
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);
Question 18: What is a stored procedure?
A stored procedure is a precompiled set of SQL statements that can be executed as a single
unit. It improves performance and code reusability.
Question 19: What is a trigger?
A trigger is a set of SQL statements that are automatically executed in response to specific
events on a table, such as INSERT, UPDATE, or DELETE.
Question 20: What is ACID in databases?
ACID stands for Atomicity, Consistency, Isolation, and Durability, which are properties that
ensure reliable transaction processing in a database.