Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Problem 1 Report Contiguous Dates.sql
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions Problem2StudentReportbyGeography.sql
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions Problem3 Average Salary Department vs Company.sql
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Problem4 Game Play Analysis1.sql
Original file line number Diff line number Diff line change
@@ -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