Introtallent.
com
# Q: find count of duplicate rows in the swiggy table*/
Select id,count(id) from [Link]
Group by id
Having count(id)>1
Order by count(id) desc;
#Q: /*-----------------Remove Duplicate records --------------------*/
/*How would you delete duplicate records from the table*/
/*Step1
create a new table by taking uniques record from original table*/
Create table project.swiggy1
As
Select Distinct * from [Link];
/*Step 2:
Delete the original table*/
DROP table [Link];
/*Step3:
Rename the new table to original table name*/
Rename table project.swiggy1 to [Link];
# Q: print records from row number 4 to 9
Select * from [Link]
limit 3,6;
[Link]
# Q: Find the latest order placed by customers.
with latest_order as
(select cust_id, outlet, order_date,
row_number() over(partition by cust_id order by order_date desc) as latest_ord_dt
from [Link]) select cust_id, outlet, order_date from latest_order where latest_ord_dt = 1;
# Q: Print order_id, partner_code, order_date, comment (No issues in place of null else comment)
Select order_id, partner_code,order_date,
(case
when comments is null then 'No issues'
else comments end) as comments
from [Link];
# Q: Print outlet wise order count, cumulative order count, total bill_amount, cumulative
bill_amount
Select [Link], a. order_cnt, @running_ord_count:=@running_ord_count + a.order_cnt AS
cumulative_cnt,
a.total_sale, @running_sale:=@running_sale + a.total_sale as cum_sale
from
(Select outlet, count(order_id) as order_cnt, sum(bill_amount) as total_sale from [Link]
group by outlet) a
Join (Select @running_ord_count:=0, @running_sale:=0) b
order by outlet;
# Q: Print cust_id wise, Outlet wise 'total number of orders'
Select cust_id,
sum(if(outlet='KFC',1,0)) KFC,
sum(if(outlet='Dominos',1,0)) Dominos,
sum(if(outlet='Pizza hut',1,0)) Pizza_hut
from [Link]
group by 1;
[Link]
# Q: Create a cross tab cust_id wise outlet wise total bill amount
Select cust_id,
sum(if(outlet='KFC',bill_amount,0)) KFC,
sum(if(outlet='Dominos',bill_amount,0)) Dominos,
sum(if(outlet='Pizza hut',bill_amount,0)) Pizza_hut
from [Link]
group by 1;
Select * from [Link];