Skip to content
Advertisement

Finding the person with highest salary on mysql sample database

I have this mysql sample database (https://dev.mysql.com/doc/employee/en/sakila-structure.html).

The question is i need to find top10 persons with highest salaries on the database. So far ive tried next queries

select employees.emp_no, employees.last_name, employees.first_name, salaries.salary from employees join salaries on employees.emp_no = salaries.emp_no order by salary limit 10;

and:

 select max(salary), employees.last_name, employees.first_name from salaries join employees on salaries.emp_no = employees.emp_no;

This one gave me an error

ERROR 1140 (42000): In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'employees.employees.last_name';

Somewhy i think i need to include this

select max(salary) from salaries;

But i just cant figure how.

What am i missing there?

Advertisement

Answer

Your first query is basically correct but you need a descending sort:

order by salary desc

This puts the biggest values first.

Note that I would recommend uses table aliases for the query:

select e.emp_no, e.last_name, e.first_name, s.salary
from employees e join
     salaries s
     on e.emp_no = s.emp_no
order by s.salary desc
limit 10;

Table aliases make it easier to both write and read the query.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement