Remove Redundant Pairs - SQL Interview Query 1 | SQL Problem Level "HARD"

30DaySQLQueryChallenge is a series of 30 videos covering 30 SQL Interview Queries. This is the first video in this series. This video series aims to provide 30 SQL Queries that can be asked during SQL Interviews. In these videos, I will explain how to solve and address such SQL queries during interviews.
This particular video covers an SQL Interview query that I found on Reddit.
Learn and Practice SQL on LearnSQL platform below:
learnsql.com/?ref=thoufiqmoha...
Let's follow the below routine to make the best use of it:
1. Watch the KZread video (first half) to understand the problem statement.
2. Go to my discord server (link below), download the dataset for each problem, and try solving it yourself.
3. Share your solution on Discord and discuss different solutions and issues on my Discord server.
4. Watch the second half of my KZread video to find my solution to the problem.
5. Share it with your contacts and spread the knowledge.
DOWNLOAD the Dataset from below:
Discord server: / discord
Blog website: techtfq.com/blog/30daysqlquer...
Timeline:
00:00 Intro about the #30DaySQLQueryChallenge
00:58 Problem Statement Query1
05:43 #30DaySQLQueryChallenge Process to be followed
07:16 Solution to the Query1
Thanks for participating in this challenge!
Good luck and Happy Learning!

Пікірлер: 182

  • @Hsalz
    @Hsalz3 ай бұрын

    Simply amazing. Thanks, Thoufiq!

  • @ganeshsundarachary3239
    @ganeshsundarachary32393 ай бұрын

    Awesome , Great learning Experience. Pls make more such challenges.

  • @yi-lehung6311
    @yi-lehung63113 ай бұрын

    still the best SQL video!!!! I love this idea, please continue!

  • @shwetamishra1497
    @shwetamishra14973 ай бұрын

    Very helpful . Thank you so much big bro for this series, just keep continue.🙏

  • @khushboobaghel6594
    @khushboobaghel65943 ай бұрын

    Thankyou for guiding for 30 days it will help us to improove Sql.

  • @piyushnautiyal6271
    @piyushnautiyal62713 ай бұрын

    amazing que and the way of solving is awesome.. thank u so much techTFQ!!

  • @andreanlobo7373
    @andreanlobo73733 ай бұрын

    extremely insightful.. thanks

  • @rahuldey4730
    @rahuldey47303 ай бұрын

    Loved the question Sir. Even if I was not able to solve this at my own but after I saw the solution and the logic behind this I am speechless and amazed. This is the type of content we need.

  • @VivekKBangaru
    @VivekKBangaru2 ай бұрын

    Awesome TFQ

  • @padhenamit
    @padhenamit3 ай бұрын

    Thank you for your amazing lectures and feedbacks and thank you for being so supportive!

  • @aarizaziz4380
    @aarizaziz43803 ай бұрын

    Thank you very very very much for this initiative. Looking forward to a multi decade event by you just like this! You are one of the best instructors I have come across internet for SQL. hats off to you for taking time to educate us and building the community.

  • @khushbu3025
    @khushbu30253 ай бұрын

    Really, It's very nice 🙂 Thank you for this series.

  • @ramakrishnatirumala428
    @ramakrishnatirumala4282 ай бұрын

    super videos bro...keep posting

  • @beautifulworld3976
    @beautifulworld39763 ай бұрын

    It is informative from the problem statement we get idea how to solve thank you so much it is helpful for me this 30 days i will become still more strong in SQL database

  • @junaidmahmud2894
    @junaidmahmud28943 ай бұрын

    Great solution. I'm afraid I am a bit late to start but I will definitely continue this!

  • @mkgeidam
    @mkgeidam3 ай бұрын

    Thanks you Sir, it really helpful

  • @Dopamine.Moments
    @Dopamine.Moments3 ай бұрын

    You are helping me a lot to build my understanding about SQL, Thanks to you I have cleared most of the interviews by watching and following you. I will be turning in for this series. 😊

  • @prithvirajpatil7388
    @prithvirajpatil73883 ай бұрын

    Good question❤ starting it from today.

  • @kalpeshispatil
    @kalpeshispatil3 ай бұрын

    You are simply amazing buddy... I am user CTE many times, but not as you shown.. Fan of yours... ❤❤ Will be tuning daily.. Your first example has cracked and shown us where we land in DB queries... Thank You For Us Efforts

  • @livelovelaugh4050
    @livelovelaugh40503 ай бұрын

    Thank you so much for starting this series. 🙏

  • @koushikdey100
    @koushikdey1003 ай бұрын

    Great !!, Thank you

  • @nointernet...1800
    @nointernet...18003 ай бұрын

    Nice video

  • @sourabroy7787
    @sourabroy77873 ай бұрын

    great initiative and quality content 👍

  • @sandydsa
    @sandydsa3 ай бұрын

    Fantastic 🌻🌻🌻

  • @pavanigoud98
    @pavanigoud983 ай бұрын

    Please also doo the sql interview questions for data analyst. Lots of love ❤

  • @pinkfloyd2642
    @pinkfloyd26423 ай бұрын

    Subscribed right away. Great content. Can't wait to watch more of your videos this weekend itself.

  • @sabmelegarebaba1233

    @sabmelegarebaba1233

    3 ай бұрын

    Bro pz upload 2 video per day 😊

  • @johnsonrajendran6194
    @johnsonrajendran61943 ай бұрын

    Love the way you teach ❤

  • @MuhammedSavadkv
    @MuhammedSavadkv2 ай бұрын

    Great. Thank you

  • @ManiKandan-kg5ky
    @ManiKandan-kg5ky3 ай бұрын

    Waiting bro

  • @navaneeth6414
    @navaneeth64143 ай бұрын

    Great! video Thanks

  • @techTFQ

    @techTFQ

    3 ай бұрын

    Glad you liked it!

  • @srinubathina7191
    @srinubathina71913 ай бұрын

    Thank You sir

  • @MwAVlogs
    @MwAVlogs3 ай бұрын

    I am new in this field, but like the way you explain. It is a complex one I know but still after practicing I’ll get it.

  • @khushboobaghel6594
    @khushboobaghel65943 ай бұрын

    Thankyou Sir

  • @aswinc4829
    @aswinc48293 ай бұрын

    Thank you,it really helps

  • @The_Narutoboy_15
    @The_Narutoboy_153 ай бұрын

    thank you sir

  • @malcorub
    @malcorub3 ай бұрын

    I love the idea! I will be tuning in every day this month.... even on weekends with hangover. LOL

  • @techTFQ

    @techTFQ

    3 ай бұрын

    Thats the spirit 😃

  • @somnathdutta6311

    @somnathdutta6311

    3 ай бұрын

    @@techTFQ Don't give the ans in same video. give ans in next video. It will be more helpful. It will also create a hook for your next video and a short of competition as well.

  • @umangbhatnagar1415

    @umangbhatnagar1415

    2 ай бұрын

    @@somnathdutta6311 Why making things tough. Let it be easy. We are not as good as you. The video's purpose is to help community who is still in its very crude form.

  • @Useruse867

    @Useruse867

    22 күн бұрын

    @@somnathdutta6311 no one will ever watch as there are many channels who solve and give answer there.

  • @jollymeelubari9045
    @jollymeelubari90453 ай бұрын

    Thanks for this

  • @mindlessscroll
    @mindlessscroll2 ай бұрын

    With rm as (select *, IF(STRCMP(brand1,brand2) > 0, concat(brand1,brand2,year), concat (brand2,brand1,year)) as con from brands ), rm1 as( select *, row_number() OVER(PARTITION BY con order by con ) as id from rm) select brand1, brand2, year from rm1 where id=1 or (custom1 custom3 and custom2 custom4)

  • @gauravtanwar8886

    @gauravtanwar8886

    Ай бұрын

    as per the 3rd condition you need a little adjustment in your code (custom1 custom3 OR custom2 custom4)*

  • @gauravtanwar8886

    @gauravtanwar8886

    Ай бұрын

    but thanks to your answer i got to know about STRCMP

  • @fathimafarahna2633
    @fathimafarahna26333 ай бұрын

    Amazing 🔥🎉

  • @techTFQ

    @techTFQ

    3 ай бұрын

    Glad you like it!

  • @vishisbest
    @vishisbest3 ай бұрын

    This was pretty interesting

  • @Itsourfamilys
    @Itsourfamilys2 ай бұрын

    Thank you so much for SQL videos

  • @ehsanshakeri620
    @ehsanshakeri6203 ай бұрын

    Thank you

  • @ahmedjassimameenco
    @ahmedjassimameenco3 ай бұрын

    thanks

  • @sivakrishnasriram4782
    @sivakrishnasriram47823 ай бұрын

    Thank You😊.

  • @ananth.r5317
    @ananth.r53173 ай бұрын

    Thanks lots..❤❤❤❤

  • @splendidabhi
    @splendidabhi3 ай бұрын

    Excited to kick off the #30DaySQLQueryChallenge with Query #1! Let's sharpen those SQL skills together and ace those interview queries. Can't wait to see what we'll learn throughout this challenge! 💻💡

  • @techTFQ

    @techTFQ

    3 ай бұрын

    Amazing 😍

  • @12Patni
    @12Patni2 ай бұрын

    Thoufiq Mohammed Thank you for the thorough explanation. The problem's difficulty level is high. Below is my solution: I utilized others columns in the Partition By clause, ensuring accurate row numbering without the need for an additional where clause condition. ;with CTE AS ( Select * , case when brand1 from [Asif_SQL].[dbo].[brands] ), CTE_RN AS ( SELECT * , ROW_NUMBER () OVER (PARTITION BY BRAND,CUSTOM1,CUSTOM2,CUSTOM3,CUSTOM4 ORDER BY BRAND ASC ) AS RN FROM CTE ) SELECT brand1,brand2,year,custom1,custom2,custom3,custom4 FROM CTE_RN WHERE RN = 1

  • @shabbiransari9731

    @shabbiransari9731

    20 күн бұрын

    What will you do if the brand is of equal length?

  • @VijayKumar-ec3dz
    @VijayKumar-ec3dz2 ай бұрын

    Hi sir, My solution is kind of similar as below with cte as (select *,case when custom1=custom3 and custom2=custom4 then 1 when custom1custom3 or custom2custom4 then 2 else 3 end as r from t1) select * except(r,rn) from (select *,row_number() over(partition by r,year order by year) as rn from cte) a where coalesce(case when r=1 then rn=1 else null end,1=1)

  • @pathanfirozkhan5503
    @pathanfirozkhan55033 ай бұрын

    Amazing video sir, Thanks for your time

  • @CricketLivejavwad
    @CricketLivejavwad3 ай бұрын

    Techtfq is back💯😎 We all are excited.

  • @priyankapatil6835
    @priyankapatil68353 ай бұрын

    big thanks techTFQ

  • @jayavinayak2593
    @jayavinayak25933 ай бұрын

    Thank You

  • @techTFQ

    @techTFQ

    3 ай бұрын

    You're welcome

  • @user-ox4fi3ls8e
    @user-ox4fi3ls8e2 ай бұрын

    we can put union as well in place of or, and case statements for the conditions in place of Row_num window function, rest is same.

  • @dataengineeringsimplified1049
    @dataengineeringsimplified104924 күн бұрын

    with tbl as ( SELECT *,case when brand1

  • @waynegreen7970
    @waynegreen79703 ай бұрын

    Good content!

  • @techTFQ

    @techTFQ

    3 ай бұрын

    Glad you think so!

  • @inderpanda1
    @inderpanda13 ай бұрын

    In my opinion at least out of 30 question first 5 should have been Easy to Medium Level. It seems to be hard and complex for me. The questions should have been from only 1 function among these CTE, Case, Subquery, etc

  • @amlansasmal

    @amlansasmal

    3 ай бұрын

    same brother:)

  • @riyatiwari4767
    @riyatiwari47673 ай бұрын

    Can you please make video on how to write dynamic query in pyspark. That would be a lot of help. Thank u❤

  • @vishalmaurya3008
    @vishalmaurya30083 ай бұрын

    ❤ 30days of sql >>>>75 days of 😊 hard चैलेंज 😅 Thank you sir that is extremely amazing❤❤❤❤❤❤❤

  • @SamiUllah-ie7pn
    @SamiUllah-ie7pn3 ай бұрын

    Thanks you😊

  • @techTFQ

    @techTFQ

    3 ай бұрын

    Welcome 😊

  • @MrYeduguri
    @MrYeduguri3 ай бұрын

    Hello Thoufiq.. Thank you so much for sharing great content. Are you offering online training for sql & plsql?

  • @Unbox9999
    @Unbox99993 ай бұрын

    More 8 mints to go

  • @sandeepkumarsingh7981
    @sandeepkumarsingh79813 ай бұрын

    Hi Tafiq, I'm also your huge friend, and your technic is always help to improve my skill. I saw the solution and the logic behind this I am speechless and amazed. This is the type of content we need. thanks you so much. Hey Tofiq, I have downloaded you content but that file is blank. please help me on that.....

  • @sweetysweetyvghb
    @sweetysweetyvghbКүн бұрын

    I couldn't solve myself, but I find it easy after seeing the solution..not sure y.

  • @Venkatesh-bs7kb
    @Venkatesh-bs7kb3 ай бұрын

    Thanks a lot for the video. the way you explain CTE's are awesome!! in your solution line number 20, as per problem statement, it should be an OR condition ryt ? ('or (custom1 custom3 OR custom2 custom4)')

  • @shovabaral2797
    @shovabaral27973 ай бұрын

    Hello , I am looking help from you ❤

  • @ritika29
    @ritika292 ай бұрын

    Thanks, it was Helpful 👍 Quick question, what if there was a row with NULL in brand1 column Example: INSERT INTO brands VALUES ( NULL,'lava', 2020, 5, 9, NULL, NULL);

  • @vishalsonawane.8905
    @vishalsonawane.89052 ай бұрын

    Done

  • @abhishekkukreja6735
    @abhishekkukreja6735Ай бұрын

    Awesome, Can you share excel page : it's good to store the results and everthing in excel

  • @krishkhemani96
    @krishkhemani962 ай бұрын

    I am sure you love cases😂.Make a video on them please 😅

  • @phamnguyentrucgiang569
    @phamnguyentrucgiang5693 ай бұрын

    ​ @techTFQ Hi, thanks for uploading useful videos. I want to ask: this is easy, medium or hard question?

  • @leoneruri6857
    @leoneruri68573 ай бұрын

    will you cover ORMs

  • @manirahofred801
    @manirahofred8012 ай бұрын

    This was really helpful. However, if the dataset was too big and brand records were many & different, I don't think "case when" functionality would be helpful. At the moment because of the dataset we have, that would be the easiest way!

  • @sabmelegarebaba1233
    @sabmelegarebaba12333 ай бұрын

    Bro pz upload 2 video per day😊

  • @haleylearn
    @haleylearnАй бұрын

    For condition: For pairs of brands in the same year ---> if custom1 != custom3 OR custom2 != custom4 : then keep both pairs Why is it (custom1 custom3 AND custom2 custom4) NOT is (custom1 custom3 OR custom2 custom4) ?

  • @mohdtoufique3786
    @mohdtoufique37863 ай бұрын

    Thanks a lot for the content.. WITH PAIRS AS( SELECT *,CASE WHEN brand1

  • @lakshitgupta5064
    @lakshitgupta50643 ай бұрын

    What if after creating the pairid, we just perform groupby operation based on pairid, custom1, custom2, custom3 and custom4 and then filter it by using having count = 1? Will this approach work?

  • @HimanshuSingh-cf7wr
    @HimanshuSingh-cf7wr3 ай бұрын

    My Approach:- select BRAND1,BRAND2,year,Custom1,Custom2,Custom3,Custom4 from (select case when rk=2 and Custom1==custom3 and custom2==custom4 then 1 else 0 end as duplicate_entry,p.* from (select row_number() over (partition by common_name,year order by Brand1) as rk,k.* from (select *,concat(greatest(BRAND1,BRAND2),least(BRAND1,BRAND2)) as common_name from input_table)k)p)o where duplicate_entry1 order by BRAND1

  • @SivaKumarP_alliswell
    @SivaKumarP_alliswell3 ай бұрын

    Below query will also work right ? with tab1 as (select * from brands where c1 c3 or c2 c4), tab2 as (select * from brands where (b1 is null or b2 is null), tab3 as (select b1, b2, year, c1, c2, c3, c4 from brands where c1 = c3 and c2 = c4), tab4 as (select b2, b1, year, c1, c2, c3, c4 from brands where c1 = c3 and c2 = c4) select * from tab1 UNION select * from tab2 UNION select * from tab3 UNION select * from tab4;

  • @user-vo3yr8hh8w
    @user-vo3yr8hh8w3 ай бұрын

    with cte_check as ( select *, case when (custom1custom3 and custom2custom4) or custom1 is null or custom2 is null or custom3 is null or custom4 is null then 1 when brand1 = LEAD(brand2) over(order by year) and LEAD(brand1) over(order by year)=brand2 and year = LEAD(year) over(order by year) and (custom1=custom3 and custom2=custom4) then 2 end as dd from brands ) select brand1,brand2,year,custom1,custom2,custom3,custom4 from cte_check where dd in (1,2)

  • @chahatjain3745
    @chahatjain37453 ай бұрын

    with cte as (Select *, row_number() over(partition by Case when brand1>brand2 then CONCAT(brand1,brand2,year) else CONCAT(brand2,brand1,year) end order by year) as rn from brands) Select * from cte rn where rn=1 or (custom1 custom3 and custom2 custom4)

  • @rajuomkar5220
    @rajuomkar52203 ай бұрын

    By using dense_rank we can solve this

  • @surabhijagadish5210
    @surabhijagadish52102 ай бұрын

    Made complex query too easy.

  • @madhurimadas6260
    @madhurimadas62603 ай бұрын

    amazing!! can you tell me what to do in case of equal length of brand1 and brand2? thanks in advance :)

  • @bhanugoyal428

    @bhanugoyal428

    3 ай бұрын

    you can put that condition in the same case along with < condition. Ultimately we are concatenating all the records in that cte.

  • @venkateshmallisetty4196
    @venkateshmallisetty41963 ай бұрын

    it is very tipical to download the data set could you please help me with that

  • @prithvipampana1492
    @prithvipampana14923 ай бұрын

    Bro @malcorub what if brand1 and brand2 length is same

  • @shristisrivastava1324
    @shristisrivastava13243 ай бұрын

    How will you know when to use CTE by looking at the question, sir?

  • @tornado9689
    @tornado96893 ай бұрын

    My question is you can also select query with column name what you have done in last to fetch the data . What is the need of using cte then

  • @Ikhideifidon

    @Ikhideifidon

    3 ай бұрын

    For Optimization sake

  • @smrutiranjansenapati1695
    @smrutiranjansenapati16953 ай бұрын

    Hi, Since it has example of 5-6 records, when we have large amount of records, how we can mak sure that the 1st condition if custom1 = custom3 & custom2 = custom4 will not be satisfied to eliminate the duplicate? Please reply. Thank you

  • @Shruti_Gore
    @Shruti_GoreАй бұрын

    Can you make 30 days sql easy to hard playlist

  • @vikrantlonkar2898
    @vikrantlonkar28983 ай бұрын

    Sir can u make videos on trigger,Index and match in SQL

  • @techTFQ

    @techTFQ

    3 ай бұрын

    Yes, soon

  • @vikrantlonkar2898

    @vikrantlonkar2898

    3 ай бұрын

    Thanks a lot I will be waiting for it

  • @user-gr9cd6cr8q
    @user-gr9cd6cr8q3 ай бұрын

    I try to download the dataset but I could not able to do it can you help me to download the data set

  • @user-tw3rf6oo9h
    @user-tw3rf6oo9h3 ай бұрын

    i was waiting for this

  • @manvendramingwal8696
    @manvendramingwal8696Ай бұрын

    how about 100 days of database series,from 0 to 100 making people perfect in DB, i would like to join.

  • @pavankumar-cw2sx
    @pavankumar-cw2sx3 ай бұрын

    @techTFQ Can we give OR condition instead of AND in WHERE condition at final query like as: where rn = 1 or (custom1 custom3 OR custom2 custom4); ????????????????

  • @yashmistry1424
    @yashmistry14243 ай бұрын

    Can we use self join instead here!

  • @saikathazra6732
    @saikathazra67323 ай бұрын

    Where is the data set ?

  • @karangupta_DE
    @karangupta_DE3 ай бұрын

    with cte as ( select *, case when custom1 = custom3 and custom2 = custom4 then 'match' else 'not_matched' end as is_match, row_number()over(order by (select null)) as rn, row_number()over(partition by year order by (select null)) as rn_1 from brands ), cte1 as ( select *, (rn - rn_1) as pair from cte ) select min(brand1) as brand1, max(brand2) as brand2, max(year) as year, max(custom1) as custom1, max(custom2) as custom2, max(custom3) as custom3, max(custom4) as custom4 from cte1 where is_match = 'match' group by pair union all select brand1, brand2, year, custom1, custom2, custom3, custom4 from cte1 where true and is_match != 'match';

  • @user-pk7rp4ej9y
    @user-pk7rp4ej9y3 ай бұрын

    With CTEs, we dont have to deal with subqueries!

  • @ashabhumza3394
    @ashabhumza33943 ай бұрын

    I think there should have been 'or' condition in the bracket instead of 'and'

  • @armaqanalibabaei6980
    @armaqanalibabaei6980Ай бұрын

    Hi May I ask a question?even thou you have filtered where rn = 1; , I still see one rn=2 in that column why?

Келесі