Тёмный

Persistent Systems SQL Interview Question using LEAST(), GREATEST() and ROW_NUMBER() Functions 

Cloud Challengers
Подписаться 4,2 тыс.
Просмотров 3,1 тыс.
50% 1

One of the SQL questions recently asked in Persistent Systems interview.
We need to Find unique combination of records in output.
To solve this, we will write a query by using least, greatest and Rownumber() functions.
Create table syntax
CREATE TABLE routes (Origin VARCHAR(50), Destination VARCHAR(50));
INSERT INTO routes VALUES ('Bangalore', 'Chennai'), ('Chennai', 'Bangalore'), ('Pune', 'Chennai'), ('Delhi', 'Pune');
For more SQL interview questions. Check out our playlist.
• SQL Interview Questions
Contact us:
info@cloudchallengers.com
Follow us on
Instagram : cloudchallengers
Facebook : cloudchallengers
LinkedIn : linkedin.com/company/cloudchallengers

Опубликовано:

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 17   
@gouravraj1169
@gouravraj1169 2 месяца назад
Your way of solving problems is excellent.. I have seen different approaches to solve this problem however this one is the shortest and easiest to understand.. Thanks much!
@CloudChallengers
@CloudChallengers 2 месяца назад
@gouravraj1169, Thanks for the feedback 🙂. Please watch other SQL videos as well that are posted on our channel.
@gouravraj1169
@gouravraj1169 2 месяца назад
@@CloudChallengers I have already watched most of the videos posted.. Keep those coming.. Kudos!
@ChaitanyaKariya-x4q
@ChaitanyaKariya-x4q Месяц назад
select R1.origin , R1.destination from routes R1 left join routes R2 on R1.destination = R2.origin and r1.origin = r2.destination where (R2.origin is null or R2.destination is null) or r1.origin < r2.origin
@HARSHRAJ-gp6ve
@HARSHRAJ-gp6ve Месяц назад
with cte as( select origin,Destination,ROW_NUMBER()OVER() AS row_val FROM routes ),cte1 as( select origin as origin1,Destination as dist1 FROM routes ),cte2 as( select row_val,origin,Destination,origin1,dist1 FROM cte LEFT JOIN cte1 ON cte.Destination=cte1.origin1 ),cte3 as( select row_val,origin,Destination FROM cte2 where Destination=origin1 and origin=dist1 ),cte4 as( select origin,Destination FROM cte3 where row_val%2!=0 ORDER BY row_val ),cte5 as( select origin,Destination FROM cte2 where row_val NOT IN (select row_val FROM cte3) ) select * FROM cte4 union select * FROM cte5;
@sravankumar1767
@sravankumar1767 2 месяца назад
Superb explanation
@monasanthosh9208
@monasanthosh9208 2 месяца назад
Select Origin,Destination from (Select *,row_number() over (Partition by grp) as RN from (Select *,Case When Origin>Destination then concat(origin,Destination) Else concat(destination,origin) end as Grp from Routes)N) N where Rn =1;
@akash.i7391
@akash.i7391 Месяц назад
In the Oracle Database.. We don't have that function ( least () , greatest() , what should I do now..?
@CloudChallengers
@CloudChallengers Месяц назад
least (), greatest() functions should work in oracle. Please check your query
@Jhanavi.Sunilkongadi
@Jhanavi.Sunilkongadi Месяц назад
SELECT ORIGIN,DESTINATION FROM (SELECT*,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RNUM FROM ROUTE) A WHERE RNUM NOT IN (2);
@hairavyadav6579
@hairavyadav6579 Месяц назад
Great question , i solved and post daily sql question on linkdin and also tag you but you can't like or any thing, i think you are not active on linkdin
@CloudChallengers
@CloudChallengers Месяц назад
@hairavyadav6579, Due to some personal commitments I'm not much active on LinkedIn these days. I will start responding soon.
@pritambanerjee6957
@pritambanerjee6957 Месяц назад
Here is my solution using CTE and LAG() function : WITH cte AS( SELECT *, LAG(Origin,1,0) OVER() AS prev_origin, LAG(Destination,1,0) OVER() AS prev_destination FROM routes) SELECT Origin, Destination FROM cte WHERE (Origin != prev_destination OR Destination != prev_origin);
@Harish0402
@Harish0402 2 месяца назад
---using row_number() analyticalfunction and inline view select * from (select r.*, row_number () over (partition by least(origin,destination) ,greatest(origin, destination) order by origin) rn from routes r) where rn=1; --using cte and row_number () with cte as (select r.*, row_number () over (partition by least(origin,destination) ,greatest(origin, destination) order by origin) rn from routes r) select origin , destination from cte where rn=1; ---other way using row_number and case statement with cte as (select r.*, row_number () over (partition by case when origin < destination then origin else destination end, case when origin > destination then origin else destination end order by origin) rn from routes r) select origin , destination from cte where rn=1;
@mohanprasanthmanickam8292
@mohanprasanthmanickam8292 2 месяца назад
Thanks For the Video
@vivektiwari372
@vivektiwari372 2 месяца назад
my solution for same problem with cte as( select t1.orgin,t1.destination,count(*) as cnt from travel t1 left join travel t2 on t1.orgin = t2.destination group by t1.orgin,t1.destination)select orgin,destination from cte where cnt
@CloudChallengers
@CloudChallengers 2 месяца назад
@vivektiwari372, Thanks for sharing different approach.
Далее
WIPRO SQL Interview Question - FIRST_VALUE( ) Function
11:18
UUID vs INT: What’s Better For Your Primary Key?
9:40
다리찢기 고인물⁉️😱 Leg Splits Challenge
00:37
3 Solutions to a ITC Infotech SQL Interview Question
20:01
What does a Data Analyst actually do? (in 2024) Q&A
14:27
Database Replication & Sharding Explained
6:53
Просмотров 25 тыс.