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

Assignment #4

The document provides instructions for an assignment involving relational database queries on a company database. It lists 15 queries to perform on the database tables to retrieve requested information such as employee names, addresses, projects worked on, and aggregate data. The queries use joins between the Employee, Department, Project, and Works_On tables.

Uploaded by

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

Assignment #4

The document provides instructions for an assignment involving relational database queries on a company database. It lists 15 queries to perform on the database tables to retrieve requested information such as employee names, addresses, projects worked on, and aggregate data. The queries use joins between the Employee, Department, Project, and Works_On tables.

Uploaded by

Crentsil Kwayie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Relational Database

CS 3520 Database Theory


HW # 4
1. Create the tables for the company database and enter the data as given to you (Handout).
2. Print all your tables to make sure you have entered correct data.
3. Write the following queries in Oracle Express Edition and run them using the company
database. For each query you need to show:
The English statement
The SQL statement
The result of the query.

The Queries are:

Q 0. Retrieve the birth date and address of the employee(s) whose name is:
‘John B. Smith’.

Select Bdate, address from Employee where Fname = 'John' AND Minit = 'B'
AND Lname = 'Smith';

Q 1. Retrieve the birth date and address of the employee(s) who work for the:
‘Research’ department.

Select Bdate, address from Employee where Dno = '5';


Q 2. For every project located in ‘Stafford’, list the project number, the controlling
department number and the department manager’s last name, address and birth
date.

Select p.Pnumber, p.Dnum, e.Lname, e.Address, e.Bdate from Project p JOIN


Department d on p.Dnum = d.Dnumber JOIN Employee e on d.Mgr_ssn = e.Ssn where
p.Plocation = 'Stafford';

Q 3. Retrieve all employees whose address is in Houston, Texas.

Select * from Employee where Address LIKE '%Houston%';

Q 4. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Smith’ either as a worker or as a manager of the department that controls
the project.

Select p.Pnumber from Project p JOIN Department d on p.Dnum = d.Dnumber


JOIN Works_On w on p.Pnumber = w.Pno JOIN Employee e on w.Essn = e.Ssn
where e.Lname = 'Smith' OR d.Mgr_ssn = '123456789';

Q 5. Find all employees who were born during the 1950s.


select * from Employee e where e.Bdate >= '01/01/1950' AND e.Bdate <
'01/01/1960';

Q 6. Retrieve all employees in department 5 whose salary is between $30000 and


$40000.

Select e.* from Employee e WHERE TO_NUMBER(e.salary) >30000 AND


TO_NUMBER(e.salary) < 40000 AND e.Dno = '5';

Q 7. Show the resulting salaries if every employee working on the ‘ProductX’ project is
given a 10 percent raise.

Select e.Ssn, salary*1.10 from Employee e JOIN Works_ON w on e.Ssn = w.Essn


WHERE w.Pno = 1;
Q 8. Retrieve a list of employees and the projects they are working on, ordered by
department and, within each department, ordered alphabetically by last name, first
name.

Select w.Pno, e.Lname,e.Fname from Employee e JOIN Works_On w on e.Ssn


= w.Essn ORDER BY TO_NUMBER(w.Pno) Asc, e.Lname ASC, e.Fname Asc;

Q 9. Retrieve the name of all employees who do not have supervisor.

Select e.Lname,e.Fname, e.Minit from Employee e Where e.Super_ssn is null;


Q10. Retrieve the social security numbers of all employees who work on project numbers
1, 2, or 3.

Select DISTINCT e.ssn FROM Employee e JOIN WORKS_ON w on e.Ssn =


w.Essn Where w.Pno = '1' OR w.Pno = '2' OR w.Pno = '3';

Q11. Find the sum of the salaries of all employees, the maximum salaries, the minimum
salaries and the average salaries.

Select MAX(salary), MIN(salary), AVG(salary), SUM(salary) from Employee;

Q12. Retrieve (find) the number of employees in the ‘Research’ department.

Select Count(*) "Employees In Research" from Employee WHERE Dno = '5' ;


Q13. For each project, retrieve the project number, the project name, and the number of
employees who work on that project.

Select p.Pnumber, p.Pname, COUNT(*) FROM Project p JOIN Works_ON w on


p.Pnumber = w.Pno GROUP BY P.Pnumber, p.Pname ;

Q14. For each project on which more than two employees work, retrieve the project
number, the project name, and the number of employees who work on the project.

Select p.Pnumber, p.Pname, COUNT(*) FROM Project p JOIN Works_ON w on


p.Pnumber = w.Pno GROUP BY P.Pnumber, p.Pname HAVING COUNT(*) > 2;
Q15. For each project retrieve the project number, the project name, and the number of
employees from department 5 who work on the project.

Select p.Pnumber, p.Pname, COUNT(*) FROM Project p JOIN Works_ON w on


p.Pnumber = w.Pno JOIN Employee e on w.Essn = e.ssn WHERE e.Dno = '5'
GROUP BY P.Pnumber, p.Pname;

You might also like