Below is Scores Table data and structure Id Name score 1 Ram 90 2 Shyam 85 3 Dhanshyam 95 4 Sita 65 5 Geeta 98 6 Radha 45 SELECT id, name, score, FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score DESC ) FROM scores ) ) AS rank …
Read More »How to get second-highest salary of employees in a table
Method 1) Below query can be used to find the nth maximum value, just replace 1 from nth number SELECT * FROM `emp` e1 where 1 = (select count(distinct(salary)) from emp e2 where e2.salary>e1.salary) Method 2) select * from emp where salary = (select max(salary) from emp where salary < …
Read More »Query to Re-index Primary Key of MySQL Database
SET @newid=0; UPDATE tablename SET primary_key_id=(@newid:=@newid+1) ORDER BY primary_key_id; This query will reindex the primary key starts from 1
Read More »