SQL Query | How to find Maximum of multiple columns | Values

Поделиться
HTML-код
  • Опубликовано: 16 окт 2024

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

  • @LearnatKnowstar
    @LearnatKnowstar  2 года назад +2

    Register at the below link to get US $100 off for Coursera Plus membership between Sep 8 - Sep 29, 2022.
    The Coursera Plus membership gets you access to unlimited courses and unlimited certifications!
    imp.i384100.net/Ke51on

  • @xerosdestroyer
    @xerosdestroyer 2 года назад +42

    I've been doing SQL professionally for more than 10 years and didn't realize that was a valid syntax. Thanks for the great video.

    • @marvinalone
      @marvinalone 2 года назад +1

      neither did I, this actually is a sql standard that sqlserver, db2, postgresql all supports, mariadb supports it since 10.3 and mysql supports it since 8.0.19 with slight different syntax

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад +1

      Thank you so much.

    • @avinash8535
      @avinash8535 2 года назад +1

      @@LearnatKnowstar hi.. Could you please make a video on performance tuning on 10M+ rows of data.. Also i need a suggestion on comparing two tables columns data with same data with 500+ columns.. I need to know easily which columns data were changed and which columns data were not changed... Could you please suggest me your experience on these points..
      Thanks in advance

    • @mohamedelyamani7845
      @mohamedelyamani7845 2 года назад

      +1 😅

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

    Excellent! You're a lifesaver and thanks for explaining the codes. I saw these codes online but just couldn't wrap my head around it. Thanks again!

  • @ouramazingnature
    @ouramazingnature 2 года назад +1

    Hi there, awesome video, but I feel that this method is going to be difficult to apply for databases with high column number count because we have to write every column name inside the value statment. Do you have another way to do the same thing?. Thanks

  • @jagadesh4220
    @jagadesh4220 2 года назад +5

    Greatest function will also work I guess to pickup max value for multiple columns.. Correct me if I'm wrong.... Topic is super 👍👍👍

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад +1

      Yes, it should work for certain databases. Thanks for mentioning it 👍

  • @landchennai8549
    @landchennai8549 14 дней назад

    declare @Table table ( Id int,a int,b int, c int)
    insert into @Table values (1,10,20,30),(2,10,20,30)

    select *, case when a > b and a > c then a
    when b > a and b > c then b
    when c > a and c > b then c end MaxValue
    , case when a < b and a < c then a
    when b < a and b < c then b
    when c < a and c < b then c end SmallValue
    from @Table

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

    can you please provide the create & insert statement for this. Thanks for such a wonderful content.

  • @Helena-fp5rt
    @Helena-fp5rt 2 года назад +1

    Thank you very much. Great , I enjoyed! I want more such videos.

  • @HuyVo-uz6wj
    @HuyVo-uz6wj 2 года назад +1

    Thanks for your video.
    From Oracle SQL we can using bellow syntax:
    select Category, GREATEST(nvl(2015,0),nvl(2016,0),nvl(2017,0),nvl(2018,0),nvl(2019,0), nvl(2020,0)) as MAX_SALE
    from sales;

    • @subhanivasareddythummapudi3836
      @subhanivasareddythummapudi3836 2 года назад

      In this example, how can i get Max value along with the corresponding Year.

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

      @@subhanivasareddythummapudi3836 you’ll need to add Year column in the select statement.

  • @aal2002
    @aal2002 2 года назад

    This is a GREAT RUclips Channel! Thank you!

  • @sivachaitanya6330
    @sivachaitanya6330 2 года назад +1

    Is the queary same for mysql also.?

  • @samcaseybolade5368
    @samcaseybolade5368 2 года назад

    Thanks for this. If anyone else if looking at this, 4:48sec worked for me

  • @sudhakarv5134
    @sudhakarv5134 3 года назад +1

    Can you please do video on how get different highest salaries for different departments like for Department A need 2nd highest salary and for Department B need 5th highest Salary

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

    How do I get the column name for the max value selected? i am running into issue where i want to select the column header of the max value in a row. any help is appreciated

  • @TheChatGPTEngineer
    @TheChatGPTEngineer 2 года назад

    How do we select the column as well? Which shows which is having highest sales ?

  • @sanc7753
    @sanc7753 3 года назад +1

    Keep it up..thanks...hopefully more to come...

  • @rahulbansal3811
    @rahulbansal3811 2 года назад

    We can also use pivoting on it

  • @SureshKumar-lp2sb
    @SureshKumar-lp2sb 2 года назад

    Just now, I have fortunately seen this video and subscribed this channel. Awesome explanation 👍

  • @trader_adda
    @trader_adda 2 года назад +1

    Hi how i can practice these query , i am writing this query in sqllive SELECT * FROM (VALUES (1) ,(2) ,(3)) as TB1(A); grtting error invalid table

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад

      You can practice it by installing sql server on your local machine. The below tutorial can walk you through the steps
      ruclips.net/video/ncj0EDzy_rw/видео.html

  • @ranajparida5412
    @ranajparida5412 2 года назад

    Can you write for same scenario in oracle sql

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

    How to get the column header name of each row maximum value in a separate column?

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

    Great job

  • @Reellron
    @Reellron 2 года назад +11

    If you're using SQL Server, you could use unpivot instead. It's more pleasant to type and probably more performant for larger data tables. Taking a similar approach in other SQL programs isn't pleasant to type, but I'd imagine the performance scaling is the same.
    SELECT Category, MAX(unpvt.Sales) as MaxSales
    FROM dbo.Sales
    UNPIVOT (
    Sales FOR Years IN ([2016], [2017] [2018], [2019], [2020])
    ) unpvt
    GROUP BY Category

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

    Very nice - too bad Values is not supported in Azure Synapse - had to use Un pivot to achieve it

  • @jacksparrowcapitan3232
    @jacksparrowcapitan3232 3 года назад

    Great info as always!!!!

  • @bagusikhsanbagaskara844
    @bagusikhsanbagaskara844 2 года назад

    Wow awesome knowledge.. Thanks for sharing

  • @j0e721
    @j0e721 2 года назад +1

    does this work in sqlite?

  • @venkatakoti6714
    @venkatakoti6714 2 года назад

    How can we achieve with case?

  • @uttamx2016
    @uttamx2016 2 года назад

    Can you tell me how the query executes ? I mean executions steps in few lines .
    As my understanding 2015, 2016, 2017 , 2018, 2019 , 2020 are in Row and later with inner query it will be in one column and then ....

    • @rahulbansal3811
      @rahulbansal3811 2 года назад

      I think Uttam it will use pivot internally

    • @uttamx2016
      @uttamx2016 2 года назад

      Rahul I tried to understand this practically by running this case in sql server but unable to understand 100% . I will try later

  • @vaibhavkhairnar7313
    @vaibhavkhairnar7313 2 года назад

    Great video keep posting more

  • @abhisheksurkar
    @abhisheksurkar 3 года назад +1

    Awesome 👍

  • @PrakashPatil-dw5be
    @PrakashPatil-dw5be 2 года назад

    Wow nice superb...

  • @jefffuhr2393
    @jefffuhr2393 2 года назад

    *GENIUS!!!* 😂😂

  • @subhanivasareddythummapudi3836
    @subhanivasareddythummapudi3836 2 года назад

    In this example, how can i get Max value along with the corresponding Year.

  • @landchennai8549
    @landchennai8549 14 дней назад

    declare @Table table ( Category varchar(10),a int,b int, c int)
    insert into @Table values ('A',10,20,30),('B',60,70,80)
    select Category
    , min(Value) as SmallValue
    , max(Value) as MaxValue
    from
    (
    Select *
    from @Table A
    unpivot( value for xyz in ([a],[b],[c])) P
    ) v
    group by Category

  • @tiagosilva856
    @tiagosilva856 2 года назад

    Select empID, firstname, last name, (Select Max(Salary) from (Values ([2019]),([2020]), ([2021)) as People(Salary) as MaxSalaryperYear from HR

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

    Or you do Unpivot and select max

  • @Mustaq15
    @Mustaq15 2 года назад

    Query: names are Ajay Vijay Sunil having 6 subjects in separate columns display the percentage of students who scored 60% in above in overall marks

  • @timur2887
    @timur2887 2 года назад +1

    If you are faced to solve such weird tasks than maybe your data model is not properly developed. Storing rows in columns is not the best way.

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

    MYSQL QUERY for this excersis ........................................>
    select Name,greatest(First,Second,Third) AS MAXValueOfThreeColumns from MaxOfThreeColumnsDemo;

  • @biswajitbeheraCrAzYLoVeR
    @biswajitbeheraCrAzYLoVeR 2 года назад

    This could help someone if working with Oracle DB
    create table order_tab
    (
    v_dept varchar2(30),
    year_2018 number,
    year_2019 number,
    year_2020 number,
    year_2021 number,
    year_2022 number
    );
    insert into order_tab values('A',1,2,3,4,5);
    insert into order_tab values('B',10,20,30,40,50);
    insert into order_tab values('C',10,25,30,45,50);
    insert into order_tab values('D',15,20,35,40,55);
    insert into order_tab values('E',10,20,35,45,55);
    --select * from order_tab;
    select y.* from(
    with demo as ( select 1 as year_2018, 2 as year_2019, 3 as year_2020, 4 as year_2021,5 as year_2022 from dual )
    select * from order_tab
    unpivot(year_values for year_col in (year_2018, year_2019, year_2020, year_2021,year_2022))
    where year_values = (select max(year_values) from order_tab unpivot(year_values for year_col in (year_2018, year_2019, year_2020, year_2021,year_2022)))
    )x join order_tab y on(x.v_dept = y.v_dept);

  • @spookyaction
    @spookyaction 2 года назад

    1:10 it starts here

  • @landchennai8549
    @landchennai8549 14 дней назад

    select GREATEST(1,5,18)
    select LEAST(1,5,18)

  • @kunalr_ai
    @kunalr_ai 2 года назад

    RUclips is full of tutorial

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

    Didn't understand 😢

  • @markippo
    @markippo 2 года назад

    Microsoft at the pick of its laziness... Despite many requests, they didn't implement "Greatest" or "least" functions, despite they're available in many open source sql versions... But, hold a sec, actually they did... But only in Azure version... Shame on you Microsoft! BTW. Did not see the movie, but I'm sure you've presented very user friendly and straightforward approach with cross apply... The second thing, you simply cannot implement this by yourself because MS for years didn't introduce functions with optional parameters or package of functions. Giving only the "default" (not optional, you need to specify) or horribly bad sql_variant (no implicit conversion). Ohh shame on you Microsoft...

  • @nikolaybaranov2213
    @nikolaybaranov2213 2 года назад

    This is a classic case for use unpivot. Wrapping the technical part of the query as a CTE and separating the business logic makes the code more readable.
    WITH UnPvtSales AS
    (
    SELECT Category, Yr, Sale
    FROM (
    SELECT Category
    ,[2015]
    ,[2016]
    ,[2017]
    ,[2018]
    ,[2019]
    ,[2020]
    FROM dbo.Sales) P
    UNPIVOT
    (Sale FOR Yr IN ([2015],[2016],[2017],[2018],[2019],[2020])
    )AS unpvt
    )
    SELECT Category,
    Max (Sale) AS MaxSales
    FROM UnPvtSales
    GROUP BY Category