Hi Friends,
In this post, I'm going to share SQL interview questions that have been asked in multiple interviews.
Table - EmployeeDetails
EmpId
|
FullName
|
ManagerId
|
DateOfJoining
|
121
|
John Smith
|
322
|
01/10/2010
|
322
|
James
|
988
|
02/09/2009
|
421
|
Bill Gates
|
122
|
03/08/2008
|
Table - EmployeeSalary
EmpId
|
Project
|
Salary
|
121
|
P1
|
10000
|
322
|
P2
|
11000
|
421
|
P3
|
12000
|
Question 1:
Write a SQL query to fetch the count of employees working in project P1.
Answer:
Select count(*) from EmployeeSalary where project = 'p1';
Explanation:
The count(*) function returns the total number of records matching the specified project.
Question 2:
Write a SQL query to fetch employee names having salary greater than or equal to 5000 and less than or equal to 10000.
Answer:
Select FullName
from EmployeeDetails ed
where EmpId in
(Select EmpId from EmployeeSalary
where Salary between 5000 AND 10000);
The BETWEEN operator checks whether the salary falls within the specified range.
Both boundary values (5000 and 10000) are included.
Question 3:
Write a SQL query to fetch project-wise count of employees sorted by project's count in descending order.
Answer:
Select Project, count(EmpId) as count
from EmployeeSalary
Group By project
Order by count DESC;
Explanation:
GROUP BY groups employees according to their projects, while COUNT() calculates the number of employees in each project. ORDER BY DESC sorts the results in descending order.
Question 4:
Write a query to fetch only the first name from the FullName column of EmployeeDetails table.
Answer:
Select substring(FullName, 0, CharIndex(' ',FullName)) from EmployeeDetails;
Explanation:
CHARINDEX() finds the position of the first space in the full name. LEFT() extracts the characters before that space.
Question 5:
Write a query to fetch employee names and salary records. Return employee details even if the salary record is not present for the employee.
Answer:
Select ED.FullName, ES.salary
from EmployeeDetails ED
left join EmployeeSalary ES
ON ED.EmpId = ES.EmpId;
Explanation:
Write a query to fetch employee names and salary records. Return employee details even if the salary record is not present for the employee.
Answer:
Select ED.FullName, ES.salary
from EmployeeDetails ED
left join EmployeeSalary ES
ON ED.EmpId = ES.EmpId;
Explanation:
A LEFT JOIN returns all records from the left table (EmployeeDetails) , along with matching records from the right table (EmployeeSalary).
Question 6:
Write a SQL query to fetch all the employees who are also managers from EmployeeDetails table.
Answer:
Select Distinct ED.FullName
from EmployeeDetails ED
INNER JOIN EmployeeDetails M
ON ED.EmpId = M.ManagerId;
Explanation: This is an example of a self-join , where the same table is joined to itself.
Write a SQL query to fetch all the employees who are also managers from EmployeeDetails table.
Answer:
Select Distinct ED.FullName
from EmployeeDetails ED
INNER JOIN EmployeeDetails M
ON ED.EmpId = M.ManagerId;
Explanation: This is an example of a self-join , where the same table is joined to itself.
The query compares an employee's ID with the ManagerId of other employees. If they match, that employee is a manager.
Question 7:
Write a SQL query to fetch all employee records from EmployeeDetails table who have a salary record in EmployeeSalary table.
Answer:
Select * from EmployeeDetails ED
where EXISTS
(Select * from EmployeeSalary S where ED.EmpId = S.EmpId);
Write a SQL query to fetch all employee records from EmployeeDetails table who have a salary record in EmployeeSalary table.
Answer:
Select * from EmployeeDetails ED
where EXISTS
(Select * from EmployeeSalary S where ED.EmpId = S.EmpId);
Explanation:
The EXISTS operator checks whether the subquery returns atleast one matching record.
Only employees having a corresponding record in the EmployeeSalary table are returned.
Question 8:
Write a SQL query to fetch duplicate records from a table.
Answer:
Select FullName, EmpId, Count(*)
from EmployeeDetails
Group By EmpId, FullName
Having Count(*) > 1;
Explanation:
Write a SQL query to fetch duplicate records from a table.
Answer:
Select FullName, EmpId, Count(*)
from EmployeeDetails
Group By EmpId, FullName
Having Count(*) > 1;
Explanation:
GROUP BY groups records having the same Employee ID and name.
The HAVING clause filters these groups and returns only those appearing more than once.
Question 9:
Write a SQL query to remove duplicates from a table without using temporary table.
Answer:
Delete from EmployeeDetails
where EmpId IN
(Select EmpId from EmployeeDetails
Group By FullName
Having count(*) > 1);
Write a SQL query to remove duplicates from a table without using temporary table.
Answer:
Delete from EmployeeDetails
where EmpId IN
(Select EmpId from EmployeeDetails
Group By FullName
Having count(*) > 1);
Explanation:
The ROW_NUMBER() function assigns a sequential number to records within each group of duplicate records.
PARTITION BY defines which column determine whether records are duplicate.
The first record receives row number 1, while subsequent duplicate records receive higher numbers. The DELETE statement removes records having a row number greater than 1.
Question 10:
Write a SQL query to create a new table with data and structure copied from another table.
Answer:
Select * INTO newTable from oldTable;
Write a SQL query to create a new table with data and structure copied from another table.
Answer:
Select * INTO newTable from oldTable;
Explanation:
The SELECT INTO statement creates a new table using the structure and data returned by the select query. This syntax is supported by SQL server.
Question 11:
Write a SQL query to create an empty table with same structure as some other table.
Answer:
Select *INTO newTable from oldTable where 1 = 0;
Write a SQL query to create an empty table with same structure as some other table.
Answer:
Select *INTO newTable from oldTable where 1 = 0;
Explanation:
The WHERE 1 = 0 condition always evaluates to false. Therefore, no records are copied, but SQL server creates the new table with the columns and data types of the original table.
Question 12:
Write a SQL query to fetch common records between two tables.
Answer:
Select * from EmployeeDetails
INTERSECT
Select * from EmployeeSalary
Write a SQL query to fetch common records between two tables.
Answer:
Select * from EmployeeDetails
INTERSECT
Select * from EmployeeSalary
Explanation:
The INTERSECT operator returns records that are common to both SELECT statements.
In this example, it returns employee IDs that exist in both EmployeeDetails and EmployeeSalary tables.
Question 13:
Write a SQL query to fetch records that are present in one table but not in another table.
Answer:
Select * from EmployeeDetails
EXCEPT
Select * from EmoloyeeDetails
Write a SQL query to fetch records that are present in one table but not in another table.
Answer:
Select * from EmployeeDetails
EXCEPT
Select * from EmoloyeeDetails
Explanation:
The EXCEPT operator returns records from the first SELECT statement that are not present in the second SELECT statement.
In this example, it returns employee IDs that exist in EmployeeDetails but do not exist in EmployeeSalary table.
Note: SQL Server uses EXCEPT, Whereas Oracle uses MINUS for the same operation.