0% found this document useful (0 votes)
147 views5 pages

DB Lab3

The document provides the schema for a company database including tables for employees, departments, projects, works for, and dependents. It also includes 17 sample queries to retrieve information from the tables such as displaying department details, projects under each department, employee details and dependents, and aggregated data about projects and departments. The queries utilize joins between the different tables to retrieve the requested data.

Uploaded by

Amira Bahaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views5 pages

DB Lab3

The document provides the schema for a company database including tables for employees, departments, projects, works for, and dependents. It also includes 17 sample queries to retrieve information from the tables such as displaying department details, projects under each department, employee details and dependents, and aggregated data about projects and departments. The queries utilize joins between the different tables to retrieve the requested data.

Uploaded by

Amira Bahaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Company Database Schema

Here is the schema of a company database, please implement it on any RDBMS


you like and then try to create the following requests (queries): "create only the
dependent table with all data and relations"

Employee:
Fname Lname SSN BDATE Addresss Sex Salary Superssn Dno
Ahmed Ali 112233 1/1/1965
15 Ali fahmy M 1300 223344 10
St.Giza
Kamel Mohamed 223344 15/10/1970 38 Mohy el dien M 1800 321654 10
abo el Ezz
St.Cairo
Hanaa Sobhy 123456 18/3/1973 38 Abdel Khalik F 800 223344 10
Tharwat St.
Downtown.Cairo
Amr Omran 321654 14/9/1963 44 Hilopolis.Cairo M 2500 null null
Noha Mohamed 968574 1/2/1975 55 Orabi St. El F 1600 321654 20
Mohandiseen
.Cairo
Edward Hanna 512463 19/8/1972 18 Abaas El M 1500 321654 30
3akaad St. Nasr
City.Cairo
Mariam Adel 669955 12/6/1982 269 El-Haram st. F 750 512463 20
Giza
Maged Raoof 521634 6/4/1980 18 Kholosi M 1000 968574 30
st.Shobra.Cairo

Department
Dname DNum MGRSSN MGRStart date

DP1 10 223344 1/1/2005


DP2 20 968574 1/3/2006
DP3 30 512463 1/6/2006
Works for
ESSN Pno Hours
223344 100 10
223344 200 10
223344 300 10
112233 100 40
968574 400 15
968574 700 15
968574 300 10
669955 400 20
223344 500 10
669955 700 7
669955 300 10
512463 500 10
512463 600 25
521634 500 10
521634 600 20
521634 300 6
521634 400 4

Project
Pname Pnumber Plocation City Dnum
AL Solimaniah 100 Cairo_Alex Road Alex 10
Al Rabwah 200 6th of October Giza 10
City
Al Rawdah 300 Zaied City Giza 10
Al Rowad 400 Cairo_Faiyom Giza 20
Road
Al Rehab 500 Nasr City Cairo 30
Pitcho american 600 Maady Cairo 30
Ebad El 700 Ring Road Cairo 20
Rahman

Dependent
ESSN Dependent_name Sex Bdate
112233 Hala Saied Ali F 18/10/1970
223344 Ahmed Kamel M 27/3/1998
Shawki
223344 Mona Adel F 25/4/1975
Mohamed
321654 Ramy Amr M 26/1/1990
Omran
321654 Omar Amr Omran M 30/3/1993
321654 Sanaa Gawish F 16/5/1973
512463 Sara Edward F 15/9/2001
512463 Nora Ghaly F 22/6/1976
* Try to create the following Queries:

1. Display the Department id, name and id and the name of its manager.
select dnum , dname , fname
from departments d , employee e
where e.ssn = d.mgrssn;
2. Display the name of the departments and the name of the projects under its
control.
select dname , pname
from departments d , project p
where d.dnum = p.dnum;
3. Display the full data about all the dependence associated with the name of the
employee they depend on him/her.
select e.fname, de.dependent_name , de.sex , de.bdate
FROM dependent de , employee e
WHERE de.essn = e.ssn
4. Display (Using Union Function)
a. The name and the gender of the dependence that's gender is Female and
depending on Female Employee.
b. And the male dependence that depends on Male Employee.
select de.dependent_name , de.sex
FROM dependent de , employee e
where de.sex ='F' And e.ssn = de.essn And e.sex='F'
union
select de.dependent_name , de.sex
FROM dependent de , employee e
where de.sex ='M' And e.ssn = de.essn And e.sex='M';
5. Display the Id, name and location of the projects in Cairo or Alex city.
select p.pnumber , p.pname , p.plocation
from project p
where p.city = 'Cairo' or p.city = 'Alex';
6. Display the Projects full data of the projects with a name starts with "a" letter.
select *
From project
where upper(pname) like 'a%'
7. display all the employees in department 30 whose salary from 1000 to 2000 LE
monthly
select e.fname
from employee e
WHERE e.dno = 30 And salary between 1000 and 2000 ;
8. Retrieve the names of all employees in department 10 who works more than or
equal10 hours per week on "AL Rabwah" project.
select e.fname
from employee e , works_for w , project f
where e.ssn = w.essn and w.pno = f.pnumber and w.hours>=10 And e.dno =
10 And f.pname='Al Rabwah';
9. Find the names of the employees who directly supervised with Kamel Mohamed.
select e.fname
from employee e , employee s
where e.superssn = s.ssn and s.fname='Kamel';
10. For each project, list the project name and the total hours per week (for all
employees) spent on that project.
select f.pname , sum(w.hours)
from project f , works_for w
where f.pnumber = w.pno
group by f.pname ;
11. Retrieve the names of all employees and the names of the projects they are
working on, sorted by the project name.
select e.fname , f.pname
from employee e , project f , works_for w
where e.ssn = w.essn and f.pnumber = w.pno
ORDER by f.pname ;
12. Display the data of the department which has the smallest employee ID over all
employees' ID.
select d.*
from departments d , employee e
where d.dnum = e.dno and e.ssn = (
select min(ssn)
from employee
)
13. For each department, retrieve the department name and the maximum, minimum
and average salary of its employees.
select d.dname , max(e.salary) , min(e.salary) , avg(e.salary)
from departments d , employee e
where d.dnum = e.dno
group by d.dname ;
14. List the last name of all managers who have no dependents.
SELECT e.fname
from employee e , departments d
where e.ssn = d.mgrssn and d.mgrssn not in (
select essn
from dependent
)
15. For each department-- if its average salary is less than the average salary of all
employees-- display its number, name and number of its employees.
SELECT d.dname , d.dnum , count(ssn) as num_emp
from employee e , departments d
where e.dno = d.dnum
GROUP by d.dnum , d.dname
having avg(e.salary) <= (
SELECT avg (salary)
FROM employee
)
16. 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 e.lname||' '||e.fname as full_name , f.pname , d.dnum


from employee e , project f , departments d
where e.dno = d.dnum and d.dnum = f.dnum
ORDER by d.dname , e.fname ASC , e.lname ASC;

17. For each project located in Cairo City , find the project number, the controlling
department name ,the department manager last name ,address and birthdate.

select f.pnumber ,d.dname , e.lname , e.address , e.bdate


from employee e , project f , departments d
where e.ssn = d.mgrssn and d.dnum = f.dnum and f.city='Cairo'

You might also like