0% found this document useful (0 votes)
57 views15 pages

Program 1: Description

The document describes two database relations: one for student enrollment data and one for airline flight data. It provides the structure of each database and example queries to retrieve information from the databases. The student database tracks students, classes, enrollments, and faculty. The airline database tracks flights, aircrafts, employee certifications, and employees. Sample queries are provided to find information such as student enrollments, aircraft details, and flight prices.
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)
57 views15 pages

Program 1: Description

The document describes two database relations: one for student enrollment data and one for airline flight data. It provides the structure of each database and example queries to retrieve information from the databases. The student database tracks students, classes, enrollments, and faculty. The airline database tracks flights, aircrafts, employee certifications, and employees. Sample queries are provided to find information such as student enrollments, aircraft details, and flight prices.
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/ 15

PROGRAM 1

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;

Query OK, 1 row affected (0.00 sec)

mysql> USE student;

Database changed

mysql> CREATE TABLE student(

snum INT,

sname VARCHAR(10),

major VARCHAR(2),

level VARCHAR(2),

age int,primary key(snum));

Query OK, 0 rows affected (0.10 sec)

mysql> CREATE TABLE faculty(

fid INT,fname VARCHAR(20),

deptid INT,

PRIMARY KEY(fid));

Query OK, 0 rows affected (0.08 sec)

mysql> CREATE TABLE class(

cname VARCHAR(20),
metts_at VARCHAR(10),

room VARCHAR(10),

fid INT,

PRIMARY KEY(cname),

FOREIGN KEY(fid) REFERENCES faculty(fid));

Query OK, 0 rows affected (0.09 sec)

mysql> CREATE TABLE enrolled(

snum INT,

cname VARCHAR(20),

PRIMARY KEY(snum,cname),

FOREIGN KEY(snum) REFERENCES student(snum),

FOREIGN KEY(cname) REFERENCES class(cname));

Query OK, 0 rows affected (0.12 sec)

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)

Query OK, 2 rows affected (0.04 sec)

mysql> SELECT * FROM student;

mysql> INSERT INTO faculty (fid,fname, deptid) VALUES

->(11,'Harshith',1000),

->(12,'Mohan',1000),

->(13,'Kumar',1001),

->(14,'Shobha',1002),

->(15,'Shan',1000);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM faculty;

+-----+----------+--------+

mysql> INSERT INTO class (cname,meets_at,room,fid) VALUES

-> ('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);

Query OK, 8 rows affected (0.05 sec)

mysql> INSERT INTO enrolled (snum,cname) VALUES

-> (1,'class1'),

-> (2,'class1'),

-> (3,'class3'),

-> (4,'class3'),

-> (3,'class3'),

-> (5,'class4');

-> (1,'class5');

-> (2,'class5');

-> (3,'class5');

-> (4,'class5');

-> (5,'class5');

-> (6,'class5');

Query OK, 12 rows affected (0.03 sec)

mysql> SELECT * FROM enrolled;

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

FROM student s,class c,faculty f,enrolled e

WHERE s.snum=e.snum AND

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 IN (SELECT e.cname FROM enrolled e GROUP BY e.cname HAVING COUNT(*)>=5);

+---------+

| cname |

+---------+

| class10 |

| class5 |

+---------+

2 rows in set (0.00 sec)

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

WHERE s.snum IN (SELECT e1.snum

FROM enrolled e1,enrolled e2,class c1,class c2

WHERE e1.snum=e2.snum AND

e1.cname<>e2.cname AND

e1.cname=c1.cname AND

e2.cname=c2.cname AND

c1.meets_at=c2.meets_at );

+-------+

| sname |

+-------+

| jacob |

+-------+

1 row in set (0.00 sec)


Description : Outer part of the query extraxts the name of the students from table student. The inner part of the query
fetches the snum of the student, the student is enrolled to two different classes c1.cname and c2.cname, these two
classes meets at the name time.

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

WHERE f.fid in ( SELECT fid FROM class

GROUP BY fid HAVING COUNT(*)=(SELECT COUNT(DISTINCT room) FROM class) );

+--------+-----+

| fname | fid |

+--------+-----+

| Shobha | 14 |

+--------+-----+

1 row in set (0.00 sec)

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

WHERE f.fid IN ( SELECT c.fid

FROM class c, enrolled e

WHERE c.cname = e.cname GROUP BY c.cname HAVING COUNT(c.cname)< 5 );

+----------+
| 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.

5. Find the names of pilots certified for some Boeing aircraft.

6. Find the aid's of all aircraft that can be used on routes from Bangalore to Delhi.

Create:
mysql> CREATE DATABASE flights;

Query OK, 1 row affected (0.00 sec)

mysql> USE flights;

Database changed

mysql> CREATE TABLE flight(

-> no INT,

-> frm VARCHAR(20),

-> too VARCHAR(20),

-> distance INT,

-> departs VARCHAR(20),

-> arrives VARCHAR(20),

-> price REAL,

-> PRIMARY KEY (no) );

Query OK, 0 rows affected (0.17 sec)

mysql> CREATE TABLE aircraft(

-> aid INT,

-> aname VARCHAR(20),

-> cruisingrange INT,

-> PRIMARY KEY (aid) );


Query OK, 0 rows affected (0.19 sec)

mysql> CREATE TABLE employees(

-> eid INT,

-> ename VARCHAR(20),

-> salary INT,

-> PRIMARY KEY (eid) );

Query OK, 0 rows affected (0.29 sec)

mysql> CREATE TABLE certified(

-> eid INT,

-> aid INT,

-> PRIMARY KEY (eid,aid),

-> FOREIGN KEY (eid) REFERENCES employees (eid),

-> FOREIGN KEY (aid) REFERENCES aircraft (aid) );

Query OK, 0 rows affected (0.43 sec)

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);

Query OK, 7 rows affected (0.06 sec)

Records: 7 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM flight;

mysql> INSERT INTO aircraft (aid,aname,cruisingrange) values

(123,'Airbus',1000),

(302,'Boeing',5000),

(306,'Jet01',5000),

(378,'Airbus380',8000),

(456,'Aircraft',500),

(789,'Aircraft02',800),

(951,'Aircraft03',1000);

Query OK, 7 rows affected (0.07 sec)

mysql> SELECT * FROM aircraft;

mysql> INSERT INTO employees (eid,ename,salary) VALUES


(1,'Ajay',30000),

(2,'Ajith',85000),

(3,'Arnab',50000),

(4,'Harry',45000),

(5,'Ron',90000),

(6,'Josh',75000),

(7,'Ram',100000);

Query OK, 7 rows affected (0.29 sec)

Records: 7 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM employees;

mysql> INSERT INTO certified (edit,aid) VALUES

(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);

Query OK, 17 rows affected (0.30 sec)

Records: 17 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM certified;

Queries:
###1.Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs 80,000.

mysql> SELECT DISTINCT a.aname

-> FROM aircraft a,certified c,employees e

-> WHERE a.aid=c.aid

-> AND c.eid=e.eid


-> AND NOT EXISTS

-> (SELECT *

-> FROM employees e1

-> WHERE e1.eid=e.eid

-> AND e1.salary<80000);

+------------+

| aname |

+------------+

| Airbus |

| Boeing |

| Jet01 |

| Airbus380 |

| Aircraft02 |

+------------+

5 rows in set (0.00 sec)

###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.

mysql> SELECT c.eid,MAX(cruisingrange)

-> FROM certified c,aircraft a

-> WHERE c.aid=a.aid

-> GROUP BY c.eid

-> HAVING COUNT(*)>3;

+-----+--------------------+

| eid | max(cruisingrange) |

+-----+--------------------+

| 1 | 8000 |

+-----+--------------------+

1 row in set (0.00 sec)

###3.Find the names of all pilots whose salary is less than the price of the cheapest route from Bangalore to Frankfurt.

mysql> SELECT DISTINCT e.ename

-> FROM employees e

-> WHERE e.salary<

-> (SELECT MIN(f.price)

-> FROM flight f

-> WHERE f.frm='Bangalore'

-> AND f.too='Frankfurt');

+-------+

| ename |

+-------+

| Ajay |
| Ajith |

| Arnab |

| Harry |

| Ron |

| Josh |

+-------+

6 rows in set (0.00 sec)

###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.

mysql> SELECT a.aid,a.aname,AVG(e.salary)

-> FROM aircraft a,certified c,employees e

-> WHERE a.aid=c.aid

-> AND c.eid=e.eid

-> AND a.cruisingrange>1000

-> GROUP BY a.aid,a.aname;

+-----+-----------+---------------+

| aid | aname | avg(e.salary) |

+-----+-----------+---------------+

| 302 | Boeing | 73333.3333 |

| 306 | Jet01 | 57500.0000 |

| 378 | Airbus380 | 53333.3333 |

+-----+-----------+---------------+

3 rows in set (0.01 sec)

###5.Find the names of pilots certified for some Boeing aircraft.

mysql> SELECT distinct e.ename

-> FROM employees e,aircraft a,certified c

-> WHERE e.eid=c.eid

-> AND c.aid=a.aid

-> AND a.aname='Boeing';

+-------+

| ename |

+-------+

| Ajay |

| Ron |

| Ram |

+-------+

3 rows in set (0.00 sec)

###6.Find the aid's of all aircraft that can be used on routes from Bangalore to Delhi.

mysql> SELECT a.aid


-> FROM aircraft a
-> WHERE a.cruisingrange>
-> (SELECT MIN(f.distance)
-> FROM flight f
-> WHERE f.frm='Bangalore'
-> AND f.too='Delhi');
+-----+
| aid |
+-----+
| 378 |
+-----+
1 row in set (0.00 sec)

### PROGRAM 3
Consider the following schema for OrderDatabase: 
SALESMAN (Salesman_id, Name, City, Commission) 

CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id)

ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id) 

Write SQL queries to 

1. Count the customers with grades above Bangalore’saverage.

 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.

--Create Table SALESMAN with Primary Key as SALESMAN_ID

CREATE TABLE SALESMAN(


SALESMAN_ID INTEGER PRIMARY KEY,
NAME VARCHAR(20),
CITY VARCHAR(20),
COMMISSION VARCHAR(20));

DESC SALESMAN;

--------------------------------------

--Create Table CUSTOMER with Primary Key as CUSTOMER_ID and Foreign Key SALESMAN_ID referring the SALESMAN table

CREATE TABLE CUSTOMER(


CUSTOMER_ID INTEGER PRIMARY KEY,
CUST_NAME VARCHAR(20),
CITY VARCHAR(20),
GRADE INTEGER,
SALESMAN_ID INTEGER,
FOREIGN KEY (SALESMAN_ID) REFERENCES SALESMAN(SALESMAN_ID) ON DELETE SET NULL);

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

CREATE TABLE ORDERS(


ORDER_NO INTEGER PRIMARY KEY,
PURCHASE_AMOUNT DECIMAL(10,2),
ORDER_DATE DATE,
CUSTOMER_ID INTEGER,
SALESMAN_ID INTEGER,
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER(CUSTOMER_ID)ON DELETE CASCADE,
FOREIGN KEY (SALESMAN_ID) REFERENCES SALESMAN(SALESMAN_ID) ON DELETE CASCADE);

DESC ORDERS;

--Inserting records into SALESMAN table

INSERT INTO SALESMAN VALUES(1000,'RAHUL','BANGALORE','20%');


INSERT INTO SALESMAN VALUES(2000,'ANKITA','BANGALORE','25%');
INSERT INTO SALESMAN VALUES(3000,'SHARMA','MYSORE','30%');
INSERT INTO SALESMAN VALUES(4000,'ANJALI','DELHI','15%');
INSERT INTO SALESMAN VALUES(5000,'RAJ','HYDERABAD','15%');

SELECT * FROM SALESMAN;

------------------------------------------

--Inserting records into CUSTOMER table

INSERT INTO CUSTOMER VALUES(1,'ADYA','BANGALORE',100,1000);


INSERT INTO CUSTOMER VALUES(2,'BANU','MANGALORE',300,1000);
INSERT INTO CUSTOMER VALUES(3,'CHETHAN','CHENNAI',400,2000);
INSERT INTO CUSTOMER VALUES(4,'DANISH','BANGALORE',200,2000);
INSERT INTO CUSTOMER VALUES(5,'ESHA','BANGALORE',400,3000);

SELECT * FROM CUSTOMER;

------------------------------------------

--Inserting records into ORDERS table

INSERT INTO ORDERS VALUES(201,5000,'02-JUN-2020',1,1000);


INSERT INTO ORDERS VALUES(202,450,'09-APR-2020',1,2000);
INSERT INTO ORDERS VALUES(203,1000,'15-MAR-2020',3,2000);
INSERT INTO ORDERS VALUES(204,3500,'09-JUL-2020',4,3000);
INSERT INTO ORDERS VALUES(205,550,'05-MAY-2020',2,2000);

SELECT * FROM ORDERS;

-- Count the customers with grades above Bangalore’s average

SELECT GRADE,COUNT(DISTINCT CUSTOMER_ID)


FROM CUSTOMER
GROUP BY GRADE
HAVING GRADE>(SELECT AVG(GRADE)
FROM CUSTOMER
WHERE CITY='BANGALORE');

----------------------------------

--Find the name and numbers of all salesman who had more than one customer

SELECT SALESMAN_ID, NAME


FROM SALESMAN S
WHERE (SELECT COUNT(*)
FROM CUSTOMER C
WHERE C.SALESMAN_ID=S.SALESMAN_ID) > 1;

----------------------------------

--List all the salesman and indicate those who have and don’t have customers in their cities (Use UNION operation.)

SELECT S.SALESMAN_ID, S.NAME, C.CUST_NAME, S.COMMISSION


FROM SALESMAN S, CUSTOMER C
WHERE S.CITY=C.CITY
UNION
SELECT S.SALESMAN_ID,S.NAME,'NO MATCH',S.COMMISSION
FROM SALESMAN S
WHERE CITY NOT IN
(SELECT CITY
FROM CUSTOMER)
ORDER BY 1 ASC;

-----------------------------------

--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);

SELECT * FROM V_SALESMAN;

-----------------------------------

--Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also be deleted.

DELETE FROM SALESMAN


WHERE SALESMAN_ID=1000;

SELECT * FROM SALESMAN;

SELECT * FROM ORDERS;

##PROGRAM 4

Create a PL/SQL cursor program for electricity bill calculation


create table elect
(mno number(3) primary key,
cname varchar2(20),
cur_read number(5),
prev_read number(5),
no_units number(5),
amount number(8,2),
ser_tax
number(8,2),
net_amt number(9,2)
);

create or replace procedure powerbill


as
no_un elect.no_units%type;
amt elect.amount%type;
stax
elect.ser_tax%type; net
elect.net_amt%type; e
elect%rowtype;
cursor c9 is select * from elect for update;
begin
open c9;
loop
fetch c9 into e;
exit when c9%notfound;
no_un:=e.cur_read-
e.prev_read; amt:=no_un*1.5;
stax:=(5/100)*amt;
net:=amt+stax;
update elect set no_units=no_un,amount=amt,
ser_tax=stax,net_amt=net where current of c9;
dbms_output.put_line(&#39;customer meter number&#39;||e.mno);
dbms_output.put_line(&#39;customer name&#39;||e.cname);
dbms_output.put_line(&#39;total amount is &#39;||net);
end loop;
close c9;
end;

##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.

SQL>select * from student_enrollment;


ROLL_NO COURSE COURSE_COD SEM TOTAL_MARKS PERCENTAGE
--
111 cs 1001 1 300 50
112 cs 1001 1 400 66
113 is 1002 1 465 77
114 is 1002 1 585 97
SQL> create or replace procedure rank(crc varchar)
is
dis number:=0;
first number:=0;
sec number:=0;
pass number:=0;
cursor st is select * from student_enrollment;
r st%rowtype;
begin
0 open st;
loop
fetch st into r;
exit when st%notfound;
if(r.course=crc)
then
if(r.percentage&gt;=70 and r.percentage&lt;=100)
then
dis:=dis+1;
end if;
if(r.percentage&gt;=60 and r.percentage&lt;70)
then
first:=first+1;
end if;
if(r.percentage&gt;=50 and r.percentage&lt;60)
then
sec:=sec+1;
end if;
if(r.percentage&gt;=35 and r.percentage&lt;50)
then
pass:=pass+1;
end if;
end if;
end loop;
close st;
dbms_output.put_line(&#39;distinction is &#39;||dis);
dbms_output.put_line(&#39;first class is &#39;||first);
dbms_output.put_line(&#39;second class is &#39;||sec);
dbms_output.put_line(&#39;just pass is &#39;||pass);
end;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy