Advance SQL With Rajan Chettri
Advance SQL With Rajan Chettri
use world;
Please Note: the Limitation of Subquery : observe the line no 11 : we are fetching Name column
from Country Table.
If i wish to fetch columns from countrylanguage table and include it in output i can’t do it in
subquery.
Because subquery cannot fetch columns from multiple tables , it can fetch column from single
table only.
This is exactly where Joins are needed, joins allows us to include columns from multiple tables.
Summary: you can correlate rows of 2 tables using both subquery and join but u need join to
display columns from multiple tables as subquery can show column from 1 table only.
File : restaurant.sql
Link:
https://drive.google.com/file/d/1mr3RJY4OXEEqS_s9pXs76mq0Mn7Oz1uo/view?usp=sharing
Answer: PhoneNo
Task : You are asked to find the Loyal Customers of the Restaurant !
Mathematically ? Intersection
SQL : inner join
Inner join fetches common rows from both table using common columns.
Select
<columns>
<Table1.CommonColumn> = <Table2.CommonColumn>;
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
Note: please check walkinContact & Online Contact Column are we getting only common
contacts from both Table ?
-- Left Join : Left Join Fetches ALL Rows from Left Table and ONly Matching Rows from
Right Table
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
-- Right Join : All Rows from Right Table and Only Matching Rows from Left Table .
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
-- Full Join : Full JOin Fetches ALL ROWS FROM BOTH TABLES.
-- Full Join is implemented By Merging Output of Left and Right Join using union Keyword.
We will also answer interview question difference between union vs union All.
-- here is an example
Summary : union eliminates duplicate rows where as union all retains duplicate rows.
Implementing Full Join :
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
union
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
You are asked to Calculate New Prices when Each Product is Sold with All the other
Accessories
select productid , pname , pprice, aname, aprice , (pprice+aprice) as 'ComboPrice' from
products cross join accessories;
Ranking Functions
Use rankingdb;
from employee;
select * ,
from employee;
select * ,
from employee;
Task : you are asked to Rank Each Employee for Each Fiscal year based on sale in Desc!!
select * ,
dense_rank() over(order by sale desc) as 'empRank'
from sales1;
select * ,
dense_rank() over(partition by fiscal_year order by sale desc) as 'empRank'
from sales1;
select * ,
dense_rank() over(partition by fiscal_year order by sale desc) as 'empRank'
from sales1
)
select * from salesNew where emprank=1;
select name,countrycode,population ,
dense_rank() over (partition by countrycode order by population desc) as cityRank
from city where countrycode in ('chn','idn','aus');
with topCities
as
(
select name,countrycode,population ,
dense_rank() over (partition by countrycode order by population desc) as cityRank
from city where countrycode in ('chn','idn','aus')
)
select * from topCities where cityRank=1;
-- you are asked to find the name Most Populated CITY from CHINA , INDONESIA ,
AUSTRALIA
Employees.sql
https://drive.google.com/file/d/1rDTvxl4eTMj_ovZKP9w3Bh_u3A19tasb/view?usp=sharing
Task : You are Asked to Prepare a Report which show
select emp_no,salary,
-- the Script that we just RAN created a Database in our system named 'world'
-- check database
use world;
-- now we will check Tables Inside World Database
show tables;
-- using AND
-- another example
-- using NOT
-- Range Operator i.e Between and Not Between (this is used to capture data in Range)
-- Activity
-- Task : Find the details of Languages which are spoken by 80 to 90 percent of People and are
Official
-- Languages ONLY.
select * from countrylanguage where percentage between 80 and 90 and IsOfficial = 'T';
-- Aggregate Functions : agg. functions help us find statistical Facts from Data.
select
max(population) as 'Max Population',
min(population) as 'Min Population',
sum(population) as 'Sum Population',
avg(population) as 'Avg Population',
count(population) as 'Count Population'
from city;
-- count: function returns count of values in column excluding Nulls.
-- Rajdeep's Doubt
-- difference in union vs union all
union
-- Grouping
-- Nested Queries
-- case 1
-- case 2
-- Step 1 : we will fetch CountryCodes from Countrylanguage Table where language matches
with
-- ‘Hindi’;
CODE IN
(
select countrycode from countrylanguage where language='hindi'
);
use employees;
-- First we will fetch the emp_no from titles where title matches with ‘senior Engineer’
-- Then we will compare emp_no from titles with emp_no from employees to find the
-- First_name and last_name;
use restaurants_db;
-- Select
-- <columns>
-- From <Table1> Inner join <Table2>
-- ON
-- <Table1.CommonColumn> = <Table2.CommonColumn>;
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
-- Left Join : Left Join Fetches ALL Rows from Left Table and ONly The Matching Rows from
Right Table
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
-- Right Join : All Rows from Right Table and Only Matching Rows from Left Table .
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
-- Full Join : Full JOin Fetches ALL ROWS FROM BOTH TABLES.
-- Full Join is implemented By Merging Output of Left and Right Join using union Keyword.
-- here is an example
union
select
wc.customername as 'walkinCustomer',
oc.customername as 'onlineCustomer',
wc.phoneno as 'walkinContact',
oc.phoneno as 'onlineContact',
ratings
-- Cross Join : Cross join is also called Cartesian Product , in Cross Join each row of one table
-- is multiple with all the rows of Other Table.
-- Diwali is near , the Electronic store owner has decided to launch combo Offers on Products
select * from products;
select * from accessories;
use rankingdb;
select * from employee;
-- 1 Row_number()
-- 2 Rank()
-- 3 Dense_Rank()
select * ,
from employee;
select * ,
from employee;
-- if 2 people got the same rank they will split the prize money
-- 3rd one will get the 3rd RANK so he gets prize money for the 3rd Rank only.
-- mohan 1 50rs
-- ramesh 1 50rs
-- suresh 3 10rs
select * ,
from employee;
select * ,
dense_rank() over(partition by fiscal_year order by sale desc) as 'empRank'
from sales1;
-- Syntax :
-- with TempTable
-- as
-- ( <QueryGoes Here> )
-- select * from TempTable;
with salesNew
as
(
select * ,
dense_rank() over(partition by fiscal_year order by sale desc) as 'empRank'
from sales1
)
select * from salesNew;
select * ,
dense_rank() over(partition by fiscal_year order by sale desc) as 'empRank'
from sales1 where empRank=1;
with salesNew
as
(
select * ,
dense_rank() over(partition by fiscal_year order by sale desc) as 'empRank'
from sales1
)
select * from salesNew where emprank=1;
USE world;
-- you are asked to find the name Most Populated CITY from CHINA , INDONESIA ,
AUSTRALIA
select name,countrycode,population ,
dense_rank() over (partition by countrycode order by population desc) as cityRank
from city where countrycode in ('chn','idn','aus');
-- LEAD
use rankingdb;
select* from sales1;
select * ,
from sales1;
select * ,
from sales1;
use employees;
select * from salaries;
select emp_no,salary,
with SalaryHistory
as
(select emp_no,salary,