Max vs limit, which is more efficient

This may be a very simple question, but could not find the perfect answer. The query is to find the second highest salary that can be made with max and limit both.

Using MAX

 select max(salary) from table1 where 
 salary < (select max(salary) from table1);

Use of restriction

select salary from table1 where 
salary < (select max(salary) from table1)
order by salary desc
limit 1;

So which query will be better and less time given that it has 1000 entries.

Thanks in advance.

+3
source share
2 answers

None of the queries are suitable for this. You get MAX (salary) from records with a salary less than MAX (salary) for all records. In other words, the second largest salary. The way to do this is:

SELECT salary FROM table1 ORDER BY salary DESC LIMIT 1,1

If you really want max, just do

SELECT MAX(salary) FROM table1

. , , , , .

+5

, MAX .

, . , , .

: SQL-, .

+2

All Articles