Skip to content
Open
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
51 changes: 51 additions & 0 deletions sql2 done
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Q1:Rank Scores
--Using window functions
select score,
dense_rank() over (order by score desc) as 'rnk'
from scores
order by rnk;

--Without using window functions
select t.score,
(select count(distinct s.score)
from scores s
where s.score>=t.score) as 'rnk'
from scores t
order by rnk;

Q2:Exchange Seats
SELECT
CASE
WHEN mod(id 2) !=0 AND id != cnts THEN id + 1
WHEN mod(id 2) != 0 AND id = cnts THEN id
ELSE id - 1
END
AS id,
student
FROM Seat, (select count(*) as cnts from seat) as Seat_Counts
ORDER BY id;

Q3:TREE NODE
select distinct t1.id,
case
when t1.p_id is null then 'root'
when t2.id is null then 'leaf'
else 'inner'
end as type
from tree t1
left join tree t2
on t1.id=t2.p_id;

Q4:DEPARTMENT TOP3 SALARIES
# Write your MySQL query statement below
SELECT Department, Employee, Salary
FROM (
SELECT d.name AS Department,
e.name AS Employee,
e.salary,
DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) AS dr
FROM Employee e
LEFT JOIN Department d ON e.departmentId = d.id
) ranked
WHERE dr <= 3
ORDER BY Department, Salary DESC, Employee;