You are on page 1of 9

ddl commands:

create table employee


(
ssn int,
name varchar(15),
address varchar(15),
sex varchar(2),
salary float,
superssn int,
dno int,
primary key(ssn)
);

create table department


(
dno int,
dname varchar(15),
mgrssn int,
mgrstartdate date,
primary key(dno)
);

create table dlocation


(
dno int,
dloc varchar(15),
primary key(dno,dloc),
foreign key(dno) references department(dno) on delete cascade
);

create table project


(
pno int,
pname varchar(10),
plocation varchar(15),
dno int,
primary key(pno),
foreign key(dno) references department(dno) on delete cascade
);

create table works_on


(ssn int,
pno int,
hours float,
primary key(ssn,pno),
foreign key(ssn) references employee(ssn) on delete cascade,
foreign key(pno) references project(pno) on delete cascade
);

dml statements:

inserting values into employee table:

insert into employee values(12345,'scott','housten,tx','m',300000,33344,5);


insert into employee values(33344,'franklin','housten,tx','m',400000,88866,5);
insert into employee values(99988,'alicia','spring,tx','f',250000,98765,4);
insert into employee values(98765,'jennifer','bellaire,tx','f',430000,88866,4);
insert into employee values(66688,'ramesh','humble oak,tx','m',380000,33344,5);
insert into employee values(45345,'joyce','houston,tx','f',250000,33344,5);
insert into employee values(98798,'ahmad','bellaire,tx','m',250000,98765,4);
insert into employee values(88866,'scott','houston,tx','m',550000, null,1);
insert into employee values(66677,'william','houston,tx','m',650000, 33344,5);
insert into employee values(22233,'daniel','spring,tx','m',650000, 88866,5);
inserting values into department table:

insert into department values(5,'research',33344,'1988-05-22');


insert into department values(4,'administration',98765,'1995-01-01');
insert into department values(1,'headquaters',88866,'1981-06-19');

ddl statements to alter the table employee and department due to cross references:

alter table employee add foreign key(superssn) references employee (ssn);

alter table employee add foreign key(dno) references department (dno);

alter table department add foreign key(mgrssn) references employee (ssn);

dml statements:

inserting values into dlocation table:

insert into dlocation values(1,'houston');


insert into dlocation values(4,'stafford');
insert into dlocation values(5,'bellaire');
insert into dlocation values(5,'sugarland');
insert into dlocation values(5,'houston');

inserting values into project table:

insert into project values(1,'productx','bellaire',5);


insert into project values(2,'producty','sugarland',5);
insert into project values(3,'productz','houston',5);
insert into project values(10,'computer','stafford',4);
insert into project values(20,'reorganize','houston',1);
insert into project values(30,'iot','stafford',4);

inserting values into works_on table:

insert into works_on values(12345,1,32.5);


insert into works_on values(12345,2,7.5);
insert into works_on values(66688,3,40.0);
insert into works_on values(45345,1,20.0);
insert into works_on values(45345,2,20.0);
insert into works_on values(33344,2,10.0);
insert into works_on values(33344,3,10.0);
insert into works_on values(33344,10,10.0);
insert into works_on values(33344,20,10.0);
insert into works_on values(99988,30,30.0);
insert into works_on values(99988,10,10.0);
insert into works_on values(98798,10,35.0);
insert into works_on values(98798,30,5.0);
insert into works_on values(98765,30,20.0);
insert into works_on values(98765,20,15.0);
insert into works_on values(88866,20,null);
insert into works_on values(12345,3,15.0)

sql> select * from employee;

s
dn
ssn name address e salary superssn
o
1234 m 30000
scott housten,tx 33344 5
5 0
3334 frankli m 40000
housten,tx 88866 5
4 n 0
9998 25000
alicia spring,tx f 98765 4
8 0
9876 jennife 43000
bellaire,tx f 88866 4
5 r 0
6668 m 38000
ramesh humble oak,tx 33344 5
8 0
4534 25000
joyce houston,tx f 33344 5
5 0
9879 m 25000
ahmad bellaire,tx 98765 4
8 0
8886 m 55000
scott houston,tx 1
6 0
6667 willia m 65000
houston,tx 33344 5
7 m 0
2223 m 65000
daniel spring,tx 88866 5
3 0

sql> select * from department;

dname mgrssn mgrstartd


dno
5 research 33344 22-may-88
4 administration 98765 01-jan-95
1 headquaters 88866 19-jun-81
sql> select * from dlocation;

dloc
dno
1 houston
4 stafford
5 bellaire
5 sugarland
5 houston

sql> select * from works_on;

pno hours
ssn
12345 1 32.5
12345 2 7.5
66688 3 40
45345 1 20
45345 2 20
33344 2 10
33344 3 10
33344 10 10
33344 20 10
99988 30 30
99988 10 10
98798 10 35
98798 30 5
98765 30 20
98765 20 15
88866 20
12345 3 15

sql> select * from project;

pno pname plocation dno


1 productx bellaire 5
2 producty sugarland 5
3 productz houston 5
30 iot stafford 4
10 computer stafford 4
20 reorganize houston 1
queries:
make a list of all project numbers for projects that involve an employee whose last name
is ‘scott’, either as a worker or as a manager of the department that controls the
project.

(select distinct pno


from project p, department d, employee e where p.dno=d.dno
and d.mgrssn=e.ssn
and e.name='scott')
union
(select distinct p1.pno
from project p1, works_on w, employee e1 where p1.pno=w.pno
and e1.ssn=w.ssn
and e1.name='scott');

output:

pno
1
2
3
20

show the resulting salaries if every employee working on the ‘iot’ project is given a 10
percent raise.

select e.ssn,name,((salary*0.10)+salary) as new_salary


from employee e,works_on w
where e.ssn=w.ssn and pno=(select pno from project where pname='iot');
or

output:

ssn name new_salary


99988 alicia 27500
98765 jennifer 47300
98798 ahmad 27500
find the sum of the salaries of all employees of the ‘accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department

select sum(salary),max(salary),min(salary),avg(salary)
from employee e,department d
where e.dno=d.dno and dname='research';
or
select sum(salary),max(salary),min(salary),avg(salary)
from employee natural join department
where dname='research';

output:

max(salary) min(salary) avg(salary)


sum(salary)
133000 40000 25000 33250

retrieve the name of each employee who works on all the projects controlledby
department number 5 (use not exists operator).

select e.name
from employee e
where not exists(select pno
from project
where dno=5 and pno not in
(select pno
from works_on
where e.ssn=ssn));
output:

name
scott

for each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than rs. 6,00,000.

select d.dno, count(*)


from department d, employee e
where d.dno=e.dno
and e.salary>600000
and d.dno in (select e1.dno
from employee e1
group by e1.dno
having count (*)>5)
group by d.dno;
output:

count(*)
dno
5 2

You might also like