Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Apples and Oranges
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

WITH APPLES AS
( SELECT SALE_DATE, SOLD_NUM FROM SALES WHERE FRUIT='APPLES') ,

ORANGES AS(SELECT SALE_DATE, SOLD_NUM FROM SALES WHERE FRUIT='ORANGES')

SELECT A.SALE_DATE, (A.SOLD_NUM-O.SOLD_NUM ) AS 'DIFF' FROM APPLES A JOIN ORANGES O ON A.SALE_DATE=O.SALE_DATE ORDER BY A.SALE_DATE;
1 change: 1 addition & 0 deletions Customer Who Bought All Products
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select distinct(c.customer_id) from customer c right join product p on c.product_key=p.product_key group by c.customer_id having count( distinct p.product_key) = (select count(distinct(product_key)) from product)
11 changes: 11 additions & 0 deletions Market Analysis II
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
with cte as(
select o.order_date, o.item_id, o.seller_id, rank() over (partition by o.seller_id order by o.order_date) as 'rank' from orders o
),
ACTE AS
(select c.item_id, c.seller_id , i.item_brand from cte c join items i on c.item_id=i.item_id where c.rank=2)

select u.user_id as 'seller_id',
CASE
when a.item_brand = u.favorite_brand then 'yes'
else 'no'
END as '2nd_item_fav_brand' from ACTE a right join users u on a.seller_id=u.user_id
16 changes: 16 additions & 0 deletions Sales Analysis III
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
WITH cte AS (
SELECT
product_id,
year,
quantity,
price,
RANK() OVER (PARTITION BY product_id ORDER BY year ASC) AS rnk
FROM Sales
)
SELECT
product_id,
year AS first_year,
quantity,
price
FROM cte
WHERE rnk = 1;
1 change: 1 addition & 0 deletions Top Travellers
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select u.name, IFNULL(sum(r.distance),0) as travelled_distance from users u left JOIN RIDES r on u.id=r.user_id group by r.user_id order by travelled_distance DESC, u.name ASC;
11 changes: 11 additions & 0 deletions Tournament Winners
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

with cte as(
select first_player as player_id, sum(first_score) as total from (select first_player, first_score from matches
union all
select second_player, second_score from matches) a group by first_player
) ,

acte as (select c.player_id, p.group_id, c.total , row_number() over (partition by p.group_id order by c.total desc ,c.player_id ASC) as rnk from cte c join players p on
c.player_id=p.player_id)

select group_id, player_id from acte where rnk=1;