55 SQL Interview Questions And Answers
Here is a list of 55 SQL interview questions for freshers and experienced candidates with brief answers for each. This will help you prepare thoroughly for your SQL interview.
Basic SQL Interview Questions
-
What is SQL?
- Answer: SQL (Structured Query Language) is a language used for managing and manipulating relational databases. It is used to create, modify, query, and manage data in databases.
-
What are the different types of SQL statements?
- Answer: SQL statements are categorized into:
- 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
- Answer: SQL statements are categorized into:
-
What is the difference between DDL, DML, DCL, and TCL in SQL?
- Answer:
- DDL defines the structure (e.g., CREATE, ALTER).
- DML handles data manipulation (e.g., SELECT, INSERT).
- DCL controls access permissions (e.g., GRANT, REVOKE).
- TCL manages transactions (e.g., COMMIT, ROLLBACK).
- Answer:
-
What is a Primary Key?
- Answer: A Primary Key is a column (or a set of columns) in a table that uniquely identifies each row in that table. It cannot contain NULL values.
-
What is a Foreign Key?
- Answer: A Foreign Key is a column in one table that uniquely identifies a row of another table. It is used to establish a link between the two tables.
-
What is a Unique Key?
- Answer: A Unique Key is a column (or combination of columns) that ensures all values in that column are unique, but unlike Primary Key, it allows NULL values.
-
What is a Composite Key?
- Answer: A Composite Key is a combination of two or more columns in a table that together uniquely identify a row.
-
What is a NULL value in SQL?
- Answer: NULL represents a missing or undefined value. It is different from zero or an empty string.
-
What is the difference between
WHERE
andHAVING
?- Answer:
- WHERE is used to filter rows before grouping.
- HAVING is used to filter groups after the
GROUP BY
clause.
- Answer:
-
What is the difference between
INNER JOIN
,LEFT JOIN
,RIGHT JOIN
, andFULL JOIN
?- Answer:
- INNER JOIN returns matching rows from both tables.
- LEFT JOIN returns all rows from the left table, and matching rows from the right table.
- RIGHT JOIN returns all rows from the right table, and matching rows from the left table.
- FULL JOIN returns all rows from both tables, matching where possible.
- Answer:
-
What is the difference between
JOIN
andUNION
?- Answer:
- JOIN is used to combine rows from two or more tables based on a related column.
- UNION combines the results of two or more queries, removing duplicates.
- Answer:
-
What are Aggregate Functions in SQL? Provide examples.
- Answer: Aggregate functions perform a calculation on a set of values and return a single value, such as:
COUNT()
,SUM()
,AVG()
,MIN()
,MAX()
- Answer: Aggregate functions perform a calculation on a set of values and return a single value, such as:
-
What is the
GROUP BY
clause used for?- Answer:
GROUP BY
groups rows that have the same values into summary rows, like finding the sum or average of a column.
- Answer:
-
How does
ORDER BY
work in SQL?- Answer:
ORDER BY
sorts the result set in ascending or descending order based on one or more columns.
- Answer:
-
What are subqueries? Give an example.
- Answer: A subquery is a query inside another query. Example:
-
What is the difference between a subquery and a join?
- Answer:
- A subquery is a query within another query.
- A join combines rows from two or more tables based on a related column.
- Answer:
-
What is the
IN
operator in SQL?- Answer: The
IN
operator is used to filter records that match any value in a list or subquery. Example:
- Answer: The
-
What is the
LIKE
operator in SQL?- Answer: The
LIKE
operator is used to search for a specified pattern in a column. Example:
- Answer: The
-
What is the
BETWEEN
operator in SQL?- Answer: The
BETWEEN
operator is used to filter the result set within a certain range. Example:
- Answer: The
-
How do you use the
DISTINCT
keyword in SQL?- Answer: The
DISTINCT
keyword is used to return unique values from a column. Example:
- Answer: The
-
What are indexes in SQL? Why are they used?
- Answer: Indexes are used to speed up the retrieval of data from a database by providing quick access to rows based on indexed columns.
-
What are the types of indexes in SQL?
- Answer: The two primary types of indexes are:
- Clustered Index: Reorganizes the data to follow the order of the index.
- Non-Clustered Index: A separate structure from the data that points to the rows.
- Answer: The two primary types of indexes are:
-
What is the difference between
UNION
andUNION ALL
?- Answer:
- UNION removes duplicates.
- UNION ALL includes duplicates.
- Answer:
-
What is a view in SQL?
- Answer: A view is a virtual table that consists of a stored query. It allows users to view data from one or more tables without modifying the actual data.
-
What is a stored procedure in SQL?
- Answer: A stored procedure is a precompiled collection of one or more SQL statements that can be executed as a unit.
-
What is a trigger in SQL?
- Answer: A trigger is a set of SQL statements that automatically executes when a specified event occurs on a specified table or view.
-
What is the difference between
CHAR
andVARCHAR
data types?- Answer:
- CHAR is a fixed-length data type.
- VARCHAR is a variable-length data type.
- Answer:
-
What is the difference between
DELETE
,TRUNCATE
, andDROP
in SQL?- Answer:
- DELETE removes records from a table but can be rolled back.
- TRUNCATE removes all records from a table but cannot be rolled back.
- DROP deletes a table or database permanently.
- Answer:
-
What is the purpose of the
ALTER
command in SQL?- Answer: The
ALTER
command is used to modify an existing database object (e.g., table) structure, such as adding or deleting columns.
- Answer: The
-
What is the
CASE
statement in SQL?- Answer: The
CASE
statement allows conditional logic in SQL queries. Example:
- Answer: The
Intermediate SQL Interview Questions
-
What is the
COALESCE
function in SQL?- Answer: The
COALESCE
function returns the first non-null value in a list of expressions. Example:
- Answer: The
-
What is the
NULLIF
function in SQL?- Answer: The
NULLIF
function returns NULL if the two arguments are equal, otherwise, it returns the first argument. Example:
- Answer: The
-
How do you find the total number of records in a table?
- Answer:
-
What is normalization? Explain the different normal forms.
- Answer: Normalization is the process of organizing data to reduce redundancy. The normal forms are:
- 1NF: Eliminate duplicate columns.
- 2NF: Eliminate partial dependencies.
- 3NF: Eliminate transitive dependencies.
- BCNF: A stricter version of 3NF.
- Answer: Normalization is the process of organizing data to reduce redundancy. The normal forms are:
-
What is denormalization?
- Answer: Denormalization is the process of combining tables or adding redundant data to improve query performance, usually at the expense of data integrity.
-
What is the difference between a
Clustered
andNon-Clustered
index?- Answer:
- Clustered Index: Data is stored in the order of the index.
- Non-Clustered Index: Data is stored separately from the index.
- Answer:
-
What are constraints in SQL? Give examples.
- Answer: Constraints are rules applied to columns in a table to enforce data integrity. Examples include:
- PRIMARY KEY
- FOREIGN KEY
- NOT NULL
- CHECK
- Answer: Constraints are rules applied to columns in a table to enforce data integrity. Examples include:
-
What is a transaction in SQL?
- Answer: A transaction is a sequence of SQL operations that are executed as a single unit, ensuring data integrity.
-
What is
ACID
in SQL?- Answer: ACID stands for:
- Atomicity: All operations are completed or none.
- Consistency: Data is valid before and after the transaction.
- Isolation: Transactions do not affect each other.
- Durability: Changes are permanent after the transaction.
- Answer: ACID stands for:
-
What is
SELECT INTO
in SQL?- Answer:
SELECT INTO
creates a new table and inserts the result of a query into it. Example:
- Answer:
Advanced SQL Interview Questions
-
What is query optimization in SQL?
- Answer: Query optimization involves rewriting SQL queries to improve performance, like using indexes, avoiding subqueries, and reducing the number of joins.
-
What are database locks and how do they work in SQL?
- Answer: Database locks prevent multiple transactions from conflicting with each other. Locks can be row-level, table-level, or database-level.
-
How do you implement a many-to-many relationship in SQL?
- Answer: A many-to-many relationship is implemented using a junction table that contains foreign keys referencing the two tables.
-
What is partitioning in SQL? How is it used?
- Answer: Partitioning divides a large table into smaller, more manageable pieces called partitions, usually for performance optimization.
-
Explain the concept of a
Foreign Key
constraint in SQL.- Answer: A Foreign Key is a column in a table that refers to the primary key in another table to establish a relationship.
-
What is a
Database Trigger
? Explain the types of triggers in SQL.- Answer: A trigger is a stored procedure that automatically executes when a specified event occurs (e.g.,
INSERT
,UPDATE
,DELETE
).
- Answer: A trigger is a stored procedure that automatically executes when a specified event occurs (e.g.,
-
How do you implement pagination in SQL queries?
- Answer: Pagination can be implemented using
LIMIT
andOFFSET
in SQL.
- Answer: Pagination can be implemented using
-
How can you perform a
pivot
operation in SQL?- Answer: Pivoting is performed using
CASE
statements orPIVOT
operator to transform data rows into columns.
- Answer: Pivoting is performed using
-
What is the
MERGE
statement in SQL?- Answer: The
MERGE
statement combinesINSERT
,UPDATE
, andDELETE
operations based on matching conditions.
- Answer: The
-
What is the difference between
RAID
andClustering
in SQL databases?- Answer:
- RAID (Redundant Array of Independent Disks) improves storage performance and redundancy.
- Clustering is a method to group multiple database instances together for improved performance and availability.
- Answer:
-
What is the purpose of using
TEMPORARY
tables in SQL?- Answer: Temporary tables are used for storing intermediate results and are automatically dropped at the end of the session.
-
What is the difference between a
Function
and aStored Procedure
in SQL?- Answer:
- A Function returns a single value and can be used in SQL expressions.
- A Stored Procedure may or may not return a value and is typically used for executing multiple operations.
- Answer:
-
How do you check the database performance using SQL?
- Answer: Performance can be checked using SQL Server tools like
EXPLAIN
,SQL Profiler
, orPerformance Monitor
. Index optimization and query analysis can also help improve performance.
- Answer: Performance can be checked using SQL Server tools like
-
What are SQL injection attacks and how do you prevent them?
- Answer: SQL injection is an attack where malicious SQL code is injected into a query. It can be prevented by using parameterized queries or prepared statements.
-
Explain the concept of
Sharding
in SQL databases.- Answer: Sharding is the process of distributing data across multiple servers to improve performance and scalability.