Beauty of SQL RANK Function | SQL Interview Question and Answers | Covid Cases

Beauty of SQL RANK Function | SQL Interview Question and Answers | Covid Cases

Ankit Bansal

2 года назад

14,036 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@RohitSaini52945
@RohitSaini52945 - 27.01.2024 20:03

with cte as (
select
*,
case when
lag(cases,1,1) over(partition by city order by days) < cases
then 1 else 0 end as rn
from covid
)
select city
from cte
group by city
having count(1) = sum(rn)

Ответить
@vandanaK-mh9zo
@vandanaK-mh9zo - 24.01.2024 11:54

My Approach -
with cte_rn as (
select
city, days, cases,
rank() over (partition by city order by days asc) as days_rn,
rank() over (partition by city order by cases asc) as cases_rn,
abs(days_rn - cases_rn) as diff
from covid )
select city from cte_rn
group by city having sum(diff) = 0;

Ответить
@souravbarik8470
@souravbarik8470 - 21.01.2024 18:48

My Solution using LEAD and group by

select city
from(
select *,
lead(cases) over(partition by city order by days) as next_day_cases
from covid) a
group by city
having count(*) = sum(case when (next_day_cases is null or next_day_cases > cases) then 1 else 0 end)

Ответить
@prajwalns1416
@prajwalns1416 - 09.01.2024 19:58

Hi Ankit, Thanks for the video.
Below is my approach:

WITH CTE AS(SELECT *,
CASE WHEN CASES - LAG(CASES,1,0) OVER(PARTITION BY CITY ORDER BY DAYS ) > 0 THEN 1 ELSE 0 END AS CHK
FROM COVID)

SELECT CITY
FROM CTE
GROUP BY 1
HAVING COUNT(DISTINCT CHK) = 1 AND SUM(DISTINCT CHK) <> 0

Ответить
@ishanshubham8355
@ishanshubham8355 - 07.01.2024 18:15

My solution On Mysql

with cte as (
select city
from
(select *,
if(v=t , "Yes","NO") as logic
from
(
select *,cast(casess as signed) as v,cast(dayss as signed) as t
from
(
select *,
rank() over(partition by city order by cases ) as casess,
rank() over(partition by city order by days) as dayss

from covid
) as table1) t2) t3
where logic = "no"
group by city)
select distinct city
from covid
where city not in (select * from cte)

Ответить
@gauravgupta5530
@gauravgupta5530 - 31.12.2023 08:30

with cte as (select *, ROW_NUMBER() OVER (partition by city order by cases) as row
from covid order by city, days),

cte2 as (select *,
CASE WHEN LAG(row) OVER (partition by city) = row-1 then TRUE
ELSE FALSE END as lag
from cte)

select city from cte2 where lag = TRUE group by city having count(lag) >= 2;

Ответить
@abdkumar1300
@abdkumar1300 - 29.12.2023 19:04

my approach
MS SQL:

with cte as (
select city, (case when cases>lag(cases, 1, 0) over(partition by city order by days) then 1 else 0 end) as flag from covid
)
select distinct city from cte where city not in (select city from cte where flag=0)

Ответить
@MixedUploader
@MixedUploader - 27.12.2023 14:42

Ankit thanks for the rank method. I tried using lag method below:
with cte as(select *
,lag(cases,1,0) over(partition by city order by days) as prev_cases
,cases-lag(cases,1,0) over(partition by city order by days) as cases_diff
from covid)
select city
from cte
group by city
having count(case when cases_diff<=0 then cases_diff end)=0

Ответить
@simranpreetsingh3052
@simranpreetsingh3052 - 24.12.2023 23:22

with cte as(
select [city], RANK() over(partition by city order by cases) as rnk_cases,
RANK() over(partition by city order by [days]) as rnk_days,
case when RANK() over(partition by city order by cases) = RANK() over(partition by city order by [days]) then 1
else 0 end as inc_order
from [dbo].[covid]
),
cte2 as (
select city, COUNT(distinct inc_order) as cnt from cte
group by [city]
)
select city from cte2 where cnt=1

Ответить
@sirimaddala8661
@sirimaddala8661 - 22.12.2023 03:45

WITH CTE AS(
SELECT *,
ABS(RANK() OVER(PARTITION BY city ORDER BY days)-RANK() OVER(PARTITION BY city ORDER BY cases)) AS diff
FROM covid)

SELECT city FROM CTE
GROUP BY city HAVING SUM(DIFF)=0

Ответить
@harishkanta3711
@harishkanta3711 - 18.12.2023 11:52

with cte as (select city,DAY(days) as dt,rank() over(partition by city order by cases) as rn
from covid order by city,days)

select city from(select city,dt-rn*1.0 as diff from cte) as t group by city having count( distinct diff)=1

Ответить
@debdattachatterjee1395
@debdattachatterjee1395 - 12.12.2023 08:05

SELECT city
FROM (
SELECT city,
LAG(cases, 1) OVER (PARTITION BY city ORDER BY days) as prev_day_cases,
cases
FROM CovidData
) as SubQuery
GROUP BY city
HAVING COUNT(CASE WHEN cases > prev_day_cases THEN 1 END) = COUNT(*) - 1

Ответить
@mradulgupta
@mradulgupta - 14.11.2023 19:15

select city from (select * from (select *,
lead(cases,1) over (partition by city order by days) as next_1,
lead(cases,2) over (partition by city order by days) as next_2
from covid) a where (a.next_1 is not null) and (a.next_2 is not null)) b where b.cases<b.next_1 and b.next_1<b.next_2

Ответить
@vinaytekkur
@vinaytekkur - 05.11.2023 19:59

WITh get_cases_info AS(
SELECT
*,
LEAD(cases, 1) OVER(PARTITION BY city ORDER BY days) AS next_cases
FROM covid),
get_case_diff AS(
SELECT
*,
next_cases - cases AS diff_case
FROM get_cases_info),
identify_city AS(
SELECT
city,
COUNT(CASE WHEN diff_case <= 0 THEN 1 END) AS cnt
FROM get_case_diff
GROUP BY city)
SELECT
city FROM identify_city
WHERE cnt=0;

Ответить
@ankitdhar3692
@ankitdhar3692 - 30.10.2023 08:58

with cte1 as(
select *,lead(cases,1)over(partition by city order by days) nextm
from covid),
cte2 as(
select *,(nextm-cases) as [diff] from cte1
where nextm-cases<=0)
select distinct(city) from cte1 where city not in (select city from cte2)
SIR kindly comment on this approach

Ответить
@aapbeete9761
@aapbeete9761 - 21.10.2023 13:05

with cte as (
select city, days, cases,
lag(cases, 1, 0) over (partition by city order by days) as pre_cases
from covid
)
select distinct city
from cte
where cases > pre_cases
order by city, days;

Ответить
@user-ju8en2wl3p
@user-ju8en2wl3p - 11.10.2023 20:03

select city from(
select * ,
rank() over(partition by city order by cases) as rn,
row_number() over(partition by city order by days,cases) as row_no,
case when rank() over(partition by city order by cases) <> row_number() over(partition by city order by days,cases) then 1 else 0 end as flag
from covid) as a
group by city
having count(distinct flag) =1

Ответить
@kirangadhe4962
@kirangadhe4962 - 10.09.2023 14:34

slightly different approach

SELECT city, sum(disc) ,count(*)-1
from (
SELECT *,
lead(cases,1) over (partition by city order by days) as lead_cases ,
case when lead(cases,1) over (partition by city order by days)> cases then 1 else 0 end as disc
from covid)A
group by city
having sum(disc) = count(*)-1

Ответить
@ujjwalvarshney3188
@ujjwalvarshney3188 - 14.08.2023 18:17

create temp table uj as
(
select city , (lead(cases) over(partition by city order by days ) - cases) as ld from covid );

select distinct city from covid where city not in
( select distinct city from uj where ld < 0 or ld = 0)

Ответить
@ayushsrivastav8220
@ayushsrivastav8220 - 12.08.2023 09:44

Hey ankit, thanks for posting this. Below is my solution in mysql workbench :
with cte1 as(
select *,
lag(cases,1,0) over(partition by city order by days) as prev
from covid)
select city
from cte1
group by city
having SUM(case when cases>prev then 0 else 1 end)=0

Ответить
@radhikamaheshwari4835
@radhikamaheshwari4835 - 08.08.2023 09:28

My solution
with cte as
(
select *,
lead(cases) over(partition by city order by days) as next_day,
lead(cases) over(partition by city order by days) - cases as diff
from covid
),
cte2 as
(
select distinct city
from cte
where diff <= 0
)
select distinct city from covid
where city not in (select city from cte2)

Ответить
@kachvejathin
@kachvejathin - 18.07.2023 15:25

Hello sir, here avg(diff) =0 should be satisfied by bangalore and chennai right

Ответить
@RamaKrishna-ll8iz
@RamaKrishna-ll8iz - 17.07.2023 00:32

with cte1 as
(
select city,
rank()over(partition by city order by days asc) as rn_days,
rank()over(partition by city order by cases asc) as rn_cases,
rank()over(partition by city order by days asc)-rank()over(partition by city order by cases asc) as diff
from covid
)
select city from cte1
group by city
having count(distinct diff)=1 and max(diff)=0 and min(diff)=0

Ответить
@RamaKrishna-ll8iz
@RamaKrishna-ll8iz - 17.07.2023 00:27

I have taken absolute difference then aggregated(sum)

with cte1 as
(
select city,
rank()over(partition by city order by days asc) as rn_days,
rank()over(partition by city order by cases asc) as rn_cases,
abs(rank()over(partition by city order by days asc)-rank()over(partition by city order by cases asc)) as diff
from covid
)
select city from cte1
group by city
having sum(diff)=0

Ответить
@ujjwalvarshney3188
@ujjwalvarshney3188 - 16.07.2023 08:09

create temp table t1 as
(
select distinct city from
(
select city,(row_number()over (partition by city order by cases) - rank()over (partition by city order by cases)) as rk from covid
)
where rk=1 );
select distinct city from covid where city not in (select * from t1)

Ответить
@sontijagadeesh3990
@sontijagadeesh3990 - 10.07.2023 15:54

select city,sum(flag) from (
select *,lag(cases,1,0) over(partition by city order by days) as prev,
case when lag(cases,1,0) over(partition by city order by days) < cases then 0 else 1 end as flag
from covid) a
group by city
having sum(flag)= 0;

Ответить
@subhojitchatterjee6312
@subhojitchatterjee6312 - 30.06.2023 16:17

My approach though I had to take help from the video once:

SELECT CITY FROM (SELECT *, RANK() OVER(PARTITION BY CITY ORDER BY DAYS) AS RN_D,RANK() OVER(PARTITION BY CITY ORDER BY CASES) AS RN_CASES,
CAST(RANK() OVER(PARTITION BY CITY ORDER BY DAYS) AS SIGNED)-CAST(RANK() OVER(PARTITION BY CITY ORDER BY CASES) AS SIGNED) AS DIFF
FROM COVID) T1 GROUP BY CITY HAVING MAX(DIFF)=MIN(DIFF);

Ответить
@vikaskumar-qr5tj
@vikaskumar-qr5tj - 29.06.2023 07:29

With cte as
(
Select *,lag(cases,1,0) over(partition by city order by days asc) as previous_cases from covid
)
Select city from cte
group by city
having sum(case when cases>previous_cases then 0 else 1 end)=0;
Hi Ankit,solve the same using lag function above.

Ответить
@shubhamkapoor85
@shubhamkapoor85 - 18.06.2023 11:56

When I'm doing rank1 - rank2 it is giving me error that
Error 1690 begint unsigned value out of range in

How to solve it ?

Ответить
@user-zx1ii2cx2j
@user-zx1ii2cx2j - 07.06.2023 19:24

with cte1 as
(select *,lag(cases,1,0) over(partition by city order by days) cases2,
count(*) over(partition by city) cnt from covid)
,cte2 as
(select city,cnt,count(*) cnt2 from cte1 where cases>cases2 group by city)
select city from cte2 where cnt=cnt2
(easy logical answer)

Ответить
@anish_bhateja
@anish_bhateja - 09.05.2023 10:15

with cities as (select *, lag(cases,1,1) over(partition by city order by days) as prev_day_case from covid),
filter_city as (select *, case when cases-prev_day_case <= 0 then 'Decreasing' else 'Increasing' end as trend from cities order by city,days)
select distinct city from cities where city not in (select distinct city from filter_city where trend = 'Decreasing');

Ответить
@user-um8rt4vv7b
@user-um8rt4vv7b - 30.04.2023 23:18

My solution is :
with cte as (
select *
, lead(cases) over (partition by city order by days,cases) nxt
from covid)
, cte2 as (select city from cte where cases-nxt>=0)
select distinct city from cte where city not in (select city from cte2)

Ответить
@satyajitbiswal6162
@satyajitbiswal6162 - 03.03.2023 15:48

with cte1 as (select *,row_number() over(partition by city order by days) as row_number,
dense_rank() over(partition by city order by cases) as dense_rank
from covid),

cte2 as (select *,case when row_number=dense_rank then 'true' else 'false' end as test from cte1)

select * from cte2 where city not in (select distinct city from cte2 where test='false')

Ответить
@ASHISH517098
@ASHISH517098 - 24.02.2023 08:28

with cte as (
select city, rank() over(partition by city order by days) as days_rank, rank() over(partition by city order by cases) as cases_rank, days_rank - cases_rank as diff
from covid)
select city from cte
group by city having count(distinct(diff)) = 1

Ответить
@vijaywadhwa3495
@vijaywadhwa3495 - 21.02.2023 20:31

My Submission using Lag function


with cte as (
select *,
case
when lag(cases) over(Partition by city order by days) < cases then 'yes'
when lag(cases) over(Partition by city order by days) is null then 'yes'
else 'no'
end as increasing_day
from covid
)

select City
from cte
group by city
having count(distinct increasing_day) = 1

Ответить
@dineshgudla3735
@dineshgudla3735 - 18.02.2023 10:09

COUNT(DISTTINCT(diff))=1 should be good enough, but what happens when there is a new city with one date record , we should add min(days)!=max(days) to the having condition

Ответить
@kumz202
@kumz202 - 07.02.2023 15:03

SELECT city
FROM
(SELECT *,
lag(cases, 1) over(PARTITION BY city
ORDER BY days) AS prev_cases,
(cases - coalesce(lag(cases, 1) over(PARTITION BY city
ORDER BY days), 0)) AS diff
FROM covid)a
GROUP BY city
HAVING min(diff) > 0;

Ответить
@mathianandhan
@mathianandhan - 25.01.2023 17:35

Solution that I tried,
with tab1 as(select *,lead(cases,1) over(partition by city order by days ) as prev_month,
lead(cases,1) over(partition by city order by days ) - cases as sumcount
from covid),
tab2 as (select distinct city from tab1
where sumcount <= 0)
select distinct city from covid where city not in (select city from tab2)

Ответить
@shivammadaan9498
@shivammadaan9498 - 16.01.2023 09:30

my solution - kinda same but differs on where/having conditions

with cte as(
select *, rank()over(partition by city order by cases asc) as rnk, row_number()over(partition by city order by days asc) as rn
from covid),
cte1 as(
select *, case when rnk=rn then 1 else 0 end as flag
from cte)

select city, count(1) as galf, sum(flag)
from cte1
group by city
having count(1) = sum(flag)

Ответить
@satviknaren9681
@satviknaren9681 - 09.01.2023 15:16

WITH CTE AS ( SELECT * ,
RANK() over(partition by city order by days ) rk1 ,
rank() over(partition by city order by cases asc ) rk2 ,
RANK() over(partition by city order by days ) - rank() over(partition by city order by cases asc ) rk3
from covid )

SELECT city
from cte
group by city
having count(distinct rk3) = 1

Ответить
@rajunaik8803
@rajunaik8803 - 12.10.2022 15:19

with cte1 as(
select city,days,cases,rank() over(partition by city order by cases asc) as rnk from covid
),cte2 as(
select *,ROW_NUMBER() over(partition by city order by days) rm from cte1 --order by city,days
)select city as diff from cte2 group by city having count(distinct rnk-rm)=1

Ответить
@Abhi-rk3on
@Abhi-rk3on - 05.09.2022 18:18

my approach -

with cte1 as (
with cte as (
select * , Rank() over(partition by city order by cases asc ) as rank1 ,
Rank() over(partition by city order by days asc ) as rank2 from covid )

select * , case when rank1 > rank2 then 1 when rank1<rank2 then 0 else 1 end as status1 from cte )

select city from cte1 group by city having sum(status1) = count(status1) ;

Ответить
@prashantmhatre9225
@prashantmhatre9225 - 25.08.2022 19:34

hello Ankit ,
My solution is as below -


-- this will work in snowflake , used qualify clause just to simplify.

select city, diff , count(*) over ( partition by city) cnt from (
select city , diff from (
select * , row_number() over ( partition by city order by days ) row_num
,rank() over ( partition by city order by cases ) rnk , rnk - row_num diff from covid )
group by city , diff ) qualify cnt = 1 ;

Ответить
@sandippalit8990
@sandippalit8990 - 21.08.2022 15:10

with covidAnalysis as
(
select city, days, cases, (case when cases - lag(cases) over (partition by city order by days) <=0 then 0 else 1 end) flag
from covid
)

select city
from covidAnalysis
group by city
having count(flag)=sum(flag);

Ответить
@bishwarup1429
@bishwarup1429 - 10.08.2022 18:14

SET sql_mode='NO_UNSIGNED_SUBTRACTION';
select * from covid;
with cte as (
select *, rank() over(partition by city order by cases) as ranked
from covid
order by city,days ),
cte2 as(
select *, case when day(days) = ranked then 1 else 0 end as sts
from cte)
select city from cte2
group by 1
having sum(sts) = 3;

Ответить
@shubhamagrawal7068
@shubhamagrawal7068 - 20.07.2022 20:46

with t as (
select *,
cast(row_number() over(partition by city) as signed) rn,
cast(dense_rank() over(partition by city order by cases) as signed) rnk,
cast(row_number() over(partition by city) as signed)-cast(dense_rank() over(partition by city order by cases) as signed) diff
from covid)


select distinct city from covid where city not in (select distinct city from t where diff <> 0)

Ответить
@Artouple
@Artouple - 09.07.2022 11:12

I was thinking, instead of count(disitnct diff) = 1 and avg(diff) = 0, we can use max(diff) = 0 alone?

Ответить