How to get ONLY the name of second (or nth) highest salaried person?
This is the query I have tried but this gives me only the name of highest salary paid:
SELECT emp_name FROM emp ORDER BY salary DESC LIMIT 1;
Not sure if that the best solution, but here is an example how you could do it:
SELECT * FROM (
SELECT customerName, length(customerName), #rownum := #rownum + 1 AS rank
FROM zenyatech.customer, (SELECT #rownum := 0) r
ORDER BY length(customerName)
) X WHERE rank = 2
You create a rank column first, and then use a query around that query and get only rank = 2 or N.
(The example is a little different, you would need to apply that to your table/database scenario)
This might help
SELECT name FROM employees ORDER BY salary DESC LIMIT 1,1
Related
I have a scenario where I need to obtain a set of results in ascending order but only given the last ID in the descending sequence.
The order will always be sequentially ascending as the ID is auto generated however, I cannot guarantee that ID sequence is unbroken. i.e. I cannot say 8 - 3 = 5 therefore do a forward lookup from 5.
What is the most efficient was of achieving this. If possibly I want to avoid manipulating the collection in the application logic but can as a last resort.
You could do with an inner select:
SELECT *
FROM (
SELECT *
FROM your_table
WHERE id <= X
ORDER BY id DESC
LIMIT Y
) d
ORDER BY id ASC
You can define your own rownum with query and apply mod to get every 3rd record regardless of it's id, e.g.:
SELECT *
FROM (SELECT id, name, #rownum := #rownum + 1 as `num`
FROM test, (SELECT #rownum := 0) a ) b
WHERE b.num % 3 = 0;
Here's the SQL Fiddle.
I know this query but it is sub query can anyone explain me to confused i asked this quation in interview but i can't explain proper
select * from dept e where 2 =(select count(distinct salary) from dept where e.salary<=salary)
Try this its very simple:
SELECT id, dept_name, DISTINCT salary FROM dept_table DESC salary LIMIT 2 OFFSET 2
You can use SQL below to get data.
select *
from dept
where salary < max(salary)
order by salary desc
limit 2;
You can do it without limit using a temporary count column.
SELECT
(#cnt := #cnt + 1) AS rowNumber, *
FROM dept AS t
CROSS JOIN (SELECT #cnt := 0) AS dummy
ORDER BY t.salary DESC ;
you can refer to this example
You can order by salary and get only top 3 items which would represent highest, second highest and third highest salary like
select *
from dept
order by salary desc
limit 3;
You can use sub query as well as using Top keyword.
Using Top
SELECT TOP 2 salary
FROM
(SELECT TOP 3 salary
FROM Table_Name
ORDER BY salary DESC) AS a
ORDER BY salary ASC
Here is my query:
SET #rank=0;
SELECT #rank := #rank +1 AS rank_id, name, SUM(points) AS points
FROM battle_points
WHERE category = 'test'
AND user_id !=0
GROUP BY user_id
ORDER BY points DESC;
I'd like to add a column rank based on the total points. With this query, the points are fine but the rank_id virtual column doesn't match up.
For example, the top user with the most points has rank 26, yet the rank_id column has a value of 24.
How do I matchup the rank_id column with the points column?
Note: while I am fully versed in PHP, I need a solution for MySQL only.
You are on the right path, but you need to put the main query in a subquery so that the ordering occurs before the rank calculation, like so:
SET #rank=0;
SELECT #rank := #rank +1 AS rank_id, mainQ.*
FROM (
SELECT name, SUM(points) AS points
FROM battle_points
WHERE category = 'test'
AND user_id !=0
GROUP BY user_id
ORDER BY points DESC
) AS mainQ
;
Edit: Qualified * to mainQ.*.
I have to create a column at run time (RANK of salary ) ,which depends on the value of a salary column from one table(COLLAGE ) and this salary is associated with an employee table. Can you suggest how to generate it . The RANK column will contain the value based on salary i.e if the salary is highest than RANK is 1 ... in ascending order.
This would get you id of your employee, their salary and a rank column.
SELECT
*,
#currentRank := #currentRank + 1 AS rank_of_salary
FROM (
SELECT
c.employee_id,
e.salary
FROM
collage c
INNER JOIN employee e ON c.employee_id = e.employee_id
) t, (SELECT #currentRank := 0) r
ORDER BY salary
(SELECT #currentRank := 0) initializes a variable so that you do not need separate SET statement.
For each row #currentRank variable is being increased and stored in rank_of_salary column. It's actually more a row_number equivalent I believe. Proper ordering of this rank is maintained by sorting the output with ORDER BY salary clause.
having a mysql table with multiple records belonging many different users like this:
id score
1 , 50
1 , 75
1 , 40
1, 20
2 , 85
2 , 60
2 , 20
i need to get the rank of each id but after finding the sum of their score;
the rank should be the same if the total score for each player is the same.
this gives me the total for each player:
select id,sum(score) as total from table_scores group by id order by total desc;
is it posssible to find the sum like above and use it to rank the players in one query?
Something big missing from the accepted answer. The rank needs to be bumped after a tie. If you've got 2 tied for 3rd place, there is no 4th place.
The following query is an adjustment of the accepted SQL to account for this and reset the rank variable (#r in the query) to match the row value. You can avoid the extra addition in the CASE/WHEN but initializing #row to 1 instead of 0 but then the row value is off by 1 and my OCD won't let that stand even if row number is not valuable.
select
id, total,
CASE WHEN #l=total THEN #r ELSE #r:=#row + 1 END as rank,
#l:=total,
#row:=#row + 1
FROM (
select
id, sum(score) as total
from
table_scores
group by
id
order by
total desc
) totals, (SELECT #r:=0, #row:=0, #l:=NULL) rank;
You can rank rows using variables:
select
id, total,
CASE WHEN #l=total THEN #r ELSE #r:=#r+1 END as rank,
#l:=total
FROM (
select
id, sum(score) as total
from
table_scores
group by
id
order by
total desc
) totals, (SELECT #r:=0, #l:=NULL) rank;
Please see it working here.
i find one more way to this problem... This one is based on JOIN clause
SET #rank = 0;
SELECT t1.id, t1.score, t2.rank
FROM (SELECT id, SUM(score) as score
FROM table_scores GROUP BY id ORDER BY score Desc) AS t1
INNER JOIN
(SELECT x.score, #rank:=#rank + 1 as rank FROM
(SELECT DISTINCT(SUM(score)) AS score
FROM table_scores
GROUP BY id ORDER BY score DESC) AS x) AS t2
ON t1.score = t2.score
Here is SQL Fiddle: http://sqlfiddle.com/#!9/2dcfc/16
P.S. it's interesting to see there is more then one way to solve a problem...