Most Asked SQL Interview Question - Solved Using 3 Methods

Поделиться
HTML-код
  • Опубликовано: 31 июл 2024
  • In this video, we will learn solving a most asked SQL interview question, which was asked in Google, Amazon and Uber interview. We will solve this question using 3 different methods.
    If you liked this video, then please hit LIKE button and SUBSCRIBE this channel for more videos related to Data Science- skills and jobs
    Timestamp:
    00:00 Intro
    00:17 SQL Interview Question
    01:30 Method 1- SQL Query Solution
    06:51 Method 2- SQL Query Solution
    11:28 Method 3- SQL Query Solution
    14:26 Outro
    ➖➖➖➖➖➖➖➖➖➖➖➖➖
    Related Videos
    Introduction to SQL - What Is SQL + Database | SQL Tutorial In Hindi 1
    • What Is SQL ? | Databa...
    Data Types, Primary-Foreign Keys & Constraints in SQL | SQL Tutorial In Hindi 2
    • Data Types, Primary-Fo...
    Create Table In SQL & Create Database | SQL Tutorial In Hindi 3
    • Create Table In SQL & ...
    INSERT UPDATE, DELETE & ALTER Table in SQL With Example | SQL Tutorial in Hindi 4
    • INSERT UPDATE, DELETE ...
    SELECT Statement & WHERE Clause with Example | Operators in SQL | SQL Tutorial in Hindi 5
    • SELECT Statement & WHE...
    How To Import Excel File (CSV) to SQL | Import Data From File | SQL Tutorial in Hindi 6
    • How To Import Excel Fi...
    String Functions in SQL | Functions in SQL | SQL Tutorial in Hindi 7
    • String Functions in SQ...
    Aggregate Functions in SQL - COUNT, SUM, AVG, MAX, MIN | SQL Tutorial in Hindi 8
    • Aggregate Functions in...
    Group By and Having Clause in SQL | SQL Tutorial in Hindi 9
    • Group By and Having Cl...
    TimeStamp and Extract Function | Date Time Function | SQL Tutorial in Hindi 10
    • TimeStamp and Extract ...
    Complete SQL JOINS For Beginners | SQL JOIN Queries with Examples | SQL Tutorial in Hindi 11
    • Complete SQL JOINS For...
    SQL SELF JOIN | UNION & UNION ALL in SQL | SQL Tutorial in Hindi 12
    • SQL SELF JOIN | UNION ...
    Complete SQL Subquery Using Comparison and Logical Operators | SQL Tutorial in Hindi 13
    • Complete SQL Subquery ...
    Useful videos
    How to Become Data Analyst in 2022 | Data Analyst Roadmap
    • How to Become Data Ana...
    Data Analyst skills, jobs and Roadmap
    • Data Analyst- Skills, ...
    Excel Tutorial for Beginners
    • Excel for Beginners | ...
    ➖➖➖➖➖➖➖➖➖➖➖➖➖
    Connect on other Social Media handles:
    Instagram: / rishabhnmishra
    LinkedIn: / rishabhnmishra
    Twitter: / rishabhnmishra
    ➖➖➖➖➖➖➖➖➖➖➖➖➖
    If this is the first ever video you're watching allow me to introduce myself, Hey I'm Rishabh Mishra and currently working as Senior Data Analyst at Bangalore. On this channel I like to guide and help my juniors and data science aspirants regarding Data Science jobs and useful tips for college students.
    Have a great day buddy!
    #sqlinterviewquestions #sqlinterviewquestionsandanswers

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

  • @subhradipbarik2032
    @subhradipbarik2032 Год назад +4

    In the first approach without doing group by and all,we can just apply distinct after greatest and and least things

  • @OmkarShinde-bz7oy
    @OmkarShinde-bz7oy Год назад

    here we can use mod function mod(id,2)=0 actully we can solve many methods but sir you solved very easy way great

  • @shivendusharma3592
    @shivendusharma3592 Год назад +2

    Mishra ji 🙏, you are wonderful teacher and it is reflected in your videos. 🙂 It is also evident from your prof. experience.

  • @hassamulhaq7762
    @hassamulhaq7762 Год назад

    I go with inner join. Method 2.
    Method 3 slightly difficult.

  • @floyedmoras1882
    @floyedmoras1882 Месяц назад +2

    Hi Rishabh, I was trying with multiple methods and i found if we use the query like
    select *
    from Travel
    where source > destination;
    the answer would be the same and this is the shortest method. though i m not sure if there is any discrepancy with this query, Please clarify or suggest.

  • @sauravlabade1366
    @sauravlabade1366 11 месяцев назад

    If you give the link of the tables in description which you used to solved the problems in this video then it will be better rishabh

  • @user-kp2uk8ei3d
    @user-kp2uk8ei3d 3 месяца назад +4

    create table travel (
    source varchar(50),
    destination varchar(50),
    distance int);
    insert into travel (source, destination, distance)
    values
    ('Mumbai', 'Bangalore', 500),
    ('Bangalore','Mumbai', 500),
    ('Delhi', 'Mathura', 150),
    ( 'Mathura','Delhi', 150),
    ('Nagpur', 'Pune', 500),
    ( 'Pune','Nagpur', 500);

    • @user-kk8xf1jc8h
      @user-kk8xf1jc8h Месяц назад

      should have checked comments before starting the video lol

  • @amitsakhare17
    @amitsakhare17 Год назад

    Sir in using self join I am getting only 2 rows of output but as you doing in your slide it's having 3 rows of data

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

    Hello Rishabh, Thanks for creating such simple explanation for complex problems, Your approach was really easy to understand. I have seen the same question on other youtube channels but it was quite difficult for me to understand the same problem.

  • @ashwanikumarkaushik2531
    @ashwanikumarkaushik2531 Год назад +4

    Sir, I enjoyed this session. Great explanation.
    I learnt SQL from a course recommended by you in one of your videos. SQL Go from Zero to hero by Jose Portilla. Few concepts were not covered in that course, such as windows functions, greatest, least etc. Can you please tell where can I also learn such concepts?

  • @vinaygupta7469
    @vinaygupta7469 9 месяцев назад +1

    hi, love your teaching technique, please make a video on advance power bi interview Q&A. it will really appreciated.
    thankyou

  • @venuamrutham6179
    @venuamrutham6179 11 месяцев назад

    My personal two methods,
    M1:
    select distance,GROUP_CONCAT(source) from (select distinct * from table) as sub_query
    GROUP BY distance; (:: "select distinct * from table" important if you have original duplicates cities pair)
    M2:
    select DISTINCT greatest(source,destination) as "from",least(source,destination) as "to",distance from table;

  • @piyushgupta5044
    @piyushgupta5044 Год назад +1

    great and excellence sir

  • @govindshah6037
    @govindshah6037 Год назад +17

    Please find the code to create the mentioned table in this tutorial:
    create table travel (
    source varchar(50),
    destination varchar(50),
    distance varchar(50));
    insert into travel (source, destination, distance)
    values
    ('Mumbai', 'Bangalore', 500),
    ('Bangalore','Mumbai', 500),
    ('Delhi', 'Mathura', 150),
    ( 'Mathura','Delhi', 150),
    ('Nagpur', 'Pune', 500),
    ( 'Pune','Nagpur', 500);

    • @RishabhMishraOfficial
      @RishabhMishraOfficial  Год назад

      thanks for sharing :)

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

      thnx alot man....how these greatest is working and giving output i got confused in that...u saved me

  • @rahul1994negi
    @rahul1994negi Год назад

    using cte :
    with cte1 as
    (select source, row_number() over(order by (select null)) as rn_s
    from travel
    )
    select t1.source, t1.destination, t1.distance from travel as t1
    join
    (
    select c1.source as source, c2.source as destination from cte1 c1
    join cte1 c2
    where c1.source c2.source and c1.rn_s < c2.rn_s
    ) as t2
    on t1.source = t2.source and t1.destination = t2.destination ;

  • @shraddhadhakad1154
    @shraddhadhakad1154 Год назад +1

    Using greatest and least method
    select greatest(source,destination),least(source,destination),max(distance)
    from travel
    group by greatest(source,destination),least(source,destination);

  • @user-vm3qu6ne9i
    @user-vm3qu6ne9i 2 месяца назад

    Hello, could you please help on min vs least ,while doing least are we need partion on table ?

  • @spokenenglishworld1650
    @spokenenglishworld1650 9 месяцев назад

    Sir, Could u pls tell me in Sub query what is the use of NOT EXISTS Function.....what will this not exists function suppose to do here?pls help

  • @1997somnathsaha
    @1997somnathsaha 11 месяцев назад

    I have a query, there are multiple data sciences projects available in RUclips can you please tell me if I can inspire by those projects and make similar projects for my project portfolio, please reply me so that I can start building my project portfolio

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

    please comeup with more interview questions (sql) that would be helpful. Thank you

    • @RishabhMishraOfficial
      @RishabhMishraOfficial  9 месяцев назад

      Already uploaded 4 videos on sql interview questions and answers

  • @user-ew2nw1my7r
    @user-ew2nw1my7r 2 месяца назад +1

    Good work bro keep it up.
    My simple approach
    Select * from (Select *,ROW_NUMBER()over( order by distance) as number from travel) n
    where n.number % 2=1

  • @JayantSorteBCS
    @JayantSorteBCS Месяц назад +1

    why not just use select statement
    SELECT * FROM travel WHERE source > destination

  • @Itstajnoor1
    @Itstajnoor1 Год назад +1

    Why u use not exist in subquery method?

  • @NiteshKumar-ys1kq
    @NiteshKumar-ys1kq Год назад +5

    Bhaiya direct bhi ho skta h na ye to
    Select * from travel Where source>deatination;
    Aise

    • @tanishqtomar9786
      @tanishqtomar9786 15 дней назад

      usme last wala source thdi aayega kyuki p>n hta h na shyd

  • @anuradhavaradha2257
    @anuradhavaradha2257 Год назад

    Do you have in English version?

  • @paragsharma1764
    @paragsharma1764 Год назад

    Sir, Is there any chance of connecting with you over a call?

  • @Xavier-jq4ex
    @Xavier-jq4ex 8 месяцев назад

    platform to practise advance sql??

  • @SayedReyaz
    @SayedReyaz Год назад +8

    I am sorry but I must say beginners will find it hard to understand your Queries
    In your 3rd method beginners will gona ask why is there NOT EXISTS clause.
    Even after watching your whole SQL playlist 😅

  • @kumarlalit1840
    @kumarlalit1840 Год назад

    Can't understand why the max function is used with distance? We are not using distance in the group by then why is it throwing in an error to add an aggregate function. Is it necessary to add all the called variables while using the group by on any one of them?🤔 Also, why the *where not exists* is added in third method? ThanQ in advance. @rishabh mishra gr8 going...🖖

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

      When we use GROUP BY, it's a must to use an aggregate function. That's why MAX is used here.

  • @Amann7040
    @Amann7040 Год назад +4

    Hi, sir aap data ka file post kr diya kijiye so practice kr sake hmlog

    • @RishabhMishraOfficial
      @RishabhMishraOfficial  Год назад +3

      Hi, it's a simple data set with 3 columns. You can prepare on your own 😄

    • @Amann7040
      @Amann7040 Год назад +1

      @@RishabhMishraOfficial ok sir

  • @DharmendraKumar-DS
    @DharmendraKumar-DS Год назад +2

    first method was the easiest...other 2 I did not understand😅

  • @putulsaini6788
    @putulsaini6788 10 месяцев назад +1

    Sir I am using 1st and 2nd method in Microsoft SQL server and it is showing error. In 2nd method it is saying 'the row_number must have an over clause with order by'. And in 1st method it is showing ' greatest ' is not a recognised built-in function name. Please help me

    • @shubhamswami7275
      @shubhamswami7275 8 дней назад +1

      He is using postgre SQL, in MS SQL it doesn't support least and greatest commands.
      And secondly, u need to specify an order by clause in MS SQL
      Use order by source, destination it will work.

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

    why not just SELECT * FROM travel WHERE source > destination, also only exists keyword in place of not exists can be used...

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

    with Travel as (select * ,row_number() over() as SrNo from Table_Name)
    select * from Travel where SrNo%2=0
    another Easiest Solution

  • @vishnudatt6000
    @vishnudatt6000 Год назад

    select * from travel where source>destination;
    is this query right or wrong it show the same result can anyone please explain this query how this is working ????

    • @SyedHaidy
      @SyedHaidy 10 месяцев назад

      It's nice, I also came up with it but it won't work if we change the question a little by adding a single entry with no reversed entry, Method 2 will also fail in that situation.

  • @Shubhangisingh55
    @Shubhangisingh55 Год назад +1

    Sir, u have given 3 method bt we can simply write it as follows -
    Select * from travel
    Where source IN ( Mumbai, mathura, nagpur);
    Plz tell me if it is correct?

    • @gauravsingh-gn4zz
      @gauravsingh-gn4zz 11 месяцев назад

      What if there is n no. Of city. And you don't know the names .. will you pass n no. Of city.

  • @arpitgupta520
    @arpitgupta520 Год назад +1

    sirji third method mein agar dono city same alphabet se start hogi to appka favorite method fail kr jayega (iss table mein it worked) lol!

    • @RishabhMishraOfficial
      @RishabhMishraOfficial  Год назад +1

      Yess issiliye 3 methods share kiya hai.. and already mentioned in the video that 3rd method is specifically gor this problem. Watch video properly 🤦‍♂️

    • @SyedHaidy
      @SyedHaidy 10 месяцев назад

      Comparison operator doesn't just compare first letter. It works by comparing lexicographically.
      For example:
      Mumbai < Mvmbai
      In this case, first letter is equal but in the second letter, "u" comes before "v" so u < v and hence the result Mumbai < Mvmbai.

  • @Subh32
    @Subh32 Год назад +5

    Being from non IT background, If i learn SQL and other required things how i get job ???

    • @Mr.tejaspatil96k
      @Mr.tejaspatil96k Месяц назад +3

      Are bhai jo hain unhe nhi lag rahi tum kaha bhid badha rahe ho😂

    • @whyme_141
      @whyme_141 Месяц назад +1

      U can get, ignore these Auswholes, learn basics from RUclips and than Start getting certification from Microsoft or Google. If u get these Certificates, u can easily switch

  • @irfanansari3811
    @irfanansari3811 Год назад +1

    sir group by distance kr denge to ho jaega

  • @DharmendraKumar-DS
    @DharmendraKumar-DS Год назад

    I solved it this way:
    SELECT * FROM travel
    WHERE source IN ('Mumbai','Mathura','Nagpur')
    is it right ??...coz it is giving the same results

    • @govindshah6037
      @govindshah6037 Год назад +1

      This is not correct because if the data is huge then how you gonna get this list ('Mumbai','Mathura','Nagpur')...?

    • @SayedReyaz
      @SayedReyaz Год назад +1

      Hardcoding Values which is Updateble or replacable in any way is not recommended in any programming language.
      Always Remember

  • @shubhamvarude8898
    @shubhamvarude8898 Год назад +1

    table download link description mai nhii diya sir?

  • @user-rh1hr5cc1r
    @user-rh1hr5cc1r 9 месяцев назад

    using sub query ,, inside query is giving the same results, then why we need to use outer not exists

  • @SrikantPal-jl1he
    @SrikantPal-jl1he Год назад +1

    SELECT source,destination,distance FROM travel WHERE source LIKE 'M%' OR source LIKE 'N%';

    • @eeshangautam
      @eeshangautam 9 месяцев назад

      But it will fail as soon as the use case is changed.

  • @Hellyeah22916
    @Hellyeah22916 Год назад

    I don't understand the leaste and greatest concept of text not number only text how it work?

    • @SyedHaidy
      @SyedHaidy 10 месяцев назад +1

      It compares a string lexicographically.
      For example:
      "Mumbai < Mvmbai" returns true.
      In this case, first letter is equal but in the second letter, "u" comes before "v" so u < v and hence the result Mumbai < Mvmbai.
      Now use this concept in GREATEST and LEAST.
      If we find GREATEST(Mumbai, Mvmbai), it will give us "Mvmbai".

  • @sayalidige3500
    @sayalidige3500 10 месяцев назад

    Hi sir, i am not getting the exact output

  • @govindshah6037
    @govindshah6037 Год назад +1

    Method 2 Will not work
    If there is duplicate value in either source or destination please have a thought on that
    e.g You can add a new record like ('Mumbai', 'Goa', 700) & ('Goa', 'Mumbai', 700) then try this method-2 and it will not work

    • @arupchandra8602
      @arupchandra8602 Год назад

      Then another condition need to add
      t1.Source = t2.destination AND
      t1.destination = t2.source AND
      t1.source > t2.source

    • @SyedHaidy
      @SyedHaidy 10 месяцев назад

      It will still work, because we are checking both Source and Destination by that AND statement.
      t1.source = t2.destination
      AND
      t1.destination = t2.source
      --------------------------------------------------------
      The situation where Method 2 will fail is if we add a single entry with no reversed entry.

  • @UECSayandeepBanik-et5ev
    @UECSayandeepBanik-et5ev Год назад

    i found out the easiest one
    select source, destination, distance FROM travel
    WHERE length(source) between 6 and 7

    • @superrv0023
      @superrv0023 5 месяцев назад

      this method is not dynamic. If a new value comes up then the code needs to be changed

  • @bricknkeyproperties1464
    @bricknkeyproperties1464 Год назад +3

    code created below.
    create table Travel
    (
    source varchar(20),
    destination varchar(20),
    distance int8
    );
    insert into travel
    (source,destination,distance)
    values
    ('Mumbai','Bnagalore',500),
    ('bangalore','Mumbai',500),
    ('delhi','mathura',150),
    ('mathura','delhi',150),
    ('nagpur','pune',500),
    ('pune','nagpur',500);

    • @kumarlalit1840
      @kumarlalit1840 Год назад

      ThanQ use this insert into
      create table Travel
      (
      source varchar(20),
      destination varchar(20),
      distance int8
      );
      insert into travel
      (source,destination,distance)
      values
      ('mumbai','bangalore',500),
      ('bangalore','mumbai',500),
      ('delhi','mathura',150),
      ('mathura','delhi',150),
      ('nagpur','pune',500),
      ('pune','nagpur',500);
      I hope this helps 😊

  • @abhigyapranshu4791
    @abhigyapranshu4791 7 месяцев назад

    In MySQL, it is working even without Group By Clause using Greatest and Least functions. Any idea how its working

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

    Explanation should be improved...

  • @RS_02_
    @RS_02_ Год назад

    I m failed to do also understand

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

    I have found a normal easy way where we can just filter with distance.
    SELECT C1.SOURCEID, C1.SOURCE, C1.DESTINATION , C1.DISTANCE
    FROM CITY C1
    INNER JOIN CITY C2 ON C1.DISTANCE = C2.DISTANCE
    WHERE C1.SOURCEID < C2.SOURCEID ;