SQL Practise

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

WITH total_energy AS

(SELECT *
FROM fb_eu_energy eu
UNION ALL SELECT *
FROM fb_asia_energy asia
UNION ALL SELECT *
FROM fb_na_energy na),
energy_by_date AS
(SELECT date, sum(consumption) AS total_energy
FROM total_energy
GROUP BY date
ORDER BY date ASC),
max_energy AS
(SELECT max(total_energy) AS max_energy
FROM energy_by_date)
SELECT ebd.date,
ebd.total_energy
FROM energy_by_date ebd
JOIN max_energy me ON ebd.total_energy = me.max_energy

-----------------------------------------------------------
SELECT DISTINCT(a1.user_id)
FROM amazon_transactions a1
JOIN amazon_transactions a2 ON a1.user_id=a2.user_id
AND a1.id <> a2.id
AND a2.created_at::date-a1.created_at::date BETWEEN 0 AND 7
ORDER BY a1.user_id
------------------------------------------------------------
SELECT first_name,
sum(total_order_cost) AS total_order_cost,
order_date
FROM orders o
LEFT JOIN customers c ON o.cust_id = c.id
WHERE order_date BETWEEN '2019-02-1' AND '2019-05-1'
GROUP BY first_name,
order_date
HAVING sum(total_order_cost) =
(SELECT max(total_order_cost)
FROM
(SELECT sum(total_order_cost) AS total_order_cost
FROM orders
WHERE order_date BETWEEN '2019-02-1' AND '2019-05-1'
GROUP BY cust_id,
order_date) b)
---------------------------------------------------
with all_user_sessions as (
SELECT t1.user_id, t1.timestamp::date as date,
min(t2.timestamp::TIMESTAMP) - max(t1.timestamp::TIMESTAMP) as
session_duration
FROM facebook_web_log t1
JOIN facebook_web_log t2 ON t1.user_id = t2.user_id
WHERE t1.action = 'page_load'
AND t2.action = 'page_exit'
AND t2.timestamp > t1.timestamp
GROUP BY 1, 2)
SELECT user_id, avg(session_duration)
FROM all_user_sessions
GROUP BY user_id
------------------------------
with max_votes as ( select review_text, sum(cool) as votes from yelp_reviews group
by 1)
select business_name, y.review_text from max_votes v inner join yelp_reviews y on
v.review_text = y.review_text where votes = (select max(votes) from max_votes)
=============================
with highest as (select max(salary)as ms,department from employee group by
department)
select e.department,first_name,salary from employee e left join highest h on
h.department=e.department
where h.ms=e.salary
--------------------------
select first_name,order_date,order_details,total_order_cost from (select c.id,
c.first_name, o.order_date, o.order_details, o.total_order_cost from customers c
left join orders o on o.cust_id=c.id
where first_name in ('Jill','Eva')
order by c.id) d
----------------
SELECT to_char(created_at::date, 'YYYY-MM') AS year_month,
round(((sum(value) - lag(sum(value), 1) OVER w) / (lag(sum(value), 1) OVER
w)) * 100, 2) AS revenue_diff_pct
FROM sf_transactions
GROUP BY year_month
WINDOW w AS (
ORDER BY to_char(created_at::date, 'YYYY-MM'))
ORDER BY year_month ASC
-------------------------------------
select cust_id,sum(total_order_cost) as revenue from orders
where order_date::date between '2019-03-01' and '2019-03-31'
group by cust_id
order by revenue desc
------------------------------
with joined as (select count(o.id) as oid,count(c.id) as
cust_id,c.city,sum(o.total_order_cost),count(city) as countc from customers c
left join orders o on o.cust_id=c.id
group by c.city)
select j.oid as number_of_orders,j.cust_id as number_of_customers, j.city,sum as
total_cost_of_orders from joined j
where countc>=5
======================
with cte as
(
select
empid,
Time,
In_Out,
lag(In_Out, 1) over (partition by empid order by Time as previous_In_Out,
lag(Time), 1) over (partition by empid order by Time) as previous_Time

from Employee
)

select empid, convert(date,Time), datediff(minute, previous_Time, Time) as


time_between_minutes
where previous_In_Out = 'In'
group by empid, date
-------------------------------------------------------
WITH hosts AS
(SELECT concat(price, room_type, host_since, zipcode, number_of_reviews) AS
host_id,
number_of_reviews,
price
FROM airbnb_host_searches
GROUP BY 1,2,3)
SELECT host_popularity AS host_pop_rating,
min(price) AS min_price,
avg(price) AS avg_price,
max(price) AS max_price
FROM
(SELECT CASE
WHEN number_of_reviews = 0 THEN 'New'
WHEN number_of_reviews BETWEEN 1 AND 5 THEN 'Rising'
WHEN number_of_reviews BETWEEN 6 AND 15 THEN 'Trending Up'
WHEN number_of_reviews BETWEEN 16 AND 40 THEN 'Popular'
WHEN number_of_reviews > 40 THEN 'Hot'
END AS host_popularity,
price
FROM hosts) a
GROUP BY host_pop_rating
---------------------
SELECT a.date,
count(b.user_id_receiver)/count(a.user_id_sender)::decimal AS
percentage_acceptance
FROM
(SELECT date, user_id_sender,
user_id_receiver
FROM fb_friend_requests
WHERE action='sent' ) a
LEFT JOIN
(SELECT date, user_id_sender,
user_id_receiver
FROM fb_friend_requests
WHERE action='accepted' ) b ON a.user_id_sender=b.user_id_sender
AND a.user_id_receiver=b.user_id_receiver
GROUP BY a.date
--------------------------
select count(distinct violation_id),extract(year from inspection_date::date) as
year from sf_restaurant_health_violations
where business_name ='Roxanne Cafe' group by year
-------------------------------------------------------
with f1 as (select sum(value) as summ,to_char(created_at::date, 'YYYY-MM') as
year_month from sf_transactions group by year_month order by year_month),
f2 as (select lag(summ) over() as last_month_summ,* from f1)
select year_month,round(((summ-last_month_summ)/last_month_summ)*100,2) as
revenue_diff_pct from f2
=-----------------------------------------------------------
with f1 as (select m1.user_id,m1.acc_id,m2.paying_customer,m3.date,m3.downloads
from ms_user_dimension m1 left join ms_acc_dimension m2 on m1.acc_id=m2.acc_id left
join ms_download_facts m3 on m1.user_id=m3.user_id),
f2 as (select date,sum(downloads) as paying from f1 where paying_customer='yes'
group by date),
f3 as (select date,sum(downloads) as non_paying from f1 where paying_customer='no'
group by date)
select f2.paying,f3.non_paying,f2.date from f2 left join f3 on f2.date=f3.date
where f2.paying<f3.non_paying
same------------------------------
SELECT date, non_paying,
paying
FROM
(SELECT date, sum(CASE
WHEN paying_customer = 'yes' THEN downloads
END) AS paying,
sum(CASE
WHEN paying_customer = 'no' THEN downloads
END) AS non_paying
FROM ms_user_dimension a
INNER JOIN ms_acc_dimension b ON a.acc_id = b.acc_id
INNER JOIN ms_download_facts c ON a.user_id=c.user_id
GROUP BY date
ORDER BY date) t
WHERE (non_paying - paying) >0
ORDER BY t.date ASC
-----------
SELECT split_part('ordno-#-orddt-#-ordamt', '-#-', 2);
-----------------------------
with full as (select e.name as Employee,d.name as Department, e.salary as
Salary1,max(e.salary) over(partition by d.name) as Salary from Employee e
left join Department d on d.id=e.departmentId)
select Department,Employee,Salary from full
where Salary1=Salary

same-----------------
with temp as
(select d.name as Department,e.name as Employee,e.salary,dense_rank()
over(partition by d.name order by e.salary desc) as salary_rank from Employee e
join Department d on e.departmentId=d.id)
select Department,Employee,salary from temp where salary_rank < 2
------------------------
declare @retrunVal int;
with cte as
(
select e.*, dense_rank() over (order by salary desc) rank
from employee e
)
select @retrunVal = max(distinct salary)
from cte where rank = @N

return @retrunVal;
--------------------------------
There's a grocery store sales transaction table with columns Tnx ID, customer
ID, product ID, Date of Tnx, quantity, price. Every sales transaction that happens
at the store, gets stored in this table.

How will you calculate the number of customers that made their first purchase last
month, using SQL?
with total as (select *, row_number over(partition by date customer_id order by
date) as buying_count from transaction),total2 as
(select * from total where
date_trunc('month', Date of Tnx)= date_trunc('month', current_date - interval '1'
month))
select count(distinct cust_id) from total2 where buying_count=1

You might also like