SQL
BASIC TO ADVANCE
INTERVIEW
QUESTIONS
~ By Learn Coding
Basic Level
Theoretical Questions:
1. What is SQL?
2. What are the different types of SQL commands?
3. What is the difference between SQL and
NoSQL?
4. What are the different types of joins in SQL?
5. What is a primary key in SQL?
6. What is a foreign key in SQL?
7. What is normalization? What are its different
types?
8. What is denormalization? When is it used?
9. What is a relational database?
10. What is an index in SQL, and why is it used?
1
Basic Level
Theoretical Questions:
11. What is the difference between DELETE,
TRUNCATE, and DROP?
12. What are aggregate functions in SQL?
13. What is a subquery? How is it different from a join?
14. What is the purpose of the GROUP BY clause?
15. What is the difference between HAVING and
WHERE?
16. What is a union in SQL, and how is it different from
a join?
17. What is the difference between INNER JOIN and
OUTER JOIN?
18. What is a view in SQL?
19. What is a stored procedure?
20. What is a trigger in SQL?
2
Basic Level
Practical Questions:
21. Write a SQL query to find the second highest salary from
a employees table.
22. Write a query to find the count of employees in each
department from the employees table. 23. Write a query to
retrieve all employees who joined after January 1, 2020.
24. Write a query to get the total number of orders placed by
a customer from an orders table.
25. Write a SQL query to retrieve the top 5 highest-selling
products from a sales table.
26. Write a query to find the employees who are not assigned
to any department.
27. Write a query to fetch employee names and their
managers from an employees table.
28. Write a query to list all products with a price greater than
100 in the products table.
29. Write a query to list all employees who have worked for
more than 5 years.
30. Write a SQL query to calculate the average salary from
an employees table.
3
Basic Level
Practical Questions:
31. Write a SQL query to delete all employees who left
before 2010.
32. Write a query to show the first and last names of all
employees.
33. Write a SQL query to get the total revenue from the
sales table.
34. Write a query to retrieve all products with the highest
rating in the products table.
35. Write a SQL query to list customers who placed orders
last month.
36. Write a SQL query to list the orders that are still open.
37. Write a SQL query to find out if there are any duplicate
records in the customers' table.
38. Write a SQL query to join two tables orders and
customers on customer ID and get the order details.
39. Write a SQL query to list all employees with no
supervisors.
40. Write a query to display the first 10 rows of a table.
4
Medium Level
Theoretical Questions:
41. What is a transaction in SQL? What are ACID
properties?
42. What is the difference between 'TRUNCATE` and
`DELETE`?
43. What is the difference between UNION' and 'UNION
ALL?
44. What is a self-join? Can you provide an example?
45. What are window functions in SQL? How do they
work? 46. What is the purpose of indexing in SQL?
47. What is a composite key in SQL?
48. What is a recursive query in SQL? Provide an example.
49. What is a CTE (Common Table Expression) in SQL?
50. What is the 'EXISTS' operator in SQL? How does it
differ from `IN`?
5
Medium Level
Theoretical Questions:
51. What is the 'CASE` statement in SQL? Provide an
example.
52. What is a normalization anomaly?
53. What is a deadlock in SQL, and how can it be
resolved?
54. What is an outer join, and what types are there?
55. What is the difference between CHAR and VARCHAR`
data types in SQL?
56. What is a surrogate key?
57. What is a pivot in SQL?
58. What are SQL injection attacks, and how can they be
prevented?
59. What is a database schema? What are its benefits?
60. What is referential integrity in SQL?
6
Medium Level
Practical Questions:
61. Write a SQL query to find the total sales for each
month.
62. Write a query to find the customers who have not
placed any orders in the last year. 63. Write a query to list
employees who have the same salary as their manager.
64. Write a query to update the salary of employees who
are in a specific department.
65. Write a query to list the employees who have worked
in more than one department.
66. Write a query to find the total number of employees in
each department sorted by department name.
67. Write a SQL query to rank products by total revenue.
68. Write a query to find all products that were ordered by
more than one customer.
69. Write a SQL query to list all employees whose name
starts with 'J'.
70. Write a query to fetch employees who joined between
two specific dates.
7
Medium Level
Practical Questions:
71. Write a SQL query to find the employees who have
been with the company the longest.
72. Write a query to calculate the sum of salaries of
employees grouped by department.
73. Write a query to list the customers who have placed
the most orders.
74. Write a query to show all products and the number of
orders for each product.
75. Write a SQL query to find the employee with the
highest salary in each department.
76. Write a query to find the orders that were shipped on
the same day they were placed.
77. Write a SQL query to find out how many products
were sold in each category.
78. Write a query to return all employees who are
managers in any department.
79. Write a SQL query to find the average order value for
each customer.
80. Write a query to list employees who have more than
2 dependents.
8
Hard Level
Theoretical Questions:
81. What is a materialized view in SQL? How does it differ
from a regular view?
82. What is database partitioning, and how is it
implemented in SQL?
83. What are some techniques to optimize SQL queries?
84. What is a clustered index and how does it differ from
a non-clustered index?
85. Explain how a database performs query optimization.
86. What are the differences between horizontal and
vertical scaling in database architecture?
87. What is sharding in a database? How is it
implemented in SQL?
88. Explain the difference between a hash join and a
merge join in SQL.
89. What is the difference between inner and outer
queries in SQL? Provide examples.
90. What is a full-text index in SQL, and when would you
use it?
9
Hard Level
Theoretical Questions:
91. What is the purpose of `ROW_NUMBER()`,
`RANK(), and `DENSE_RANK()` window functions?
92. What is a cross join, and when should it be used?
93. What is the difference between `FOR XML PATH'
and `FOR JSON PATH' in SQL Server?
94. What is query execution plan, and how can you read
it for optimization?
95. How does SQL handle large datasets? What
strategies can be used to manage large-scale data?
96. What is denormalization in database design, and
when would you use it?
97. What are the challenges in maintaining referential
integrity in a distributed database?
98. What is the difference between eager loading and
lazy loading in SQL?
99. What are SQL transactions, and how do you handle
concurrency control?
100. What is a database lock, and what types of locks
are there? text
10
Hard Level
Practical Questions:
101. Write a query to find out which products are being sold
but have zero inventory.
102. Write a query to calculate the cumulative sum of sales
for each customer in a date range.
103. Write a SQL query to find missing values in a
sequence of numbers.
104. Write a query to list all duplicate records based on
multiple columns.
105. Write a query to optimize the performance of a slow-
running SQL query.
106. Write a query to implement paging (fetching a subset
of records) for large datasets.
107. Write a SQL query to merge data from two tables
without duplicates.
108. Write a query to find the most popular combination of
products in orders.
109. Write a query to find the customers who ordered the
same product multiple times.
110. Write a SQL query to calculate the moving average of
sales over the past 30 days.
11
Hard Level
Practical Questions:
111. Write a SQL query to find the top 3 performing sales
representatives.
112. Write a query to analyze customer purchasing behavior
over time (e.g., seasonal trends).
113. Write a query to calculate the percentage change in
sales between two periods.
114. Write a query to detect and report data anomalies in a
given dataset.
115. Write a query to split a single column containing JSON
data into separate rows.
116. Write a SQL query to detect missing foreign key
references in the database.
117. Write a query to find the longest continuous sequence
of sales.
118. Write a query to identify and list the top N salespersons
for each region.
119. Write a query to calculate a weighted average of sales
based on different criteria.
120. Write a SQL query to identify all products that have not
been sold in the last year.
12
Join the free coding
Revolution
with
earn Coding
L
follow on social media handles
YouTube channel
Facebook
Instagram
WhatsAapp
LinkedIn
Telegram channel