-- Create the Carrier table
CREATE TABLE Carrier (
    carrierID INT PRIMARY KEY,
    CarrierName VARCHAR(50),
    DiscountPercentageThirtyDaysAdvanceBooking INT,
    DiscountPercentageSixtyDaysAdvanceBooking INT,
    DiscountPercentageNinetyDaysAdvanceBooking INT,
    RefundPercentageForTicketCancellation2DaysBeforeTravelDate INT,
    RefundPercentageForTicketCancellation10DaysBeforeTravelDate INT,
    RefundPercentageForTicketCancellation20DaysBeforeTravelDate INT,
    BulkBookingDiscount INT,
    SilverUserDiscount INT,
    GoldUserDiscount INT,
    PlatinumUserDiscount INT
);

-- Insert data into the Carrier table
INSERT INTO Carrier (
    carrierID, CarrierName, DiscountPercentageThirtyDaysAdvanceBooking,
    DiscountPercentageSixtyDaysAdvanceBooking, DiscountPercentageNinetyDaysAdvanceBooking,
    RefundPercentageForTicketCancellation2DaysBeforeTravelDate, RefundPercentageForTicketCancellation10DaysBeforeTravelDate,
    RefundPercentageForTicketCancellation20DaysBeforeTravelDate, BulkBookingDiscount, SilverUserDiscount, GoldUserDiscount, PlatinumUserDiscount
) VALUES
    (1, 'Indigo', 10, 15, 20, 60, 80, 100, 25, 10, 20, 25),
    (2, 'JetAirways', 15, 20, 25, 50, 70, 90, 25, 15, 20, 25),
    (3, 'KingFisher', 5, 10, 15, 40, 75, 90, 20, 10, 15, 20),
    (4, 'AirIndia', 8, 15, 20, 50, 75, 90, 30, 15, 20, 25);

-- Select all records from the Carrier table to verify data
SELECT * FROM Carrier;

-- Create the Flight table
CREATE TABLE Flight (
    FlightID INT AUTO_INCREMENT PRIMARY KEY,
    CarrierID INT,
    Origin VARCHAR(50),
    Destination VARCHAR(50),
    Airfare INT,
    SeatCapacityEconomyClass INT CHECK (SeatCapacityEconomyClass >= 20),
    SeatCapacityBusinessClass INT CHECK (SeatCapacityBusinessClass >= 10),
    SeatCapacityExecutiveClass INT CHECK (SeatCapacityExecutiveClass >= 10),
    FOREIGN KEY (CarrierID) REFERENCES Carrier(carrierID)
);

-- Insert data into the Flight table with corrected CarrierID
INSERT INTO Flight (FlightID, CarrierID, Origin, Destination, AirFare, SeatCapacityEconomyClass, SeatCapacityBusinessClass, SeatCapacityExecutiveClass)
VALUES
    (123, 1, 'Origin1', 'Destination1', 200, 100, 50, 20),
    (124, 2, 'Origin2', 'Destination2', 300, 150, 70, 30),
    (125, 3, 'Origin3', 'Destination3', 400, 200, 100, 40),
    (126, 4, 'Origin4', 'Destination4', 250, 120, 60, 25);

-- Select all records from the Flight table to verify data
SELECT * FROM Flight;


-- Write SQL Query to get Carrier Name and total count of Flights that are tagged to each Carrier. Should display the result in the ascending order of flights count for each carrier
-- As an Admin, I should be able to search count of flights that are tagged to a each carrier

SELECT C.CarrierName, COUNT(F.FlightID) AS TotalFlights
FROM Carrier C
LEFT JOIN Flight F ON C.CarrierID = F.CarrierID
GROUP BY C.CarrierID, C.CarrierName
ORDER BY TotalFlights ASC;

-- Write SQL Query to get FligthID, Carrier Name, Origin, Destination and Airfare for a given travel destination that has the lowest airfare for the given trave destination

SELECT F.FlightID, C.CarrierName, F.Origin, F.Destination, F.Airfare
FROM Flight F
JOIN Carrier C ON F.CarrierID = C.CarrierID
WHERE F.Destination = 'Destination1'
ORDER BY F.Airfare ASC
LIMIT 1;

-- creating flight shedule
CREATE TABLE FlightSchedule(
    FlightScheduleID INT PRIMARY KEY,
    FlightID INT,
    DateOfTravel DATE,
    BusinessClassBookedCount INT,
    EconomyClassBookedCount INT,
    ExecutiveClassBookedCount INT,
    FOREIGN KEY (FlightID) REFERENCES Flight(FlightID)
);

INSERT INTO FlightSchedule (FlightScheduleID, FlightID, DateOfTravel, BusinessClassBookedCount, EconomyClassBookedCount, ExecutiveClassBookedCount)
VALUES
    (1, 123, '2024-06-01', 5, 50, 10),
    (2, 124, '2024-06-15', 10, 30, 5),
    (3, 125, '2024-07-01', 15, 20, 5),
    (5, 126, '2024-08-01', 10, 20, 5);
-- displaying flight shedule 
  select * from FlightSchedule;
  
-- total no of seats that are not booked yet in Economy, Business and executive class for a given travel destination 

SELECT F.FlightID, C.CarrierName, F.Origin, F.destination, F.Airfare, (F.SeatCapacityEconomyClass - FS.EconomyClassBookedCount) AS AvailableEconomySeats,
(F.SeatCapacityBusinessClass - FS.BusinessClassBookedCount) AS AvailableBusinessSeats,
(F.SeatCapacityExecutiveClass - FS.ExecutiveClassBookedCount) AS AvailableEconomySeats FROM Flight F

JOIN Carrier C on F.CarrierID = C.CarrierID
JOIN FlightSchedule FS ON F.FlightID = FS.FlightID
WHERE F.destination = 'Destination1';

-- Create the FLightBooking TABLE

CREATE TABLE FlightBooking(
   BookingID INT PRIMARY KEY,
   FlightID INT,
   UserID INT,
   NoOfSeats INT,
   SeatCategory VARCHAR(50) CHECK (SeatCategory IN('Economy','Executive','Business')),
   DateOfTravel DATE,
   BookingStatus VARCHAR(50) CHECK (BookingStatus IN('Booked', 'Travel Completed','Cancelled')),
   BookingAmount INT,
   FOREIGN KEY (FlightID) REFERENCES Flight(FlightID)
);

-- Inserting data into the flightbooking TABLE

INSERT INTO Flightbooking(BookingID,FlightID,UserID,NoOfSeats,SeatCategory,DateOfTravel,BookingStatus,BookingAmount)
VALUES (1,123,101,2,'Economy','2024-06-01','Booked',400),
(2,124,102,1,'Business','2024-06-15','Booked',300),
(3,125,103,3,'Executive','2024-07-01','Booked',600),
(4,126,104,1,'Economy','2024-08-01','Travel Completed',200);


SELECT * FROM FlightBooking;

-- sql query to remove all flight that belongto given carrier and for a specific travel destination 
DELETE FS 
FROM FlightSchedule FS
JOIN Flight F on FS.FlightID = F.FlightID
WHERE F.CarrierID =1 and F.destination= 'Destination1';

DELETE FB 
FROM FlightBooking FB
JOIN Flight F on FB.FlightID = F.FlightID
WHERE F.CarrierID =1 and F.destination= 'Destination1';

DELETE FROM Flight
WHERE CarrierID = 1 and Destination= 'Destination1';

select *from Flight;


-- sql query to view allcarrier names that operate on a given travel rote 

select distinct C.CarrierName
from Carrier C
Join Flight F on C.CarrierID = F.CarrierID
where F.Origin = 'Origin3' and F.Destination = 'Destination3';

-- uodate airfare by 10% for a given travel route and carrier if the minimum discount percentage 
-- is greater than 3% 


update Flight 
SET Airfare = Airfare * 1.10
where CarrierID = (select CarrierID
from Carrier 
where CarrierID = 1
AND LEAST (DiscountPercentageThirtyDaysAdvanceBooking,DiscountPercentageNinetyDaysAdvanceBooking
,DiscountPercentageSixtyDaysAdvanceBooking,RefundPercentageForTicketCancellation20DaysBeforeTravelDate,
RefundPercentageForTicketCancellation10DaysBeforeTravelDate,RefundPercentageForTicketCancellation2DaysBeforeTravelDate,
BulkBookingDiscount,SilverUserDiscount,GoldUserDiscount,PlatinumUserDiscount) > 3)

AND Origin = 'Origin4'
and Destination = 'Destination4';




 

MySQL online editor

Write, Run & Share MySQL queries online using OneCompiler's MySQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MySQL. Getting started with the OneCompiler's MySQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MySQL' and start writing queries to learn and test online without worrying about tedious process of installation.

About MySQL

MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.

Key Features:

  • Open-source relational database management systems.
  • Reliable, very fast and easy to use database server.
  • Works on client-server model.
  • Highly Secure and Scalable
  • High Performance
  • High productivity as it uses stored procedures, triggers, views to write a highly productive code.
  • Supports large databases efficiently.
  • Supports many operating systems like Linux*,CentOS*, Solaris*,Ubuntu*,Windows*, MacOS*,FreeBSD* and others.

Syntax help

Commands

1. CREATE

CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

Example

CREATE TABLE EMPLOYEE (
  empId INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL
);

2. ALTER

ALTER TABLE Table_name ADD column_name datatype;

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');

3. TRUNCATE

TRUNCATE table table_name;

4. DROP

DROP TABLE table_name;

5. RENAME

RENAME TABLE table_name1 to new_table_name1; 

6. COMMENT

Single-Line Comments:

 --Line1;

Multi-Line comments:

   /* Line1,
   Line2 */

DML Commands

1. INSERT

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Note: Column names are optional.

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');

2. SELECT

SELECT column1, column2, ...
FROM table_name
[where condition]; 

Example

SELECT * FROM EMPLOYEE where dept ='sales';

3. UPDATE

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

Example

UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001'; 

4. DELETE

DELETE FROM table_name where condition;

Example

DELETE from EMPLOYEE where empId='0001'; 

Indexes

1. CREATE INDEX

  CREATE INDEX index_name on table_name(column_name);
  • To Create Unique index:
  CREATE UNIQUE INDEX index_name on table_name(column_name);

2. DROP INDEX

DROP INDEX index_name ON table_name;

Views

1. Create a View

Creating a View:
CREATE VIEW View_name AS 
Query;

2. How to call view

SELECT * FROM View_name;

3. Altering a View

ALTER View View_name AS 
Query;

4. Deleting a View

DROP VIEW View_name;

Triggers

1. Create a Trigger

CREATE TRIGGER trigger_name trigger_time trigger_event
    ON tbl_name FOR EACH ROW [trigger_order] trigger_body
/* where
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } */

2. Drop a Trigger

DROP TRIGGER [IF EXISTS] trigger_name;

Stored Procedures

1. Create a Stored Procedure

CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;

2. How to call Stored procedure

CALL sp_name;

3. How to delete stored procedure

DROP PROCEDURE sp_name;

Joins

1. INNER JOIN

SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;

2. LEFT JOIN

SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;

3. RIGHT JOIN

SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;

4. CROSS JOIN

SELECT select_list from TABLE1 CROSS JOIN TABLE2;