SQL Interview Questions _ GeeksforGeeks
SQL Interview Questions _ GeeksforGeeks
Search...
Table of Content
SQL Interview Questions and Answers for Freshers
Intermediate SQL Interview Questions and Answers
Advanced SQL Interview Questions and Answers
https://www.geeksforgeeks.org/sql-interview-questions/ 1/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
1. What is SQL?
2. What is a database?
VARCHAR2:
Databases SQL MySQLVariable-length storage.
PostgreSQL PL/SQL Only the
MongoDB SQL actual data is stored,
Cheat Sheet Sign In
saving space when the full length is not needed.
A foreign key is a column (or set of columns) in one table that refers to
the primary key in another table. It establishes and enforces a
relationship between the two tables, ensuring data integrity.
https://www.geeksforgeeks.org/sql-interview-questions/ 3/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Arithmetic Operators: +, -, *, /, %
Comparison Operators: =, !=, <>, >, <, >=, <=
Logical Operators: AND, OR, NOT
Set Operators: UNION, INTERSECT, EXCEPT
Special Operators: BETWEEN, IN, LIKE, IS NULL
INNER JOIN: Returns rows that have matching values in both tables.
LEFT JOIN (LEFT OUTER JOIN): Returns all rows from the left table,
and matching rows from the right table.
RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right
table, and matching rows from the left table.
FULL JOIN (FULL OUTER JOIN): Returns all rows when there is a
match in either table.
CROSS JOIN: Produces the Cartesian product of two tables.
https://www.geeksforgeeks.org/sql-interview-questions/ 4/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
https://www.geeksforgeeks.org/sql-interview-questions/ 5/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
HAVING: Filters grouped data after the GROUP BY clause has been
applied.
In short, WHERE applies to individual rows, while HAVING applies to
groups.
The ORDER BY clause sorts the result set of a query in either ascending
(default) or descending order, based on one or more columns. This
helps present the data in a more meaningful or readable sequence.
SQL Databases:
Use structured tables with rows and columns.
Rely on a fixed schema.
Offer ACID properties.
NoSQL Databases:
https://www.geeksforgeeks.org/sql-interview-questions/ 6/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
https://www.geeksforgeeks.org/sql-interview-questions/ 7/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
The SELECT statement retrieves data from one or more tables. It is the
most commonly used command in SQL, allowing users to filter, sort,
and display data based on specific criteria.
https://www.geeksforgeeks.org/sql-interview-questions/ 8/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
existing table, and the DROP command removes a table entirely. DDL
commands primarily focus on the schema or structure of the database.
Example:
These commands deal with the actual data stored within database
objects. For instance, the INSERT command adds rows of data to a table,
the UPDATE command modifies existing data, and the DELETE command
removes rows from a table. In short, DML commands allow you to query
and manipulate the data itself rather than the structure.
Example:
Example:
https://www.geeksforgeeks.org/sql-interview-questions/ 10/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
The UNION operator combines the result sets of two or more SELECT
queries into a single result set, removing duplicate rows. The result
sets must have the same number of columns and compatible data types
for corresponding columns.
Example:
Example:
https://www.geeksforgeeks.org/sql-interview-questions/ 11/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Example:
SELECT ID,
CASE
WHEN Salary > 100000 THEN 'High'
WHEN Salary BETWEEN 50000 AND 100000 THEN 'Medium'
ELSE 'Low'
END AS SalaryLevel
FROM Employees;
Example:
The COALESCE function returns the first non-NULL value from a list of
expressions. It’s commonly used to provide default values or handle
missing data gracefully.
Example:
https://www.geeksforgeeks.org/sql-interview-questions/ 12/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Example:
Example:
Example:
https://www.geeksforgeeks.org/sql-interview-questions/ 13/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
RANK(): Assigns a rank to each row, with gaps if there are ties.
DENSE_RANK(): Assigns consecutive ranks without any gaps.
Example:
If two employees have the same salary, they get the same rank, but
RANK() will skip a number for the next rank, while DENSE_RANK() will not.
Example:
Example:
WITH TopSalaries AS (
SELECT Name, Salary
FROM Employees
WHERE Salary > 50000
)
SELECT * FROM TopSalaries WHERE Name LIKE 'A%';
46. What are window functions, and how are they used?
https://www.geeksforgeeks.org/sql-interview-questions/ 14/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
1. Index
2. Key
https://www.geeksforgeeks.org/sql-interview-questions/ 15/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Example:
The index on LastName lets the database quickly find all rows matching
‘Smith’ without scanning every record.
Advantages
Disadvantages:
1. Clustered Index:
Organizes the physical data in the table itself in the order of the
indexed column(s).
A table can have only one clustered index.
Improves range queries and queries that sort data.
Example: If EmployeeID is the clustered index, the rows in the table are
stored physically sorted by EmployeeID.
https://www.geeksforgeeks.org/sql-interview-questions/ 16/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
2. Non-Clustered Index:
51. What are temporary tables, and how are they used?
Temporary tables are tables that exist only for the duration of a session
or a transaction. They are useful for storing intermediate results,
simplifying complex queries, or performing operations on subsets of
data without modifying the main tables.
Example:
Standard View:
https://www.geeksforgeeks.org/sql-interview-questions/ 17/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Example:
1. Greater Flexibility:
Constraints enforce rules that the data must follow, preventing invalid
or inconsistent data from being entered:
Example:
https://www.geeksforgeeks.org/sql-interview-questions/ 19/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Example:
WITH CTE AS (
SELECT Column1, Column2, ROW_NUMBER() OVER (PARTITION BY
Column1 ORDER BY Column2) AS RowNum
FROM TableName
)
SELECT * FROM CTE WHERE RowNum = 1;
https://www.geeksforgeeks.org/sql-interview-questions/ 20/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Example:
SELECT Name,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.CustomerID = Customers.CustomerID) AS
OrderCount
FROM Customers;
60. What are partitioned tables, and when should we use them?
1. Atomicity:
2. Consistency:
A transaction must take the database from one valid state to another,
maintaining all defined rules and constraints.
This ensures data integrity is preserved throughout the transaction
process.
3. Isolation:
4. Durability:
1. Read Uncommitted:
https://www.geeksforgeeks.org/sql-interview-questions/ 22/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
2. Read Committed:
3. Repeatable Read:
4. Serializable:
Example:
SELECT *
FROM Orders WITH (NOLOCK);
https://www.geeksforgeeks.org/sql-interview-questions/ 23/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
This query fetches data from the Orders table without waiting for other
transactions to release their locks.
Deadlocks occur when two or more transactions hold resources that the
other transactions need, resulting in a cycle of dependency that
prevents progress. Strategies to handle deadlocks include:
Example:
66. What are the differences between OLTP and OLAP systems?
67. What is a live lock, and how does it differ from a deadlock?
1. Live Lock
https://www.geeksforgeeks.org/sql-interview-questions/ 25/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Unlike a deadlock, the transactions are not blocked; they are actively
running, but they cannot complete.
2. Deadlock
The EXCEPT operator is used to return rows from one query’s result set
that are not present in another query’s result set. It effectively performs
a set difference, showing only the data that is unique to the first query.
Example:
Use Case:
Performance Considerations:
69. How do you implement dynamic SQL, and what are its
advantages and risks?
https://www.geeksforgeeks.org/sql-interview-questions/ 26/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Syntax:
Advantages:
Risks:
Horizontal Partitioning:
Divides the rows of a table into multiple partitions based on
values in a specific column.
Example: Splitting a customer table into separate partitions
by geographic region or by year.
Use Case: When dealing with large datasets, horizontal
partitioning can improve performance by limiting the
https://www.geeksforgeeks.org/sql-interview-questions/ 27/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
71. What are the considerations for indexing very large tables?
1. Indexing Strategy:
2. Index Types:
Use clustered indexes for primary key lookups and range queries.
Use non-clustered indexes for filtering, ordering, and covering
specific queries.
3. Partitioned Indexes:
4. Maintenance Overhead:
https://www.geeksforgeeks.org/sql-interview-questions/ 28/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
1. Sharding
2. Partitioning
https://www.geeksforgeeks.org/sql-interview-questions/ 29/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
73. What are the best practices for writing optimized SQL
queries?
3. **Avoid SELECT *:
Retrieve only the columns needed. This reduces I/O and improves
performance.
https://www.geeksforgeeks.org/sql-interview-questions/ 30/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Choose INNER JOIN, LEFT JOIN, or OUTER JOIN based on the data
relationships and performance requirements.
8. Optimize Aggregations:
https://www.geeksforgeeks.org/sql-interview-questions/ 31/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
1. Indexing
Advantages:
Speeds up read operations and improves query performance
without changing the data structure.
Can be applied incrementally and is reversible if not
effective.
Consider indexing when you need faster lookups without
altering the data model.
Disadvantages:
Slows down write operations as indexes need to be
maintained.
Requires additional storage.
2. Denormalization
Advantages:
Simplifies query logic by storing pre-joined or aggregated
data.
Can improve performance for read-heavy workloads where
complex joins are frequent.
https://www.geeksforgeeks.org/sql-interview-questions/ 32/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Key Components:
Example:
https://www.geeksforgeeks.org/sql-interview-questions/ 33/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
1. Transactional Queries:
2. Analytical Queries:
3. Key Differences:
https://www.geeksforgeeks.org/sql-interview-questions/ 34/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Example:
Converting a dataset that lists monthly sales into a format that displays
each month as a separate column.
80. What is a bitmap index, and how does it differ from a B-tree
index?
1. Bitmap Index:
2. B-tree Index:
https://www.geeksforgeeks.org/sql-interview-questions/ 35/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
3. Key Difference:
Explanation:
82. Write a query to retrieve employees who earn more than the
average salary.
https://www.geeksforgeeks.org/sql-interview-questions/ 36/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
SELECT *
FROM Employee
WHERE Salary > (SELECT AVG(Salary) FROM Employee);
Explanation:
Explanation:
The query uses GROUP BY to group identical values and HAVING COUNT(*) > 1
to identify values that appear more than once in the specified column.
84. Write a query to find the employees who joined in the last
30 days.
SELECT *
FROM Employee
WHERE JoiningDate > DATE_SUB(CURDATE(), INTERVAL 30 DAY);
Explanation:
SELECT *
FROM Employee
https://www.geeksforgeeks.org/sql-interview-questions/ 37/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Explanation:
The query sorts employees by salary in descending order and uses LIMIT
3 to return only the top three earners.
Explanation:
This query retains only one row for each set of duplicates by keeping
the row with the smallest EmployeeID. It identifies duplicates using GROUP
SELECT *
FROM TableA
INNER JOIN TableB ON TableA.ID = TableB.ID;
Explanation:
https://www.geeksforgeeks.org/sql-interview-questions/ 38/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
SELECT *
FROM Employee
WHERE Name LIKE 'A%' AND Name LIKE '%A';
Explanation:
The query uses LIKE with wildcard characters to filter rows where the
Name column starts and ends with the letter 'A'.
Explanation:
SELECT *
FROM Employee
WHERE ManagerID IS NULL;
Explanation:
91. Write a query to fetch the 3rd and 4th highest salaries.
WITH SalaryRank AS (
SELECT Salary, RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM Employee
)
https://www.geeksforgeeks.org/sql-interview-questions/ 39/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
SELECT Salary
FROM SalaryRank
WHERE Rank IN (3, 4);
Explanation:
This query uses the RANK() window function to rank the salaries in
descending order. The outer query then selects the 3rd and 4th highest
salaries by filtering for those ranks.
SELECT
MAX(CASE WHEN ColumnName = 'Condition1' THEN Value END) AS
Column1,
MAX(CASE WHEN ColumnName = 'Condition2' THEN Value END) AS
Column2
FROM TableName;
Explanation:
This query converts specific row values into columns using conditional
aggregation with CASE. Each column’s value is determined based on a
condition applied to rows.
93. Write a query to fetch records updated within the last hour.
SELECT *
FROM TableName
WHERE UpdatedAt >= NOW() - INTERVAL 1 HOUR;
Explanation:
https://www.geeksforgeeks.org/sql-interview-questions/ 40/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
SELECT *
FROM Employee
WHERE DepartmentID IN (
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
HAVING COUNT(*) < 5
);
Explanation:
SELECT CASE
WHEN EXISTS (SELECT * FROM TableName) THEN 'Has Records'
ELSE 'No Records'
END AS Status;
Explanation:
The query uses EXISTS to determine if any rows exist in the table,
returning a status of 'Has Records' or 'No Records' based on the result.
Explanation:
This query joins the Employee table with itself to compare employee
salaries to their respective managers’ salaries, selecting those who earn
more.
https://www.geeksforgeeks.org/sql-interview-questions/ 41/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
WITH RowNumbered AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS
RowNum
FROM Employee
)
SELECT *
FROM RowNumbered
WHERE RowNum % 2 = 0;
Explanation:
then selects rows where the row number is even, effectively fetching
alternating rows. The ORDER BY (SELECT NULL) is used to avoid any specific
ordering and just apply a sequential numbering.
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
ORDER BY AVG(Salary) DESC
LIMIT 1;
Explanation:
WITH OrderedEmployees AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS
RowNum
FROM Employee
https://www.geeksforgeeks.org/sql-interview-questions/ 42/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
)
SELECT *
FROM OrderedEmployees
WHERE RowNum = n;
Explanation:
SELECT *
FROM Employee
WHERE MONTH(JoiningDate) = MONTH(CURDATE());
Explanation:
Conclusion
A solid understanding of SQL is essential for anyone aspiring to work in
data analysis, database administration, or software development. By
reviewing and practicing these 100 SQL interview questions, we will
gain the confidence and knowledge needed to answer even the
toughest queries during our next interview. Remember, preparation is
the key because each question we master brings us closer to securing
that dream job in the tech industry.
https://www.geeksforgeeks.org/sql-interview-questions/ 43/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Advertise with us
Similar Reads
SQL Query Interview Questions
SQL, or Structured Query Language, is the standard language for
managing and manipulating relational databases such as MySQL, Oracle…
https://www.geeksforgeeks.org/sql-interview-questions/ 44/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Spotify is a popular music streaming platform that uses data analysis and
management to improve user experience and provide personalized…
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
https://www.geeksforgeeks.org/sql-interview-questions/ 45/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
Buddh Nagar, Uttar Pradesh, 201305
Advertise with us
Company Explore
About Us Job-A-Thon
Legal Offline Classroom Program
Privacy Policy DSA in JAVA/C++
Careers Master System Design
In Media Master CP
Contact Us Videos
Corporate Solution
Campus Training Program
Tutorials DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android
https://www.geeksforgeeks.org/sql-interview-questions/ 46/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
https://www.geeksforgeeks.org/sql-interview-questions/ 47/48
5/31/25, 5:07 PM SQL Interview Questions | GeeksforGeeks
https://www.geeksforgeeks.org/sql-interview-questions/ 48/48