SQL Practise
SQL Practise
(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
)
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