diff --git a/01-ReportContiguousDates.sql b/01-ReportContiguousDates.sql new file mode 100644 index 0000000..3167f09 --- /dev/null +++ b/01-ReportContiguousDates.sql @@ -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; \ No newline at end of file diff --git a/02-StudentsReportByGeography.sql b/02-StudentsReportByGeography.sql new file mode 100644 index 0000000..3f5d8b6 --- /dev/null +++ b/02-StudentsReportByGeography.sql @@ -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 +); \ No newline at end of file diff --git a/03-AvgSalaryDepartmentsVsCompany.sql b/03-AvgSalaryDepartmentsVsCompany.sql new file mode 100644 index 0000000..86d6aaf --- /dev/null +++ b/03-AvgSalaryDepartmentsVsCompany.sql @@ -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; \ No newline at end of file diff --git a/04-GamePlayAnalysis-I.sql b/04-GamePlayAnalysis-I.sql new file mode 100644 index 0000000..a9a5ddb --- /dev/null +++ b/04-GamePlayAnalysis-I.sql @@ -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; \ No newline at end of file