Program 1: Description
Program 1: Description
DESCRIPTION:
The following relations keep track of students, their enrollment for classes along with faculty information.
● Student (snum: integer, sname: string, major: string, level: string, age: integer)
● Class (name: string, meets at: string, room: string, d: integer)
● Enrolled (snum: integer, cname: string)
● Faculty (fid: integer, fname: string, deptid: integer)
NOTE: The meaning of these relations is straight forward.For example, Enrolled has one record per student-class pair such that the
student is enrolled in the class. Level is a two character code with 4 different values (example: Junior: JR etc)
Queries:
Write the following queries in SQL. No duplicates should be printed in any of the answers.
1. Find the names of all juniors (level=Jr) who are enrolled for class taught by professor Harshith.
2. Find the names of all classes that either meet in room128 or have 5 or more students enrolled.
3. Find the names of all students who are enrolled in two classes that meet at same time.
4. Find the names of faculty members who teach in every room in which some class is taught.
5. Find the names of the faculty members for whome the combined enrollment of the classes that they teach is less then five.
Create:
mysql> CREATE DATABASE student;
Database changed
snum INT,
sname VARCHAR(10),
major VARCHAR(2),
level VARCHAR(2),
deptid INT,
PRIMARY KEY(fid));
cname VARCHAR(20),
metts_at VARCHAR(10),
room VARCHAR(10),
fid INT,
PRIMARY KEY(cname),
snum INT,
cname VARCHAR(20),
PRIMARY KEY(snum,cname),
INSERTIONS:
mysql> INSERT INTO student (snum,sname,major,level,age) VALUES
-> (1,'jhon','CS','Sr',19),
-> (2,'smith','CS','Jr',20),
-> (3,'jacob','CV','Sr',20),
-> (4,'tom','CS','Jr',20),
-> (5,'sid','CS','Jr',20),
-> (6,'harry','CS','Sr',21)
->(11,'Harshith',1000),
->(12,'Mohan',1000),
->(13,'Kumar',1001),
->(14,'Shobha',1002),
->(15,'Shan',1000);
+-----+----------+--------+
-> ('class1','noon','room1',14),
-> ('class10','morning','room128',14),
-> ('class2','morning','room2',12),
-> ('class3','morning','room3',11),
-> ('class4','evening','room4',14),
-> ('class5','night','room3',15),
-> ('class6','morning','room2',14),
-> ('class7','morning','room3',14);
-> (1,'class1'),
-> (2,'class1'),
-> (3,'class3'),
-> (4,'class3'),
-> (3,'class3'),
-> (5,'class4');
-> (1,'class5');
-> (2,'class5');
-> (3,'class5');
-> (4,'class5');
-> (5,'class5');
-> (6,'class5');
QUERIES:
Query 1: Find the names of all juniors (level=Jr) who are enrolled for
class taught by professor Harshith.
mysql> SELECT DISTINCT s.sname
e.cname=c.cname AND
s.level='jr' AND
f.fname='Harshith' AND
f.fid=c.fid;
+-------+
| sname |
+-------+
| tom |
+-------+
1 row in set (0.00 sec)
Description : Query checks whether the students are enrolled for the class and the level of the students is 'Jr', then it
extracts the name of the student who is enrolled for the class whose fid in table class correspomds to the fid of
professor Harshith.
Query 2: Find the names of all classes that either meet in room128 or
have 5 or more students enrolled.
mysql> SELECT DISTINCT cname
FROM class
WHERE room='room128'
OR
+---------+
| cname |
+---------+
| class10 |
| class5 |
+---------+
Description : Query results displays the class names that either have room number as room128 or it selects the cname from
table enrolled and "group by e.cname having count(*)>=5" statement meaning that cname where number of students enrolled
for that class is greater than or equal to five.
Query 3: Find the names of all students who are enrolled in two classes
that meet at same time.
mysql> SELECT DISTINCT s.sname
FROM student s
e1.cname<>e2.cname AND
e1.cname=c1.cname AND
e2.cname=c2.cname AND
c1.meets_at=c2.meets_at );
+-------+
| sname |
+-------+
| jacob |
+-------+
Query 4: Find the names of faculty members who teach in every room
in which some class is taught.
mysql> SELECT f.fname,f.fid
FROM faculty f
+--------+-----+
| fname | fid |
+--------+-----+
| Shobha | 14 |
+--------+-----+
Description : The outer part of the query fetches the name and id of the facuty from table faculty of the fid obtained
from the inner query.The inner query fetches the fid that has count of number of rooms taught equal to the number of
distinct rooms in which some class is taught.
Query 5: Find the names of the faculty members for whome the
combined enrollment of the classes that they teach is less then five.
mysql> SELECT DISTINCT f.fname
FROM faculty f
+----------+
| fname |
+----------+
| Harshith |
| Mohan |
| Shobha |
+----------+
3 rows in set (0.01 sec)
Description : The outer query fetches the name of the faculty for fid obtained from the inner query . The inner query
selects the fid from
PROGRAM 2
DESCRIPTION:
The following relations keep track of airline flight information:
● FLIGHTS (no:integer,from:string,to:string,distance:integer,departs:time,arrives:time,price:real)
● AIRCRAFT (aid:integer,aname:string,cruisingrange:integer)
● CERTIFIED (eid:integer,aid:integer)
● EMPLOYEES (eid:integer,ename:string,salary:integer)
NOTE that the EMPLOYEES relation describes pilots and other kinds of employees as well;Every pilot is certified for some aircraft,and
only pilots are certified to fly.
Queries:
Write each of the following queries in SQL.
1. Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs 80,000.
2. For each pilot who is certified for more than three aircrafts,find the eid and the maximum cruisingrange of the
aircraft for which he/she is certified.
3. Find the names of all pilots whose salary is less than the price of the cheapest route from Bangalore to Frankfurt.
4. For all aircrafts with cruisingrange over 1000 kms,find the name of the aircraft and the average salary of all pilots
certified for this aircraft.
6. Find the aid's of all aircraft that can be used on routes from Bangalore to Delhi.
Create:
mysql> CREATE DATABASE flights;
Database changed
-> no INT,
Insertion:
mysql> INSERT INTO flight (no,frm,too,distance,departs,arrives,price) VALUES
(1,'Bangalore','Mangalore',360,'10:45:00','12:00:00',10000),
(2,'Bangalore','Delhi',5000,'12:15:00','04:30:00',25000),
(3,'Bangalore','Mumbai',3500,'02:15:00','05:25:00',30000),
(4,'Delhi','Mumbai',4500,'10:15:00','12:05:00',35000),
(5,'Delhi','Frankfurt',18000,'07:15:00','05:30:00',90000),
(6,'Bangalore','Frankfurt',19500,'10:00:00','07:45:00',95000),
(7,'Bangalore','Frankfurt',17000,'12:00:00','06:30:00',99000);
(123,'Airbus',1000),
(302,'Boeing',5000),
(306,'Jet01',5000),
(378,'Airbus380',8000),
(456,'Aircraft',500),
(789,'Aircraft02',800),
(951,'Aircraft03',1000);
(2,'Ajith',85000),
(3,'Arnab',50000),
(4,'Harry',45000),
(5,'Ron',90000),
(6,'Josh',75000),
(7,'Ram',100000);
(1,123),
(2,123),
(1,302),
(5,302),
(7,302),
(1,306),
(2,306),
(1,378),
(2,378),
(4,378),
(6,456),
(3,456),
(5,789),
(6,789),
(3,951),
(1,951),
(1,789);
Queries:
###1.Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs 80,000.
-> (SELECT *
+------------+
| aname |
+------------+
| Airbus |
| Boeing |
| Jet01 |
| Airbus380 |
| Aircraft02 |
+------------+
###2.For each pilot who is certified for more than three aircrafts,find the eid and the maximum cruisingrange of the aircraft for which
he/she is certified.
+-----+--------------------+
| eid | max(cruisingrange) |
+-----+--------------------+
| 1 | 8000 |
+-----+--------------------+
###3.Find the names of all pilots whose salary is less than the price of the cheapest route from Bangalore to Frankfurt.
+-------+
| ename |
+-------+
| Ajay |
| Ajith |
| Arnab |
| Harry |
| Ron |
| Josh |
+-------+
###4.For all aircrafts with cruisingrange over 1000 kms,find the name of the aircraft and the average salary of all pilots certified for this
aircraft.
+-----+-----------+---------------+
+-----+-----------+---------------+
+-----+-----------+---------------+
+-------+
| ename |
+-------+
| Ajay |
| Ron |
| Ram |
+-------+
###6.Find the aid's of all aircraft that can be used on routes from Bangalore to Delhi.
### PROGRAM 3
Consider the following schema for OrderDatabase:
SALESMAN (Salesman_id, Name, City, Commission)
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities (Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a day.
5. Demonstrate the DELETE operation by removing the salesman with id 1000. All his orders must also be deleted.
DESC SALESMAN;
--------------------------------------
--Create Table CUSTOMER with Primary Key as CUSTOMER_ID and Foreign Key SALESMAN_ID referring the SALESMAN table
DESC CUSTOMER;
--------------------------------------
--Create Table ORDERS with Primary Key as ORDER_NO and Foreign Key CUSTOMER_ID and SALESMAN_ID referring the CUSTOMER and
SALESMAN tables respectively
DESC ORDERS;
------------------------------------------
------------------------------------------
----------------------------------
--Find the name and numbers of all salesman who had more than one customer
----------------------------------
--List all the salesman and indicate those who have and don’t have customers in their cities (Use UNION operation.)
-----------------------------------
--Create a view that finds the salesman who has the customer with the highest order of a day.
CREATE VIEW V_SALESMAN AS
SELECT O.ORDER_DATE, S.SALESMAN_ID, S.NAME
FROM SALESMAN S,ORDERS O
WHERE S.SALESMAN_ID = O.SALESMAN_ID
AND O.PURCHASE_AMOUNT= (SELECT MAX(PURCHASE_AMOUNT)
FROM ORDERS C
WHERE C.ORDER_DATE=O.ORDER_DATE);
-----------------------------------
--Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also be deleted.
##PROGRAM 4
##PROGRAM 5
Write a PL/SQL procedure to find the number of students ranging from 100-70%,69-60%,59-50% &
below 49% in each course from the student_course table given by the procedure as parameter.