0% found this document useful (0 votes)
0 views6 pages

SQL Interview Queries Reference

The document provides a comprehensive list of essential SQL queries for interview preparation, covering various topics such as basic SELECT statements, aggregate functions, JOIN operations, and advanced features like window functions and JSON querying. Each query is accompanied by a brief description of the concepts it demonstrates. This resource serves as a guide for candidates to practice and understand key SQL functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

SQL Interview Queries Reference

The document provides a comprehensive list of essential SQL queries for interview preparation, covering various topics such as basic SELECT statements, aggregate functions, JOIN operations, and advanced features like window functions and JSON querying. Each query is accompanied by a brief description of the concepts it demonstrates. This resource serves as a guide for candidates to practice and understand key SQL functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Top SQL Queries for Interview Preparation

1. 1. Basic SELECT and WHERE


SELECT * FROM Employee WHERE department = 'Sales';
Covers: SELECT, WHERE clause - Filtering data.

2. 2. DISTINCT Values
SELECT DISTINCT department FROM Employee;
Covers: DISTINCT - Removing duplicates.

3. 3. Aggregate Functions and GROUP BY


SELECT department, COUNT(*) AS total_employees
FROM Employee
GROUP BY department;
Covers: COUNT, GROUP BY - Aggregation by groups.

4. 4. HAVING Clause
SELECT department, AVG(salary) AS avg_salary
FROM Employee
GROUP BY department
HAVING AVG(salary) > 50000;
Covers: HAVING - Filtering after grouping.

5. 5. ORDER BY
SELECT * FROM Employee ORDER BY salary DESC;
Covers: ORDER BY - Sorting results.

6. 6. LIMIT / TOP
SELECT * FROM Employee ORDER BY salary DESC LIMIT 5;
Covers: LIMIT or TOP (in SQL Server) - Top N results.

7. 7. BETWEEN and IN
SELECT * FROM Employee WHERE salary BETWEEN 40000 AND 60000;
SELECT * FROM Employee WHERE department IN ('Sales', 'HR');
Covers: BETWEEN, IN - Range and list filters.

8. 8. INNER JOIN
SELECT e.name, d.department_name
FROM Employee e
JOIN Department d ON e.dept_id = d.id;
Covers: JOIN - Combining data from multiple tables.

9. 9. LEFT JOIN
SELECT e.name, d.department_name
FROM Employee e
LEFT JOIN Department d ON e.dept_id = d.id;
Covers: LEFT JOIN - All from left, matching from right.

10. 10. Subquery in WHERE


SELECT name FROM Employee
WHERE dept_id IN (SELECT id FROM Department WHERE location = 'New York');
Covers: Subqueries - Query within a query.

11. 11. Correlated Subquery


SELECT name, salary FROM Employee e
WHERE salary > (SELECT AVG(salary) FROM Employee WHERE dept_id = e.dept_id);
Covers: Correlated Subqueries - Row-wise dependent subquery.

12. 12. CASE Statement


SELECT name,
CASE
WHEN salary > 80000 THEN 'High'
WHEN salary > 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_band
FROM Employee;
Covers: CASE - Conditional logic in SQL.

13. 13. UNION vs UNION ALL


SELECT name FROM Employees_US
UNION
SELECT name FROM Employees_UK;
Covers: UNION / UNION ALL - Combining result sets.

14. 14. UPDATE Statement


UPDATE Employee SET salary = salary * 1.1 WHERE department = 'Sales';
Covers: UPDATE - Modifying existing data.

15. 15. DELETE Statement


DELETE FROM Employee WHERE join_date < '2020-01-01';
Covers: DELETE - Removing rows.

16. 16. INSERT INTO SELECT


INSERT INTO Employees_Archive (id, name, department)
SELECT id, name, department FROM Employee WHERE join_date < '2020-01-01';
Covers: INSERT SELECT - Data migration between tables.

17. 17. EXISTS Clause


SELECT name FROM Employee e
WHERE EXISTS (
SELECT 1 FROM Project p WHERE p.emp_id = e.id
);
Covers: EXISTS - Checking for related data existence.

18. 18. NULL Handling with IS NULL / COALESCE


SELECT name, COALESCE(manager, 'No Manager') FROM Employee;
Covers: IS NULL, COALESCE - NULL value handling.

19. 19. String Functions


SELECT UPPER(name), LENGTH(name) FROM Employee;
Covers: String manipulation - UPPER, LENGTH, CONCAT, etc.

20. 20. Window Functions (Advanced)


SELECT name, department, salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank_in_dept
FROM Employee;
Covers: OVER, RANK, PARTITION BY - Analytical queries.

21. 21. Date Functions


SELECT name, DATEDIFF(CURDATE(), join_date) AS days_worked FROM Employee;
Covers: Date functions - DATEDIFF, NOW(), CURDATE().

22. 22. CREATE TABLE & Constraints


CREATE TABLE Department (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
location VARCHAR(100)
);
Covers: Table creation and constraints - PRIMARY KEY, NOT NULL.

23. 23. ALTER TABLE


ALTER TABLE Employee ADD COLUMN bonus DECIMAL(10,2);
Covers: Schema changes.

24. 24. Indexing


CREATE INDEX idx_salary ON Employee(salary);
Covers: Performance optimization via indexing.

25. 25. Views


CREATE VIEW HighEarners AS
SELECT name, salary FROM Employee WHERE salary > 70000;
Covers: VIEW - Virtual table abstraction.

26. 26. Find Second Highest Salary


SELECT MAX(salary) AS SecondHighest
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);
Covers: Subquery with MAX - Finding ranked values.

27. 27. Find Duplicate Records


SELECT name, COUNT(*) AS count
FROM Employee
GROUP BY name
HAVING COUNT(*) > 1;
Covers: Identifying duplicates using GROUP BY, HAVING.

28. 28. Find Employees Without Department


SELECT e.name
FROM Employee e
LEFT JOIN Department d ON e.dept_id = d.id
WHERE d.id IS NULL;
Covers: LEFT JOIN and NULL filtering - Anti join.

29. 29. DELETE Duplicate Records (Retain One)


DELETE FROM Employee
WHERE id NOT IN (
SELECT MIN(id)
FROM Employee
GROUP BY name, department
);
Covers: Duplicate deletion strategy.

30. 30. CTE (Common Table Expression)


WITH DepartmentSalary AS (
SELECT department, AVG(salary) AS avg_salary
FROM Employee
GROUP BY department
)
SELECT * FROM DepartmentSalary WHERE avg_salary > 60000;
Covers: CTE - Readable subqueries.

31. 31. Intersect / Minus / Except


SELECT name FROM Employees_US
INTERSECT
SELECT name FROM Employees_UK;

SELECT name FROM Employees_US


EXCEPT
SELECT name FROM Employees_UK;
Covers: Set operations - INTERSECT, EXCEPT (use MINUS in Oracle).

32. 32. Transaction Handling


BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 1000 WHERE id = 1;
UPDATE Accounts SET balance = balance + 1000 WHERE id = 2;

COMMIT;
Covers: BEGIN, COMMIT, ROLLBACK - Transaction integrity.

33. 33. Stored Procedure Example


DELIMITER //
CREATE PROCEDURE GetEmployeeCount()
BEGIN
SELECT COUNT(*) FROM Employee;
END //
DELIMITER ;
Covers: Procedural SQL - CREATE PROCEDURE, control flow.

34. 34. Self Join


SELECT e1.name AS Employee, e2.name AS Manager
FROM Employee e1
JOIN Employee e2 ON e1.manager_id = e2.id;
Covers: SELF JOIN - Hierarchical relationships.

35. 35. Pivot Query (Aggregation by Columns)


SELECT
SUM(CASE WHEN gender = 'Male' THEN 1 ELSE 0 END) AS MaleCount,
SUM(CASE WHEN gender = 'Female' THEN 1 ELSE 0 END) AS FemaleCount
FROM Employee
GROUP BY department;
Covers: Conditional aggregation - pivot logic using CASE.

36. 36. Find Nth Highest Salary using DENSE_RANK


SELECT * FROM (
SELECT name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM Employee
) AS ranked
WHERE rnk = 3;
Covers: Window function with filtering - Nth record.

37. 37. Update with Join


UPDATE Employee e
JOIN Department d ON e.dept_id = d.id
SET e.department = d.name;
Covers: UPDATE with JOIN - Cross-table update.

38. 38. Delete with Join


DELETE e FROM Employee e
JOIN Department d ON e.dept_id = d.id
WHERE d.location = 'Remote';
Covers: DELETE with JOIN - Targeted multi-table deletion.

39. 39. Query to Transpose Rows to Columns


SELECT
SUM(CASE WHEN subject = 'Math' THEN marks END) AS Math,
SUM(CASE WHEN subject = 'Science' THEN marks END) AS Science
FROM Marks
WHERE student_id = 101;
Covers: Pivot-like transformation without built-in pivot.

40. 40. JSON Field Querying


SELECT data->>'$.address.city' AS city
FROM Customers
WHERE JSON_EXTRACT(data, '$.age') > 25;
Covers: JSON querying - modern SQL features in MySQL/PostgreSQL.

You might also like