0% found this document useful (0 votes)
6 views

SQL Interview Questions

The document contains a series of SQL-related questions and answers, covering topics such as primary keys, normalization, window functions, and ACID properties. It includes both informative and scenario-based questions, along with SQL queries to solve specific problems like retrieving top salaries, handling duplicates, and calculating averages. Additionally, it provides queries for finding missing numbers, identifying overlapping dates, and retrieving top records per group.

Uploaded by

vkscribdind
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Interview Questions

The document contains a series of SQL-related questions and answers, covering topics such as primary keys, normalization, window functions, and ACID properties. It includes both informative and scenario-based questions, along with SQL queries to solve specific problems like retrieving top salaries, handling duplicates, and calculating averages. Additionally, it provides queries for finding missing numbers, identifying overlapping dates, and retrieving top records per group.

Uploaded by

vkscribdind
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Informative Questions

1. What is the difference between a primary key and a unique key?


2. Explain the concept of normalization and its different forms.
3. What are window functions in SQL, and how do they differ from regular
aggregate functions?
4. Describe the ACID properties of transactions in SQL.
5. What is indexing, and how does it improve query performance?
Scenario-Based Questions
1. You need to retrieve the top 5 highest salaries from the employee’s table.
How would you write this query?
2. Imagine you have a sales table with duplicate entries for some
transactions. How would you identify and remove these duplicates?
3. If you wanted to find all employees who have not been assigned to any
project, how would you structure your SQL query?
4. How would you handle a situation where your query is running slowly?
What steps would you take to optimize it?
5. You need to calculate the total sales for each product category over the
last year. Describe how you would approach this task using SQL.

1. Write a SQL query to find the second highest salary from the employees
table.
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
2. Write a query to pivot data from rows to columns for sales data by month.
SELECT
product_id,
SUM(CASE WHEN MONTH(sale_date) = 1 THEN amount ELSE 0 END) AS
January,
SUM(CASE WHEN MONTH(sale_date) = 2 THEN amount ELSE 0 END) AS
February,
...
FROM sales
GROUP BY product_id;

3. Create a SQL query that retrieves all orders placed in the last 30 days along
with customer details.
SELECT o.order_id, o.order_date, c.customer_name
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= NOW() - INTERVAL 30 DAY;

4. Write a query to find employees with salaries above the average salary of
their department.
SELECT e.employee_id, e.salary
FROM employees e
WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE
department_id = e.department_id);
5. Write a SQL statement to update employee salaries by increasing them by
10% for those in the 'Sales' department.
UPDATE employees
SET salary = salary * 1.10
WHERE department = 'Sales';

6. Write a query to find all products that have never been sold (assuming there
is a sales table).
SELECT p.product_id, p.product_name
FROM products p
LEFT JOIN sales s ON p.product_id = s.product_id
WHERE s.product_id IS NULL;

7. Create a SQL query that retrieves the count of orders placed by each
customer in descending order of count.
SELECT customer_id, COUNT(order_id) AS order_count
FROM orders
GROUP BY customer_id
ORDER BY order_count DESC;

8. Write a SQL query to find customers who have made purchases on


consecutive days.
SELECT DISTINCT c.customer_id
FROM orders o1
JOIN orders o2 ON o1.customer_id = o2.customer_id AND
DATEDIFF(o2.order_date, o1.order_date) = 1;
9. Write a SQL query to retrieve the average order amount for each month in
the past year.
SELECT MONTH(order_date) AS month, AVG(order_amount) AS
avg_order_amount
FROM orders
WHERE order_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
GROUP BY MONTH(order_date);

10.Write a SQL query that retrieves all unique email domains from a user table.
SELECT DISTINCT SUBSTRING_INDEX(email, '@', -1) AS domain
FROM users;

Finding Missing Numbers in a Sequence


Question: Given a table Numbers with a single column Number, write a query to
find missing numbers in the sequence from 1 to the maximum number in the
table.
Answer:

SELECT n.Number + 1 AS MissingNumber


FROM Numbers n
LEFT JOIN Numbers n2 ON n.Number + 1 = n2.Number
WHERE n2.Number IS NULL;
Identifying Overlapping Dates
Question: Given a table Events with columns EventID, StartDate, and EndDate,
write a query to find pairs of events that have overlapping dates.
Answer:
SELECT e1.EventID, e2.EventID
FROM Events e1, Events e2
WHERE e1.EventID <> e2.EventID
AND e1.StartDate < e2.EndDate
AND e1.EndDate > e2.StartDate;

Retrieving Top N Records per Group


Question: Write a query to retrieve the top 3 highest-paid employees in each
department from the Employees table.
Answer:

WITH RankedEmployees AS (
SELECT EmployeeID, DepartmentID, Salary,
ROW_NUMBER() OVER (PARTITION BY DepartmentID ORDER BY Salary
DESC) AS Rank
FROM Employees
)
SELECT EmployeeID, DepartmentID, Salary
FROM RankedEmployees
WHERE Rank <= 3;

You might also like