Académique Documents
Professionnel Documents
Culture Documents
QUE
RIES
BAS
ED
ON
COM
PAN
Y
DAT
ABA
SE
Submitted by:
Name: Shilpi
Roll No.: 1546
mysql> ALTER TABLE PROJECT ADD CONSTRAINT FK_PROJECT FOREIGN KEY (DNUM)
REFERENCES DEPARTMENT(DNUMBER);
Query OK, 6 rows affected (1.53 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> DESC PROJECT;
mysql> ALTER TABLE WORKS_ON ADD CONSTRAINT FK_WORKS FOREIGN KEY (ESSN)
REFERENCES EMPLOYEE(SSN);
Query OK, 16 rows affected (1.03 sec)
Records: 16 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE WORKS_ON ADD CONSTRAINT FK_WORKS1 FOREIGN KEY (PNO)
REFERENCES PROJECT(PNUMBER);
Query OK, 16 rows affected (1.04 sec)
Records: 16 Duplicates: 0 Warnings: 0
mysql> DESC WORKS_ON;
mysql> ALTER TABLE DEPENDENT ADD CONSTRAINT FK_DEP FOREIGN KEY (ESSN)
REFERENCES EMPLOYEE(SSN);
Query OK, 7 rows affected (0.97 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> DESC DEPENDENT;
4.10 Specify the following queries in SQL on the COMPANY relational database schema shown
in Figure 3.5. Show the result of each query if it is applied to the COMPANY database in Figure
3.6.
a. Retrieve the names of all employees in department 5 who work more than 10 hours per week
on the ProductX project.
mysql> SELECT E.FNAME,E.MINIT,E.LNAME FROM EMPLOYEE E, PROJECT P, WORKS_ON
W WHERE E.DNO = P.DNUM AND W.PNO = P.PNUMBER AND E.SSN = W.ESSN AND E.DNO =
5 AND W.HOURS > 10 AND P.PNAME = "PRODUCTX";
b. List the names of all employees who have a dependent with the same first name as themselves.
mysql> SELECT E.FNAME,E.MINIT,E.LNAME FROM EMPLOYEE E, DEPENDENT D WHERE
E.SSN = D.ESSN AND E.FNAME = D.DEPENDENT_NAME;
Empty set (0.00 sec)
c. Find the names of all employees who are directly supervised by Franklin Wong.
mysql> SELECT FNAME,MINIT,LNAME FROM EMPLOYEE WHERE SUPER_SSN IN (SELECT
SSN FROM EMPLOYEE WHERE FNAME = "FRANKLIN" AND LNAME = "WONG");
5.5 Specify the following queries on the database in Figure 3.5 in SQL. Show the query results if
each query is applied to the database in Figure 3.6.
a. For each department whose average employee salary is more than $30,000, retrieve the
department name and the number of employees working for that department.
mysql> SELECT DNAME, COUNT(*) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER =
DNO GROUP BY DNO HAVING AVG(SALARY) > 30000;
b. Suppose that we want the number of male employees in each department making more than
$30,000, rather than all employees (as in Exercise 5.4a). Can we specify this query in SQL? Why
or why not?
Yes via nested query.
mysql> SELECT DNAME, COUNT(*) FROM DEPARTMENT, EMPLOYEE WHERE SEX = 'M'
AND DNUMBER = DNO GROUP BY DNAME HAVING AVG(SALARY) > 30000;
5.7. In SQL, specify the following queries on the database in Figure 3.5 using the concept of
nested queries and concepts described in this chapter.
a. Retrieve the names of all employees who work in the department that has the employee with
the highest salary among all employees.
mysql> SELECT FNAME, MINIT, LNAME FROM EMPLOYEE WHERE DNO IN (SELECT DNO
FROM EMPLOYEE WHERE SALARY IN (SELECT MAX(SALARY) FROM EMPLOYEE));
QUER
IES
BASE
D ON
UNIV
ERSIT
Y
DATA
BASE
b. Retrieve the names of all employees whose supervisors supervisor has 888665555 for SSN.
mysql> SELECT LNAME FROM EMPLOYEE WHERE SUPER_SSN IN (SELECT SSN FROM
EMPLOYEE WHERE SUPER_SSN="88665555");
Empty set (0.03 sec)
4.12. Specify the following queries in SQL on the database schema of Figure 1.2.
a. Retrieve the names of all senior students majoring in CS (computer science).
mysql> SELECT NAME FROM STUDENT WHERE MAJOR = "CS";
b. Retrieve the names of all courses taught by Professor King in 2007 and 2008.
mysql> SELECT C.COURSE_NAME FROM COURSE C, SECTION S WHERE
C.COUUSE_NUMBER = S.COURSE_NUMBER AND S.INSTRUCTOR = "KING" AND (S.YEAR
= 07 OR S.YEAR = 08);
c. For each section taught by Professor King, retrieve the course number, semester, year, and
number of students who took the section.
mysql> SELECT S.COURSE_NUMBER, S.SEMESTER, S.YEAR, COUNT(*) FROM SECTION S,
GRADE_REPORT G WHERE S.INSTRUCTOR = "KING" AND S.SECTION_IDENTIFIER =
G.SECTION_IDENTIFIER GROUP BY S.COURSE_NUMBER, S.SEMESTER, S.YEAR;
d. Retrieve the name and transcript of each senior student (Class = 4) majoring in CS. A
transcript includes course name, course number, credit hours, semester, year, and grade for each
course completed by the student.
mysql> SELECT NAME, COURSE_NUMBER, C.COUUSE_NUMBER, CREDIT_HOURS,
SEMESTER, YEAR, GRADE FROM STUDENT ST, COURSE C, SECTION S, GRADE_REPORT
G WHERE CLASS = 4 AND MAJOR = "CS" AND ST.STUDENT_NUMBER =
G.STUDENT_NUMBER AND G.SECTION_IDENTIFIER = S.SECTION_IDENTIFIER AND
S.COURSE_NUMBER = C.COUUSE_NUMBER;
Empty set (0.00 sec)
5.6. Specify the following queries in SQL on the database schema in Figure 1.2.
a. Retrieve the names and major departments of all straight-A students (students who have a
grade of A in all their courses).
mysql> SELECT NAME, MAJOR FROM STUDENT WHERE NOT EXISTS (SELECT * FROM
GRADE_REPORT WHERE STUDENT_NUMBER = STUDENT.STUDENT_NUMBER AND
GRADE != "A");
Empty set (0.07 sec)
b. Retrieve the names and major departments of all students who do not have a grade of A in any
of their courses.
mysql> SELECT NAME, MAJOR FROM STUDENT WHERE NOT EXISTS (SELECT * FROM
GRADE_REPORT WHERE STUDENT_NUMBER = STUDENT.STUDENT_NUMBER AND
GRADE = "A");
QUERIES BASED ON
Q1
SUPPLIERS TABLE
PARTS TABLE
PROJECT TABLE
SHIPMENT TABLE
2. Get suppliers details for suppliers who supply part P2. Display the supplier list in increasing order of
supplier numbers.
mysql> SELECT S.SNO, S.SNAME, S.STATUS, S.SCITY FROM SUPPLIERS S,
PARTS P WHERE P.PNO = "P2" AND S.SCITY = P.CITY ORDER BY S.SNO DESC
;
3.
Get suppliers names for suppliers who do not supply part P2.
mysql> SELECT SNAME FROM SUPPLIERS WHERE SNO NOT IN (SELECT SNO FROM SHIPMENT
WHERE PNO != "P2");
4. For each shipment get full shipment details, including total shipment weights.
mysql> SELECT S1.SNO, S1.PNO, S1.JNO, S1.QUANTITY, S1.QUANTITY*P.WEIGHT FROM
SHIPMENT S1, PARTS P WHERE S1.PNO = P.PNO;
5. Get all the shipments where the quantity is in the range 300 to 750 inclusive.
mysql> SELECT * FROM SHIPMENT WHERE QUANTITY >=300 AND QUANTITY <= 750;
6. Get part numbers for parts that either weigh more than 16 pounds or are supplied by suppliers S2 or
both.
mysql> SELECT PNO FROM PARTS WHERE WEIGHT > 16 UNION SELECT PNO FROM
7.
8.
9.
11. Get the total quantity of a part (say, P1) supplied by a supplier (say, S1).
mysql> SELECT SUM(QUANTITY) FROM SHIPMENT WHERE SNO = "S1";
QUERIES BASED ON
Q2
STUDENT TABLE
CLASS TABLE
ENROLLED TABLE
FACULTY TABLE
1.
Find the names of all juniors (level = JR) who are enrolled in a class taught by I. Teach.
mysql> SELECT DISTINCT S.SNAME FROM STUDENT S, CLASS C, ENROLLED E,
FACULTY F WHERE S.SNUM = E.SNUM AND E.ENAME = C.NAME AND C.FID = F.FID
AND F.FNAME = "I TEACH" AND S.LEVEL = "JR";
2.
Find the age of the oldest student who is either a History major or enrolled in a course taught by
I. Teach.
mysql> SELECT MAX(S.AGE) FROM STUDENT S WHERE (S.MAJOR = "HISTORY") OR
S.SNUM IN (SELECT E.SNUM FROM CLASS C, ENROLLED E, FACULTY F WHERE E.ENAME
= C.NAME AND C.FID = F.FID AND F.FNAME = "I TEACH");
3.
Find the names of all classes that either meet in room R128 or have five or more students
enrolled.
Find the names of all students who are enrolled in two classes that meet at the same time.
mysql> SELECT DISTINCT S.SNAME FROM STUDENT S WHERE S.SNUM IN (SELECT
E1.SNUM FROM ENROLLED E1, ENROLLED E2, CLASS C1, CLASS C2 WHERE E1.SNUM =
E2.SNUM AND E1.ENAME != E2.ENAME AND E1.ENAME = C1.NAME AND E2.ENAME =
C2.NAME AND C1.TIME = C2.TIME);
Empty set (0.00 sec)
5.
Find the names of faculty members who teach in every room in which some class is taught.
mysql> SELECT F.FNAME FROM FACULTY F WHERE F.FID = ALL (SELECT F.FID FROM
FACULTY F, CLASS C WHERE C.ROOM IN (SELECT DISTINCT C.ROOM FROM CLASS C)
AND F.FID = C.FID);
Empty set (0.16 sec)
6.
Find the names of faculty members for whom the combined enrolment of the courses that they
teach is less than five.
mysql> SELECT DISTINCT F.FNAME FROM FACULTY F WHERE 5 > (SELECT
COUNT(E.SNUM) FROM CLASS C, ENROLLED E WHERE C.NAME = E.ENAME AND C.FID =
F.FID);
7.
For each level, print the level and the average age of students for that level.
mysql> SELECT S.LEVEL, AVG(S.AGE) FROM STUDENT S GROUP BY S.LEVEL;
8.
For all levels except JR, print the level and the average age of students for that level.
mysql> SELECT S.LEVEL, AVG(S.AGE) FROM STUDENT S WHERE S.LEVEL != "JR" GROUP
BY S.LEVEL;
9.
For each faculty member that has taught classes only in room R128, print the faculty members
name and the total number of classes she or he has taught.
mysql> SELECT F.FNAME, COUNT(*) FROM FACULTY F, CLASS C WHERE F.FID = (SELECT
F.FID FROM FACULTY F, CLASS C WHERE C.ROOM = "R128" AND C.FID = F.FID) AND F.FID
= C.FID;
10.
11.