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
16 changes: 16 additions & 0 deletions AverageSalaryDepartmentVsCompany.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
3 Problem 3 : Average Salary Department vs Company (https://leetcode.com/problems/average-salary-departments-vs-company/description/ )
'''

WITH companyAvg AS (
SELECT date_format(pay_date, '%Y-%m') AS 'pay_month', AVG(amount) AS 'CompanyAvg' FROM Salary GROUP BY pay_month),
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
)
SELECT deptAvg.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 deptAvg.pay_month = companyAvg.pay_month
7 changes: 7 additions & 0 deletions GamePlayAnalysisI.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'''
4 Problem 4 : Game Play Analysis I (https://leetcode.com/problems/game-play-analysis-i/ )
'''

SELECT player_id, MIN(event_date) AS first_login
FROM Activity
GROUP BY player_id
12 changes: 12 additions & 0 deletions ReportContiguosDates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
Sql5

1 Problem 1 : Report Contiguos Dates (https://leetcode.com/problems/report-contiguous-dates/ )
'''

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
)
SELECT period_state, MIN(dat) AS start_date, MAX(dat) AS end_date FROM (SELECT * , RANK() OVER(ORDER BY dat) - rnk AS 'diff' FROM CTE) AS y GROUP BY diff, period_state ORDER BY start_date
12 changes: 12 additions & 0 deletions StudentReportByGeography.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
2 Problem 2 : Student Report By Geography (https://leetcode.com/problems/students-report-by-geography/ )
'''

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