From 6ca9f67b0b9b12993b256063074c7cc86cd7d98b Mon Sep 17 00:00:00 2001 From: Feminto Date: Thu, 29 May 2025 21:09:18 -0700 Subject: [PATCH 1/3] Adding SQL solution for problem 4 --- game_analysis_1 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 game_analysis_1 diff --git a/game_analysis_1 b/game_analysis_1 new file mode 100644 index 0000000..278b6d0 --- /dev/null +++ b/game_analysis_1 @@ -0,0 +1,21 @@ +-- Method 1 +SELECT player_id, + MIN(event_date) first_login +FROM activity +GROUP BY 1; + +-- Method 2 +SELECT DISTINCT player_id, + FIRST_VALUE(event_date) OVER(PARTITION BY player_id ORDER BY event_date RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) first_login +FROM activity; + +-- Method 3 +SELECT player_id, + event_date AS first_login +FROM ( + SELECT player_id, + event_date, + DENSE_RANK() OVER(PARTITION BY player_id ORDER BY event_date) rnk + FROM activity +)a +WHERE rnk = 1 \ No newline at end of file From edfbf21bcfc44fb5a06a53467fae72dcfe543f0e Mon Sep 17 00:00:00 2001 From: Feminto Date: Mon, 2 Jun 2025 20:42:08 -0700 Subject: [PATCH 2/3] Adding solution for AverageSalaryDepartmentvsCompany problem --- AverageSalaryDepartmentvsCompany.sql | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 AverageSalaryDepartmentvsCompany.sql diff --git a/AverageSalaryDepartmentvsCompany.sql b/AverageSalaryDepartmentvsCompany.sql new file mode 100644 index 0000000..724c241 --- /dev/null +++ b/AverageSalaryDepartmentvsCompany.sql @@ -0,0 +1,26 @@ +-- Solution for Leetcode problem AverageSalaryDepartmentvsCompany +WITH dept AS ( + SELECT DATE_FORMAT(s.pay_date, '%Y-%m') as pay_month, + e.department_id, + AVG(s.amount) AS d_avg + FROM salary s + JOIN employee e + ON s.employee_id = e.employee_id + GROUP BY 1,2 +), +comp AS( + SELECT DATE_FORMAT(pay_date, '%Y-%m') as pay_month, + AVG(amount) AS c_avg + FROM salary + GROUP BY 1 +) +SELECT d.pay_month, + d.department_id, + CASE + WHEN d.d_avg > c.c_avg THEN 'higher' + WHEN d.d_avg < c.c_avg THEN 'lower' + WHEN d.d_avg = c.c_avg THEN 'same' + END AS comparison +FROM dept d +JOIN comp c +ON d.pay_month = c.pay_month; \ No newline at end of file From c83e5f0232b0f93885a2bad50a60924cd62a4afc Mon Sep 17 00:00:00 2001 From: Feminto Date: Sun, 8 Jun 2025 16:28:50 -0700 Subject: [PATCH 3/3] Adding whole SQL solution --- Report Contiguos Dates.sql | 25 ++++++++++ Student Report By Geography.sql | 83 +++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 Report Contiguos Dates.sql create mode 100644 Student Report By Geography.sql diff --git a/Report Contiguos Dates.sql b/Report Contiguos Dates.sql new file mode 100644 index 0000000..f94e025 --- /dev/null +++ b/Report Contiguos Dates.sql @@ -0,0 +1,25 @@ +WITH cte AS ( + SELECT fail_date AS date, + 'failed' AS period_state, + RANK() OVER(ORDER BY fail_date) rnk + FROM failed + WHERE fail_date BETWEEN '2019-01-01' AND '2019-12-31' + UNION ALL + SELECT success_date AS date, + 'succeeded' AS period_state, + RANK() OVER(ORDER BY success_date) rnk + FROM succeeded + WHERE success_date BETWEEN '2019-01-01' AND '2019-12-31' +) +SELECT period_state, + MIN(date) AS start_date, + MAX(date) AS end_date +FROM ( + SELECT *, + RANK() OVER(ORDER BY date) main_rnk, + RANK() OVER(ORDER BY date) - rnk AS group_col -- this is done to identify the difference between the ranks. + -- which helps to group. All records with similar difference value is considered in same group. + FROM cte + ORDER BY date +) a +GROUP BY period_state, group_col; \ No newline at end of file diff --git a/Student Report By Geography.sql b/Student Report By Geography.sql new file mode 100644 index 0000000..38c373a --- /dev/null +++ b/Student Report By Geography.sql @@ -0,0 +1,83 @@ +-- Method 1 +WITH america AS ( + SELECT name AS america, + ROW_NUMBER() OVER(ORDER BY name) as rwn + FROM student + WHERE continent = 'America' +), +asia AS ( + SELECT name AS asia, + ROW_NUMBER() OVER(ORDER BY name) as rwn + FROM student + WHERE continent = 'Asia' +), +europe AS ( + SELECT name AS europe, + ROW_NUMBER() OVER(ORDER BY name) as rwn + FROM student + WHERE continent = 'Europe' +) +SELECT am.america, + a.asia, + e.europe +FROM america am +LEFT JOIN asia a-- left join because the questions says that America has more number of students +ON am.rwn = a.rwn +LEFT JOIN europe e +ON am.rwn = e.rwn; + + +-- Method 2 - POSTGRES QUERY - In case it is not clear which country has more number of students (Works in Postgres not MySQL) +WITH america AS ( + SELECT name AS america, + ROW_NUMBER() OVER(ORDER BY name) as rwn + FROM student + WHERE continent = 'America' +), +asia AS ( + SELECT name AS asia, + ROW_NUMBER() OVER(ORDER BY name) as rwn + FROM student + WHERE continent = 'Asia' +), +europe AS ( + SELECT name AS europe, + ROW_NUMBER() OVER(ORDER BY name) as rwn + FROM student + WHERE continent = 'Europe' +) +SELECT am.america, + a.asia, + e.europe +FROM america am +FULL JOIN asia a +ON am.rwn = a.rwn +FULL JOIN europe e +ON am.rwn = e.rwn; + +-- Method 3 - Using the session variables isntead of window funtion ROW_NUMBER +SELECT am.america, + a.asia, + e.europe +FROM ( + (SELECT @america := 0, @asia := 0, @europe := 0) sess_var, + (SELECT @america := @america + 1 AS rwn, + name AS america + FROM student + WHERE continent = 'America' + ORDER BY name) am + LEFT JOIN + (SELECT @asia := @asia + 1 AS rwn, + name AS asia + FROM student + WHERE continent = 'Asia' + ORDER BY name) a + ON am.rwn = a.rwn + LEFT JOIN + (SELECT @europe := @europe + 1 AS rwn, + name AS europe + FROM student + WHERE continent = 'Europe' + ORDER BY name) e + ON am.rwn = e.rwn +); \ No newline at end of file