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

Поделиться
HTML-код
  • Опубликовано: 25 июл 2024
  • 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 RUclips 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 RUclips 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!

Комментарии • 186

  • @ganeshsundarachary3239
    @ganeshsundarachary3239 4 месяца назад +5

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

  • @shwetamishra1497
    @shwetamishra1497 4 месяца назад +1

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

  • @aarizaziz4380
    @aarizaziz4380 4 месяца назад

    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.

  • @ContentRoomB
    @ContentRoomB 18 дней назад

    I'm finally done with query 1 with your solution help. looking forward to cracking future questions on my own

  • @beautifulworld3976
    @beautifulworld3976 4 месяца назад

    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

  • @Hsalz
    @Hsalz 4 месяца назад

    Simply amazing. Thanks, Thoufiq!

  • @khushboobaghel6594
    @khushboobaghel6594 4 месяца назад +1

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

  • @piyushnautiyal6271
    @piyushnautiyal6271 4 месяца назад +1

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

  • @padhenamit
    @padhenamit 4 месяца назад

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

  • @yi-lehung6311
    @yi-lehung6311 4 месяца назад

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

  • @junaidmahmud2894
    @junaidmahmud2894 4 месяца назад

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

  • @kalpeshispatil
    @kalpeshispatil 4 месяца назад

    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

  • @Dopamine.Moments
    @Dopamine.Moments 4 месяца назад

    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. 😊

  • @andreanlobo7373
    @andreanlobo7373 4 месяца назад +1

    extremely insightful.. thanks

  • @rahuldey4730
    @rahuldey4730 4 месяца назад +1

    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.

  • @MwAVlogs
    @MwAVlogs 4 месяца назад

    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.

  • @pinkfloyd2642
    @pinkfloyd2642 4 месяца назад

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

  • @mkgeidam
    @mkgeidam 4 месяца назад +1

    Thanks you Sir, it really helpful

  • @pavanigoud98
    @pavanigoud98 4 месяца назад +1

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

  • @khushbu3025
    @khushbu3025 4 месяца назад

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

  • @prithvirajpatil7388
    @prithvirajpatil7388 4 месяца назад

    Good question❤ starting it from today.

  • @ramakrishnatirumala428
    @ramakrishnatirumala428 3 месяца назад

    super videos bro...keep posting

  • @12Patni
    @12Patni 3 месяца назад

    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 < brand2 then CONCAT(brand1,brand2,year) else CONCAT(brand2,brand1,year) end as Brand
    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 Месяц назад

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

  • @livelovelaugh4050
    @livelovelaugh4050 4 месяца назад

    Thank you so much for starting this series. 🙏

  • @malcorub
    @malcorub 4 месяца назад +47

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

    • @techTFQ
      @techTFQ  4 месяца назад +6

      Thats the spirit 😃

    • @somnathdutta6311
      @somnathdutta6311 4 месяца назад +2

      @@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 3 месяца назад

      @@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 Месяц назад

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

  • @koushikdey100
    @koushikdey100 4 месяца назад

    Great !!, Thank you

  • @VivekKBangaru
    @VivekKBangaru 3 месяца назад

    Awesome TFQ

  • @splendidabhi
    @splendidabhi 4 месяца назад +2

    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  4 месяца назад

      Amazing 😍

  • @MuhammedSavadkv
    @MuhammedSavadkv 3 месяца назад

    Great. Thank you

  • @user-ox4fi3ls8e
    @user-ox4fi3ls8e 3 месяца назад

    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.

  • @user-js2vt3nz2k
    @user-js2vt3nz2k 23 дня назад

    Thank you for explaining thoroughly :-)

  • @sourabroy7787
    @sourabroy7787 4 месяца назад

    great initiative and quality content 👍

  • @navaneeth6414
    @navaneeth6414 4 месяца назад

    Great! video Thanks

    • @techTFQ
      @techTFQ  4 месяца назад

      Glad you liked it!

  • @johnsonrajendran6194
    @johnsonrajendran6194 4 месяца назад

    Love the way you teach ❤

  • @VijayKumar-ec3dz
    @VijayKumar-ec3dz 3 месяца назад +1

    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)

  • @nointernet...1800
    @nointernet...1800 4 месяца назад

    Nice video

  • @aswinc4829
    @aswinc4829 4 месяца назад

    Thank you,it really helps

  • @sandydsa
    @sandydsa 4 месяца назад

    Fantastic 🌻🌻🌻

  • @srinubathina7191
    @srinubathina7191 4 месяца назад

    Thank You sir

  • @jollymeelubari9045
    @jollymeelubari9045 4 месяца назад

    Thanks for this

  • @Itsourfamilys
    @Itsourfamilys 3 месяца назад

    Thank you so much for SQL videos

  • @fathimafarahna2633
    @fathimafarahna2633 4 месяца назад +2

    Amazing 🔥🎉

    • @techTFQ
      @techTFQ  4 месяца назад

      Glad you like it!

  • @khushboobaghel6594
    @khushboobaghel6594 4 месяца назад

    Thankyou Sir

  • @The_Narutoboy_15
    @The_Narutoboy_15 4 месяца назад

    thank you sir

  • @vishisbest
    @vishisbest 4 месяца назад

    This was pretty interesting

  • @abhishekkukreja6735
    @abhishekkukreja6735 2 месяца назад

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

  • @riyatiwari4767
    @riyatiwari4767 4 месяца назад

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

  • @pathanfirozkhan5503
    @pathanfirozkhan5503 4 месяца назад +1

    Amazing video sir, Thanks for your time

  • @mindlessscroll
    @mindlessscroll 3 месяца назад +2

    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 2 месяца назад

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

    • @gauravtanwar8886
      @gauravtanwar8886 2 месяца назад +1

      but thanks to your answer i got to know about STRCMP

  • @ehsanshakeri620
    @ehsanshakeri620 4 месяца назад

    Thank you

  • @dataengineeringsimplified1049
    @dataengineeringsimplified1049 Месяц назад

    with tbl as
    (
    SELECT *,case when brand1

  • @sivakrishnasriram4782
    @sivakrishnasriram4782 4 месяца назад

    Thank You😊.

  • @inderpanda1
    @inderpanda1 4 месяца назад +4

    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

  • @ManiKandan-kg5ky
    @ManiKandan-kg5ky 4 месяца назад

    Waiting bro

  • @ananth.r5317
    @ananth.r5317 4 месяца назад

    Thanks lots..❤❤❤❤

  • @sandeepkumarsingh7981
    @sandeepkumarsingh7981 4 месяца назад

    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.....

  • @ahmedjassimameenco
    @ahmedjassimameenco 4 месяца назад

    thanks

  • @ritika29
    @ritika29 3 месяца назад

    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);

  • @Venkatesh-bs7kb
    @Venkatesh-bs7kb 4 месяца назад

    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)')

  • @priyankapatil6835
    @priyankapatil6835 4 месяца назад

    big thanks techTFQ

  • @lakshitgupta5064
    @lakshitgupta5064 4 месяца назад

    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?

  • @phamnguyentrucgiang569
    @phamnguyentrucgiang569 4 месяца назад

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

  • @MrYeduguri
    @MrYeduguri 4 месяца назад

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

  • @jayavinayak2593
    @jayavinayak2593 4 месяца назад +1

    Thank You

    • @techTFQ
      @techTFQ  4 месяца назад

      You're welcome

  • @CricketLivejavwad
    @CricketLivejavwad 4 месяца назад +5

    Techtfq is back💯😎
    We all are excited.

  • @waynegreen7970
    @waynegreen7970 4 месяца назад

    Good content!

    • @techTFQ
      @techTFQ  4 месяца назад

      Glad you think so!

  • @sweetysweetyvghb
    @sweetysweetyvghb Месяц назад

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

  • @manirahofred801
    @manirahofred801 3 месяца назад

    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!

  • @venkateshmallisetty4196
    @venkateshmallisetty4196 4 месяца назад +1

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

  • @hrishiAOL
    @hrishiAOL 17 дней назад

    Can you suggest online SQL editor for practice?

  • @smrutiranjansenapati1695
    @smrutiranjansenapati1695 4 месяца назад

    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

  • @shristisrivastava1324
    @shristisrivastava1324 4 месяца назад

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

  • @prithvipampana1492
    @prithvipampana1492 4 месяца назад

    Bro @malcorub what if brand1 and brand2 length is same

  • @armaqanalibabaei6980
    @armaqanalibabaei6980 2 месяца назад

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

  • @vishalmaurya3008
    @vishalmaurya3008 4 месяца назад +1

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

  • @pavankumar-cw2sx
    @pavankumar-cw2sx 4 месяца назад

    @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);
    ????????????????

  • @madhurimadas6260
    @madhurimadas6260 4 месяца назад

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

    • @bhanugoyal428
      @bhanugoyal428 4 месяца назад

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

  • @tornado9689
    @tornado9689 4 месяца назад

    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

  • @SamiUllah-ie7pn
    @SamiUllah-ie7pn 4 месяца назад

    Thanks you😊

    • @techTFQ
      @techTFQ  4 месяца назад

      Welcome 😊

  • @cosmicball4888
    @cosmicball4888 3 месяца назад

    but how it check the condition custom1=custom3 and custom2-=custom4
    ?

  • @user-gr9cd6cr8q
    @user-gr9cd6cr8q 4 месяца назад

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

  • @Unbox9999
    @Unbox9999 4 месяца назад

    More 8 mints to go

  • @haleylearn
    @haleylearn 2 месяца назад

    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) ?

  • @SivaKumarP_alliswell
    @SivaKumarP_alliswell 4 месяца назад

    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-vo3yr8hh8w 4 месяца назад

    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)

  • @sateeshbabu5792
    @sateeshbabu5792 Месяц назад

    Awesome video. do you have video on sql performance ? if yes could you please share.

  • @yashmistry1424
    @yashmistry1424 4 месяца назад

    Can we use self join instead here!

  • @krishkhemani96
    @krishkhemani96 3 месяца назад

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

  • @shovabaral2797
    @shovabaral2797 4 месяца назад

    Hello , I am looking help from you ❤

  • @leoneruri6857
    @leoneruri6857 4 месяца назад

    will you cover ORMs

  • @mohdtoufique3786
    @mohdtoufique3786 4 месяца назад

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

  • @rajuomkar5220
    @rajuomkar5220 4 месяца назад

    By using dense_rank we can solve this

  • @user-bv2qd5wt1q
    @user-bv2qd5wt1q 4 месяца назад

    Any way to download the data without using Discord? I am not good at Discord

  • @saikathazra6732
    @saikathazra6732 4 месяца назад

    Where is the data set ?

  • @sriharidhanakshirur9245
    @sriharidhanakshirur9245 4 месяца назад

    Intresting Question!
    Im sure if anyone deligently follow this #30DaysSQLChallenge
    his/her SQL skills drastically improves

  • @Shruti_Gore
    @Shruti_Gore 2 месяца назад

    Can you make 30 days sql easy to hard playlist

  • @manvendramingwal8696
    @manvendramingwal8696 2 месяца назад

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

  • @deepakreddy5738
    @deepakreddy5738 3 месяца назад

    Can anyone explain me how to import data into SQL plz. I an unable to join discord server also

  • @asitkandpal4234
    @asitkandpal4234 3 месяца назад

    can anybody evaluate my code how efficient it is. because to me it looks pathetic..
    with trialcte1 as (
    with trialcte as(
    SELECT *, case
    when brand1 != brand2 and custom1 = custom3 and custom2 = custom4 then 'yes'
    else 'no'
    end as'topic'
    FROM product_data)
    select * , row_number() over(partition by topic order by year) as atmpt from trialcte)
    select brand1 , brand2 , year , custom1 , custom2 , custom3 , custom4 from trialcte1
    where (topic = 'no' and atmpt = 1) or
    (topic = 'no' and atmpt = 2) or
    (topic = 'no' and atmpt = 3) or
    (topic = 'no' and atmpt = 4) or
    (topic = 'yes' and atmpt = 1)
    thanks for your help

  • @amanchourasia8280
    @amanchourasia8280 4 месяца назад

    what if the brand names start with the same letter?