Show rank with SQL - mysql

I want to get the "rank" of a specific Name in my database.
So if I type in Julia, I want to get #2. (Her rank/place in the database)
Name Points
Julia 1987
Marc 1479
Sophia 2517

select rank
from
(
select name, #rank := #rank + 1 as rank
from your_table
cross join (select #rank := 0) r
order by points desc
) tmp
where name = 'Julia'
The inner select orders the data and adds a rank column. The outer select gets the rank of the specific person.

Related

Add a Column in Query

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.*.

creating a column at runtime while runnin the sql query

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.

How to get ONLY the name of second highest salaried person

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

Getting a row number of a row which is associated with a specific group of rows

I have a table in MySQL database as shown in the following snap shot.
I need to obtain a current row number based on rating_id (primary key) given in each group of products.
Currently the following SQL obtains a row number based on rating_id from all rows in the table.
SELECT rownum
FROM (SELECT #rownum := #rownum + 1 AS rownum,
tbl.rating_id
FROM rating tbl,
(SELECT #rownum := 0)t
ORDER BY tbl.rating_id DESC)t
WHERE rating_id = ?
How to restrict this query to a specific group of products (prod_id)?
In other words, how to get a row number from a group of rows which is specific a particular product (prod_id)?
I guess you need to count each group of PROD_ID separately. In this case you should add one more user defined variable to store previous PROD_ID and reset #rownum when new PROD_ID group starts.
SELECT rownum,rating_id,prod_id
FROM (SELECT if(prod_id=#prev_prod,#rownum := #rownum + 1,#rownum:=1)
AS rownum,
tbl.rating_id,
tbl.prod_id,
#prev_prod:=prod_id
FROM rating tbl,
(SELECT #rownum := 1,
#prev_prod:=NULL)t
ORDER BY tbl.prod_id)t
WHERE rating_id = ?
SQLFiddle demo

mysql rank from results

Sorry for posting another question about mysql ranking but all questions and answers which I already looked didn't help me....
I have mysql table of user points. User can have more results. My goal is to get max result from user and its rank.
CREATE TABLE results
(`user_id` int, `points` int);
INSERT INTO results VALUES
(1,10),
(2,20),
(3,20),
(4,30),
(4,60),
(5,5),
(1,80);
So upper solution would be:
rank | user_id | points
1 1 80
2 4 60
3 3 20
3 2 20
4 5 5
The following query does the trick:
SET #rank=0;
SET #points=0;
SELECT #rank := IF(#points = a.points, #rank, #rank + 1) AS rank, a.user_id, #points := a.points AS points
FROM (
SELECT user_id, MAX(points) as points
FROM results
GROUP BY user_id
) a
ORDER BY a.points DESC;
I have also created an SQLFiddle of it so you can see that it works: http://sqlfiddle.com/#!2/7ba2f/12
Use a user defined variable to produce the rank when selecting from an aggregated aliased query that calculates the maximum for each user:
select
(#rank := ifnull(#rank, 0) + 1) as rank,
user_id,
points
from (select
user_id,
max(points) as points
from results
group by 1
order by 2 desc) x
FYI, a UDV starts out life as null, hence the ifnull() call.