Hello Friends! In this post, we will discuss most common SQL Query interview questions and answers.
Consider the below tables for reference while practice the SQL queries.
Table of Contents
SQL Query interview questions and answers-
Q1. Write a SQL query to fetch the EmpId and Name of all the employees working under Manager with Id- 555.
Select EmpId, Name from Employee WHERE ManagerId = 555;
Q2. Write an SQL query to fetch the different Project available from the salary table.
Select DISTINCT(Project) from Salary;
Q3. Write an SQL query to fetch the count of employees working in project ‘P1’
Select COUNT(*) FROM Salary WHERE Project=’P1’;
Q4. Write an SQL query to find the maximum, minimum and average salary of the Employees.
SELECT Max(Salary), Min(Salary), AVG(Salary) from Salary;
Q5. Write an SQL query to find the employee id whose salary lies in the range of 10000 and 40000.
Select EmpId, Salary FROM Salary WHERE Salary BETWEEN 10000 AND 40000;
Q6. Write an SQL query to fetch those employees who live in Pune and work under manager with ManagerId – 444.
SELECT EmpId, City, ManagerId from Employees WHERE City =’Pune’ AND ManagerId = ‘444’;
Q7. Write an SQL query to fetch all employees who either live in Pune or work under manager with ManagerId – 444.
SELECT EmpId, City, ManagerId from Employees WHERE City =’Pune’ OR ManagerId = ‘444’;
Q8. Write an SQL query to fetch all employees who work on Project other than P1.
Select EmpId From Salary WHERE NOT Project =’P1’;
Select EmpId From WHERE project <> ‘P1’;
Q9. Write an SQL query to display the total salary of each employee adding the salary with variable value.
Select EmpId, Salary+Variable as TotalSalary From Salary;
Q10. Write an SQL query to fetch the employees whose name begins with any two characters, followed by a text ‘ee’ and ending with any sequences of characters.
Select Name from Employees where Name LIKE ‘__ee%’;
Tricky SQL queries for Interview
Q11. List out the employees who are not receiving commission.
Select * from employee where COMM in NULL;
Q12. List out the employees who are working in department 10 and draw the salaries more than 3500.
Select * from employee where department_id=10 and salary >3500;
Q13. List out the employee id, name in descending order based on salary column.
Select employee_id, last_name, salary from employee order by salary desc;
Q14. How many employees who are working in different department wise in the organization.
Select department_id, count(*) from employee group by department_id;
Q15. List out department id having at least 3 employees.
Select department_id, count(*), from employee group by department _id having count(*)>=3;
Q16. Display the employee who got the maximum salary.
Select * from employee where salary=(select max(salary) from employee);
Q17. Display the employees who are working in sales department.
Select * from employee where department_id IN (select department_Id from department where name=’SALES’);
Q18. Display the employee who are working in ‘New York’
Select * from employee where department_id=(select department_id from department where location_id=(select location_id from location where regional_group=’New York’));
Q19. Update the employees salary who are working as Manager on the basis of 10%
Update employee set salary =salary*10/100 where job_id=(select job_id from job where function=’MANAGER’)
Q20. Delete the employee who are working in accounting department.
Delete from employee where department_id=(select department_id from department where name=’ACCOUNTING’)
Q21. Display the highest salary from employee table.
We can find this in 3 ways-
- Using max
- Using limit
- Using top
Using max function:-
select max(salary) from employee;
Using limit function:
First we need to find descending order of salary then apply limit on this.
select salary from employee order by salary desc limit 1;
Using top function:
top function only used in MS sql server
select top 1 salary from employee order by salary desc;
Q23. Display 2nd highest salary from employee table.
Using max finction:-
select max(salary) from employee < (select max(salary) from employee);
Using limit function:-
select salary from (select salary from employee order by salary desc limit 2) emp order by salary limit 1;
Using top function:-
select salary from (select top 2 salary from employee order by salary desc) emp order by salary limit 1;
I hope you have enjoyed this post “SQL Query interview questions and answers”
Happy Learning 🙂