0% found this document useful (0 votes)
40 views

SQL Practice

The document contains 6 SQL queries that demonstrate different techniques: 1. Calculating effective work hours for employees by joining a table to itself to rank check-in and check-out times. 2. Grouping user events within 30 minutes to the same session ID. 3. Selecting the top 3 salaries by department by ranking salaries partitioned by department.

Uploaded by

Neha Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

SQL Practice

The document contains 6 SQL queries that demonstrate different techniques: 1. Calculating effective work hours for employees by joining a table to itself to rank check-in and check-out times. 2. Grouping user events within 30 minutes to the same session ID. 3. Selecting the top 3 salaries by department by ranking salaries partitioned by department.

Uploaded by

Neha Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Practice

1. Effective work hours in office by an employee.


Table headers: Empid, action (in or out), time

select a.ID, (sum(b.time-a.time)) as 'Effective' from


(select ID,Action, Time, (rank () over( partition by ID order by time))
as Rank_in
from Hours
where Action = 'in') a
join
(select ID,Action, Time, (rank () over( partition by ID order by time))
as Rank_out
from Hours
where Action = 'out') b
on a.ID=b.ID
and rank_in= rank_out
group by a.ID;

2. userID,sessionID, time, event ID

For a user, the events which are at an interval of less than or equal to 30 minutes, belong to the
same session ID. figure out those session and event iDs for every user.

Leet code hard


1) Department top 3 salaries

select a.departmentid,a.name, a.rank from (select departmentid,name, salary, (rank ()


over(partition by departmentid
order by salary desc)) as rank
from Employee) a
where a.rank<=3

2) Trips and users

select a.request_at,
ROUND(((SUM(CASE WHEN LOWER(a.Status) LIKE 'cancelled%' THEN 1.000 ELSE 0 END))
/ COUNT(a.id)), 2) AS "Cancellation Rate"
from Trips a join users b
on a.client_id= b.users_id
where b.banned = 'No'
group by request_at;
3) median salary of employee

SELECT
Employee.Id, Employee.Company, Employee.Salary
FROM
Employee,
Employee ​alias
WHERE
Employee.Company ​=​ ​alias​.Company
GROUP​ ​BY​ Employee.Company , Employee.Salary
HAVING​ ​SUM​(​CASE
​WHEN​ Employee.Salary ​=​ ​alias​.Salary ​THEN​ ​1
​ELSE​ ​0
END​) ​>=​ ​ABS​(​SUM​(SIGN(Employee.Salary ​-​ ​alias​.Salary)))
ORDER​ ​BY​ Employee.Id
;

SELECT​ CustomerId, AVG(TotalDue) ​FROM​ ( ​SELECT​ CustomerId, TotalDue, ​--


SalesOrderId in the ORDER BY is a disambiguator to break ties​ ROW_NUMBER() ​OVER
( ​PARTITION​ ​BY​ CustomerId ​ORDER​ ​BY​ TotalDue ​ASC​, SalesOrderId ​ASC​) ​AS​ RowAsc,
ROW_NUMBER() ​OVER​ ( ​PARTITION​ ​BY​ CustomerId ​ORDER​ ​BY​ TotalDue ​DESC​,
SalesOrderId ​DESC​) ​AS​ RowDesc ​FROM​ Sales.SalesOrderHeader SOH ) x ​WHERE​ RowAsc
IN​ (RowDesc, RowDesc - ​1​, RowDesc + ​1​) ​GROUP​ ​BY​ CustomerId ​ORDER​ ​BY​ CustomerId;

4) average salary: dept vs company

select t1.pay_month, t2.department_id , (case when t1.net< t2.dep_net then 'higher'


when t1.net>t2.dep_net then 'lower'
else 'same' end) as "comparison"
from
(select month(pay_date) as pay_month, avg(amount) as net
from salary
group by 1) t1
join
(select month(a.pay_date) as pay_month, b.department_id, avg(a.amount) as dep_net
from salary a left join employee b on a.employee_id=b.employee_id
group by 1,2) t2
on t1.pay_month=t2.pay_month

5) students reports by geography (pivoting rows to columns)

set @r1=0, @r2=0, @r3=0, @r4=0;


select case when Occupation='Doctor' then (@r1:=@r1+1)
when Occupation='Professor' then (@r2:=@r2+1)
when Occupation='Singer' then (@r3:=@r3+1)
when Occupation='Actor' then (@r4:=@r4+1) end as RowNumber,
case when Occupation='Doctor' then Name end as Doctor,
case when Occupation='Professor' then Name end as Professor,
case when Occupation='Singer' then Name end as Singer,
case when Occupation='Actor' then Name end as Actor
from OCCUPATIONS
order by Name

6)human traffic of stadium

SELECT s.id, s.date, s.people


from stadium s
LEFT JOIN (SELECT id, people from stadium) T1
ON​ ​T1.id​ =​ ​s.id​ + 2
LEFT JOIN (SELECT id, people from stadium) T2
ON T2.id = s.id + 1
LEFT JOIN (SELECT id, people from stadium) T3
ON T3.id = s.id - 1
LEFT JOIN (SELECT id, people from stadium) T4
ON T4.id = s.id - 2
WHERE ((s.people >= 100 AND
((T3.people >= 100 AND T4.people >= 100) OR
(T2.people >= 100 AND T3.people >= 100) OR
(T1.people >= 100 AND T2.people >= 100))))
ORDER by id

You might also like