简体   繁体   中英

Get all employee salary from employee tables from 3rd highest salary in MYSQL

Have written below query:

mysql > select * from employees where Salary NOT IN (select Salary from employees limit 3);

Gives below error:

ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

There are 2 Problems in your Query. First is the Syntax and the second is that there is no ORDER BY in the LIMIT SELECT. If there is no ORDER the result can every time changed (Ramdom)

You can do it with a QUERY like this:

SELECT e.* FROM employees e WHERE e.salery NOT IN 
(SELECT * FROM (
    SELECT s.salery
    FROM employees s
    ORDER BY salery LIMIT 3) AS tmp)
ORDER BY e.salery ;

Here is my Sample:

MariaDB [bernd]> select * FROM employees;
+----+--------+--------+
| id | salery | name   |
+----+--------+--------+
|  1 |    100 | Bernd  |
|  2 |    500 | Peter  |
|  3 |    300 | Paul   |
|  4 |   1234 | Mary   |
|  5 |    800 | Erwin  |
|  6 |    777 | Hubert |
+----+--------+--------+
6 rows in set (0.00 sec)

MariaDB [bernd]>

MariaDB [bernd]> SELECT e.* FROM employees e WHERE e.salery NOT IN
    -> (SELECT * FROM (
    ->     SELECT s.salery
    ->     FROM employees s
    ->     ORDER BY salery LIMIT 3) AS tmp)
    -> ORDER BY e.salery ;
+----+--------+--------+
| id | salery | name   |
+----+--------+--------+
|  6 |    777 | Hubert |
|  5 |    800 | Erwin  |
|  4 |   1234 | Mary   |
+----+--------+--------+
3 rows in set (0.00 sec)

Something like:

select employees.*
from employees
where (
   select count(*) from employees as e
   where e.Salary >= employees.Salary
) > 3;

...but depending on the table size/indexes, it may be too heavy for the server. SQL Fiddle: http://sqlfiddle.com/#!9/67754e/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM