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

Lesson End Project

Uploaded by

RaviBisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lesson End Project

Uploaded by

RaviBisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Air_Car Analysis

Create database AIRCARGO_Analysis;


Use AIRCARGO_Analysis;

Alter table Customer


Modify date_of_birth Date;

Alter table passengers_on_flights


Modify travel_date Date;

Alter table ticket_details


Modify p_date Date;

1)ER Diagram
Alter Table Customer
ADD primary key (Customer_ID);

Alter table passengers_on_flights


Add foreign key (Customer_id) references Customer(Customer_id);

Alter Table Routes


ADD unique (Route_id);

Alter table ticket_details


Add foreign key (Customer_id) references Customer(Customer_id);

Alter table passengers_on_flights


Add foreign key (route_id) references routes(Route_id);

2)Check Contraint Distance and Flight number (Not Null)


Alter Table routes
Add Check (flight_num IS Not Null),
Add Check (Distance_miles>0);

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.

Select * from passengers_on_flights


Where Route_id between 01 and 25;

4)Write a query to identify the number of passengers and total revenue in business class from
the ticket_details table.

Select Count(Customer_id),sum(no_of_tickets * Price_per_ticket)


from ticket_details
where Class_id="bussiness";
5)Write a query to display the full name of the customer by extracting the first name and last
name from the customer table;

Select Concat (first_name," ",last_name) from customer ;

6) Write a query to extract the customers who have registered and booked a ticket. Use data
from the customer and ticket_details tables.

Select C.First_name , C.last_name ,T.Customer_id,T.class_id


From ticket_details as T Left Join Customer as C
On T.customer_id=C.customer_id;

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.

Select C.First_name , C.last_name ,T.Customer_id,T.class_id


From ticket_details as T Left Join Customer as C
On T.customer_id=C.customer_id
where T.brand = "Emirates";

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.

Select C.First_name , C.Last_name,p.class_id from passengers_on_flights as P


Left join customer as C
on P.customer_id=C.customer_id
Group by 1,2,3
having Class_id="Economy Plus";

9)Write a query to identify whether the revenue has crossed 10000 using the IF clause on the
ticket_details table.

Select IF(sum(price_per_ticket)>10000,"yes,it crossed","No") from ticket_details;

10)Write a query to create and grant access to a new user to perform operations on a database.

Create User ravibisht@localhost;

Grant all on aircargo_analysis.* to ravibisht@localhost;

11) Write a query to find the maximum ticket price for each class using window functions on the
ticket_details table.

Select distinct class_id,price_per_ticket from (


Select t.*,
Rank() over (partition by class_id order by price_per_ticket Desc) as Max_price
from ticket_details as t) X
Where X.Max_price=1;

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;

Select Distinct Class_ID,


Max(Price_Per_Ticket) Over(partition by Class_ID ) as Max_Price
From ticket_details;

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.

Select P.customer_id,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;

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.

Select Aircraft_id,Sum(price_per_ticket) from ticket_details


Group by Aircraft_iD with rollup;

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 $$

Select p_Date,Customer_id,Class_id,Complimentary(Class_ID) from ticket_details;

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();

ScienceQtech Employee Performance Mapping


Use Employee;

Describe proj_table;
Describe data_science_team;
Describe emp_record_table;

Alter table emp_record_table


Add foreign key (Proj_ID) references Proj_table(Project_ID);

ALter Table Proj_table


Drop Column `MyUnknownColumn_[1]`;

Select Distinct Proj_ID from 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.

Select Emp_ID,First_Name,Last_Name,Gender,Dept from emp_record_table;

4.Write a query to fetch EMP_ID, FIRST_NAME, LAST_NAME, GENDER, DEPARTMENT, and


EMP_RATING if the EMP_RATING is:
less than two
greater than four
between two and four

Select Emp_ID,First_Name,Last_Name,Gender,Dept,Emp_Rating from emp_record_table


Where Emp_Rating<2;

Select Emp_ID,First_Name,Last_Name,Gender,Dept,Emp_Rating from emp_record_table


Where Emp_Rating>4;

Select Emp_ID,First_Name,Last_Name,Gender,Dept,Emp_Rating from emp_record_table


Where Emp_Rating between 2 and 4;

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.

Select concat(First_Name," ",Last_name) as NAME from emp_record_table


Where DEPT="Finance";

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.

Select First_Name,Last_Name,Dept From emp_record_table


Where Dept="Finance"
Union
Select First_Name,Last_Name,Dept From emp_record_table
Where Dept="Healthcare";

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.

Select Role,min(Salary),Max(Salary) from emp_record_table


Group By Role
Order By Salary DESC;

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.

Create View Employee_In_Various_Country as


Select First_Name,Last_Name,Gender,Role,Salary,Country
From emp_record_table
Where Salary>6000;

Select * from employee_in_various_country;

12.Write a nested query to find employees with experience of more than ten years. Take data from
the employee record table.

Select Emp_ID,First_Name,Last_Name,Exp From emp_record_table


Where Exp in ( Select Exp from emp_record_table
Where Exp>10);
Method 2:
SELECT EMP_ID,FIRST_NAME,LAST_NAME,EXP FROM emp_record_table
WHERE EMP_ID IN(SELECT manager_id FROM emp_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 $$

Select Exp,Employee_Role(Exp) from data_science_team;

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.

Create Index Id_First_Name on emp_record_table (First_Name(20));


Select * from emp_record_table
Where First_name = "Eric";

Drop Index IdX_First_Name on emp_record_table;

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).

Alter Table emp_record_table


Add Bonus Int;
Update emp_record_table
Set Bonus = (Select Salary *0.05*Emp_Rating)
Select * from emp_record_table;

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;

SELECT * FROM layoffs;

CREATE TABLE Layoffs_Staging


Like layoffs;

INSERT Layoffs_Staging
SELECT * from layoffs;

Select * From (Select *,


ROW_NUMBER() OVER
(PARTITION BY
company,location,industry,total_laid_off,percentage_laid_off,date,stage,country,funds_raised_mi
llions) As row_num
from Layoffs_Staging) Layoffs_Staging
where row_num>1;

Select * from layoffs_1


where company = "Casper";

Delete from Layoffs_Staging


where row_num>1;

CREATE TABLE `layoffs_1` (


`company` text,
`location` text,
`industry` text,
`total_laid_off` int DEFAULT NULL,
`percentage_laid_off` text,
`date` text,
`stage` text,
`country` text,
`funds_raised_millions` int DEFAULT NULL,
`row_num` int
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO layoffs_1


Select *,
ROW_NUMBER() OVER
(PARTITION BY
company,location,industry,total_laid_off,percentage_laid_off,date,stage,country,funds_raised_mi
llions) As row_num
from layoffs;

Select * from layoffs_1;

DELETE FROM layoffs_1


WHERE row_num>1;

ALTER TABLE layoffs_1


DROP column row_num;

SELECT TRIM(COMPANY) FROM layoffs_1;

UPDATE layoffs_1
SET Company = TRIM(COMPANY);

SELECT distinct(INDUSTRY) From layoffs_1;

SELECT * from layoffs_1


where INDUSTRY like 'CRYPTO%';

UPDATE layoffs_1
SET INDUSTRY = "Crypto"
where INDUSTRY like "CRYPTO%";

SELECT DISTINCT(COuntry) from layoffs_1;

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");

Alter table layoffs_1


MODIFY COLUMN `DATE` DATE ;

SELECT * From layoffs_1


WHERE total_laid_off is null and percentage_laid_off is null;

SELECT * From layoffs_1


WHERE Industry ='' or InDUSTRY is null;

Select * From layoffs_1


WHERE Company = 'Airbnb';

Select * from layoffs_1 as L1


JOIN layoffs_1 as L2
ON L1.company = L2.Company
where (L1.Industry is Null or L1.Industry = '')
And L2.Industry is Not Null AND L2.Industry<>'';

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<>'';

You might also like