R
R
RoboCat2015-12-26 14:24:59
MySQL
RoboCat, 2015-12-26 14:24:59

mysql: how to select top5 from groups?

There is a table:
id,name,otdel,money
-----------------
1,alex,it,1000
2,anna,callcenter,2000
3,anton,it,3000
etc.
You need to choose from each department the top 5 employees with the highest salary.
For one group, I can make a query select * from users group by otdel ordeer by money asc limit 3 or something like that ..
Please write a query with an explanation, otherwise I met solutions to such problems on the Internet, but I couldn’t comprehend it ..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
romy4, 2015-12-26
@romy4

Well, you have a ready request

R
RoboCat, 2015-12-26
@RoboCat

I need to select the top 5 employees with the highest salary from all departments with one request.

R
res2001, 2015-12-26
@res2001

Something like this, apparently:
select u.* from users as u
where u.id in (select top 5 id from users as u1 where u1.otdel=u.otdel order by u1.money desc)
order by u.otdel, u.money desc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question