Top 100+ SQL Interview Questions and Answers for 2025
Top 100+ SQL Interview Questions and Answers for 2025
Home > Blog > Interview Questions > Top 100+ SQL Interview Questions and Answers for 2025
Table of content
SQL is one of the most popular database programming languages that is used
for creating, modifying, retrieving, and manipulating data in databases.
Databases are widely used in all companies for its robust features & security.
We have complied the list of SQL interview questions with answers asked in the
top product MNC’s like Google, Microsoft, Meta, & others services companies to
help you crack your next SQL interview. These questions are segregated into
multiple sections for ease of learning and interview preparation.
Primary Key: The primary key is used to uniquely identify every record in a
table. It ensures that NULL values are not present in the table.
Unique Key: Unique key also identifies each record uniquely but it can accept
NULL values.
Live Chat
Chat with Us
Welcome to Intellipaat
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 1/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
SQL constraints are a set of rules applied to a column or table to maintain data
integrity. SQL consists of many constraints which are as follows:
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 2/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
5. What is an alias?
Field: A field is a specific attribute or property of the entity that the table
represents. It is also known as a column; each field holds data of a specific type,
such as text, numbers, or dates.
The DROP and DELETE commands in SQL are used for removing data, but they
are differ significantly: DROP: Drop command is a DDL command. It completely
removes a table or database, including its structure and all data from the
database.
The COALESCE function is used to take a set of inputs values and returns the
first non-null value from the record.
Syntax:
COALESCE(val1,val2,val3,……,nth val)
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 3/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
The BETWEEN operator is employed to identify rows that fall within a specified
range of values, encompassing numerical, textual, or date values. It returns the
count of values that exist between the two defined boundaries.
On the other hand, the IN operator serves as a condition operator utilized for
searching values within a predetermined range. When multiple values are
available for selection, the IN operator is utilized.
Explore Program
13. What are DDL, DML, DCL, TCL, and DQL in SQL?
DDL: Data Definition Language is used to create, modify, and drop the schema of
database objects. CREATE, ALTER TABLE, DROP, TRUNCATE, ADD COLUMN are
DDL commands.
DQL: Data Query Language is used to retrieve data from databases using the
SELECT statement.
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 4/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
For Example,:
UNION: Union operation combines results from two queries, and removes
duplicates.
MINUS: Minus operation returns rows from the first query that are not in the
second query.
A subquery is a query nested within another query, enabling more complex data
retrieval.
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 5/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
Correlated Subquery: References data from the outer query in its WHERE
clause.
Nested Subquery: This can be placed anywhere in the outer query and does not
directly reference the outer table.
A view is a virtual table representing data from one or more tables without
physically storing it. It can simplify complex queries.
Simple View: It is a view based on a single table and does not have a GROUP BY
clause or other SQL features.
Complex View: It is a view built from several tables and includes a GROUP BY
clause as well as functions.
Inline View: It is a view built on a subquery in the FROM clause, which provides a
temporary table and simplifies a complicated query.
Materialized View: It is a view that saves both the definition and the details. It
builds data replicas by physically preserving them.
The UNION and UNION ALL operators both are used for combine the output of
two or more SELECT queries, but they differ in handling duplicate rows:
UNION: UNION operators combine the results of multiple SELECT queries and
remove duplicate rows. It returns only distinct values across all queries.
UNION ALL: UNION ALL operator combines the output of multiple SELECT
queries, including duplicates
A stored procedure is a set of SQL statements stored in the database that can
be reused, promoting modular programming.
Temporary table allow us to store and process the data just like a normal table;
however, the benefit of using temporary tables is that it will created on the fly
and will be deleted automatically. It is primarily used for complex data
transformation or data selection from a large number of tables. This enables
writing simple SQL queries and data manipulation activities on intermediate
datasets.
Syntax:
CREATE TABLE #table_name();
The below query will create a temporary table:
create table #book(b_id int, b_cost int)
Now, we will insert the records
Output:
23. How would you optimize a slow-moving SQL query? List the
SQL optimization techniques.
We can optimize a slow-moving SQL query by using indexing in the DBMS to find
the specific rows in a table very quickly.
1. Using Indexes
24. How can you secure your SQL queries against SQL injection
attacks?
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 7/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
26. What are the third-party tools that are used in SQL Server?
The following is the list of third-party tools that are used in SQL Server:
SQL CHECK
SQL DOC 2
SQL Backup 5
SQL Prompt
Litespeed 5.0
The SQL JOIN component joins rows from one or more tables in a relational
database. Create sets that can be stored in tabular form or used routinely. JOIN
is to combine columns from one table or multiple tables using the same value.
There are different types of JOINS in SQL, which are the following:
INNER JOIN – An INNER JOIN is used to return records of the same value in
two tables.
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 8/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
LEFT JOIN – LEFT JOIN is used to join all the rows in the left table with
matching rows in the right table.
RIGHT JOIN – RIGHT JOIN is used to join all the rows in the right table with
the corresponding rows in the left table.
FULL JOIN – A FULL JOIN is used to return all records from two tables if there
are matching records in each table.
SELF JOIN – A SELF JOIN is a join used to join a table to itself. SELF JOINS
treats one table as two tables.
29. What is the difference between INNER JOIN and SELF JOIN?
The most important difference between INNER and SELF JOIN is:
INNER JOIN is used to return the records which are present in both tables.
Whereas, in SELF JOIN, a table is joined to itself.
SQL JOINS is a method to integrate databases so that they are easy to read
and use.
Using JOINS can reduce data usage and storage on the database. Here you
can use one JOIN query instead of multiple queries. So you can use a large
database to search, filter, organize, and more.
31. State the difference between the RIGHT JOIN and the LEFT
JOIN.
Both RIGHT JOIN and LEFT JOIN do the same thing: they return the result of a
query that contains all the records in the table. The only difference is that the
left view shows all the records in the left table of the query, and the right view
shows all the records in the right table.
The SELF JOIN can be an INNER JOIN, OUTER JOIN, or can also be CROSS
JOIN. Tables are automatically linked based on columns that contain duplicate
data in multiple rows.
The combination of the LEFT and the RIGHT OUTER JOIN is called a FULL JOIN.
If the ON condition cannot be satisfied, it returns all rows in both tables that
match the WHERE clause with a NULL value.
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 9/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
An EQUI JOIN is a type of join operation in a database that combines rows from
two or more tables based on a matching condition using the equality operator
(=). It is used to retrieve data where values in specified columns are equal.
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;
The length of the retrieved rows is greater than the length of the rows in the
corresponding tables.
Whereas In the case of UNION, a JOIN can be used if the query has the same
number of columns and the corresponding attributes are the same.
The number of rows returned is greater than the number of rows in each table in
the query.
A HASH JOIN requires two inputs, an INNER table, and an OUTER table. HASH
JOINS involve using a HASH table to identify matching rows between two
tables. HASH JOINS are an option when other joins are not recommended.
When joining large data sets that are unsorted or non-indexed HASH JOINS are
better.
MERGE JOIN is the most important join in SQL Server. In MERGE JOIN, your
query plan is effective and you don’t need to make many changes to improve
query performance. Because the MERGE JOIN operator uses ordered data entry,
it can use two large data sets.
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 10/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
JOIN is one of the methods we use to join data from multiple tables into a
relational database, and NESTED JOIN is one of the simplest methods to join
two tables. Typically, one table is used as the OUTER JOIN table with NESTED
JOINS and the other is used as the INNER JOIN table. Nested loop collections
can be classified into indexed nesting and temporary Index Nested Loop Join.
To create a one-to-many, you need to add the primary key from one side to many
sides as a column. To create many-to-many, you need a middle table that
contains the primary keys from many-to-many tables.
45. Write a query to select specific columns, say name and age,
from a table called Employees.
FROM Intellipaat_Emp;
Email Address
By providing your contact details, you agree to our Terms of Use & Privacy Policy
Submit
SELECT *
FROM Intellipaat_Emp
WHERE age > 35
47. Write a query to find the average salary for each department.
SELECT
department,
AVG(salary) AS avg_salary
FROM
Intellipaat_Emp
GROUP BY
department;
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 11/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
48. Write a query to find employees whose names start with 'Int'.
FROM employees
WHERE employee_name LIKE 'Int%';
50. Write a query to retrieve the last five records from the
Employees table based on the id column.
SELECT *
FROM Intellipaat_Emp
ORDER BY id DESC
LIMIT 5;
SELECT name,
salary,
CASE
END AS salary_category
FROM Intellipaat_Emp;
52. Write a query to get all employees and their project names,
showing NULL if an employee is not assigned a project.
SELECT Employees.name,
Projects.project_name
ON Employees.project_id = Projects.id;
SELECT dept.DepartmentName,
int_emp.Name
ON int_emp.DepartmentID = dept.DepartmentID;
UPDATE Intellipaat_Emp
SET salary = salary * 1.1
SELECT name
FROM Intellipaat_Emp
GROUP BY name
HAVING COUNT(*) = 1;
SELECT *
FROM Intellipaat_Emp
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 12/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
57. How will you calculate the total sales in each category of a
product sales table?
To calculate the total sales in each category of a product sales table, we can
use the aggregate function (SUM) with the sales amount column and group it by
the category column.
SELECT category,
SUM(sales_amt) AS Total_Sales
FROM sales
GROUP BY category;
58. How can you copy data from one table to another table?
FROM employees;
59. Write a query to fetch employees who earn more than the
average salary.
SELECT *
FROM Intellipaat_Emp
60. How would you find the 2nd highest salary from a table
called Employees?
SELECT MAX(salary)
FROM Intellipaat_Emp
61. Write a query to select only even or odd rows from a table
based on an id field.
— Even rows
SELECT *
FROM Intellipaat_Emp
WHERE id % 2 = 0;
— Odd rows
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 13/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
SELECT *
FROM Intellipaat_Emp
WHERE id % 2 = 1;
SELECT *
FROM (
SELECT name, department, salary,
rank
FROM Intellipaat_Emp
) AS ranked
WHERE rank <= 2;
SELECT id + 1 AS missing_id
FROM Intellipaat_Emp
UPDATE Intellipaat_Emp
SET gender = CASE
END;
65. Write a query to find pairs of employees who have the same
salary.
FROM Intellipaat_Emp A
JOIN Employees B ON A.salary = B.salary
67. Find pairs of employees who were hired on the same day.
WITH RankedSalaries AS (
FROM Intellipaat_Emp
)
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 14/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
GROUP BY department;
SELECT *
FROM (
SELECT name, salary,
) AS Ranked
WHERE pct_rank <= 0.1;
FROM Intellipaat_Emp;
71. Write a query to calculate the time gap (in hours) between
consecutive logins for each user.
TIMESTAMPDIFF(HOUR,
LAG(login_time) OVER (PARTITION BY user_id ORDER BY login_time),
login_time) AS hours_since_last_login
FROM Logins;
total_sales
FROM product_dim p
UNION
SELECT 1
FROM sales_fact s
WHERE p.product_id = s.product_id
);
SELECT product_id,
ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS ytd_sales
FROM sales_fact
ORDER BY product_id;
74. How would you find the second-highest salary from a table?
Code:
SELECT MAX(e_salary)
FROM employee
75. How will you fetch the most recent entries in a database?
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 15/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
We can fetch the most recent entries in a database by using the ORDER BY
clause along with the timestamp column in descending order.
SELECT *
FROM table_name
76. How will you find the IDs or details where there have been no
entries in terms of sales?
To find the IDs or details where there have been no entries in terms of sales, we
can use the LEFT JOIN or NOT EXISTS clause.
Assume we have two tables: ‘product’ with product details and ‘sales’ with sales
data.
Left Joins:
Here, the WHERE s.product_id is NULL condition helps us filter out the rows
where a match in the sales table is not found.
Not Exists:
FROM products p
FROM sales s
);
To find the average salary of each vertical and the highest salary among the
employees, we can use the group by clause along with the aggregate functions
(AVG and MAX).
SELECT vertical,
AVG(salary) AS average_salary,
MAX(salary) AS highest_salary
FROM employees
GROUP BY vertical;
Where,
78. Given data where store inventory is stored, your task is to find
the top 3 products in each category in terms of prices.
To find the top 3 products in each category in terms of price, we can group by
clause along with the aggregate function (MAX) with the price column and set
the limit as 3 in descending order.
SELECT category,
product_name,
MAX(price) AS max_price
FROM inventory
GROUP BY category, product_name
LIMIT 3;
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 16/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
SUM(quantity_sold) AS total_sales
FROM sales
WHERE product_id = 'your_product_id'
GROUP BY year_month
ORDER BY year_month;
To fetch the managers and employees working under them, we can use a self-
join to fetch the managers and the employees working under them.
M.manager_name AS manager_name,
E.employee_id AS employee_id,
E.employee_name AS employee_name
FROM employees E
JOIN employees M ON E.manager_id = M.employee_id
To fetch the total quantity of the top product purchased by the customers, we
can use a group by clause along with the limit in descending order.
SELECT product_id,
SUM(quantity_purchased) AS total_quantity_purchased
FROM purchases
GROUP BY product_id
LIMIT 1;
FROM Supervision
UNION ALL
FROM Hierarchy H
SELECT root
FROM Hierarchy
GROUP BY root
HAVING COUNT(DISTINCT manager_id) <> COUNT(manager_id);
SELECT
product_id,
YEAR(sale_date) AS year,
MONTH(sale_date) AS month,
SUM(sale_amount) AS total_sales
FROM sales_fact
GROUP BY product_id, YEAR(sale_date), MONTH(sale_date);
WITH DateSeries AS (
FROM sales
)
SELECT DATE_ADD(start_date, INTERVAL seq DAY) AS missing_date
FROM DateSeries,
FROM sales,
(SELECT @rownum := 0) AS r) AS seq_numbers
FROM sales)
ORDER BY missing_date;
85. You have an order table with millions of rows and you
frequently run a query that filters it by customer_id, order_date,
and status. What indexes would you create to optimize this
query, and why?
We need to create a composite index on columns that are frequently used for
filtering
WITH RankedCustomers AS (
FROM orders
GROUP BY customer_id
FROM RankedCustomers
WHERE rank <= 10;
Explore Program
UPDATE Intellipaat_Emp e
SET total_sales = (
SELECT SUM(sale_amount)
FROM sales s
WHERE s.employee_id = e.employee_id
WHERE EXISTS (
SELECT 1
FROM sales s
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 18/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
88. Can you identify the employee who has the third-highest
salary from the given employee table (with salary-related data)?
Consider the following employee table. In the table, Sabid has the third-highest
salary (60,000).
Name Salary
Tarun 70,000
Sabid 60,000
Adarsh 30,000
Vaibhav 80,000
Below is a simple query to find out which employee has the third-highest salary.
The functions RANK, DENSE RANK, and ROW NUMBER are used to obtain the
increasing integer value by imposing the ORDER BY clause in the SELECT
statement, based on the ordering of the rows. The ORDER BY clause is
necessary when the RANK, DENSE RANK, or ROW NUMBER functions are used.
On the other hand, the PARTITION BY clause is optional.
WITH CTE AS
FROM EMPLOYEE
)
FROM CTE
WHERE RN = 3;
Fig. 1.1
Fig. 1.2
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 19/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
So, based on these two tables, let’s look into some of the questions related to
SQL JOINS and queries.
90. Get customer name and product name order by first name
from
SELECT a.first_name,b.Product_name
FROM [customer] A
ON A.customer_id = B.customer_id
ORDER BY a.first_name
91. Get the customer name, and product name order by first
name from
ON A.customer_id = B.customer_id
ORDER BY a.first_name
92. Get the Customer name and product name order by firstname
from
93. Get all product names even if they have not matched any
customer ID, in the left table, order by first name from
SELECT a.first_name,b.Product_name
ON a.customer_id = b.customer_id
ORDER BY a.first_name
95. Write a query to find out the Customer name who has not
been assigned any product, and display
AS [Product]
FROM [customer] A
LEFT OUTER JOIN [product] B
ON a.customer_id = b.customer_id
96. Write a query to find out the product name that is not
assigned to any employee( tables:- [CustomerDetail],
[ProductDetail]).
ON a.customer_id = b.customer_id
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 20/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
97. Write down the query to fetch CustomerName & Product who
has assigned more than one project.
ON c.customer_id = p.customer_id
WHERE c.customer_id IN
(SELECT customer_id FROM [product] GROUP BY customer_id HAVING
COUNT(*)
>1 )
*The output will not come as there is no duplicate record in the product table.
customer c
on p.customer_id = c.customer_id
*The output will not come as there is no duplicate record in the product table.
order_id INT,
order_date DATE
);
Yes, after using the DELETE command you can rollback if you are using the
TRANSACTION command. DELETE is a DML command, so when you rollback all
the transactions are undone and the records are restored. Here is an example
for the same:
BEGIN TRANSACTION;
ROLLBACK;
The PIVOT command is used to summarise the data. It basically converts rows
into columns that helps in better analysis. Lets understand this using an
example:
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 21/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
Input Data:
A 2022 100
B 2022 150
A 2023 200
B 2023 250
Output Data:
A 100 200
B 150 250
Query Used:
FROM (
FROM sales
) AS SourceTable
PIVOT (
SUM(sales)
A Dynamic SQL query is a technique in SQL wherein the query is built during
runtime giving dynamic inputs in the query. This makes the query flexible
enough to create multiple use cases using the same query. Here is an example
of the same:
''HR''';
EXEC(@query);
Conclusion:
In conclusion, we covered the top 100+ SQL interview questions and answers in
this article that help you prepare and crack SQL jobs. We focused on covering
essential concepts of SQL, like the basics of SQL queries, joins, subqueries,
indexing, and performance optimization. Whether you are a beginner or an
expert, understanding these topics will help you confidently handle SQL
interviews.
If you want to know how data analysis helps in real-world applications and
advance your skills further, consider exploring a data analytics course to gain
comprehensive knowledge and hands-on experience.
Go through the following SQL Cheat Sheets and download their PDF:
Download the PDF for SQL Constraints, Joins, Set Operators Cheat Sheet
Download the PDF of SQL Grouping, inbuilt, subquery, views, and temp table
Cheat Sheet
Download the PDF for SQL User Define Function Cheat Sheet
Download the PDF for SQL Exception Handling Index, Pivot, Transactions
Cheat Sheet
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 22/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
Kishore Kumar
Vice President
Recommended Videos
SQL Full Course 2025 DBMS Interview Questions Data Science Full Course For Data Analyt
Beginners Beginner to
Recommended Programs
SQL Server DBA Certification Free SQL Certification Course Database Architect Training: SQL Course
Training Combo Course
5 (4568) Co- Created with 5 (323) 5 (512) Co- Created with 5 (190000
Microsoft Microsoft Microsoft
Recommended Articles
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 23/24
1/17/25, 11:44 PM Top 100+ SQL Interview Questions and Answers for 2025
Python Interview Questions and Java Interview Questions and Javascript Interview Questions Python Tuto
Answers Answers and Answers
Updated on: Jan 16, 2025 Updated on: Jan 17, 2025 Updated on: Jan 9, 2025 Updated on: J
Address: 6th Floor, Primeco Towers, Arekere Gate Junction, Bannerghatta Main Road, Bengaluru, Karnataka 560076, India.
Disclaimer: The certification names are the trademarks of their respective owners.
https://intellipaat.com/blog/interview-question/sql-interview-questions/ 24/24