0% found this document useful (0 votes)
39 views9 pages

SQL Assignment

sql query

Uploaded by

bavani456bavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views9 pages

SQL Assignment

sql query

Uploaded by

bavani456bavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

-- Create the Carrier table

CREATE TABLE Carrier (

CarrierId INTEGER PRIMARY KEY AUTOINCREMENT, -- Auto-generated unique identifier for each carrier

CarrierName VARCHAR(255) NOT NULL, -- Carrier name (e.g., Indigo, Jet Airways)

DiscountPercentageThirtyDaysAdvanceBooking INTEGER, -- Discount for bookings made 30 days in advance

DiscountPercentageSixtyDaysAdvanceBooking INTEGER, -- Discount for bookings made 60 days in advance

DiscountPercentageNinteyDaysAdvanceBooking INTEGER, -- Discount for bookings made 90 days in advance

RefundPercentageForTicketCancellation2DaysBeforeTravelDate INTEGER, -- Refund percentage for cancellation


2 days before travel

RefundPercentageForTicketCancellation10DaysBeforeTravelDate INTEGER, -- Refund percentage for cancellation


10 days before travel

RefundPercentageForTicketCancellation20DaysBeforeTravelDate INTEGER, -- Refund percentage for cancellation


20 days before travel

BulkBookingDiscount INTEGER, -- Discount percentage for bulk bookings

SilverUserDiscount INTEGER, -- Discount percentage for silver users

GoldUserDiscount INTEGER, -- Discount percentage for gold users

PlatinumUserDiscount INTEGER -- Discount percentage for platinum users

);

-- Insert sample data into the Carrier table

INSERT INTO Carrier (

CarrierName,

DiscountPercentageThirtyDaysAdvanceBooking,

DiscountPercentageSixtyDaysAdvanceBooking,
DiscountPercentageNinteyDaysAdvanceBooking,

RefundPercentageForTicketCancellation2DaysBeforeTravelDate,

RefundPercentageForTicketCancellation10DaysBeforeTravelDate,

RefundPercentageForTicketCancellation20DaysBeforeTravelDate,

BulkBookingDiscount,

SilverUserDiscount,

GoldUserDiscount,

PlatinumUserDiscount

VALUES

('Indigo', 5, 10, 15, 20, 40, 60, 5, 10, 15, 20),

('Jet Airways', 3, 6, 9, 10, 20, 30, 4, 8, 12, 16);

SELECT * FROM Carrier;

2.

-- Insert carrier details into the Carrier table

INSERT INTO Carrier (

CarrierName,

DiscountPercentageThirtyDaysAdvanceBooking,

DiscountPercentageSixtyDaysAdvanceBooking,

DiscountPercentageNinteyDaysAdvanceBooking,

RefundPercentageForTicketCancellation2DaysBeforeTravelDate,

RefundPercentageForTicketCancellation10DaysBeforeTravelDate,

RefundPercentageForTicketCancellation20DaysBeforeTravelDate,

BulkBookingDiscount,
SilverUserDiscount,

GoldUserDiscount,

PlatinumUserDiscount

VALUES

('Indigo', 5, 10, 15, 20, 40, 60, 5, 10, 15, 20),

('Jet Airways', 3, 6, 9, 10, 20, 30, 4, 8, 12, 16),

('SpiceJet', 4, 8, 12, 15, 25, 35, 6, 12, 18, 24),

('Air India', 7, 14, 21, 25, 45, 65, 7, 14, 21, 28);

3.

-- Create the Flight table

CREATE TABLE IF NOT EXISTS Flight (

FlightID INTEGER PRIMARY KEY AUTOINCREMENT, -- Auto-generated unique identifier for each flight

CarrierID INTEGER, -- Foreign Key referencing Carrier table

Origin VARCHAR(255) NOT NULL, -- Origin airport code or name

Destination VARCHAR(255) NOT NULL, -- Destination airport code or name

Airfare INTEGER NOT NULL, -- Airfare for the flight

SeatCapacityEconomyClass INTEGER NOT NULL, -- Seat capacity in Economy Class

SeatCapacityBusinessClass INTEGER NOT NULL, -- Seat capacity in Business Class

SeatCapacityExecutiveClass INTEGER NOT NULL, -- Seat capacity in Executive Class

FOREIGN KEY (CarrierID) REFERENCES Carrier (CarrierID) -- Foreign Key constraint

);

-- Optional: Insert sample data into the Flight table

INSERT INTO Flight (


CarrierID,

Origin,

Destination,

Airfare,

SeatCapacityEconomyClass,

SeatCapacityBusinessClass,

SeatCapacityExecutiveClass

VALUES

(1, 'JFK', 'LHR', 499, 180, 30, 10), -- Assuming CarrierID 1 exists in Carrier table

(2, 'LAX', 'ORD', 299, 150, 25, 5), -- Assuming CarrierID 2 exists in Carrier table

(1, 'SFO', 'MIA', 399, 160, 20, 8); -- Assuming CarrierID 1 exists in Carrier table

4.

-- Insert flight details into the Flight table

INSERT INTO Flight (

CarrierID,

Origin,

Destination,

Airfare,

SeatCapacityEconomyClass,

SeatCapacityBusinessClass,

SeatCapacityExecutiveClass

VALUES

(1, 'JFK', 'LHR', 499, 180, 30, 10), -- Flight 1 with CarrierID 1
(2, 'LAX', 'ORD', 299, 150, 25, 5), -- Flight 2 with CarrierID 2

(3, 'SFO', 'MIA', 399, 160, 20, 8), -- Flight 3 with CarrierID 3

(1, 'DFW', 'ATL', 350, 200, 35, 12), -- Flight 4 with CarrierID 1

(2, 'SEA', 'SFO', 220, 140, 20, 6); -- Flight 5 with CarrierID 2

5.

-- Query to get Carrier Name and total count of Flights for each Carrier

SELECT

c.CarrierName, -- Name of the carrier

COUNT(f.FlightID) AS FlightCount -- Total number of flights for the carrier

FROM

Carrier c

LEFT JOIN

Flight f ON c.CarrierID = f.CarrierID -- Join Carrier and Flight tables

GROUP BY

c.CarrierID, c.CarrierName -- Group by carrier ID and name

ORDER BY

FlightCount ASC; -- Order by flight count in ascending order

6.

-- Query to get FlightID, Carrier Name, Origin, Destination, and Airfare for the flight with the lowest airfare to a
given destination

SELECT

f.FlightID,

c.CarrierName,

f.Origin,

f.Destination,
f.Airfare

FROM

Flight f

JOIN

Carrier c ON f.CarrierID = c.CarrierID -- Join with Carrier table to get the carrier name

WHERE

f.Destination = 'YOUR_DESTINATION' AND -- Replace 'YOUR_DESTINATION' with the actual destination code or
name

f.Airfare = ( -- Subquery to find the lowest airfare for the given destination

SELECT MIN(Airfare)

FROM Flight

WHERE Destination = 'YOUR_DESTINATION'

);

7.

-- Query to retrieve FlightID, Carrier Name, Origin, Destination, Airfare, and available seats for flights to LHR

SELECT

f.FlightID,

c.CarrierName,

f.Origin,

f.Destination,

f.Airfare,

(f.SeatCapacityEconomyClass - COALESCE(fs.BookedSeatsEconomyClass, 0)) AS AvailableSeatsEconomyClass,

(f.SeatCapacityBusinessClass - COALESCE(fs.BookedSeatsBusinessClass, 0)) AS AvailableSeatsBusinessClass,

(f.SeatCapacityExecutiveClass - COALESCE(fs.BookedSeatsExecutiveClass, 0)) AS AvailableSeatsExecutiveClass

FROM
Flight f

JOIN

Carrier c ON f.CarrierID = c.CarrierID

LEFT JOIN

FlightSchedule fs ON f.FlightID = fs.FlightID

WHERE

f.Destination = 'LHR';

8.

-- Insert flight booking details into the FlightBooking table

INSERT INTO FlightBooking (CustomerID, FlightID, SeatClass, NumberOfSeats, BookingDate)

VALUES (YOUR_CUSTOMER_ID, YOUR_FLIGHT_ID, 'YOUR_SEAT_CLASS', YOUR_NUMBER_OF_SEATS, NOW());

9.

-- Update the booking status to 'Cancelled'

UPDATE FlightBooking

SET Status = 'Cancelled'

WHERE BookingID = YOUR_BOOKING_ID;

-- Update the FlightSchedule table to decrement booked seats

UPDATE FlightSchedule

SET

BookedSeatsEconomyClass = BookedSeatsEconomyClass - CASE WHEN (SELECT SeatClass FROM FlightBooking


WHERE BookingID = YOUR_BOOKING_ID) = 'Economy' THEN (SELECT NumberOfSeats FROM FlightBooking WHERE
BookingID = YOUR_BOOKING_ID) ELSE 0 END,

BookedSeatsBusinessClass = BookedSeatsBusinessClass - CASE WHEN (SELECT SeatClass FROM FlightBooking


WHERE BookingID = YOUR_BOOKING_ID) = 'Business' THEN (SELECT NumberOfSeats FROM FlightBooking WHERE
BookingID = YOUR_BOOKING_ID) ELSE 0 END,
BookedSeatsExecutiveClass = BookedSeatsExecutiveClass - CASE WHEN (SELECT SeatClass FROM FlightBooking
WHERE BookingID = YOUR_BOOKING_ID) = 'Executive' THEN (SELECT NumberOfSeats FROM FlightBooking
WHERE BookingID = YOUR_BOOKING_ID) ELSE 0 END

WHERE

FlightID = (SELECT FlightID FROM FlightBooking WHERE BookingID = YOUR_BOOKING_ID)

AND

BookingDate = (SELECT BookingDate FROM FlightBooking WHERE BookingID = YOUR_BOOKING_ID);

10

-- Delete flights that belong to a given carrier and for a specific travel destination

DELETE FROM Flight

WHERE CarrierID = YOUR_CARRIER_ID

AND Destination = 'YOUR_DESTINATION';

11.

-- Query to view all carrier names operating on a given travel route

SELECT DISTINCT c.CarrierName

FROM Carrier c

JOIN Flight f ON c.CarrierID = f.CarrierID

WHERE f.Origin = 'YOUR_ORIGIN'

AND f.Destination = 'YOUR_DESTINATION';

12.

-- Update airfare by increasing it by 10% for a given route and carrier

-- only if the minimum discount percentage is greater than 3%

UPDATE Flight

SET Airfare = Airfare * 1.10

WHERE CarrierID = YOUR_CARRIER_ID


AND Origin = 'YOUR_ORIGIN'

AND Destination = 'YOUR_DESTINATION'

AND EXISTS (

SELECT 1

FROM Carrier c

WHERE c.CarrierID = Flight.CarrierID

AND LEAST(

c.DiscountPercentageThirtyDaysAdvanceBooking,

c.DiscountPercentageSixtyDaysAdvanceBooking,

c.DiscountPercentageNinteyDaysAdvanceBooking

)>3

);

You might also like