Mod 4 Queries
Mod 4 Queries
Table Creation;
create table department (dno varchar(10) primary key, dname varchar(20), mgrstartdate
date);
create table employee (ssn varchar(10) primary key, fname varchar(10),lname varchar(10),
addr varchar(10), sex char (1),salary decimal(10,2)superssn varchar(10), dno varchar(10)
references department (dno));
create table project (pno integer primary key, pname varchar(20),ploc varchar(20), dno
varchar(10) references department (dno));
insert into workson values (4000,900,10); insert into workson values (4010,902,5); insert
into workson values (4001,900,0); insert into workson values (4002,900,9); insert into
workson values (4011,902,8); insert into workson values (4012,902,5);
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
Update employee set dno=101 where ssn=4000;
Update employee set dno=102 where ssn=4010;
Queries:
1) Display all the details of all employees working in the company.
select * from employee;
3) Retrieve the birthdate and address of the employee whose name is'Franklin T.Wong'
select bdate,address from employee where fname="Franklin" and mname="T" and
lname="Wong";
11)Retrieve the name and address of all employees who work for Research department
12) For every project located in 'Stafford', list the project
number,the controlling department number, and the department
manager's lastname, address, and birthdate
select p.pnumber,p.dnum,e.lname,e.address,e.bdate
from project p, department d, employee e
where p.plocation="Stafford" and p.dnum= d.dnumber and d.mgrssn=e.ssn;
13) : For each employee, retrieve the employee's name, and the name of
his or her immediate supervisor
select e.fname,e.lname,s.fname,s.lname
from employee as e, employee as s
where s.superssn=e.ssn;
15) Make a list of all project numbers for projects that involve an
employee whose last name is 'Narayan’ either as a worker or as a
manager of the department that controls the project
union
17) Retrieve a list of employees and the project name each works in,
ordered by the employee's department, and within each department
ordered alphabetically by employee first name
select dname,lname,fname,pname
from department,employee,works_on,project
where dnumber=dno and ssn=essn and pno=pnumber
order by dname,lname,fname;
18) Select the names of employees whose salary does not match with
salary of any employee in department 10
select fname
from employee
where salary > all(select salary from employee where dno=5);
19) Retrieve the name of each employee who has a dependent with the
same first name and same sex as the employee
select e.fname,e.lname
from employee as e
where e.ssn in (select essn from dependent where
e.fname=dependent_name and e.sex=sex);
20) Retrieve the employee numbers of all employees who work on project
located in Bellaire, Houston, or Stafford
select ssn
from employee
where ((select pno
from works_on
where ssn=essn) contains
(select pnumber
from project
where dnum=5));
21) Find the sum of the salaries of all employees, the maximum
salary, the minimum salary, and the average salary. Display with
proper headings
select sum(salary),max(salary),min(salary),avg(salary)
from employee;
22) Find the sum of the salaries and number of employees of all
employees of the ‘Marketing’ department, as well as the maximum
salary, the minimum salary, and the average salary in this department
select sum(salary),count(*)
from employee, department
where dname like "market%";
23) Select the names of employees whose salary is greater than the
average salary of all employees in department 10
select fname
from employee
where dno=10
group by salary
having salary>avg(salary);
24) For each department, retrieve the department number, the number of
employees in the department, and their average salary
select dno,count(*),avg(salary)
from employeegroup by dno;
25) For each project, retrieve the project number, the project name,
and the number of employees who work on that project
select pnumber,pname,count(*)
from project
group by pnumber;
26) Change the location and controlling department number for all
projects having more than 5 employees to ‘Bellaire’ and 6 respectively
update project
set plocation="Bellaire", dnum=6
where (select count(essn)
from works_on
where pno=pnumber)>5;
27) : For each department having more than 10 employees, retrieve the
department no, no of employees drawing more than 40,000 as salary
select dno
from employee
where salary>40000
group by dno
having count(*)>10;
30) Delete an employee from Employee table with ssn = ‘12345’( make
sure that this employee has some dependents, is working on some
project, is a manager of some department and is supervising some
employees). Check and display the cascading effect on Dependent and
Works on table. In Department table MGRSSN should be set to default
value and in Employee table SUPERSSN should be set to NULL
delete from employee
where ssn=1234567891 cascade****;