BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer...

10
BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015 Querying a Relational Database COMPANY database For Lab4, you use the Company database you used for Lab3 1. Update the following new changes into the database: 1) Joyce English with Ssn = 453453453 got married with Joe Anderson. 2) Jenifer Wallace with Ssn = 987654321 just had a new daughter named Erica. 3) Jenifer Wallace with Ssn = 987654321 is just assigned to a new project number ‘10’ to work on with 0 initial hours. Add these new entries into Dependent, Works_On tables in your database then Select * from Dependent and Select * from Works_On to show the updated table content. INSERT INTO dependent VALUES ('453453453','Joe','M','07-JUN-1999','Spouse'); INSERT INTO dependent VALUES ('987654321','Erica','F','07-SEP-1999','Daughter'); INSERT INTO works_on VALUES ('987654321','10', '0'); select * from dependent; select * from works_on; 2. Write SQL Select statements to retrieve data in the followings: Q1: Get SSN and the last name of married female employees who work on three or more projects --Q1 SELECT E.SSN, E.LNAME FROM EMPLOYEE E WHERE E.Sex = ‘F’ and E.SSN IN (SELECT Dp.ESSN FROM Dependent Dp WHERE Dp.relationship = 'Spouse') AND E.SSN IN (SELECT W.ESSN FROM WORKS_ON W GROUP BY W.ESSN HAVING COUNT(W.ESSN) >= 3); -- OR Q2 SELECT E.SSN, E.LNAME FROM EMPLOYEE E, dependent Dp WHERE E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND E.SSN IN (SELECT W.ESSN

Transcript of BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer...

Page 1: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015

Querying a Relational Database COMPANY database

For Lab4, you use the Company database you used for Lab3

1. Update the following new changes into the database:

1) Joyce English with Ssn = 453453453 got married with Joe Anderson.

2) Jenifer Wallace with Ssn = 987654321 just had a new daughter named Erica.

3) Jenifer Wallace with Ssn = 987654321 is just assigned to a new project number

‘10’ to work on with 0 initial hours.

Add these new entries into Dependent, Works_On tables in your database then

Select * from Dependent and Select * from Works_On to show the updated table

content. INSERT INTO dependent VALUES ('453453453','Joe','M','07-JUN-1999','Spouse'); INSERT INTO dependent VALUES ('987654321','Erica','F','07-SEP-1999','Daughter');

INSERT INTO works_on VALUES ('987654321','10', '0');

select * from dependent;

select * from works_on;

2. Write SQL Select statements to retrieve data in the followings:

Q1:

Get SSN and the last name of married female employees who work on three or more

projects --Q1 SELECT E.SSN, E.LNAME

FROM EMPLOYEE E

WHERE E.Sex = ‘F’ and E.SSN IN (SELECT Dp.ESSN

FROM Dependent Dp

WHERE Dp.relationship = 'Spouse') AND

E.SSN IN (SELECT W.ESSN

FROM WORKS_ON W GROUP BY W.ESSN

HAVING COUNT(W.ESSN) >= 3);

-- OR Q2

SELECT E.SSN, E.LNAME

FROM EMPLOYEE E, dependent Dp WHERE E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND

E.SSN IN (SELECT W.ESSN

Page 2: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

FROM WORKS_ON W

GROUP BY W.ESSN HAVING COUNT(W.ESSN) >= 3);

-- Q2 SELECT E.SSN, E.LNAME

FROM EMPLOYEE E, Dependent Dp

WHERE E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = ‘spouse’ AND

(SELECT COUNT(*) FROM WORKS_ON W

WHERE E.SSN = W.ESSN ) >= 3;

-- OR

-- Q3 SELECT E.SSN, E.LNAME

FROM EMPLOYEE E, Dependent Dp,

(SELECT ESSN

FROM WORKS_ON GROUP BY ESSN

HAVING COUNT(PNO) >= 3) AS THREEMOREPROJECT(ESSN)

WHERE E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND E.SSN = THREEMOREPROJECT.ESSN;

-- OR

-- Q4 select e.ssn, e.Lname

from EMPLOYEE e, Dependent Dp

where E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND

e.Ssn IN (select wo.Essn

from WORKS_ON wo

group by wo.Essn having count(wo.Pno) >= 3);

Page 3: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

Q1:

For each department, list all the employees who are working in the department with

the employee’s first and last name and first and last name of his or her immediate

supervisor. Include all the departments and all the employees who do not have any

supervisor. List the result in the order of each department number and first name of

each employee.

SELECT D.Dnumber, E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM DEPARTMENT D LEFT OUTER JOIN EMPLOYEE E ON D.Dnumber = E.Dno

LEFT OUTER JOIN EMPLOYEE S ON E.SuperSsn = S.SSN

Order BY D.Dnumber, E.fname;

Page 4: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

OR

If we don’t want to include non matching Department Number

Q1:

SELECT D.Dnumber, E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SuperSsn = S.SSN,

DEPARTMENT D

WHERE D.Dnumber = E.Dno Order BY D.Dnumber, E.Fname;

Page 5: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

Q3:

List the name of employees who is working for ‘Research’ department and are

married but have no children. --Married = Select ESSN From Dependent Where relationship = ‘spouse’;

--Girls = Select ESSN From Dependent Where relationship = ‘doughter’; --Boys = Select ESSN From Dependent Where relationship = ‘son’;

--INSERT INTO dependent VALUES ('453453453','John','M','07-JUN-1999','Spouse');

--INSERT INTO dependent VALUES ('987654321','Erica','F','07-SEP-1999','Daughter');

--Q1 SELECT E.fname, E.lname

FROM Employee E, Dependent D, Department DT

WHERE D.essn = E.ssn AND E.dno = DT.dnumber AND D. relationship = 'Spouse' AND DT.Dname = 'Research' AND

D.essn Not IN (SELECT D2.ESSN

FROM Dependent D2

WHERE D. essn = D2.essn and D2.relationship = 'Daughter') AND

D.essn NOT IN (SELECT D3.ESSN

FROM Dependent D3 WHERE D. essn = D3.essn and D3.relationship = 'Son');

--OR --Q2:

Select E.fname, E.lname

Page 6: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

From EMPLOYEE E, DEPENDENT D, Department DT

Where D.essn = E.ssn AND E.dno = DT.dnumber AND D.relationship = 'Spouse' AND DT.Dname = 'Research' AND

NOT EXISTS (Select D2.ESSN

From Dependent D2 Where D.essn = D2.essn and D2.relationship = 'Daughter') AND

NOT EXISTS (Select D3.ESSN

From Dependent D3

Where D.essn =D3.essn and D3.relationship = 'Son');

Joyce English

Q4:

List the name of employees who are married but have no children and are making

less than $30,000.

--List the name of employees who are making less than $30,000 and are married but have no children.

--Married = Select ESSN From Dependent Where relationship = ‘spouse’;

--Girls = Select ESSN From Dependent Where relationship = ‘daughter’;

--Boys = Select ESSN From Dependent Where relationship = ‘son’;

--INSERT INTO dependent VALUES ('453453453','John','M','07-JUN-1999','Spouse');

--INSERT INTO dependent VALUES ('987654321','Erica','F','07-SEP-1999','Daughter');

select * from dependent;

Page 7: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

--Q1 SELECT E.fname, E.lname

FROM Employee E, Dependent D

WHERE D.essn = E.ssn AND D. relationship = 'Spouse' AND E.salary < 30000 AND D.essn Not IN (SELECT D2.ESSN

FROM Dependent D2

WHERE D. essn = D2.essn and D2.relationship = 'Daughter')

AND D.essn NOT IN (SELECT D3.ESSN

FROM Dependent D3

WHERE D. essn = D3.essn and D3.relationship = 'Son');

--OR

--Q2:

Select E.fname, E.lname

From EMPLOYEE E, DEPENDENT D

Where E.ssn = D.essn and D.relationship = 'Spouse' AND E.salary < 30000 AND NOT EXISTS (Select D2.ESSN

From Dependent D2

Where D. essn = D2.essn and D2.relationship = 'Daughter') AND NOT EXISTS (Select D3.ESSN

From Dependent D3

Where D. essn =D3.essn and D3.relationship = 'Son');

Page 8: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

Q5:

Get the last name of married employees who only have daughters.

Married = Select ESSN From Dependent Where relationship = ‘spouse’;

Girls = Select ESSN From Dependent Where relationship = ‘daughter’;

Boys = Select ESSN From Dependent Where relationship = ‘son’;

--Get the last name of married employees who only have daughters.

--Married = Select ESSN From Dependent Where relationship = ‘spouse’; --Girls = Select ESSN From Dependent Where relationship = ‘doughter’;

--Boys = Select ESSN From Dependent Where relationship = ‘son’;

INSERT INTO dependent VALUES ('453453453','John','M','07-JUN-1999','Spouse'); INSERT INTO dependent VALUES ('987654321','Erica','F','07-SEP-1999','Daughter');

select * from dependent;

--Q1

SELECT E.fname, E.lname FROM Employee E, Dependent D

WHERE D.essn = E.ssn AND D. relationship = 'Spouse' AND

D.essn IN (SELECT D2.ESSN

FROM Dependent D2 WHERE D. essn = D2.essn and D2.relationship = 'Daughter')

AND

D.essn NOT IN (SELECT D3.ESSN FROM Dependent D3

WHERE D. essn = D3.essn and D3.relationship = 'Son');

--OR

--Q2:

Select E.fname, E.lname

From EMPLOYEE E, DEPENDENT D

Where E.ssn = D.essn and D.relationship = 'Spouse' AND

EXISTS (Select D2.ESSN Fom Dependent D2

Where D. essn = D2.essn and D2.relationship = 'Daughter') AND

NOT EXISTS (Select D3.ESSN From Dependent D3

Where D. essn =D3.essn and D3.relationship = 'Son');

Page 9: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung

Q6: Give the last name and ssn of those employees who work in any project(s) where there are more female than male employees.

Q6: Give the last name and ssn of those employees who work in any project(s) where for the project, there are more female employees working on than male employees working on. List the last name and ssn of such employee with the project number together. Select W2.pno, E2.ssn, E2.lname

From Works_on w2, employee E2, (Select W.pno, Count(W.essn)

From Works_on W, Employee E

Where W.essn = E.ssn and E.sex = 'M' Group By W.pno) As MEmp(pno, Mcnt),

(Select W1.pno, Count(W1.essn)

From Works_on W1, Employee E1 Where W1.essn = E1.ssn and E1.sex = 'F'

Group By W1.pno) As FEmp(pno, Fcnt)

where E2.ssn = W2.essn and W2.pno = FEmp.pno and MEmp.pno = FEmp.pno and FEmp.Fcnt > MEmp.Mcnt;

Page 10: BUS601 Lab Assignment 4 Due: June 17 SS Chung Summer 2015cis.csuohio.edu/~sschung/BUS601/Lab4_BUS601_Summer2015... · 2015-06-19 · BUS601 Lab Assignment 4 Due: June 17 SS Chung