Skip to content
Open

SQL2 #72

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 Rankemails.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Write your MySQL query statement below
# -------------------------- Using Window Function DENSE_RANK()--------------
-- SELECT
-- Score,
-- DENSE_RANK() OVER (ORDER BY Score DESC) AS 'rank'
-- FROM
-- Scores;

# ------------------------- Handling Null with Null as rank 1 -----------------
SELECT
Score,
DENSE_RANK() OVER (ORDER BY COALESCE(Score, 1) DESC) AS 'RANK'
FROM
Scores;

# ------------------------- Using subquery-------------------------------
-- SELECT
-- Score,
-- (SELECT COUNT(DISTINCT Score)
-- FROM scores s2
-- WHERE s2.score >= s1.score
-- ) AS 'rank'
-- FROM
-- Scores s1
-- ORDER BY
-- score DESC;
15 changes: 15 additions & 0 deletions Top3Dept.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
WITH RankedSalaries AS (
SELECT
d.name AS Department,
e.name AS Employee,
e.salary AS Salary,
DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) AS salary_rank
FROM Employee e
JOIN Department d ON e.departmentId = d.id
)
SELECT
Department,
Employee,
Salary
FROM RankedSalaries
WHERE salary_rank <= 3;
32 changes: 32 additions & 0 deletions TreeNode.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Write your MySQL query statement below
SELECT
id, 'Root' as Type
FROM
Tree
WHERE p_id IS NULL
UNION
SELECT
id, 'Leaf' as Type
FROM
Tree
WHERE id NOT IN (
SELECT
DISTINCT p_id
FROM
Tree
WHERE p_id IS NOT NULL
) AND
p_id IS NOT NULL
UNION
SELECT
id, 'Inner' as Type
FROM
Tree
WHERE id IN (
SELECT
DISTINCT p_id
FROM
Tree
WHERE p_id IS NOT NULL
) AND
p_id IS NOT NULL;
22 changes: 22 additions & 0 deletions exchangeSeats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Write your MySQL query statement below
#-------------------Using ODD/EVEN ---------------
-- SELECT(
-- CASE
-- WHEN mod(id, 2) != 0 AND id != cnt THEN id + 1
-- WHEN mod(id, 2) != 0 AND id = cnt THEN id
-- ELSE id - 1
-- END) AS id, student
-- FROM
-- Seat, (SELECT COUNT(*) AS cnt FROM Seat) as count_table
-- ORDER BY id
#---------------- Using LEFT JOIN and XOR function------------------
SELECT
s1.id, COALESCE(s2.student, s1.student) AS 'student'
FROM
Seat s1
LEFT JOIN
Seat s2
ON
(s1.id + 1) ^ 1 - 1 = s2.id
ORDER BY
s1.id