From 399e9931a8f9a888c79746861a6717028c1825de Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:37:36 -0600 Subject: [PATCH 1/2] Done --- Problem 1 Report Contiguous Dates.sql | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Problem 1 Report Contiguous Dates.sql diff --git a/Problem 1 Report Contiguous Dates.sql b/Problem 1 Report Contiguous Dates.sql new file mode 100644 index 0000000..7392da4 --- /dev/null +++ b/Problem 1 Report Contiguous Dates.sql @@ -0,0 +1,22 @@ +# Write your MySQL query statement below +-- Step 1: Create a Common Table Expression (CTE) to compare the current row with the next two rows +WITH ConsecutiveNumsCTE AS ( + SELECT + l1.id AS CurrentID, -- ID of the current row + l1.num AS CurrentNum, -- Number of the current row + l2.num AS NextNum, -- Number from the next row + l3.num AS SecondNextNum -- Number from the second next row + FROM Logs l1 + -- Join the table with itself to get the next row (ID = CurrentID + 1) + LEFT JOIN Logs l2 + ON l1.id = l2.id - 1 + -- Join the table again to get the second next row (ID = CurrentID + 2) + LEFT JOIN Logs l3 + ON l1.id = l3.id - 2 +) + +-- Step 2: Select numbers that appear three times consecutively +SELECT DISTINCT CurrentNum AS ConsecutiveNums +FROM ConsecutiveNumsCTE +-- Filter rows where the current number is equal to both the next and the second next number +WHERE CurrentNum = NextNum AND NextNum = SecondNextNum; \ No newline at end of file From ba8d1a24d01a16ece97a8af055a08105719cd20f Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Thu, 9 Jan 2025 14:21:07 -0600 Subject: [PATCH 2/2] Done --- Problem2StudentReportbyGeography.sql | 11 ++++++++++ ...3 Average Salary Department vs Company.sql | 22 +++++++++++++++++++ Problem4 Game Play Analysis1.sql | 2 ++ 3 files changed, 35 insertions(+) create mode 100644 Problem2StudentReportbyGeography.sql create mode 100644 Problem3 Average Salary Department vs Company.sql create mode 100644 Problem4 Game Play Analysis1.sql diff --git a/Problem2StudentReportbyGeography.sql b/Problem2StudentReportbyGeography.sql new file mode 100644 index 0000000..1c35320 --- /dev/null +++ b/Problem2StudentReportbyGeography.sql @@ -0,0 +1,11 @@ +# Write your MySQL query statement below +WITH RankedStudents AS ( + SELECT name, continent, ROW_NUMBER() OVER (PARTITION BY continent ORDER BY name) AS 'rnk' FROM student +) + +SELECT MAX(CASE WHEN continent = 'America' THEN name END) AS America, + MAX(CASE WHEN continent = 'Asia' THEN name END) AS Asia, + MAX(CASE WHEN continent = 'Europe' THEN name END) AS Europe + FROM RankedStudents + GROUP BY rnk + ORDER BY rnk \ No newline at end of file diff --git a/Problem3 Average Salary Department vs Company.sql b/Problem3 Average Salary Department vs Company.sql new file mode 100644 index 0000000..ed1997a --- /dev/null +++ b/Problem3 Average Salary Department vs Company.sql @@ -0,0 +1,22 @@ +# Write your MySQL query statement below +WITH DepAndMonth AS( + SELECT DATE_FORMAT(pay_date, '%Y-%m') AS pay_month, +s.employee_id, s.amount, s.pay_date, e.department_id +FROM salary s +LEFT JOIN employee e +ON s.employee_id = e. employee_id +), +AverageSalaries AS ( + + SELECT pay_month, amount, department_id, + AVG(amount) OVER (PARTITION BY pay_month) AS 'CompanyAverageSalary', + AVG(amount) OVER (PARTITION BY pay_month, department_id) AS 'DepartmentAverage' + FROM DepAndMonth +) + +SELECT DISTINCTpay_month, department_id, CASE +WHEN DepartmentAverage > CompanyAverageSalary THEN 'higher' +WHEN DepartmentAverage < CompanyAverageSalary THEN 'lower' +ELSE 'same' END AS comparison +FROM AverageSalaries +ORDER BY pay_month \ No newline at end of file diff --git a/Problem4 Game Play Analysis1.sql b/Problem4 Game Play Analysis1.sql new file mode 100644 index 0000000..b3f663b --- /dev/null +++ b/Problem4 Game Play Analysis1.sql @@ -0,0 +1,2 @@ +SELECT DISTINCT player_id, FIRST_VALUE(event_date) OVER (PARTITION BY player_id ORDER BY event_date) AS 'first_login' FROM ACTIVITY +