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 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 +