Skip to content

Commit 35cb490

Browse files
committedJun 2, 2024
new codes
1 parent f4226a8 commit 35cb490

8 files changed

+157
-23
lines changed
 

‎first_last.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- By default, the frame clause is set to 'RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW'.
2-
-- It will access all records from the start to the end of the partition with 'RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING'.
2+
-- It will access/copy the final column value from the start to the end of that column with 'RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING'.
33
-- If you use 'ROWS' instead of 'RANGE' and end with 'CURRENT ROW' like this - 'ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW' - it will consider only the current row.
44

55
SELECT *,

‎lead_lag.sql

+10-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ select *,
3636
case when salary > lag(salary) over(partition by dept order by id) then 'Higher than previous employee'
3737
when salary = lag(salary) over(partition by dept order by id) then 'Same as previous employee'
3838
else 'Less than previous employee' end
39-
from employees;
39+
from employees;
40+
41+
42+
43+
-- Wthout LAG or LEAD
44+
45+
with cte as (select row_number() over() id, amount from orders)
46+
select a.id, a.amount - b.amount as rv from cte a left join cte b on a.id = b.id + 1 -- for previous add
47+
union all
48+
select a.id, a.amount - b.amount from cte a left join cte b on a.id = b.id - 1 -- for next subract

‎median.sql

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- median (odd set of numbers) = ((n+1)/2)th term
2+
-- median (even set of numbers) = ((n/2)th term + ((n/2)+1)th term)/2
3+
4+
with cte as (
5+
select student_id, sat_writing, rank() over(order by sat_writing) rn
6+
from sat_scores
7+
),
8+
cte2 as (
9+
select
10+
case
11+
when
12+
(count(*) % 2) = 0 then (count(*) / 2) + (count(*) / 2) + 1 -- even median
13+
else
14+
(count(*) + 1 ) / 2 end as cn -- odd median
15+
from cte
16+
)
17+
select cte.student_id, cte2.cn
18+
from cte, cte2
19+
where cte.rn=cte2.cn
20+
order by cte.student_id

‎recursive_cte.sql

+29-21
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
1-
WITH RECURSIVE ManagerHierarchy AS (
1+
-- Counting 1 to N
2+
3+
with recursive cte as (
4+
select 1 as n
5+
union all
6+
select n + 1 from cte where n < 10
7+
)
8+
select * from cte;
9+
10+
-- Pairs of number like [(0, 1), (1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
11+
12+
WITH RECURSIVE pairs AS (
13+
SELECT 0 AS first_number, 1 AS second_number
14+
UNION ALL
15+
SELECT first_number + 2, second_number + 2
16+
FROM pairs
17+
WHERE first_number + 2 <= 10
18+
)
19+
SELECT * from pairs;
20+
21+
-- Classical example of employee manager finding recursively
22+
23+
WITH RECURSIVE cte AS (
224
-- Anchor member: Select top-level managers (where manager_id is NULL)
325
SELECT
4-
id, name, manager_id, 0 AS level
26+
employee_id, name, manager_id
527
FROM
628
Employees
729
WHERE
8-
manager_id IS NULL
30+
manager_id IS NULL -- starting point with root node
931

1032
UNION ALL
1133

1234
-- Recursive member: Join with Employees table to find subsequent managers
1335
SELECT
14-
e.id, e.name,
15-
e.manager_id,
16-
mh.level + 1 AS level
36+
e.employee_id, e.name, e.manager_id
1737
FROM
1838
Employees e
1939
JOIN
20-
ManagerHierarchy mh
40+
cte c
2141
ON
22-
e.manager_id = mh.id
23-
)
24-
-- Final query: Select from the CTE to retrieve all levels of managers
25-
SELECT * FROM ManagerHierarchy
26-
ORDER BY level, id;
27-
28-
29-
30-
31-
with recursive cte as (
32-
select 1 as n
33-
union all
34-
select n + 1 from cte where n < 10
42+
e.employee_id = c.manager_id
3543
)
36-
select * from cte;
44+
SELECT * FROM cte;

‎tricky_questions/Cookbook_Recipes.sql

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
https://platform.stratascratch.com/coding/2089-cookbook-recipes/discussion?code_type=1
4+
5+
pairs cte will give this
6+
7+
First_number Second_number
8+
0 1
9+
2 3
10+
4 5
11+
6 7
12+
8 9
13+
10 11
14+
12 13
15+
14 15
16+
17+
*/
18+
19+
WITH pairs as (
20+
select
21+
(ROW_NUMBER() OVER (ORDER BY page_number) - 1 ) * 2 AS first_number,
22+
(ROW_NUMBER() OVER (ORDER BY page_number) - 1 ) * 2 + 1 AS second_number
23+
from cookbook_titles
24+
)
25+
SELECT
26+
p.first_number AS left_page_number,
27+
a.title AS left_title,
28+
b.title AS right_title
29+
FROM
30+
pairs p
31+
LEFT JOIN
32+
cookbook_titles a ON a.page_number = p.first_number
33+
LEFT JOIN
34+
cookbook_titles b ON b.page_number = p.second_number
35+
ORDER BY
36+
p.first_number;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
with cte as (
2+
select *,
3+
rank() over(partition by user_id order by created_at) as rn
4+
from marketing_campaign
5+
),
6+
first_day as (
7+
select * from cte where rn = 1
8+
),
9+
another_day as (
10+
select * from cte where rn > 1
11+
)
12+
select count(distinct a.user_id) from another_day a
13+
left join first_day b
14+
on a.user_id = b.user_id and a.product_id = b.product_id
15+
where b.product_id is null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- https://platform.stratascratch.com/coding/10319-monthly-percentage-difference/discussion?code_type=1
2+
3+
-- Using lag window function
4+
5+
with cte as (
6+
select
7+
to_char(created_at, 'YYYY-MM') year_month,
8+
sum(value) as curr_rv,
9+
lag(sum(value)) over() prev_rv
10+
from sf_transactions
11+
group by to_char(created_at, 'YYYY-MM')
12+
)
13+
select
14+
year_month,
15+
(curr_rv - prev_rv) * 100 / prev_rv as revenue_diff_pct
16+
from cte
17+
order by year_month;
18+
19+
20+
-- Without lag and using left join
21+
22+
with cte as (
23+
select
24+
to_char(created_at, 'YYYY-MM') as year_month,
25+
sum(value) as curr_rv
26+
from sf_transactions
27+
group by to_char(created_at, 'YYYY-MM')
28+
)
29+
select
30+
c1.year_month, c1.curr_rv, c2.curr_rv, c2.year_month,
31+
(c1.curr_rv - c2.curr_rv) * 100 / c2.curr_rv as revenue_diff_pct
32+
from cte c1
33+
left join cte c2
34+
on c1.year_month = to_char(to_date(c2.year_month, 'YYYY-MM') + interval '1 month', 'YYYY-MM')
35+
order by c1.year_month;

‎word_count.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- https://platform.stratascratch.com/coding/9814-counting-instances-in-text/official-solution?code_type=1
2+
3+
-- Counting Instances in Text
4+
5+
SELECT
6+
word,nentry
7+
FROM
8+
ts_stat('SELECT to_tsvector(contents) FROM google_file_store')
9+
WHERE
10+
ILIKE 'bull' or word ILIKE 'bear'
11+

0 commit comments

Comments
 (0)
Please sign in to comment.