karim karim - 14 days ago 5
SQL Question

sql min function and other column

i want to know if it is possible to add another column to a select statement that contain an aggregate function like min, max ...

example :

SELECT user_id, MAX(salary) FROM users;


is this statement correct in the sql standard(in mysql its work );
its work in mysql, but i think i read somewhere that if i put an aggregate function in the select clause, i can't put anything but an aggregate function or if there is a group by, the grouped column can be in the select clause (in mysql)




EDIT :

User(user_id, name, last_name, salary)


i want to select the
user_id, name, (maximum salary column)
from the
User
table; is it possible to do it without sub query?

User Table

User_id, Name, Salary

| 1 | user1 | last1 | 500 | |
|---|-------|-------|------|---|
| 2 | user2 | last2 | 1000 | |
| 3 | user3 | last3 | 750 | |
| | | | | |


the output must be the
user_id, username, lastname, and salary of the user who have the max salary
, so here the ouput must be :

2 user2 last2 1000




Answer

Told you there are different solutions depending on what you want....

no group by, no subquery, Easy cake

select * 
from users
ORDER BY salary DESC
LIMIT 1