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
26 changes: 26 additions & 0 deletions AverageSalaryDepartmentvsCompany.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Solution for Leetcode problem AverageSalaryDepartmentvsCompany
WITH dept AS (
SELECT DATE_FORMAT(s.pay_date, '%Y-%m') as pay_month,
e.department_id,
AVG(s.amount) AS d_avg
FROM salary s
JOIN employee e
ON s.employee_id = e.employee_id
GROUP BY 1,2
),
comp AS(
SELECT DATE_FORMAT(pay_date, '%Y-%m') as pay_month,
AVG(amount) AS c_avg
FROM salary
GROUP BY 1
)
SELECT d.pay_month,
d.department_id,
CASE
WHEN d.d_avg > c.c_avg THEN 'higher'
WHEN d.d_avg < c.c_avg THEN 'lower'
WHEN d.d_avg = c.c_avg THEN 'same'
END AS comparison
FROM dept d
JOIN comp c
ON d.pay_month = c.pay_month;
25 changes: 25 additions & 0 deletions Report Contiguos Dates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
WITH cte AS (
SELECT fail_date AS date,
'failed' AS period_state,
RANK() OVER(ORDER BY fail_date) rnk
FROM failed
WHERE fail_date BETWEEN '2019-01-01' AND '2019-12-31'
UNION ALL
SELECT success_date AS date,
'succeeded' AS period_state,
RANK() OVER(ORDER BY success_date) rnk
FROM succeeded
WHERE success_date BETWEEN '2019-01-01' AND '2019-12-31'
)
SELECT period_state,
MIN(date) AS start_date,
MAX(date) AS end_date
FROM (
SELECT *,
RANK() OVER(ORDER BY date) main_rnk,
RANK() OVER(ORDER BY date) - rnk AS group_col -- this is done to identify the difference between the ranks.
-- which helps to group. All records with similar difference value is considered in same group.
FROM cte
ORDER BY date
) a
GROUP BY period_state, group_col;
83 changes: 83 additions & 0 deletions Student Report By Geography.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
-- Method 1
WITH america AS (
SELECT name AS america,
ROW_NUMBER() OVER(ORDER BY name) as rwn
FROM student
WHERE continent = 'America'
),
asia AS (
SELECT name AS asia,
ROW_NUMBER() OVER(ORDER BY name) as rwn
FROM student
WHERE continent = 'Asia'
),
europe AS (
SELECT name AS europe,
ROW_NUMBER() OVER(ORDER BY name) as rwn
FROM student
WHERE continent = 'Europe'
)
SELECT am.america,
a.asia,
e.europe
FROM america am
LEFT JOIN asia a-- left join because the questions says that America has more number of students
ON am.rwn = a.rwn
LEFT JOIN europe e
ON am.rwn = e.rwn;


-- Method 2 - POSTGRES QUERY - In case it is not clear which country has more number of students (Works in Postgres not MySQL)
WITH america AS (
SELECT name AS america,
ROW_NUMBER() OVER(ORDER BY name) as rwn
FROM student
WHERE continent = 'America'
),
asia AS (
SELECT name AS asia,
ROW_NUMBER() OVER(ORDER BY name) as rwn
FROM student
WHERE continent = 'Asia'
),
europe AS (
SELECT name AS europe,
ROW_NUMBER() OVER(ORDER BY name) as rwn
FROM student
WHERE continent = 'Europe'
)
SELECT am.america,
a.asia,
e.europe
FROM america am
FULL JOIN asia a
ON am.rwn = a.rwn
FULL JOIN europe e
ON am.rwn = e.rwn;

-- Method 3 - Using the session variables isntead of window funtion ROW_NUMBER
SELECT am.america,
a.asia,
e.europe
FROM (
(SELECT @america := 0, @asia := 0, @europe := 0) sess_var,
(SELECT @america := @america + 1 AS rwn,
name AS america
FROM student
WHERE continent = 'America'
ORDER BY name) am
LEFT JOIN
(SELECT @asia := @asia + 1 AS rwn,
name AS asia
FROM student
WHERE continent = 'Asia'
ORDER BY name) a
ON am.rwn = a.rwn
LEFT JOIN
(SELECT @europe := @europe + 1 AS rwn,
name AS europe
FROM student
WHERE continent = 'Europe'
ORDER BY name) e
ON am.rwn = e.rwn
);
21 changes: 21 additions & 0 deletions game_analysis_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Method 1
SELECT player_id,
MIN(event_date) first_login
FROM activity
GROUP BY 1;

-- Method 2
SELECT DISTINCT player_id,
FIRST_VALUE(event_date) OVER(PARTITION BY player_id ORDER BY event_date RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) first_login
FROM activity;

-- Method 3
SELECT player_id,
event_date AS first_login
FROM (
SELECT player_id,
event_date,
DENSE_RANK() OVER(PARTITION BY player_id ORDER BY event_date) rnk
FROM activity
)a
WHERE rnk = 1