How to get 2nd greatest record in SQL - AI HitsToday

How to get 2nd greatest record in SQL

Home Forums SQL Discussion How to get 2nd greatest record in SQL

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #290
    Team AHT
    Keymaster
      1. 1.SELECT MAX(column_name) AS second_greatest
        FROM table_name
        WHERE column_name < (SELECT MAX(column_name) FROM table_name);

      Replace column_name with the column you want to find the second greatest value for, and table_name with your table’s name.

      1. Using Window Function:
      SELECT DISTINCT column_name
      FROM (
      SELECT column_name, ROW_NUMBER() OVER (ORDER BY column_name DESC) AS row_num
      FROM table_name
      ) AS sub
      WHERE row_num = 2;
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.