In this tutorial I am representing some sql queries with answer. It will help all to improve your sql skills. These SQL Queries categorized into two part. In first part i have discussed basic sql queries with answers. in Second part i have discussed nested queries.
For this tutorial i am using following tables:-
Table Name:- Employee
Empid
EmpName
Department
ContactNo
EmailId
EmpHeadId
101
Isha
E-101
1234567890
isha@gmail.com
105
102
Priya
E-104
1234567890
priya@yahoo.com
103
103
Neha
E-101
1234567890
neha@gmail.com
101
104
Rahul
E-102
1234567890
rahul@yahoo.com
105
105
Abhishek
E-101
1234567890
abhishek@gmail.com
102
Schema:-
create table employee(empid int primary key,empname varchar(100), department varchar(50),contactno bigint, emaildid varchar(100), empheadid int)
1. Select the detail of the employee whose name start with P.
select * from employee where empname like 'p%'
output:-
2. How many permanent candidate take salary more than 5000.
select count(salary) as count from empsalary where ispermanent='yes' and salary>5000
output:-
3. Select the detail of employee whose emailId is in gmail.
select * from employee where emaildid like '%@gmail.com'
output:-
4. Select the details of the employee who work either for department E-104 or E-102.
select * from employee where department='E-102' or department='E-104'
or
select * from employee where department in ('E-102','E-104')
output:-
5. What is the department name for DeptID E-102?
select deptname from empdept where deptid ='E-102'
output:-
6. What is total salarythat is paid to permanent employees?
select sum(salary) as salary from empsalary where ispermanent='yes'
output:-
7. List name of all employees whose name ends with a.
select * from employee where empname like '%a'
output:-
8. List the number of department of employees in each project.
select count(empid) as employee, projectid from empproject group by projectid
output:-
9. How many project started in year 2010.
select count(projectid) as project from empproject where startyear=2010
output:-
10. How many project started and finished in the same year.
select count(projectid) as project from empproject where startyear=endyear
output:-
11. select the name of the employee whose name's 3rd charactor is 'h'.
select * from employee where empname like '__h%'
output:-
Nested Queries
1. Select the department name of the company which is assigned to the employee whose employee id is grater 103.
select deptname from empdept where deptid in (select department from employee where empid>103)
output:-
2. Select the name of the employee who is working under Abhishek.
select empname from employee where empheadid =(select empid from employee where empname='abhishek')
output:-
3. Select the name of the employee who is department head of HR.
select empname from employee where empid =(select depthead from empdept where deptname='hr')
output:-
4. Select the name of the employee head who is permanent.
select empname from employee where empid in(select empheadid from employee) and empid in(select empid from empsalary where ispermanent='yes')
output:-
5. Select the name and email of the Dept Head who is not Permanent.
select empname, emaildid from employee where empid in(select depthead from empdept ) and empid in(select empid from empsalary where ispermanent='no')
output:-
6. Select the employee whose department off is monday
select * from employee where department in(select deptid from empdept where dept_off='monday')
output:-
7. select the indian clinets details.
select * from clienttable where cid in(select cid from country where cname='india')
output:-
8. select the details of all employee working in development department.
select * from employee where department in(select deptid from empdept where deptname='development')
output:-