Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Created June 8, 2017 11:56
Show Gist options
  • Save Blasanka/39426095a349eca68cc551e84d747036 to your computer and use it in GitHub Desktop.
Save Blasanka/39426095a349eca68cc551e84d747036 to your computer and use it in GitHub Desktop.
This gist for airport database. This was design to give some question and relevant queries for those questions. In query you have also provided queries for creating tables and and sample exercises.
-------------------DDL-----------------------
CREATE TABLE Customer(
customerId INT PRIMARY KEY,
name VARCHAR(30),
age INT,
phone CHAR(10)
)
CREATE TABLE Reservation(
flno CHAR(6),
custId INT,
CONSTRAINT pks PRIMARY KEY(flno, custId),
CONSTRAINT fk_fl FOREIGN KEY(flno) REFERENCES Flights(flno),
)
CREATE TABLE Flights(
flno CHAR(6) PRIMARY KEY,
from_ VARCHAR(30),
to_ VARCHAR(30),
departs DATETIME,
arrives DATETIME,
regNo CHAR(8),
pilotId int,
CONSTRAINT fk_pilot FOREIGN KEY(pilotId) REFERENCES Pilots(id)
)
CREATE TABLE Aircraft(
regNo CHAR(8) PRIMARY KEY,
model VARCHAR(15),
capacity INT
)
CREATE TABLE Pilots(
id INT PRIMARY KEY,
name VARCHAR(30),
salary FLOAT,
flyingHours INT
)
----------------------DML-----------------------
--INSERT
INSERT INTO Customer VALUES(1, 'asanka', 22, '07777777'),
(2, 'blasanka', 21, '07766777'),
(3, 'bl', 20, '067666677')
INSERT INTO Aircraft VALUES('12a', 'JET', 15),
('a31', 'Air', 56),
('33as', 'Air', 230),
('aa22', 'Air', 200),
('422s', 'Air', 130),
('ab12', 'Air', 201)
INSERT INTO Pilots VALUES(123, 'Nigma', 920000, 21),
(321, 'Jim', 453330,10),
(231, 'Capton Harvy', 295300, 10)
INSERT INTO Flights VALUES('a1', 'SL', 'India', GETDATE(), '2017-06-09', '12a', 123),
('b1', 'America', 'England', '2010-01-01', '2010-01-02', 'a31', 321),
('c1', 'China', 'Japan', '2010-01-30', '2010-01-30', '33as', 231),
('B04', 'Japan', 'Brisbane', '2010-02-28', '2010-02-28', '33as', 123),
('c4', 'Japan', 'Brisbane', '2010-02-28', '2010-02-28', '33as', 123)
--------------SELECT
--1. Find the registration number and the model of aircrafts that
-- departs to 'Brisbane' between pt January 2010 and 3pt May 2010.
SELECT a.RegNo, a.model
FROM flights f, Aircraft a
WHERE a.regNo = f.regNo AND f.to_ = 'Brisbane'
AND f.departs BETWEEN '2010-01-01'
AND '2010-05-31'
---2. For each aircraft that has a capacity over 200 display the registrtion
--number and the number of flights it has taken. If the aircraft has not taken
-- any flights display '0' for the number of flights.
SELECT a.RegNo, COUNT(f.regNo) AS NoOfFlights
FROM Aircraft a LEFT JOIN Flights f ON a.regNo = f.regNo
WHERE a.capacity > 200
GROUP BY a.regNo
--3. For each pilot who has flown an aircraft more than 100times ard has the number
-- of flying hours exceeding 5000, print the pilots name and the salary
SELECT p.name, p.salary
FROM Pilots p, Flights f
WHERE p.id = f.pilotId AND p.flyingHours > 5000
GROUP BY p.name, p.salary
HAVING COUNT(f.flno) > 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment