Top 40 SQL Query Interview Questions and Answers for Practice
Top 40 SQL Query Interview Questions and Answers for Practice
Interview Questions
and Answers for
Practice
Table – Employee Details
421 P1 12000 0
1. Write an SQL query to fetch the EmpId and FullName of all the
employees working under the Manager with id – ‘986’.
We can use the EmployeeDetails table to fetch the employee details with a
where clause for the manager-
FROM EmployeeDetails
2. Write an SQL query to fetch the different projects available from the
EmployeeSalary table.
While referring to the EmployeeSalary table, we can see that this table
contains project values corresponding to each employee, or we can say
that we will have duplicate project values while selecting Project values
from this table.
So, we will use the distinct clause to get the unique values of the Project.
SELECT DISTINCT(Project)
FROM EmployeeSalary;
SELECT COUNT(*)
FROM EmployeeSalary
WHERE Project = 'P1';
SELECT Max(Salary),
Min(Salary),
AVG(Salary)
FROM EmployeeSalary;
5. Write an SQL query to find the employee id whose salary lies in the
range of 9000 and 15000.
Here, we can use the ‘Between’ operator with a where clause.
FROM EmployeeSalary
FROM EmployeeDetails
7. Write an SQL query to fetch all the employees who either live in
California or work under a manager with ManagerId – 321.
This interview question requires us to satisfy either of the conditions –
employees living in ‘California’ and working under Manager with ManagerId
– 321. So, we will use the OR operator here-
FROM EmployeeDetails
SELECT EmpId
FROM EmployeeSalary
SELECT EmpId
FROM EmployeeSalary
For the difference between NOT and <> SQL operators, check this link
– Difference between the NOT and != operators.
SELECT EmpId,
Salary+Variable as TotalSalary
FROM EmployeeSalary;
10. Write an SQL query to fetch the employees whose name begins
with any two characters, followed by a text “hn” and ends with any
sequence of characters.
For this question, we can create an SQL query using like operator with ‘_’
and ‘%’ wild card characters, where ‘_’ matches a single character and ‘%’
matches ‘0 or multiple characters.
SELECT FullName
FROM EmployeeDetails
11. Write an SQL query to fetch all the EmpIds which are present in
either of the tables – ‘EmployeeDetails’ and ‘EmployeeSalary’.
In order to get unique employee ids from both tables, we can use the Union
clause which can combine the results of the two SQL queries and return
unique rows.
UNION
12. Write an SQL query to fetch common records between two tables.
SQL Server – Using INTERSECT operator-
INTERSECT
FROM EmployeeSalary
WHERE EmpId IN
13. Write an SQL query to fetch records that are present in one table
but not in another table.
SQL Server – Using MINUS- operator-
MINUS
SELECT EmployeeSalary.*
FROM EmployeeSalary
LEFT JOIN
14. Write an SQL query to fetch the EmpIds that are present in both
the tables – ‘EmployeeDetails’ and ‘EmployeeSalary.
Using subquery-
EmployeeDetails
where EmpId IN
(SELECT EmpId FROM EmployeeSalary);
15. Write an SQL query to fetch the EmpIds that are present in
EmployeeDetails but not in EmployeeSalary.
Using subquery-a
EmployeeDetails
16. Write an SQL query to fetch the employee’s full names and replace
the space with ‘-’.
Using the ‘Replace’ function-
FROM EmployeeDetails;
FROM EmployeeDetails;
18. Write an SQL query to display both the EmpId and ManagerId
together.
Here we can use the CONCAT command.
FROM EmployeeDetails;
19. Write a query to fetch only the first name(string before space)
from the FullName column of the EmployeeDetails table.
In this question, we are required to first fetch the location of the space
character in the FullName field and then extract the first name out of the
FullName field.
For finding the location we will use the LOCATE method in MySQL and
CHARINDEX in SQL SERVER and for fetching the string before space, we
will use the SUBSTRING OR MID method.
FROM EmployeeDetails;
FROM EmployeeDetails;
20. Write an SQL query to uppercase the name of the employee and
lowercase the city values.
We can use SQL Upper and Lower functions to achieve the intended
results.
FROM EmployeeDetails;
21. Write an SQL query to find the count of the total occurrences of a
particular character – ‘n’ in the FullName field.
Here, we can use the ‘Length’ function. We can subtract the total length of
the FullName field from the length of the FullName after replacing the
character – ‘n’.
SELECT FullName,
LENGTH(FullName) - LENGTH(REPLACE(FullName, 'n', ''))
FROM EmployeeDetails;
UPDATE EmployeeDetails
23. Fetch all the employees who are not working on any project.
This is one of the very basic interview questions in which the interviewer
wants to see if the person knows about the commonly used – Is NULL
operator.
SELECT EmpId
FROM EmployeeSalary
SELECT FullName
FROM EmployeeDetails
WHERE EmpId IN
SELECT NOW();
SQL Server-
SELECT getdate();
Oracle-
26. Write an SQL query to fetch all the Employee details from
the EmployeeDetails table who joined in the Year 2020.
Using BETWEEN for the date range ’01-01-2020′ AND ’31-12-2020′-
AND '2020/12/31';
Also, we can extract the year part from the joining date (using YEAR in
MySQL)-
27. Write an SQL query to fetch all employee records from the
EmployeeDetails table who have a salary record in the
EmployeeSalary table.
Using ‘Exists’-
SELECT * FROM EmployeeDetails E
WHERE EXISTS
For project-wise count, we will be using the GROUP BY clause and for
sorting, we will use the ORDER BY clause on the alias of the project count.
FROM EmployeeSalary
GROUP BY Project
Here, we can use the left join with the EmployeeDetail table on the left side
of the EmployeeSalary table.
FROM EmployeeDetails E
LEFT JOIN
EmployeeSalary S
ON E.EmpId = S.EmpId;
FROM TableA
For more questions on SQL Joins, you can also check our top SQL Joins
Interview Questions.
Advertisement
Here is a list of some of the most frequently asked SQL query interview
questions for experienced professionals. These questions cover SQL
queries on advanced SQL JOIN concepts, fetching duplicate rows, odd and
even rows, nth highest salary, etc.
31. Write an SQL query to fetch all the Employees who are also
managers from the EmployeeDetails table.
Here, we have to use Self-Join as the requirement wants us to analyze the
EmployeeDetails table as two tables. We will use different aliases ‘E’ and
‘M’ for the same EmployeeDetails table.
FROM EmployeeDetails E
ON E.EmpID = M.ManagerID;
FROM EmployeeDetails
34. Write an SQL query to fetch only odd rows from the table.
In case we have an auto-increment field e.g. EmpId then we can simply
use the below query-
In case we don’t have such a field then we can use the below queries.
Using Row_number in SQL server and checking that the remainder when
divided by 2 is 1-
FROM (
FROM EmployeeSalary
)E
WHERE E.RowNumber % 2 = 1;
SELECT *
FROM (
FROM EmployeeSalary
JOIN (SELECT @rowNumber:= 0) r
)t
WHERE rn % 2 = 1;
35. Write an SQL query to fetch only even rows from the table.
In case we have an auto-increment field e.g. EmpId then we can simply
use the below query-
In case we don’t have such a field then we can use the below queries.
Using Row_number in SQL server and checking that the remainder, when
divided by 2, is 1-
FROM (
FROM EmployeeSalary
)E
WHERE E.RowNumber % 2 = 0;
SELECT *
FROM (
FROM EmployeeSalary
WHERE rn % 2 = 0;
36. Write an SQL query to create a new table with data and structure
copied from another table.
37. Write an SQL query to create an empty table with the same
structure as some other table.
Here, we can use the same query as above with the False ‘WHERE’
condition-
SELECT *
FROM EmployeeSalary
SELECT TOP N *
FROM EmployeeSalary
FROM (
FROM Employee
SELECT Salary
FROM Employee
40. Write SQL query to find the 3rd highest salary from a table without
using the TOP/limit keyword.
This is one of the most commonly asked interview questions. For this, we
will use a correlated subquery.
In order to find the 3rd highest salary, we will find the salary value until the
inner query returns a count of 2 rows having a salary greater than other
distinct salaries.
SELECT Salary
WHERE 2 = (
SELECT COUNT( DISTINCT ( Emp2.Salary ) )
SELECT Salary
WHERE N-1 = (