I have the database in MySQL like this
id employee_id salary
1 1 10000
2 2 20000
3 3 10000
4 4 40000
5 5 30000
I want to select the maximum two salary by using LIMIT.So how to select that?
Just sort your results in descending order of salary and limit the resultset as desired:
SELECT * FROM mytable ORDER BY salary DESC LIMIT 2
See it on sqlfiddle.
SELECT
employee_id,
salary
From
employee
order by salary desc
limit 2 ;
Related
How to select name with the highest power and highest salary
Id
Name
Salary
1
Kobe
50000
2
Lebron
500099
3
Steph
628228
4
Thompson
50505
5
Shabu
393828
The CTE represents your table. The query you need is the last 3 lines, where it's sorting the output by salary in descending order, then limiting to 1 row.
with salaries as (
select 1 as id, 'Kobe' as name, 50000 as salary union all
select 2, 'Lebron', 1000000 union all
select 3, 'Steph', 222222
)
select name
from salaries
order by salary desc limit 1
Output:
Lebron
UPDATE
Per your comment about using a Max function, here's one way, using a sub-query:
select name
from salaries
where salary = (select max(salary) from salaries);
You can use a sub-query to to so
SELECT
t.name
FROM <your_table_name> t
WHERE t.id =
(SELECT
t1.id
FROM
<your_table_name> t1
ORDER BY t1.salary
DESC LIMIT 1);
EDIT
Or you can just use
SELECT
t1.name
FROM
<your_table_name> t1
ORDER BY t1.salary
DESC LIMIT 1
Expanding on 53RT's comment, for MySQL:
SELECT Name
FROM myTable
ORDER BY Salary DESC
LIMIT 1
Suppose i have following table
id Salary
1 5
2 3
1 3
1 6
2 5
3 5
3 2
4 1
4 3
2 9
I want to get the id of highest total(sum) salary from all groups. In this case the result should be id=2 sum=17( i.e. 3+5+9 = 17)
If you really only expect/need a single id group, then using LIMIT is probably the most straightforward approach here:
SELECT id, SUM(Salary) AS total
FROM yourTable
GROUP BY id
ORDER BY SUM(Salary) DESC
LIMIT 1;
If there could be ties for the highest salary, then we would need to do more work. Before MySQL 8+, the query given by #MKhalid is what we would need to do. Starting with MySQL 8+, we can use the RANK analytic function:
SELECT id, total
FROM
(
SELECT id, SUM(Salary) AS total,
RANK() OVER (ORDER BY SUM(salary) DESC) rank
FROM yourTable
GROUP BY id
) t
WHERE rank = 1;
WITH CTEName AS
(SELECT id, SUM(salary) as total_salary from testTable
GROUP BY id )
select top 1 id from CTEName ORDER BY total_salary desc
i have a employee table with their name ,dept , salary as column .
I want to get the 3rd employee whose salary comes into 3rd heigest category
empl dept salary
sant x 3000
temb x 4000
porty z 4000
xati x 2000
tres t 3000
werbt z 2000
amiq t 3000
desired result :
amiq t 3000
what will be the query ?
Two alternatives:
SELECT * FROM TableName ORDER BY Salary DESC,empl LIMIT 2,1
Fiddle example here.
OR:
SELECT empl,dept,salary
FROM
(SELECT T.*,#rownum := #rownum + 1 AS rank
FROM TableName T,(SELECT #rownum := 0) as R
ORDER BY T.Salary DESC,empl) as T2
WHERE rank=3
Result:
EMPL DEPT SALARY
amiq t 3000
Explanation:
The query will select the records with a rank in descending order of salary. Advantage is that you can find the nth highest salary by replacing 3 by n (which ofcourse, ordered by empl).
See result in SQL Fiddle.
SELECT * FROM employee ORDER BY Salary DESC,empl LIMIT 2,1
Working Fiddle
You may try this
SELECT * FROM (SELECT * FROM table_name b ORDER BY empl) b
ORDER BY salary desc LIMIT 2,1
Simple and sort Query
SELECT * FROM
(
SELECT * FROM <Table Name> b ORDER BY empl desc
) b
ORDER BY salary desc
LIMIT 2,1
Result:-
empl dept salary
amiq t 3000
I am trying to get a MYSql statement to spit out the most common number in a field. I believe I am supposed to use COUNT(QUANTITY) but I am confused by which to GROUP BY and ORDER BY, I can't seem to get the correct MODE (Most common number).
*EDIT*
Here is a sample table:
QUANTITY | ORDER_NUMBER
1 51541
4 12351
5 11361
5 12356
6 12565
8 51424
10 51445
25 51485
The MYSql statement should spit out the number 5 because it appears most often
SELECT QUANTITY,COUNT(*)
FROM ...
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
SELECT ORDER_NUMBER AS ORDER, COUNT(QUANTITY) as numorders
FROM table
GROUP BY ORDER_NUMBER
ORDER BY numorders
to get the top 10 order_numbers do
select order_number, count(order_number) as quantity
from your_table
group by order_number
order by quantity desc
limit 10
SELECT QUANTITY, COUNT(QUANTITY) AS TOTAL_Q
FROM MYTABLE
GROUP BY QUANTITY
ORDER BY TOTAL_Q DESC
this will give you number of quanity from most to least number....
I have 3 columns with id,usrnameand salary. I want to find maximum salary of 5 records.
How will i write query in mysql ?
In MySQL you can use ORDER BY to sort the rows in descending order and use LIMIT to return only the top 5 rows:
SELECT id, usrname, salary
FROM yourtable
ORDER BY salary DESC
LIMIT 5
You have to use LIMIT, like so:
SELECT * FROM mytable ORDER BY salary DESC LIMIT 5