50 SQL Questions with Answers
Q1. What is SQL?
Ans - SQL (Structured Query Language) is used to manage and manipulate relational
databases.
Q2. What is the difference between SQL and MySQL?
Ans - SQL is a language; MySQL is a relational database management system (RDBMS)
that uses SQL.
Q3. What are the different types of SQL statements?
Ans - DDL (Data Definition Language), DML (Data Manipulation Language), DCL (Data
Control Language), TCL (Transaction Control Language).
Q4. What is the difference between WHERE and HAVING?
Ans - WHERE filters rows before grouping; HAVING filters groups after aggregation.
Q5. What is the primary key?
Ans - A primary key uniquely identifies each row in a table. It does not allow NULL or
duplicate values.
Q6. What is a foreign key?
Ans - A foreign key establishes a relationship between two tables by referencing the
primary key of another table.
Q7. Difference between DELETE, TRUNCATE, and DROP?
Ans - DELETE removes rows (can have WHERE). TRUNCATE removes all rows but keeps
structure. DROP removes table completely.
Q8. What is the difference between CHAR and VARCHAR?
Ans - CHAR has a fixed length, VARCHAR has a variable length.
Q9. What is an index in SQL?
Ans - Indexes improve query performance by allowing faster search but may slow down
insert/update.
Q10. What is normalization?
Ans – Process of organizing data to reduce redundancy and improve integrity (1NF,
2NF, 3NF, BCNF).
Intermediate Level
Q11. What is denormalization?
Ans - Adding redundancy to improve query performance.
Q12. Explain INNER JOIN vs LEFT JOIN.
Ans - INNER JOIN returns only matching rows; LEFT JOIN returns all from left table +
matched from right.
Q13. Write an SQL query to find the second highest salary.
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Q14. How to find duplicate rows in a table?
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
Q15. What is a subquery?
Ans - A query inside another query.
Q16. What is the difference between UNION and UNION ALL?
Ans - UNION removes duplicates; UNION ALL includes duplicates.
Q17. Write a query to count employees in each department.
SELECT department_id, COUNT(*)
FROM employees
GROUP BY department_id;
Q18. What are views in SQL?
Ans - A view is a virtual table created using a query.
Q19. Can we update data in a view?
Ans - Yes, if the view is based on a single table without aggregations or GROUP BY.
Q20. Difference between clustered and non-clustered index?
Ans – Clustered index determines the physical order of data, one per table. Non-
clustered index is logical, many allowed per table.
Advanced Level
Q21. What is a stored procedure?
Ans - A stored procedure is a group of SQL statements stored in the database for reuse.
Q22. What is a trigger in SQL?
Ans - A trigger is an automatic action executed in response to events (INSERT, UPDATE,
DELETE).
Q23. Write a query to get the nth highest salary.
SELECT salary
FROM employees e1
WHERE N-1 = (
SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE [Link] > [Link]
);
Q24. What are ACID properties?
Ans - Atomicity, Consistency, Isolation, Durability.
Q25. Difference between OLTP and OLAP?
Ans - OLTP: Transactional databases (banking).
OLAP: Analytical databases (data warehouse).
Q26. What is window function in SQL?
Ans - Functions like RANK(), ROW_NUMBER(), LAG() that work on partitions of data.
Q27. Write a query using RANK() to find top 3 salaries.
SELECT employee_id, salary,
RANK() OVER(ORDER BY salary DESC) AS rank
FROM employees
WHERE rank <= 3;
Q28. What is the difference between correlated and non-correlated subquery?
Ans - Correlated subquery depends on outer query row by row. Non-correlated runs
independently.
Q29. What is the difference between DELETE with JOIN vs EXISTS?
Ans - DELETE with JOIN removes directly by joining; EXISTS checks for matching rows
before deleting.
Q30. Explain CTE (Common Table Expression).
Ans - A temporary result set defined with WITH keyword, used for recursion and
simplifying queries.
Q31. Find employees who joined in the last 6 months.
SELECT employee_id, name, hire_date
FROM employees
WHERE hire_date >= DATEADD(MONTH, -6, GETDATE());
Q32. Retrieve all customers who have not placed any orders.
SELECT c.customer_id, c.customer_name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
Q33. Get the top 5 highest selling products.
SELECT TOP 5 product_id, SUM(quantity) AS total_sold
FROM sales
GROUP BY product_id
ORDER BY total_sold DESC;
Q34. Find employees with the same salary.
SELECT [Link], [Link]
FROM employees e1
JOIN employees e2
ON [Link] = [Link] AND e1.employee_id <> e2.employee_id;
Q35. Display departments with more than 10 employees.
SELECT department_id, COUNT(*) AS emp_count
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 10;
Q36. Find orders placed in the last 7 days.
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date >= DATEADD(DAY, -7, GETDATE());
Q37. Get customers who spent more than $500 in total purchases.
SELECT c.customer_id, c.customer_name, SUM([Link]) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING SUM([Link]) > 500;
Q38. List employees who don’t have a manager assigned.
SELECT employee_id, name
FROM employees
WHERE manager_id IS NULL;
Q39. Retrieve the second highest order amount.
SELECT MAX(amount) AS second_highest
FROM orders
WHERE amount < (SELECT MAX(amount) FROM orders);
Q40. Show students who scored above average in exams.
SELECT student_id, name, marks
FROM students
WHERE marks > (SELECT AVG(marks) FROM students);
Q41. Find duplicate email addresses in a user table.
SELECT email, COUNT(*) AS count
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
Q42. List all employees along with their manager’s name.
SELECT e.employee_id, [Link] AS employee, [Link] AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
Q43. Find the total revenue generated each month.
SELECT YEAR(order_date) AS year, MONTH(order_date) AS month, SUM(amount) AS
revenue
FROM orders
GROUP BY YEAR(order_date), MONTH(order_date)
ORDER BY year, month;
Q44. Find the highest salary in each department.
SELECT department_id, MAX(salary) AS highest_salary
FROM employees
GROUP BY department_id;
Q45. Show products that were never sold.
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;
Q46. Retrieve the top 3 customers based on order amount.
SELECT TOP 3 c.customer_id, c.customer_name, SUM([Link]) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spent DESC;
Q47. Find employees who earn more than their manager.
SELECT e.employee_id, [Link], [Link], [Link] AS manager_salary
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
WHERE [Link] > [Link];
Q48. Get customers who placed orders in both 2024 and 2025.
SELECT customer_id
FROM orders
WHERE YEAR(order_date) = 2024
INTERSECT
SELECT customer_id
FROM orders
WHERE YEAR(order_date) = 2025;
Q49. Retrieve the running total of sales per day.
SELECT order_date,
SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM orders;
Q50. Find the 3rd highest salary using ROW_NUMBER().
WITH salary_rank AS (
SELECT employee_id, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn
FROM employees
)
SELECT employee_id, salary
FROM salary_rank
WHERE rn = 3;