From 418333f0b25a4ebf180890cff919add3757dd558 Mon Sep 17 00:00:00 2001 From: tejas274 Date: Thu, 29 May 2025 21:16:03 -0400 Subject: [PATCH 1/4] problem 1 added --- problem4-511-game-play-analysis.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 problem4-511-game-play-analysis.sql diff --git a/problem4-511-game-play-analysis.sql b/problem4-511-game-play-analysis.sql new file mode 100644 index 0000000..33f34e2 --- /dev/null +++ b/problem4-511-game-play-analysis.sql @@ -0,0 +1,6 @@ +select + player_id, + min(event_date) as first_login +from activity +group by player_id +order by player_id \ No newline at end of file From 4d17ff50dc0e5b8240757383bb7d1b0ee934ace9 Mon Sep 17 00:00:00 2001 From: tejas274 Date: Tue, 3 Jun 2025 10:53:00 -0400 Subject: [PATCH 2/4] problem 3 added --- ...5-average-salary-department-vs-company.sql | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 problem3-615-average-salary-department-vs-company.sql diff --git a/problem3-615-average-salary-department-vs-company.sql b/problem3-615-average-salary-department-vs-company.sql new file mode 100644 index 0000000..92d2f1b --- /dev/null +++ b/problem3-615-average-salary-department-vs-company.sql @@ -0,0 +1,70 @@ +-- window function + +with cte as ( + select + e.department_id, + date_format(pay_date, '%Y-%m') as pay_month, + case + when + avg(amount) over (partition by date_format(pay_date, '%Y-%m')) + > avg(amount) over (partition by e.department_id, date_format(pay_date, '%Y-%m')) + then 'lower' + when + avg(amount) over (partition by date_format(pay_date, '%Y-%m')) + < avg(amount) over (partition by e.department_id, date_format(pay_date, '%Y-%m')) + then 'higher' + else + 'same' + end as 'comparison' + from salary as s + inner join + employee as e + on s.employee_id = e.employee_id +) + +select + pay_month, + department_id, + comparison +from cte +group by pay_month, department_id, comparison +order by pay_month, department_id + + + +--- with aggregation way + +with cte_monthly_avg as ( + select + date_format(pay_date, '%Y-%m') as pay_month, + avg(amount) as avg_amount + from + salary + group by date_format(pay_date, '%Y-%m') + +), + +cte_depart_monthly_avg as ( + select + e.department_id, + date_format(pay_date, '%Y-%m') as pay_month, + avg(amount) as dpt_avg_salary + from salary as s + inner join + employee as e + on s.employee_id = e.employee_id + group by date_format(pay_date, '%Y-%m'), e.department_id + +) + +select + cdma.pay_month, + cdma.department_id, + case + when cdma.dpt_avg_salary > cma.avg_amount then 'higher' + when cdma.dpt_avg_salary < cma.avg_amount then 'lower' + else 'same' + end as comparison +from cte_depart_monthly_avg as cdma +inner join cte_monthly_avg as cma + on cdma.pay_month = cma.pay_month \ No newline at end of file From b76c402cb6c1f32a85b9d1ecc7eb2a8b2730283c Mon Sep 17 00:00:00 2001 From: tejas274 Date: Wed, 4 Jun 2025 12:25:12 -0400 Subject: [PATCH 3/4] problem1-1225-Report-Contiguos-Dates.sql --- problem1-1225-Report-Contiguos-Dates.sql | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 problem1-1225-Report-Contiguos-Dates.sql diff --git a/problem1-1225-Report-Contiguos-Dates.sql b/problem1-1225-Report-Contiguos-Dates.sql new file mode 100644 index 0000000..2656bfc --- /dev/null +++ b/problem1-1225-Report-Contiguos-Dates.sql @@ -0,0 +1,43 @@ +WITH combine_states AS ( + SELECT + fail_date AS `date`, + 'failed' AS period_state + FROM Failed + where year(fail_date) = 2019 + + UNION ALL + + SELECT + success_date AS `date`, + 'succeeded' AS period_state + FROM Succeeded + where year(success_date) = 2019 +) + +, cte_based_on_state as ( + SELECT + `date`, + period_state, + CASE + WHEN LAG(period_state) OVER (ORDER BY date) = period_state THEN 0 + ELSE 1 + END AS state_change_flag + FROM combine_states + order by `date` +) + +,grouped_rank AS ( + SELECT + `date`, + period_state, + SUM(state_change_flag) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rnk + FROM cte_based_on_state +) + +select +period_state, +min(`date`) as start_date, +max(`date`) as end_date +from +grouped_rank +group by rnk \ No newline at end of file From 3c42648be115fce5a49976aac79541a30939ebca Mon Sep 17 00:00:00 2001 From: tejas274 Date: Thu, 5 Jun 2025 12:03:52 -0400 Subject: [PATCH 4/4] problem2 added --- problem2-618-student-report-geography.sql | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 problem2-618-student-report-geography.sql diff --git a/problem2-618-student-report-geography.sql b/problem2-618-student-report-geography.sql new file mode 100644 index 0000000..d2dc9db --- /dev/null +++ b/problem2-618-student-report-geography.sql @@ -0,0 +1,34 @@ +WITH am_con AS ( + SELECT + NAME AS America, + ROW_NUMBER() OVER (ORDER BY name) AS rnk + FROM Student + WHERE continent = 'America' +), +eu_con AS ( + SELECT + NAME AS Europe, + ROW_NUMBER() OVER (ORDER BY name) AS rnk + FROM Student + WHERE continent = 'Europe' +), +as_con AS ( + SELECT + NAME AS Asia, + ROW_NUMBER() OVER (ORDER BY name) AS rnk + FROM Student + WHERE continent = 'Asia' +) + +select + am_con.America, + as_con.Asia, + eu_con.Europe +from + am_con +left join + eu_con +on am_con.rnk = eu_con.rnk +left join + as_con +on am_con.rnk = as_con.rnk \ No newline at end of file