Most Important SQL Interview Questions
1. What is SQL?
SQL, or Structured Query Language, is a domain-specific language used to manage and manipulate relational databases. It is essential for performing tasks such as querying, updating, and managing databases efficiently.
2. What are the different types of SQL statements?
SQL statements are classified into five main categories:
- DDL (Data Definition Language): Create, Drop, Alter
- 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 a Primary Key?
A primary key is a field (or combination of fields) that uniquely identifies each record in a table. It ensures that no duplicate values exist and cannot be NULL.
4. What is a Foreign Key?
A foreign key is a field in a table that links to the primary key of another table. It creates a relationship between the two tables and ensures referential integrity.
5. What is a Unique Key?
A unique key ensures all the values in a column are unique. Unlike the primary key, it allows NULL values but restricts duplicates.
6. What is an Index in SQL?
An index is used to speed up the retrieval of data from the database. It creates a structure in the background that the database engine uses to locate rows more quickly than if it had to search through the entire table.
7. What is a Join in SQL? Explain its types.
A Join clause is used to combine rows from two or more tables based on a related column.
- Inner Join: Returns records with matching values in both tables.
- Left Join (Outer): Returns all records from the left table and the matched records from the right table.
- Right Join (Outer): Returns all records from the right table and the matched records from the left table.
- Full Join (Outer): Returns all records when there is a match in either left or right table.
- Self Join: Joins the table with itself.
8. What is a Subquery in SQL?
A subquery is a query nested inside another query. It can return data to be used in the main query and is enclosed within parentheses.
9. What is Normalization? Explain its types.
Normalization is the process of organizing data to minimize redundancy. The types include:
- 1NF (First Normal Form): No repeating groups.
- 2NF (Second Normal Form): No partial dependency.
- 3NF (Third Normal Form): No transitive dependency.
10. What is Denormalization?
Denormalization is the process of combining tables to optimize read performance. While it reduces complexity, it may increase redundancy.
11. Explain the concept of ACID properties.
ACID stands for Atomicity, Consistency, Isolation, Durability. These properties ensure reliable processing of database transactions.
12. What is a Stored Procedure?
A stored procedure is a set of SQL statements that can be executed as a program. It allows code reusability and enhances performance.
13. What is a View in SQL?
A view is a virtual table based on the result of an SQL query. It provides a filtered perspective of data, allowing for better security and data abstraction.
14. Explain the difference between DELETE and TRUNCATE.
- DELETE: Removes rows one at a time and allows rollback. It is slower and can trigger triggers.
- TRUNCATE: Removes all rows without logging individual row deletions. It is faster but cannot be rolled back.
15. What are SQL Constraints?
SQL constraints are used to specify rules for the data in a table. The common types include:
- NOT NULL
- UNIQUE
- CHECK
- DEFAULT
- FOREIGN KEY
- PRIMARY KEY
16. What is a Transaction in SQL?
A transaction refers to a sequence of operations performed as a single logical unit of work. It follows the ACID properties to ensure the integrity of the database.
17. What is the difference between HAVING and WHERE?
- WHERE: Used to filter records before any groupings are made.
- HAVING: Used to filter groups after the
GROUP BY
clause has been applied.
18. What is the purpose of the GROUP BY clause?
The GROUP BY clause groups rows that share a property, allowing for aggregate functions like COUNT()
, SUM()
, AVG()
, etc., to be performed on each group.
19. What is an Aggregate Function?
An aggregate function performs calculations on multiple values and returns a single value. Common examples include:
- SUM()
- COUNT()
- AVG()
- MAX()
- MIN()
20. What are Aliases in SQL?
An alias is a temporary name given to a table or column for better readability. It is created using the AS
keyword.
21. Explain the SQL Wildcards.
Wildcards are used with the LIKE
operator to search for a specified pattern in a column. Common wildcards include:
- %: Represents zero or more characters.
- _ (underscore): Represents a single character.
22. What is the UNION operator?
The UNION operator combines the result sets of two or more SELECT statements and removes duplicates.
23. What is the difference between UNION and UNION ALL?
While both UNION and UNION ALL combine result sets, UNION removes duplicates, whereas UNION ALL retains all values, including duplicates.
24. Explain the concept of an Index Scan and Index Seek.
- Index Scan: The entire index is scanned to find a match.
- Index Seek: A more efficient method where the database engine directly navigates to the matching data using the index.
25. What is the difference between Cross Join and Full Outer Join?
- Cross Join: Combines every row of the first table with every row of the second table.
- Full Outer Join: Returns all records when there is a match in either left or right table.