From 02b98c75cba5cedc0b0da8d9a1a9b1566cb65801 Mon Sep 17 00:00:00 2001 From: Paneri Patel Date: Wed, 25 Jun 2025 20:47:05 -0400 Subject: [PATCH] Done Sql5 --- AverageSalaryDepartmentVsCompany.sql | 16 ++++++++++++++++ GamePlayAnalysisI.sql | 7 +++++++ ReportContiguosDates.sql | 12 ++++++++++++ StudentReportByGeography.sql | 12 ++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 AverageSalaryDepartmentVsCompany.sql create mode 100644 GamePlayAnalysisI.sql create mode 100644 ReportContiguosDates.sql create mode 100644 StudentReportByGeography.sql diff --git a/AverageSalaryDepartmentVsCompany.sql b/AverageSalaryDepartmentVsCompany.sql new file mode 100644 index 0000000..e93ed5c --- /dev/null +++ b/AverageSalaryDepartmentVsCompany.sql @@ -0,0 +1,16 @@ +''' +3 Problem 3 : Average Salary Department vs Company (https://leetcode.com/problems/average-salary-departments-vs-company/description/ ) +''' + +WITH companyAvg AS ( + SELECT date_format(pay_date, '%Y-%m') AS 'pay_month', AVG(amount) AS 'CompanyAvg' FROM Salary GROUP BY pay_month), +deptAvg AS ( + SELECT date_format(pay_date, '%Y-%m') AS 'pay_month', department_id, AVG(amount) AS 'departmentAvg' FROM Salary JOIN Employee ON Salary.employee_id = Employee.employee_id GROUP BY department_id, pay_month +) +SELECT deptAvg.pay_month, department_id, ( + CASE + WHEN departmentAvg > CompanyAvg THEN 'higher' + WHEN departmentAvg < CompanyAvg THEN 'lower' + ELSE 'same' + END +) AS 'comparison' FROM companyAvg JOIN deptAvg ON deptAvg.pay_month = companyAvg.pay_month \ No newline at end of file diff --git a/GamePlayAnalysisI.sql b/GamePlayAnalysisI.sql new file mode 100644 index 0000000..c498fd1 --- /dev/null +++ b/GamePlayAnalysisI.sql @@ -0,0 +1,7 @@ +''' +4 Problem 4 : Game Play Analysis I (https://leetcode.com/problems/game-play-analysis-i/ ) +''' + +SELECT player_id, MIN(event_date) AS first_login +FROM Activity +GROUP BY player_id diff --git a/ReportContiguosDates.sql b/ReportContiguosDates.sql new file mode 100644 index 0000000..a441d8d --- /dev/null +++ b/ReportContiguosDates.sql @@ -0,0 +1,12 @@ +''' +Sql5 + +1 Problem 1 : Report Contiguos Dates (https://leetcode.com/problems/report-contiguous-dates/ ) +''' + +WITH CTE AS ( + SELECT fail_date AS 'dat', 'failed' AS period_state, RANK() OVER(ORDER BY fail_date) AS 'rnk' FROM Failed WHERE YEAR(fail_date) = 2019 + UNION ALL + SELECT success_date AS 'dat', 'succeeded' AS period_state, RANK() OVER(ORDER BY success_date) AS 'rnk' FROM Succeeded WHERE YEAR(success_date) = 2019 +) +SELECT period_state, MIN(dat) AS start_date, MAX(dat) AS end_date FROM (SELECT * , RANK() OVER(ORDER BY dat) - rnk AS 'diff' FROM CTE) AS y GROUP BY diff, period_state ORDER BY start_date \ No newline at end of file diff --git a/StudentReportByGeography.sql b/StudentReportByGeography.sql new file mode 100644 index 0000000..d0c4bde --- /dev/null +++ b/StudentReportByGeography.sql @@ -0,0 +1,12 @@ +''' +2 Problem 2 : Student Report By Geography (https://leetcode.com/problems/students-report-by-geography/ ) +''' + +WITH first AS ( + SELECT name AS 'America', ROW_NUMBER() OVER(ORDER BY name) AS 'rnk' FROM student WHERE continent = 'America'), +second AS ( + SELECT name AS 'Asia', ROW_NUMBER() OVER(ORDER BY name) AS 'rnk' FROM student WHERE continent = 'Asia'), +third AS ( + SELECT name AS 'Europe', ROW_NUMBER() OVER(ORDER BY name) AS 'rnk' FROM student WHERE continent = 'Europe') + +SELECT America, Asia, Europe FROM second RIGHT JOIN first ON first.rnk = second.rnk LEFT JOIN third ON first.rnk = third.rnk \ No newline at end of file