0% found this document useful (0 votes)
10 views2 pages

Lab2 Script Ali Mohsen

The document contains SQL commands for creating and populating tables for a university database, specifically for professors and departments. It includes table definitions, foreign key constraints, and various insert statements for data entry. Additionally, it features SQL queries to retrieve information such as managers of full professors, professor counts by department location, and professors in different departments.

Uploaded by

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

Lab2 Script Ali Mohsen

The document contains SQL commands for creating and populating tables for a university database, specifically for professors and departments. It includes table definitions, foreign key constraints, and various insert statements for data entry. Additionally, it features SQL queries to retrieve information such as managers of full professors, professor counts by department location, and professors in different departments.

Uploaded by

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

Exercise 1:

a)

CREATE TABLE PROFESSOR (


pno INT NOT NULL,
pname VARCHAR(20) NOT NULL,
pjob VARCHAR(25) NOT NULL,
pmgr INT,
phiredate DATE NOT NULL,
psalary INT NOT NULL,
pbonus INT NULL,
pdno INT NOT NULL
PRIMARY KEY (pno)
);

CREATE TABLE DEPARTMENT (


dno INT NOT NULL,
dname VARCHAR(20) NOT NULL,
dlocation VARCHAR(20) NOT NULL,
PRIMARY KEY (dno)
);

ALTER TABLE PROFESSOR


ADD FOREIGN KEY pdno REFERENCES DEPARTMENT (dno),
ADD FOREIGN KEY pmgr REFERENCES PROFESSOR (pno);

b)
INSERT INTO DEPARTMENT
VALUES
(10, 'Computer Science', 'Bliss Hall'),
(20, 'Business', 'West Hall'),
(30, 'English', 'Fisk Hall'),
(40, 'Mathematics', 'Bliss Hall');

c)
INSERT INTO PROFESSOR
VALUES
(7839, 'King', 'Dean', NULL, '2003-11-17', 6500, 0, 10),
(7566, 'Jones', 'Chair', 7839, '2003-04-2', 3375, 0, 20),
(7698, 'Blake', 'Chair', 7839, '2003-05-1', 3250, 0, 30),
(7782, 'Clark', 'Chair', 7839, '2003-06-9', 2850, 0, 10),
(7499, 'Allen', 'Full Professor', 7698, '2003-02-20', 2000, 500, 30),
(7521, 'Ward', 'Full Professor', 7698, '2003-02-22', 1650, 800, 30),
(7654, 'Martin', 'Full Professor', 7698, '2003-09-28', 1650, 1400, 30),
(7844, 'Turner', 'Full Professor', 7698, '2003-09-8', 1900, 0, 30),
(7900, 'James', 'Assistant Professor', 7698, '2003-12-3', 1350, 0, 30),
(7788, 'Scott', 'Associate Professor', 7566, '2002-06-27', 3500, 0, 20),
(7902, 'Ford', 'Associate Professor', 7566, '2003-12-3', 3500, 0, 20),
(7369, 'Smith', 'Assistant Professor', 7902, '2002-12-17', 1200, 0, 20),
(7876, 'Adams', 'Assistant Professor', 7788, '2002-07-31', 1500, 0, 20),
(7934, 'Miller', 'Assistant Professor', 7782, '2003-01-23', 1700, 0, 10);

Exercise 2:
a)
SELECT DISTINCT m.pname AS PManager_Name
FROM PROFESSOR p
JOIN PROFESSOR m ON p.pmgr = m.pno
WHERE p.pjob = 'Full Professor';

b)
SELECT d.dlocation AS hall, COUNT(p.pno) AS Professor_Count, AVG(p.psalary) AS
Average_Salary
FROM PROFESSOR p
JOIN DEPARTMENT d ON p.pdno = d.dno
GROUP BY d.dlocation;

c)
SELECT d.dname AS DEPARTMENT_Name
FROM DEPARTMENT d
WHERE d.dno NOT IN (SELECT DISTINCT pjob FROM PROFESSOR WHERE pjob != 'Full
Professor') and d.dno NOT IN (SELECT DISTINCT pno FROM PROFESSOR WHERE pno IS
NULL);

d)
SELECT COUNT(p.pno) AS Professors_In_Different_departments
FROM PROFESSOR p
JOIN DEPARTMENT ed ON p.pdno = ed.dno
JOIN PROFESSOR m ON p.pmgr = m.pno
JOIN DEPARTMENT md ON m.pdno = md.dno
WHERE ed.dlocation != md.dlocation;

You might also like