Sei sulla pagina 1di 24

SQL ASSIGNMENT

QUE
RIES
BAS
ED
ON
COM
PAN
Y
DAT
ABA
SE

Submitted by:
Name: Shilpi
Roll No.: 1546

CREATING DATABASE COMPANY


mysql> CREATE DATABASE COMPANY;
Query OK, 1 row affected (0.00 sec)
mysql> USE COMPANY;
Database changed
CREATING TABLES
mysql> CREATE TABLE EMPLOYEE
-> ( FNAME VARCHAR(15) NOT NULL,
-> MINIT CHAR,
-> LNAME VARCHAR(15) NOT NULL,
-> SSN CHAR(9) NOT NULL,
-> BDATE DATE,
-> ADDRESS VARCHAR(30),
-> SEX CHAR,
-> SALARY DECIMAL(10,2),
-> SUPER_SSN CHAR(9),
-> DNO INT NOT NULL,
-> PRIMARY KEY(SSN)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> CREATE TABLE DEPARTMENT
-> ( DNAME VARCHAR(15) NOT NULL,
-> DNUMBER INT NOT NULL,
-> MGR_SSN CHAR(9) NOT NULL,
-> MGR_SATRT_DATE DATE,

-> PRIMARY KEY(DNUMBER),


-> UNIQUE(DNAME)
-> );
Query OK, 0 rows affected (0.13 sec)
mysql> CREATE TABLE DEPT_LOCATIONS
-> ( DNUMBER INT NOT NULL,
-> DLOCATION VARCHAR(15) NOT NULL,
-> PRIMARY KEY(DNUMBER,DLOCATION)
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> CREATE TABLE PROJECT
-> ( PNAME VARCHAR(15) NOT NULL,
-> PNUMBER INT NOT NULL,
-> PLOCATION VARCHAR(15),
-> DNUM INT NOT NULL,
-> PRIMARY KEY(PNUMBER),
-> UNIQUE(PNAME)
-> );
Query OK, 0 rows affected (0.12 sec)
mysql> CREATE TABLE WORKS_ON
-> ( ESSN CHAR(9) NOT NULL,
-> PNO INT NOT NULL,
-> HOURS DECIMAL(3,1) NOT NULL,
-> PRIMARY KEY(ESSN,PNO)
-> );
Query OK, 0 rows affected (0.12 sec)
mysql> CREATE TABLE DEPENDENT
-> ( ESSN CHAR(9) NOT NULL,
-> DEPENDENT_NAME VARCHAR(15) NOT NULL,
-> SEX CHAR,
-> BDATE DATE,
-> RELATIONSHIP VARCHAR(8),
-> PRIMARY KEY(ESSN,DEPENDENT_NAME)
-> );
Query OK, 0 rows affected (0.09 sec)
INSERTING VALUES INTO DEPENDENT TABLE
mysql> INSERT INTO DEPENDENT VALUES('333445555','ALICE','F','1986-04-05','DAUGHTER');
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO DEPENDENT VALUES('333445555','THEODORE','M','1983-10-25','SON');

Query OK, 1 row affected (0.04 sec)


mysql> INSERT INTO DEPENDENT VALUES('123456789','ELIZABETH','F','1967-0505','SPOUSE');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO DEPENDENT VALUES('123456789','ALICE','F','1988-12-30','DAUGHTER');
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO DEPENDENT VALUES('123456789','MICHAEL','M','1988-01-04','SON');
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO DEPENDENT VALUES('333445555','JOY','F','1958-05-03','SPOUSE');
Query OK, 1 row affected (0.07 sec)
mysql> INSERT INTO DEPENDENT VALUES('987654321','ABNER','M','1942-02-28','SPOUSE');
Query OK, 1 row affected (0.06 sec)
mysql> SELECT * FROM DEPENDENT;

INSERTING VALUES INTO DEPT_LOCATIONS TABLE


mysql> INSERT INTO DEPT_LOCATIONS VALUES(1,'HOUSTON');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO DEPT_LOCATIONS VALUES(4,'STAFFORD');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO DEPT_LOCATIONS VALUES(5,'BELLAIRE');
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO DEPT_LOCATIONS VALUES(5,'SUGARLAND');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO DEPT_LOCATIONS VALUES(5,'HOUSTON');
Query OK, 1 row affected (0.04 sec)
mysql> SELECT * FROM DEPT_LOCATIONS;

INSERTING VALUES INTO WORKS_ON TABLE


mysql> INSERT INTO WORKS_ON VALUES('123456789',1,32.5);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('123456789',2,7.5);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('666884444',3,40.0);
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO WORKS_ON VALUES('453453453',1,20.0);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('453453453',2,20.0);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('333445555',2,10.0);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('333445555',3,10.0);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('333445555',10,10.0);
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO WORKS_ON VALUES('333445555',20,10.0);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('999887777',30,30.0);
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO WORKS_ON VALUES('999887777',10,10.0);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('987987987',10,35.5);
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO WORKS_ON VALUES('987987987',30,5.0);
Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO WORKS_ON VALUES('987654321',30,20.0);


Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON VALUES('987654321',20,15.0);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO WORKS_ON(ESSN,PNO) VALUES('888665555',20);
Query OK, 1 row affected (0.05 sec)
mysql> SELECT * FROM WORKS_ON;

INSERTING VALUES INTO EMPLOYEE TABLE


mysql> INSERT INTO EMPLOYEE VALUES('JOHN','B','SMITH','123456789','1965-01-09','731
FONDREN, HOUSTON, TX','M',30000,'333445555',5);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO EMPLOYEE VALUES('FRANKLIN','T','WONG','333445555','1955-1208','638 VOSS, HOUSTON, TX','M',40000,'888665555',5);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO EMPLOYEE VALUES('ALICIA','J','ZELAYA','999887777','1968-01-19','3321
CASTLE, SPRING, TX','F',25000,'987654321',4);
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO EMPLOYEE VALUES('JENNIFER','S','WALLACE','987654321','1941-0620','291 BERRY, BELLAIRE, TX','F',43000,'888665555',4);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO EMPLOYEE VALUES('RAMESH','K','NARAYAN','666884444','1962-0915','975 FIRE OAK, HUMBLE, TX','M',38000,'333445555',5);
Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO EMPLOYEE VALUES ("JOYCE","A","ENGLISH","453453453","1972-0731","5631 RICE,HOUSTON, TX","F",25000,"3334455555",5);


Query OK, 1 row affected (0.36 sec)
mysql> INSERT INTO EMPLOYEE VALUES ("AHMAD","V","JABBAR","987987987","1969-0329","980 DALLAS,HOUSTON, TX","M",25000,"987654321",4);
Query OK, 1 row affected (0.38 sec)
mysql> INSERT INTO
EMPLOYEE(FNAME,MINIT,LNAME,SSN,BDATE,ADDRESS,SEX,SALARY,DNO)
VALUES("JAMES","E","BORG","888665555","1937-11-10","450 STONE, HOUSTON,
TX","M",55000,1);
Query OK, 1 row affected (0.08 sec)
INSERTING VALUES INTO DEPARTMENT TABLE
mysql> INSERT INTO DEPARTMENT VALUES("RESEARCH",5,"333445555","1988-05-22");
Query OK, 1 row affected (0.38 sec)
mysql> INSERT INTO DEPARTMENT VALUES("ADMINISTRATION",4,"987654321","1995-0101");
Query OK, 1 row affected (0.34 sec)
mysql> INSERT INTO DEPARTMENT VALUES("HEADQUARTERS",1,"888665555","1981-0619");
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM DEPARTMENT;

INSERTING VALUES INTO PROJECT TABLE


mysql> INSERT INTO PROJECT VALUES("PRODUCTX",1,"BELLAIRE",5);
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO PROJECT VALUES("PRODUCTY",2,"SUGARLAND",5);
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO PROJECT VALUES("PRODUCTZ",3,"HOUSTON",5);
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO PROJECT VALUES("COMPUTERIZATION",10,"STAFFORD",4);

Query OK, 1 row affected (0.08 sec)


mysql> INSERT INTO PROJECT VALUES("REORGANIZATION",20,"HOUSTON",1);
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO PROJECT VALUES("NEWBENEFITS",30,"STAFFORD",4);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM PROJECT;

ADDING FOREIGN KEYS IN ALL TABLES USING ALTER COMMAND


mysql> ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE FOREIGN KEY
(DNO) REFERENCES DEPARTMENT(DNUMBER);
Query OK, 8 rows affected (1.40 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE1 FOREIGN KEY
(SUPER_SSN) REFERENCES EMPLOYEE(SSN);
Query OK, 8 rows affected (1.75 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> DESC EMPLOYEE;

mysql> ALTER TABLE DEPARTMENT ADD CONSTRAINT FK_DEPT FOREIGN KEY


(MGR_SSN) REFERENCES EMPLOYEE(SSN);
Query OK, 3 rows affected (1.78 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> DESC DEPARTMENT;

mysql> ALTER TABLE DEPT_LOCATIONS ADD CONSTRAINT FK_LOC FOREIGN KEY


(DNUMBER) REFERENCES DEPARTMENT(DNUMBER);
Query OK, 5 rows affected (1.17 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> DESC DEPT_LOCATIONS;

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)

CREATING DATABASE UNIVERSITY


mysql> CREATE DATABASE UNIVERSITY;
Query OK, 1 row affected (0.00 sec)
mysql> USE UNIVERSITY;
Database changed
CREATING TABLE STUDENT AND INSERTING VALUES
mysql> CREATE TABLE STUDENT
-> ( NAME VARCHAR(10) NOT NULL,
-> STUDENT_NUMBER INTEGER PRIMARY KEY,
-> CLASS INTEGER,
-> MAJOR VARCHAR(5) );
Query OK, 0 rows affected (0.44 sec)
mysql> INSERT INTO STUDENT VALUES ("SMITH",17,1,"CS");
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO STUDENT VALUES ("BROWN",8,2,"CS");
Query OK, 1 row affected (0.03 sec)
mysql> SELECT * FROM STUDENT;

CREATING TABLE COURSE AND INSERTING VALUES


mysql> CREATE TABLE COURSE
-> ( COURSE_NAME VARCHAR(30) ,
-> COUUSE_NUMBER VARCHAR(10) PRIMARY KEY,
-> CREDIT_HOURS INTEGER,
-> DEPARTMENT VARCHAR(5) );
Query OK, 0 rows affected (0.29 sec)
mysql> INSERT INTO COURSE VALUES("INTRO TO COMPUTER SCIENCE","CS1310",4,"CS");
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO COURSE VALUES("DATA STRUCTURES","CS3320",4,"CS");
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO COURSE VALUES("DISCRETE
MATHEMATICS","MATH2410",3,"MATH");
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO COURSE VALUES("DATABASE","CS3380",3,"CS");
Query OK, 1 row affected (0.09 sec)
mysql> SELECT * FROM COURSE;

CREATING TABLE SECTION AND INSERTING VALUES


mysql> CREATE TABLE SECTION
-> ( SECTION_IDENTIFIER INTEGER,
-> COURSE_NUMBER VARCHAR(10),
-> SEMESTER VARCHAR(10),
-> YEAR INTEGER,
-> INSTRUCTOR VARCHAR(15),
-> PRIMARY KEY(SECTION_IDENTIFIER,COURSE_NUMBER) );
Query OK, 0 rows affected (0.49 sec)
mysql> INSERT INTO SECTION VALUES(85,"MATH2410","FALL",07,"KING");
Query OK, 1 row affected (0.10 sec)
mysql> INSERT INTO SECTION VALUES(92,"CS1310","FALL",07,"ANDERSON");
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO SECTION VALUES(102,"CS3320","SPRING",08,"KNUTH");


Query OK, 1 row affected (0.10 sec)
mysql> INSERT INTO SECTION VALUES(112,"MATH2410","FALL",08,"CHANG");
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO SECTION VALUES(119,"CS1310","FALL",08,"ANDERSON");
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO SECTION VALUES(135,"CS3380","FALL",08,"STONE");
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM SECTION;

CREATING TABLE GRADE_REPORT AND INSERTING VALUES


mysql> CREATE TABLE GRADE_REPORT
-> ( STUDENT_NUMBER INTEGER,
-> SECTION_IDENTIFIER INTEGER,
-> GRADE CHAR,
-> PRIMARY KEY(STUDENT_NUMBER,SECTION_IDENTIFIER) );
Query OK, 0 rows affected (0.58 sec)
mysql> INSERT INTO GRADE_REPORT VALUES(17,112,"B");
Query OK, 1 row affected (0.11 sec)
mysql> INSERT INTO GRADE_REPORT VALUES(17,119,"C");
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO GRADE_REPORT VALUES(8,85,"A");
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO GRADE_REPORT VALUES(8,92,"A");
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO GRADE_REPORT VALUES(8,102,"B");
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO GRADE_REPORT VALUES(8,135,"A");

Query OK, 1 row affected (0.08 sec)


mysql> SELECT * FROM GRADE_REPORT;

CREATING TABLE PREREQUISITE AND INSERTING VALUES


mysql> CREATE TABLE PREREQUISITE
-> (COURSE_NUMBER VARCHAR(10),
-> PREREQUISITE_NUMBER VARCHAR(10),
-> PRIMARY KEY(COURSE_NUMBER,PREREQUISITE_NUMBER) );
Query OK, 0 rows affected (0.73 sec)
mysql> INSERT INTO PREREQUISITE VALUES("CS3380","CS3320");
Query OK, 1 row affected (0.10 sec)
mysql> INSERT INTO PREREQUISITE VALUES("CS3380","MATH2410");
Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO PREREQUISITE VALUES("CS3320","CS1310");
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM PREREQUISITE;

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

FOREIGN KEYS IN THE ABOVE DATABASES


mysql> ALTER TABLE SHIPMENT ADD CONSTRAINT FK1 FOREIGN KEY(SNO)
REFERENCES SUPPLIERS(SNO);
Query OK, 5 rows affected (1.05 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE SHIPMENT ADD CONSTRAINT FK2 FOREIGN KEY(PNO) REFERENCES
PARTS(PNO);
Query OK, 5 rows affected (0.86 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE SHIPMENT ADD CONSTRAINT FK3 FOREIGN KEY(JNO) REFERENCES
PROJE
CT(JNO);
Query OK, 5 rows affected (3.22 sec)
Records: 5 Duplicates: 0 Warnings: 0
1. Get supplier numbers for suppliers in Paris with status > 20.
mysql> SELECT SNO FROM SUPPLIERS WHERE SCITY = "PARIS" AND STATUS > 20;

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

SHIPMENT WHERE SNO = "S2";

7.

Get the names of cities that store more than five


red parts.
mysql> SELECT CITY FROM PARTS WHERE COLOR = "RED" HAVING COUNT(COLOR) > 5;
Empty set (0.00 sec)

8.

Get full details of parts supplied by a supplier in London.


mysql> SELECT PNO, PNAME, COLOR, WEIGHT, CITY FROM PARTS WHERE
CITY = "LONDON";

9.

Get part numbers for part supplied by a supplier in London to a


project in London.
mysql> SELECT P.PNO FROM PARTS P, SHIPMENT S1 WHERE
S1.PNO = P.PNO AND S1.JNO IN (SELECT JNO FROM PROJECT WHERE JCITY = "LONDON")
AND S1.SNO IN (SELECT SNO FROM SUPPLIERS WHERE SCITY = "LONDON");

10. Get total number of project supplied by a supplier (say, S1).


mysql> SELECT COUNT(DISTINCT PNO) FROM SHIPMENT WHERE SNO = "S1";

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.

mysql> SELECT C.NAME FROM CLASS C WHERE C.ROOM = "R128" OR C.NAME IN


(SELECT E.ENAME FROM ENROLLED E GROUP BY E.ENAME HAVING COUNT(*) >= 5);
4.

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.

Find the names of students enrolled in the maximum number of classes.


mysql> SELECT DISTINCT S.SNAME FROM STUDENT S WHERE S.SNUM IN (SELECT
E.SNUM FROM ENROLLED E GROUP BY E.SNUM HAVING COUNT(*) >= ALL(SELECT
COUNT(*) FROM ENROLLED E2 GROUP BY E2.SNUM));

11.

Find the names of students not enrolled in any class.


mysql> SELECT DISTINCT S.SNAME FROM STUDENT S WHERE S.SNUM NOT IN (SELECT
E.SNUM FROM ENROLLED E);