Thursday, November 21, 2019

MIN/MAX vs LEAST/GREATEST

Today, I learned about the SQL functions LEAST() and GREATEST().

Unlike MAX(), GREATEST() takes multiple arguments:


While both functions return the largest number in a group, MAX() can only have one argument (GREATEST() needs to have more than one):



MIN() vs. LEAST() works similarly:

More testing and research will have to be done to figure out why GREATEST() and LEAST() return a list of unordered values when you give it a column name:



I'll edit with my findings! Feel free to comment if you know why this happens!

6 comments:

  1. You learn something new every day! Thanks for this awesome post.

    ReplyDelete
  2. Nice! Now I don't have to do the research.

    ReplyDelete
  3. You're curious why "GREATEST() and LEAST() return a list of unordered values when you give it a column name". It's for the same reason that SQRT(seconds) would return a list of unordered second values; sql is applying that function to each value in that column. Greatest(x) = x, so it just spits back the raw data. It's unordered because all queries return unordered data unless you specifically ORDER it.

    ReplyDelete