SQL Interview Queries
SQL Interview Queries
INTERVIEW QUERIES
By Rishabh Mishra
Source: https://youtu.be/aGGJLDgIrD8
Practice Dataset
EmpID EmpName Gender Salary City
1 Arjun M 75000 Pune
2 Ekadanta M 125000 Bangalore
3 Lalita F 150000 Mathura Employee Table
4 Madhav M 250000 Delhi
5 Visakha F 120000 Mathura
Rishabh Mishra
2
Create Tables: Employee and EmployeeDetail
--- first run the above code then below code INSERT INTO EmployeeDetail
INSERT INTO Employee VALUES (1, 'P1', 'Executive', '26-01-2019'),
VALUES (1, 'Arjun', 'M', 75000, 'Pune'), (2, 'P2', 'Executive', '04-05-2020'),
(2, 'Ekadanta', 'M', 125000, 'Bangalore'), (3, 'P1', 'Lead', '21-10-2021'),
(3, 'Lalita', 'F', 150000 , 'Mathura'), (4, 'P3', 'Manager', '29-11-2019'),
(4, 'Madhav', 'M', 250000 , 'Delhi'), (5, 'P2', 'Manager', '01-08-2020')
(5, 'Visakha', 'F', 120000 , 'Mathura')
Rishabh Mishra
3
Q1(a): Find the list of employees whose salary ranges between 2L to 3L.
SELECT EmpName, Salary FROM Employee
WHERE Salary > 200000 AND Salary < 300000
--- OR –--
SELECT EmpName, Salary FROM Employee
WHERE Salary BETWEEN 200000 AND 300000
Q1(b): Write a query to retrieve the list of employees from the same city.
SELECT E1.EmpID, E1.EmpName, E1.City
FROM Employee E1, Employee E2
WHERE E1.City = E2.City AND E1.EmpID != E2.EmpID
Rishabh Mishra
4
Q2(a): Query to find the cumulative sum of employee’s salary.
SELECT EmpID, Salary, SUM(Salary) OVER (ORDER BY EmpID) AS CumulativeSum
FROM Employee
(COUNT(*) * 100.0 / SUM(CASE WHEN Gender = 'M' THEN 1 ELSE 0 END)) AS MalePct MySQL
Q2(c): Write a query to fetch 50% records from the Employee table.
SELECT * FROM Employee
WHERE EmpID <= (SELECT COUNT(EmpID)/2 from Employee)
If EmpID is not auto-increment field or numeric, then we can use Row NUMBER function
Rishabh Mishra
5
Q3: Query to fetch the employee’s salary but replace the LAST 2 digits with
‘XX’
i.e 12345 will be 123XX
SELECT Salary,
CONCAT(SUBSTRING(Salary::text, 1, LENGTH(Salary::text)-2), 'XX') as
masked_number
FROM Employee
--- OR –--
SELECT Salary,
CONCAT(LEFT(Salary, LEN(Salary)-2), 'XX') as masked_salary MySQL
FROM Employee
Rishabh Mishra
6
Q4: Write a query to fetch even and odd rows from Employee table.
Rishabh Mishra
7
Q5(a): Write a query to find all the Employee names whose name:
• Begin with ‘A’
• Contains ‘A’ alphabet at second place
• Contains ‘Y’ alphabet at second last place
• Ends with ‘L’ and contains 4 alphabets
• Begins with ‘V’ and ends with ‘A’
Rishabh Mishra
8
Q5(b): Write a query to find the list of Employee names which is:
• starting with vowels (a, e, i, o, or u), without duplicates
• ending with vowels (a, e, i, o, or u), without duplicates
• starting & ending with vowels (a, e, i, o, or u), without duplicates
Rishabh Mishra
9
Q6: Find Nth highest salary from employee table with and without using
the
TOP/LIMIT keywords.
General Solution without using TOP/LIMIT Using LIMIT
Rishabh Mishra
10
Q7(a): Write a query to find and remove duplicate records from a table.
WITH CTE AS
(SELECT e.EmpID, e.EmpName, ed.Project
FROM Employee AS e
INNER JOIN EmployeeDetail AS ed
ON e.EmpID = ed.EmpID)
SELECT c1.EmpName, c2.EmpName, c1.project
FROM CTE c1, CTE c2
WHERE c1.Project = c2.Project AND c1.EmpID != c2.EmpID AND c1.EmpID
< c2.EmpID
Rishabh Mishra
11
Q8: Show the employee with the highest salary for each project
SELECT ed.Project, MAX(e.Salary) AS ProjectSal
FROM Employee AS e
INNER JOIN EmployeeDetail AS ed Similarly we can find Total Salary for
ON e.EmpID = ed.EmpID each project, just use SUM() instead of
GROUP BY Project MAX()
ORDER BY ProjectSal DESC;
Alternative, more dynamic solution: here you can fetch EmpName, 2nd/3rd highest value, etc
WITH CTE AS
(SELECT project, EmpName, salary,
ROW_NUMBER() OVER (PARTITION BY project ORDER BY salary DESC) AS
row_rank
FROM Employee AS e
INNER JOIN EmployeeDetail AS ed
ON e.EmpID = ed.EmpID)
SELECT project, EmpName, salary
FROM CTE
WHERE row_rank = 1;
Rishabh Mishra
12
Q9: Query to find the total count of employees joined each year
Q10: Create 3 groups based on salary col, salary less than 1L is low,
between 1 - 2L is medium and above 2L is High
SELECT EmpName, Salary,
CASE
WHEN Salary > 200000 THEN 'High'
WHEN Salary >= 100000 AND Salary <= 200000 THEN
'Medium'
ELSE 'Low'
END AS SalaryStatus
FROM Employee
Rishabh Mishra
13
BONUS: Query to pivot the data in the Employee table and retrieve the
total salary for each city.
The result should display the EmpID, EmpName, and separate columns for
each city (Mathura, Pune, Delhi), containing the corresponding total salary.
SELECT
EmpID,
EmpName,
SUM(CASE WHEN City = 'Mathura' THEN Salary END) AS "Mathura",
SUM(CASE WHEN City = 'Pune' THEN Salary END) AS "Pune",
SUM(CASE WHEN City = 'Delhi' THEN Salary END) AS "Delhi"
FROM Employee
GROUP BY EmpID, EmpName;
Rishabh Mishra
14
YouTube: https://www.youtube.com/@RishabhMishraOfficial
Instagram: https://www.instagram.com/rishabhnmishra/
LinkedIn: https://www.linkedin.com/in/rishabhnmishra/