0% found this document useful (0 votes)
4 views

22BCE11101-LAB-EXPERIMENT-DBMS[1]

The document contains a lab manual for a database management experiment, detailing the creation and manipulation of airline and sailing-related databases. It includes SQL commands for creating tables, inserting data, and querying information about flights, aircraft, employees, sailors, boats, and reservations. Various SQL queries are provided to extract specific information from the created tables.
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)
4 views

22BCE11101-LAB-EXPERIMENT-DBMS[1]

The document contains a lab manual for a database management experiment, detailing the creation and manipulation of airline and sailing-related databases. It includes SQL commands for creating tables, inserting data, and querying information about flights, aircraft, employees, sailors, boats, and reservations. Various SQL queries are provided to extract specific information from the created tables.
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/ 34

NAME: ABHINAV PORWAL

REG.NO:22BCE11101

LAB-MANUAL
EXPERIMENT-01

Q1) Consider the following relations containing airline flight


information:
Flights(flno: integer, from: string, to: string,distance: integer, departs:
time, arrives: time)
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 (otherwise, he or she would
not
qualify as a
pilot), and only pilots are certified to fly.
CREATE DATABASE AirlineDB;
USE AirlineDB;

CREATE TABLE Flights_22BCE11101 (


flno INT PRIMARY KEY,
`from` VARCHAR(50),
`to` VARCHAR(50),
distance INT,
departs TIME,
arrives TIME
);

INSERT INTO Flights_22BCE11101 (flno, `from`, `to`, distance,


departs, arrives) VALUES
(1, 'Bonn', 'Madras', 7000, '08:00:00', '20:00:00'),
(2, 'Bonn', 'London', 500, '09:00:00', '11:00:00'),
(3, 'London', 'New York', 3500, '12:00:00', '18:00:00');

CREATE TABLE Aircraft_22BCE11101(


aid INT PRIMARY KEY,
aname VARCHAR(50),
cruisingrange INT
);

INSERT INTO Aircraft (aid, aname, cruisingrange) VALUES


(1, 'Boeing 737', 3000),
(2, 'Airbus A320', 3500),
(3, 'Boeing 747', 8000);

CREATE TABLE Certified (


eid INT,
aid INT,
PRIMARY KEY (eid, aid),
FOREIGN KEY (eid) REFERENCES Employees(eid),
FOREIGN KEY (aid) REFERENCES Aircraft(aid)
);

CREATE TABLE Employees_22BCE11101 (


eid INT PRIMARY KEY,
ename VARCHAR(50),
salary INT
);

INSERT INTO Employees_22BCE11101 (eid, ename, salary)


VALUES
(1, 'John Doe', 120000),
(2, 'Jane Smith', 90000),
(4, 'Alice Johnson', 115000),
(5, 'Charlie Lee', 95000),
(6, 'Eve Davis', 85000);

INSERT INTO Employees (eid, ename, salary) VALUES


(1, 'John Doe', 120000),
(2, 'Jane Smith', 90000),
(4, 'Alice Johnson', 115000),
(5, 'Charlie Lee', 95000),
(6, 'Eve Davis', 85000);

-- Insert Sample Data into Certified


INSERT INTO Certified (eid, aid) VALUES
(1, 1),
(1, 3),
(2, 2);
Write the following queries in SQL
1) Find the eids of pilots certified for some Boeing aircraft

SELECT DISTINCT C.eid


FROM Certified C
JOIN Aircraft A ON C.aid = A.aid
WHERE A.aname LIKE '%Boeing%';

OUTPUT:
2) Find the names of pilots certified for some Boeing aircraft.
SELECT DISTINCT E.ename
FROM Employees E
JOIN Certified C ON E.eid = C.eid
JOIN Aircraft A ON C.aid = A.aid
WHERE A.aname LIKE '%Boeing%';

OUTPUT:
3) Find the aids of all aircraft that can be used on non-stop
flights from Bonn to Madras.
SELECT A.aid
FROM Aircraft A
JOIN Flights F ON F.distance <= A.cruisingrange
WHERE F.from = 'Bonn' AND F.to = 'Madras';
OUTPUT:
4) Identify the flights that can be piloted by every pilot whose
salary is more than $100,000.

SELECT F.flno
FROM Flights F
WHERE NOT EXISTS (
SELECT 1
FROM Employees E
WHERE E.salary > 100000
AND NOT EXISTS (
SELECT 1
FROM Certified C
JOIN Aircraft A ON C.aid = A.aid
WHERE C.eid = E.eid AND A.cruisingrange >= F.distance
)
);

OUTPUT:
5. Find the names of pilots who can operate planes with a range
greater than 3,000 miles but are not certified on any Boeing
aircraft.

SELECT E.ename
FROM Employees E
WHERE E.eid IN (
SELECT C.eid
FROM Certified C
JOIN Aircraft A ON C.aid = A.aid
WHERE A.cruisingrange > 3000
AND C.eid NOT IN (
SELECT C1.eid
FROM Certified C1
JOIN Aircraft A1 ON C1.aid = A1.aid
WHERE A1.aname LIKE '%Boeing%'
)
);
OUTPUT:
Q2) SAILORS (SID:INTEGER, SNAME:STRING, RATING:INTEGER,
AGE:REAL)
BOATS (BID:INTEGER, BNAME:STRING, COLOR:STRING)
RESERVES (SID:INTEGER, BID:INTEGER, DAY:DATE)

CREATE TABLE SAILORS (


SID INT PRIMARY KEY,
SNAME VARCHAR(50),
RATING INT,
AGE FLOAT
);

INSERT INTO SAILORS_22BCE11101 (SID, SNAME, RATING, AGE)


VALUES

(1, 'John Doe', 5, 25.5),


(2, 'Jane Smith', 7, 30.0),
(3, 'Bob Brown', 3, 35.2),
(4, 'Alice Johnson', 8, 22.1),
(5, 'Charlie Lee', 9, 29.3),
(6, 'Eve Davis', 4, 24.5),
(7, 'Frank Martin', 6, 28.0),
(8, 'Grace Kim', 5, 32.8);
CREATE TABLE BOATS_22BCE11101 (
BID INT PRIMARY KEY,
BNAME VARCHAR(50),
COLOR VARCHAR(50)
);
INSERT INTO BOATS_22BCE11101 (BID, BNAME, COLOR)
VALUES
(1, 'Boaty McBoatface', 'Red'),
(2, 'Sea Breeze', 'Blue'),
(3, 'Wave Rider', 'Green'),
(4, 'Ocean Explorer', 'Yellow'),
(5, 'Marine Marvel', 'White');
CREATE TABLE RESERVES_22BCE11101 (
SID INT,
BID INT,
DAY DATE,
PRIMARY KEY (SID, BID, DAY),
FOREIGN KEY (SID) REFERENCES SAILORS(SID),
FOREIGN KEY (BID) REFERENCES BOATS(BID)
);
INSERT INTO RESERVES_22BCE11101 (SID, BID, DAY) VALUES
(1, 1, '2024-07-20'),
(2, 2, '2024-07-21'),
(3, 3, '2024-07-22'),
(4, 4, '2024-07-23'),
(5, 5, '2024-07-24'),
(1, 2, '2024-07-25'),
(2, 3, '2024-07-26'),
(3, 4, '2024-07-27'),
(4, 5, '2024-07-28'),
(5, 1, '2024-07-29'),
(6, 1, '2024-07-30'),
(7, 2, '2024-07-31'),
(8, 3, '2024-08-01');

1. Display names & ages of all sailors.

SELECT SNAME, AGE


FROM SAILORS_22BCE11101;
2. Find all sailors with a rating above 7.

SELECT SNAME, RATING


FROM SAILORS_22BCE11101
WHERE RATING > 7;

3. Display all the names & colors of the boats.


SELECT BNAME, COLOR
FROM BOATS_22BCE11101;
4. Find all the boats with Red color.
SELECT BNAME, COLOR
FROM BOATS_22BCE11101
WHERE COLOR = 'Red';

5. Find the names of sailors who have reserved boat number


123.

SELECT S.SNAME
FROM SAILORS_22BCE11101 S
JOIN RESERVES R ON S.SID = R.SID
WHERE R.BID = 123;
6.Find SIDs of sailors who have reserved Pink Boat;

SELECT DISTINCT R.SID


FROM RESERVES_22BCE11101 R
JOIN BOATS B ON R.BID = B.BID
WHERE B.COLOR = 'Pink';
7. Find the color of the boats reserved by Rajesh.

SELECT B.COLOR
FROM SAILORS S
JOIN RESERVES R ON S.SID = R.SID
JOIN BOATS B ON R.BID = B.BID
WHERE S.SNAME = 'Rajesh';
8. Find names of the sailors who have reserved at least one
boat.

SELECT DISTINCT S.SNAME


FROM SAILORS S
JOIN RESERVES R ON S.SID = R.SID;
9. Find the names of sailors who have reserved a red or a green
boat.

SELECT DISTINCT S.SNAME


FROM SAILORS S
JOIN RESERVES R ON S.SID = R.SID
JOIN BOATS B ON R.BID = B.BID
WHERE B.COLOR IN ('Red', 'Green');
10. Find the names of sailors who have reserved boat 103.

SELECT S.SNAME
FROM SAILORS_22BCE11101 S
JOIN RESERVES R ON S.SID = R.SID
WHERE R.BID = 103;
11. Find the names of sailors who have not reserved boat 103.

SELECT S.SNAME
FROM SAILORS S
LEFT JOIN RESERVES R ON S.SID = R.SID AND R.BID = 103
WHERE R.BID IS NULL;
12. Find sailors whose rating is better than some sailor called
Rajesh.

SELECT S.SNAME, S.RATING


FROM SAILORS S
WHERE S.RATING > (
SELECT RATING
FROM SAILORS
WHERE SNAME = 'Rajesh'
);
13. Find the sailor's with the highest rating using ALL.

SELECT S.SNAME, S.RATING


FROM SAILORS S
WHERE S.RATING >= ALL (
SELECT RATING
FROM SAILORS_22bce11101
);

14. To count number SIDs of sailors in Sailors table


SELECT COUNT(SID) AS NumberOfSailors
FROM SAILORS_22BCE1101;
15. To count numbers of boats booked in Reserves table.

SELECT COUNT(DISTINCT BID) AS NumberOfBookedBoats


FROM RESERVES_22BCE11101;
16. To count number of Boats in Boats table.

SELECT COUNT(*) AS NumberOfBoats


FROM BOATS_22BCE11101;

17. To find age of Oldest Sailor.

SELECT MAX(AGE) AS OldestSailorAge


FROM SAILORS_22BCE11101;
18. To find age of Youngest Sailor.

SELECT MIN(AGE) AS YoungestSailorAge


FROM SAILORS_22BCE11101;
19. Find the average age of sailors with a rating of 10.

SELECT AVG(AGE) AS AverageAge


FROM SAILORS
WHERE RATING = 10;

20. Count the number of different sailor names.

SELECT COUNT(DISTINCT SNAME) AS NumberOfDifferentSailorNames


FROM SAILORS_22BCE11101;
21. Find the name and age of the oldest sailor.

SELECT SNAME, AGE


FROM SAILORS_22BCE11101
WHERE AGE = (
SELECT MAX(AGE)
FROM SAILORS
);
22. Count the number of Sailors.

SELECT COUNT(*) AS NumberOfSailors


FROM SAILORS_22BCE11101;
23. Find the names of sailors who are older than the oldest sailor
with a rating of 10.

SELECT SNAME
FROM SAILORS
WHERE AGE > (
SELECT MAX(AGE)
FROM SAILORS
WHERE RATING = 10
);
24. Display all the sailors according to their ages.

SELECT SNAME, AGE


FROM SAILORS_22BCE11101
ORDER BY AGE;
25. To display names of sailors according to alphabetical order.

SELECT SNAME
FROM SAILORS_22BCE11101
ORDER BY SNAME;

You might also like