Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Can I use MAX() - math SQL function in WHERE clause ?
I searched msdn forum but did not get any working query which uses MAX() function in WHERE clause.
Posted

You can't use it directly in a query because it is an aggregate function, meaning it does an operation on a set of data. So that is max(age) in the context of the sql below?
select * from person where age = max(age)


You probably mean the max(age) every person, but you must be specific on that. So what you can do is:
select * from person where age = (select max(age) from person)

Hopefully the above helps.

Good luck!
 
Share this answer
 
v2
Comments
Swap9 21-Nov-13 10:19am    
Thanks for responding - E.F. Nijboer.
Yes when i executed the first query it gave me this error.

"Msg 147, Level 15, State 1, Line 2
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."

where as the second worked fine.
To specify condition for a group or an aggregate, instead WHERE statement, use HAVING[^]. There you'll find an example.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900