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
19 changes: 19 additions & 0 deletions 01-ReportContiguousDates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Sql5 Question 1 - Report Contiguous Dates (https://leetcode.com/problems/report-contiguous-dates/)
-- Creating a CTE where we combine failed and succeeded tables and rank both failed and succeeded separately by date
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
),
-- Labelled ranks the above CTE table by date regardless of fail or success, which is basically group rank and also finds difference between group rank and rnk. If difference is same they belong to same interval, basically meaning they are continuous
labelled AS (
SELECT *, RANK() OVER(ORDER BY dat) - rnk AS grp FROM CTE
)
-- Final Output by giving the period_state, minimum date from labelled as start_date and maximum date from labelled as end_date while grouping them by success and failure and also by difference between group rank and rnk
SELECT period_state, MIN(dat) AS start_date, MAX(dat) AS end_date
FROM labelled
GROUP BY period_state, grp;
39 changes: 39 additions & 0 deletions 02-StudentsReportByGeography.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- Sql5 Question 2 - Students Report By Geography (https://leetcode.com/problems/students-report-by-geography/)
-- Approach 1 : Using ROW_NUMBER() and JOIN
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;

-- Approach 2 - Using session variables
SELECT America, Asia, Europe FROM (
(SELECT @am := 1, @as := 1, @eu := 1)t1,
(SELECT @as := @as + 1 AS 'asrnk', name AS 'Asia'
FROM Student
WHERE continent = 'Asia'
ORDER BY Asia)t2
RIGHT JOIN
(SELECT @am := @am + 1 AS 'amrnk', name AS 'America'
FROM Student
WHERE continent = 'America'
ORDER BY America)t3 ON asrnk = amrnk
LEFT JOIN
(SELECT @eu := @eu + 1 AS 'eurnk', name AS 'Europe'
FROM Student
WHERE continent = 'Europe'
ORDER BY Europe)t4 ON amrnk = eurnk
);
26 changes: 26 additions & 0 deletions 03-AvgSalaryDepartmentsVsCompany.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Sql5 Question 3 - Average Salary Departments vs Company (https://leetcode.com/problems/average-salary-departments-vs-company/description/)
-- Finding the avergae salary of company
WITH companyavg AS(
SELECT date_format(pay_date, '%Y-%m') AS 'pay_month', AVG(amount) AS 'CompanyAvg'
FROM Salary
GROUP BY pay_month
),
-- Finding the average salary of employees in a department
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
)
-- Comparing the results of companyAvg and deptAvg CTEs and returning the result
SELECT deptAvg.pay_month AS '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 companyAvg.pay_month = deptAvg.pay_month;
7 changes: 7 additions & 0 deletions 04-GamePlayAnalysis-I.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Sql5 Question 4 - Game Play Analysis I (https://leetcode.com/problems/game-play-analysis-i/ )
-- Approach 1 : Using MIN and GROUP BY
SELECT player_id, MIN(event_date) AS first_login
FROM Activity
GROUP BY player_id;
-- Approach 2 : Using RANK
SELECT a.player_id, a.event_date AS 'first_login' FROM (SELECT b.player_id, b.event_date, RANK() OVER(PARTITION BY b.player_id ORDER BY b.event_date) AS 'rnk' FROM Activity b) AS a WHERE a.rnk = 1;