Lesson End Project
Lesson End Project
1)ER Diagram
Alter Table Customer
ADD primary key (Customer_ID);
3)Write a query to display all the passengers (customers) who have travelled in routes 01 to 25.
Take data from the passengers_on_flights table.
4)Write a query to identify the number of passengers and total revenue in business class from
the ticket_details table.
6) Write a query to extract the customers who have registered and booked a ticket. Use data
from the customer and ticket_details tables.
7)Write a query to identify the customer’s first name and last name based on their customer ID
and brand (Emirates) from the ticket_details table.
8)Write a query to identify the customers who have travelled by Economy Plus class using
Group By and Having clause on the passengers_on_flights table.
9)Write a query to identify whether the revenue has crossed 10000 using the IF clause on the
ticket_details table.
10)Write a query to create and grant access to a new user to perform operations on a database.
11) Write a query to find the maximum ticket price for each class using window functions on the
ticket_details table.
With Max_price as (
select Class_id,Price_per_ticket as Max_price,
Rank() Over (partition by Class_id Order by Price_Per_ticket DESC) as Max_rank
from ticket_details)
Select distinct* from Max_price
where Max_rank=1;
12)Write a query to extract the passengers whose route ID is 4 by improving the speed and
performance of the passengers_on_flights table.
13)For the route ID 4, write a query to view the execution plan of the passengers_on_flights table
Select P.*,C.First_name,C.Last_name from passengers_on_flights as P
Left join Customer as C
On P.customer_id=C.customer_id
Where P.route_id=4;
14. Write a query to calculate the total price of all tickets booked by a customer across different
aircraft IDs using rollup function.
15)Write a query to create a view with only business class customers along with the brand of
airlines.
Create View Luxury
as
select C.customer_id,C.first_name,C.last_name,T.brand,T.class_id
from ticket_details as T Left join Customer as C
On T.customer_ID=C.customer_id
where Class_id="bussiness" ;
16)Write a query to create a stored procedure to get the details of all passengers flying between a
range of routes defined
in run time. Also, return an error message if the table doesn't exist----CREATION OF STORED
PROCEDURE QUERY-.
Delimiter $$
Create procedure Route2(In Range1 int,In Range2 int)
Begin
Select C.first_name,C.last_name,R.route_Id,P.customer_id
from routes as R inner join passengers_on_flights as P
On R.Route_id=P.Route_id
Left join customer as C
using (customer_id)
where R.route_id between Range1 and Range2;
End $$
Delimiter ;
Call Route2(7 , 13);
17)Write a query to create a stored procedure that extracts all the details from the routes table
where the travelled distance is more than 2000 miles
Delimiter $$
Create procedure Distance()
Begin
Select * from routes
where distance_miles>2000;
End $$
Call Distance();
18)Write a query to create a stored procedure that groups the distance travelled by each flight into
three categories.
The categories are, short distance travel (SDT) for >=0 AND <= 2000 miles, intermediate distance
travel (IDT) for >2000 AND <=6500,
and long-distance travel (LDT) for >6500.
Delimiter $$
Create Procedure Dist_cat(in Flight_num1 int)
Begin
Select *,
Case
when Distance_miles>0 and Distance_miles<=2000 then "short distance"
When Distance_miles>2000 and Distance_miles<=6500 then "intermediate Distance"
Else "long Distance"
end as catagory
from routes
where Flight_num=flight_num1;
End $$
Call Dist_cat(1111);
19)Write a query to extract ticket purchase date, customer ID, class ID and specify if the
complimentary services are provided for the
specific class using a stored function in stored procedure on the ticket_details table.
Condition: If the class is Business and Economy Plus, then complimentary services are given as
Yes, else it is No
Delimiter $$
Create procedure Complimentary (in customerID int)
Begin
select p_date,customer_id,class_id,
Case
when class_id="bussiness" or class_id="economy plus" then "Yes"
Else "No"
end as Complimentary_service
from ticket_details
where customer_id=CustomerID;
End $$
Call Complimentary(27);
Alternative Method:
Delimiter $$
Create function Complimentary(Class_ID Varchar(80))
Returns Varchar(20)
Deterministic
Begin
Declare Complimentary_service Varchar(20);
If Class_ID="Bussiness" or Class_ID="Economy Plus" Then
Set Complimentary_service="Yes";
Else
Set Complimentary_Service="No";
End IF;
Return (Complimentary_Service);
End $$
20)Write a query to extract the first record of the customer whose last name ends with Scott using
a cursor from the customer table.
Delimiter $$
Create procedure first_record()
Begin
declare a varchar(20);
declare b varchar(20);
declare c int ;
declare cursor_1 cursor for select first_name,Last_name,Customer_id from customer
where last_name="scott";
Open Cursor_1;
Fetch cursor_1 into a,b,c;
select a as first_name,b as Last_name,c as Customer_id;
close cursor_1;
End $$
call first_record();
Describe proj_table;
Describe data_science_team;
Describe emp_record_table;
Update emp_record_table
SET Proj_Id = Null
Where Proj_ID="NA";
3.Write a query to fetch EMP_ID, FIRST_NAME, LAST_NAME, GENDER, and DEPARTMENT from the
employee record table, and make a list of employees and details of their department.
5.Write a query to concatenate the FIRST_NAME and the LAST_NAME of employees in the Finance
department from the employee table and then give the resultant column alias as NAME.
6.Write a query to list only those employees who have someone reporting to them. Also, show the
number of reporters (including the President).
Select X.EMP_ID,X.First_Name,X.Last_Name,X.Role,X.Exp,Count(Y.EMP_ID)
From emp_record_table As X Inner Join emp_record_table as Y
On X.EMp_ID=Y.Manager_ID
Group By X.Emp_ID
Order By X.Emp_ID;
7.Write a query to list down all the employees from the healthcare and finance departments using
union. Take data from the employee record table.
8.Write a query to list down employee details such as EMP_ID, FIRST_NAME, LAST_NAME, ROLE,
DEPARTMENT, and EMP_RATING grouped by dept. Also include the respective employee rating
along with the max emp rating for the department.
Select Emp_ID,First_Name,Last_Name,Role,Dept,Emp_Rating,max(Emp_Rating)
OVER (Partition By Dept)
From emp_record_table;
9.Write a query to calculate the minimum and the maximum salary of the employees in each role.
Take data from the employee record table.
10.Write a query to assign ranks to each employee based on their experience. Take data from the
employee record table.
Select First_Name,Last_Name,Exp,
dense_rank() Over ( Order By Exp) as Position
From emp_record_table;
11.Write a query to create a view that displays employees in various countries whose salary is
more than six thousand. Take data from the employee record table.
12.Write a nested query to find employees with experience of more than ten years. Take data from
the employee record table.
13.Write a query to create a stored procedure to retrieve the details of the employees whose
experience is more than three years. Take data from the employee record table.
Delimiter $$
Create Procedure Emp_Exp()
Begin
Select * From emp_record_table
Where Exp>3;
End $$
Call Emp_Exp
14.Write a query using stored functions in the project table to check whether the job profile
assigned to each employee in the data science team matches the organization’s set standard.
The standard being:
For an employee with experience less than or equal to 2 years assign 'JUNIOR DATA SCIENTIST',
For an employee with the experience of 2 to 5 years assign 'ASSOCIATE DATA SCIENTIST',
For an employee with the experience of 5 to 10 years assign 'SENIOR DATA SCIENTIST',
For an employee with the experience of 10 to 12 years assign 'LEAD DATA SCIENTIST',
For an employee with the experience of 12 to 16 years assign 'MANAGER'.
Delimiter $$
Create procedure Job_Profile()
Begin
Select Emp_ID,First_Name,Last_name,Role,Exp,
Case
When Exp<=2 Then'JUNIOR DATA SCIENTIST'
When Exp>2 and Exp<=5 Then'ASSOCIATE DATA SCIENTIST'
When Exp>5 and Exp<=10 Then'SENIOR DATA SCIENTIST'
When Exp>10 and Exp<=12 Then'LEAD DATA SCIENTIST'
When Exp>12 and Exp<=16 Then 'MANAGER'
End as Role
From emp_record_table;
End $$
Call Job_profile();
Method 2:
Delimiter $$
Create Function Employee_Role(Exp int)
Returns Varchar(50)
Deterministic
Begin
Declare Emp_Role Varchar(50);
IF Exp>12 and 16 Then
Set Emp_Role="Manager";
Elseif Exp>10 and 12 Then
Set Emp_Role = "Lead Data Scientist";
Elseif Exp>5 and 10 Then
Set Emp_Role = "Senior Data Scientist";
Elseif Exp>2 and 5 Then
Set Emp_Role="Associate Data Scientist";
Elseif Exp<=2 Then
Set Emp_Role = "Junior Data Scientist";
End IF;
Return (Emp_Role);
End $$
15.Create an index to improve the cost and performance of the query to find the employee whose
FIRST_NAME is ‘Eric’ in the employee table after checking the execution plan.
16.Write a query to calculate the bonus for all the employees, based on their ratings and salaries
(Use the formula: 5% of salary * employee rating).
17.Write a query to calculate the average salary distribution based on the continent and country.
Take data from the employee record table.
Select Emp_ID,First_name,Last_name,Salary,Country,Continent,
Avg(Salary) Over (Partition by Country) as Avg_Country,
Avg(Salary) Over (Partition by Continent) as Avg_Continent
From emp_record_table;
DATA CLEANING
We can not work on Raw data as if something happen our data so we can not do anything so to
resolve this we can create similar table .
1. Remove Duplicates
2. Standardize the data – TRIM/TRAILING
3. Null Values and blank values
4. Remove any Columns
-- Data Cleaning
Use world_layoff;
INSERT Layoffs_Staging
SELECT * from layoffs;
UPDATE layoffs_1
SET Company = TRIM(COMPANY);
UPDATE layoffs_1
SET INDUSTRY = "Crypto"
where INDUSTRY like "CRYPTO%";
UPDATE layoffs_1
SET Country = "United State"
where INDUSTRY like "United State%";
Select `Date`,str_to_date(`date`,"%m/%d/%Y") from layoffs_1;
UPDATE layoffs_1
SET `Date`= str_to_date(`date`,"%m/%d/%Y");
UPDATE layoffs_1 as L1
JOIN layoffs_1 as L2
ON L1.company = L2.Company
SET L1.Industry = L2.Industry
where (L1.Industry is Null or L1.Industry = '')
And L2.Industry is Not Null AND L2.Industry<>'';