Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

SQL

The document contains 16 SQL queries: 1) Retrieves names and number of female dependents for all department managers. 2) Retrieves names and number of dependents for managers with more than one dependent. 3) Retrieves project names and employee counts for projects with more than one employee.

Uploaded by

Saumyajit
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

SQL

The document contains 16 SQL queries: 1) Retrieves names and number of female dependents for all department managers. 2) Retrieves names and number of dependents for managers with more than one dependent. 3) Retrieves project names and employee counts for projects with more than one employee.

Uploaded by

Saumyajit
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Queries:

--1) Retrieve names of all managers of the department along with their number of
female dependents.

select fname,mname,lname,count(*) as No_Of_Female_Dependents


from Employee,Dependent where
ssn in (select mgrssn
from Department) and ssn=essn and Dependent.sex='f'
group by ssn,fname,mname,lname;

--2) Retrieve names of all managers of the department who have more than one
dependent along with their number of dependents.

select fname,mname,lname,count(*) as No_Of_Dependents


from Employee,Dependent where
ssn in (select mgrssn
from Department) and ssn=essn
group by ssn,fname,mname,lname
having count(*)>1;

--3) For each project for which more than one employee works retrieve its name and
number of employee working.

select pname,count(*) as No_Of_Employees


from Project,Workson
where pnum=pno
group by pname
having count(*)>1;

--4) Make a list of employee name whose address is in Vidyanagar and who works on
project productx along with number of dependents.

select fname,mname,lname,count(depname) as No_Of_Dependents


from Project
inner join Workson
on pnum=pno and pname='Productx'
inner join Employee
on ssn=Workson.essn and addr like '%Vidyanagar%'
left join Dependent
on ssn=Dependent.essn
group by ssn,fname,mname,lname
order by ssn;

--5) Retrieve names of all employee who work for the department that controls more
than one project.

select fname,mname,lname
from Employee,Department,Project
where dnum=dno and dnum=deptno
group by Dnum,fname,mname,lname
having count(*)>1;

--6) Retrieve ssn, name and dept. name of all the employees who work on any project
controlled by his own department.

select distinct ssn,fname,mname,lname,dname


from Employee,Department,Project,Workson
where dnum=dno and dnum=deptno and pnum=pno and ssn=essn
order by ssn;

--7) For each department that has more than one location retrieve its name and
number of project controlled by that department.

select dname,count(distinct pnum)


from Department,Dept_loc,Project
where Department.dnum=Dept_loc.dnum and Department.dnum=deptno
group by dname
having count(distinct loc)>1;

--8) For each project controlled by Research department for which more than one
employee is working Retrieve name of that project and number of employees working
for it.

select pname,count(ssn) as No_Of_Employees


from Department,Project,Workson,Employee
where dnum=deptno and dname='Research' and pnum=pno and ssn=essn
group by pname
having count(ssn)>1;

--10) For each department which controls more than one project retrieve department
name along with number of employees working in that department.

select dname,count(distinct ssn) as No_Of_Employees


from Department,Project,Employee
where dnum=deptno and dnum=deptno and dnum=dno
group by dname
having count(distinct pnum)>1;

--11) For each department which do not control any project retrieve department name
along with number of employees working in that department.

select dname,count(*) as No_Of_Employees


from Department,Employee
where dnum not in (select deptno
from Project) and dnum=dno
group by dname;

--12) For each female employee who works for ‘Admin’ department retrieve first name
in ascending order along with number of projects the employee works on.

select fname,count(*)
from Employee,Department,Workson
where dnum=dno and ssn=essn and dname='Admin' and Employee.sex='f'
group by ssn,fname
order by count(*);

--13) Retrieve names of all employees who work on at least one project controlled
by ‘Head quarters’ department.

select fname,mname,lname
from Employee,Project,Department,Workson
where dnum=deptno and ssn=essn and pnum=pno and dname='Head quarters';

--14) Retrieve name and ssn of all employees born after 1950 and having no
dependents.

select fname,mname,lname,ssn
from Employee
where ssn not in (select essn
from Dependent) and extract(year from dob) > 1950
order by ssn;

--15) Retrieve names of all department managers who have at least one dependent.

select fname,mname,lname
from Employee,Dependent
where ssn in (select mgrssn
from Department) and ssn=essn
group by ssn,fname,mname,lname;

--16) Retrieve names of all ‘CS’ department employees who have more than one female
dependents.

select fname,mname,lname
from Employee,Department,Dependent
where dnum=dno and ssn=essn and dname='CS' and Dependent.sex='f'
group by ssn,fname,mname,lname
having count(*)>1;

You might also like