SQL INTERVIEW QUESTIONS
1. What is the order of execution of SQL queries?
Ans. The correct order of execution in SQL is FROM, WHERE , GROUP BY,
HAVING, SELECT, DISTINCT, ORDER BY and LIMIT.
1. Getting Data (FROM/JOIN)
2. Row Filter (WHERE)
3. Grouping (GROUP BY)
4. Group Filter (HAVING)
5. Return Expression (SELECT)
6. Order & Paging (ORDER BY & LIMIT/OFFSET)
2. What is the difference between union and union all?
Ans. UNION removes duplicate rows from the combined result set, whereas
UNION ALL includes all rows from each query without removing duplicates.
3. What are the different types of joins?
Ans. INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, are common types of
joins used to combine rows from multiple tables based on specified
conditions or relationships.
4. If one table has 10 rows and another has 20 rows what is the
minimum and maximum number of rows we get after joining
both the tables?
Ans. The minimum number of rows we get after joining both the tables will
be 0. The minimum number of rows will be 0 if there are no matching rows
based on the join condition between Table A and Table B.
The maximum number of rows we get after joining both the tables will be 30.
The maximum number of rows will be 30, which is the sum of rows from
Table A (10 rows) and Table B (20 rows), because a full join includes all rows
from both tables.
5. Find the 3rd highest salary of employee based on department.
Ans. Select DEPARTMENT, SALARY
From EMPLOYEES
Where DEPARTMENT= xyz department
Group by DEPARTMENT
Order by salary DESC
Limit 3;
6. There is a table with employee ID, employee name, manager
ID. Write a query to find the manager’s name and no of
reporters.
Ans. SELECT manager_name , COUNT(employee_ID) as number of reporters
FROM employees
INNER JOIN employees ON tablename_manager_id = tablename_manager_id
GROUP BY manager_name;