I have some questions regarding Max()
What does the following query mean?
SELECT MAX(X) -1 FROM T
I learned that the syntax should be:
SELECT (MAX(X) -1) as max_minus_one FROM T
no?
Do aggregation function (i.e. Max()) must be followed by GRUOP BY ?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With