Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql: what does Max() -1 mean?

Tags:

sql

t-sql

I have some questions regarding Max()

  1. What does the following query mean?

    SELECT MAX(X) -1 FROM T

  2. I learned that the syntax should be: SELECT (MAX(X) -1) as max_minus_one FROM T no?

  3. Do aggregation function (i.e. Max()) must be followed by GRUOP BY ?

like image 575
Elad Benda Avatar asked Jan 31 '26 12:01

Elad Benda


2 Answers

MAX(x) - 1 simply means the max value of x in the table minus one.

You can always use parenthesis and aliases (as some_cool_name) to make thing clearer, or to change names in the result. But the first syntax is perfectly valid.

You only need GROUP BY if you intend to select anything more that the aggregated value, like this for instance:

select
    userName,
    avg(age)
from
    users
group by
    userName
like image 145
Mikael Östberg Avatar answered Feb 03 '26 04:02

Mikael Östberg


SELECT MAX(X) -1 

and

SELECT (MAX(X) -1)

are the same.

In this case you do not need a GROUP BY since you are not selecting other non aggregated fields.

like image 29
Scott Bruns Avatar answered Feb 03 '26 04:02

Scott Bruns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!