Display top 5 marks in mysql without limitsMMMurugappan Mn10yAsked Feb 8, 2016, 11:56 PM ESTSolvedShareLinkedInXCopy linkSaveDisplay top 5 marks in mysql without using limits.
Display top 5 marks in mysql without limitsMMMurugappan Mn10yAsked Feb 8, 2016, 11:56 PM ESTSolvedShareLinkedInXCopy linkSaveDisplay top 5 marks in mysql without using limits.
Pankaj Kumar ChoudharyRank #80 · 24.4K reputation10y agoPosted Feb 10, 2016, 1:22 AM EST✓ Accepted answer+1 Hello Murugappan Try this Select Tab.Salary From(SELECT Employee .Salary, @curRank := @curRank + 1 AS rank FROM Employee , (SELECT @curRank := 0) rOrder By Employee.Salary DESC )TabWhere Tab.rank<5 +1
Pankaj Kumar ChoudharyRank #80 · 24.4K reputation10y agoPosted Feb 9, 2016, 6:20 AM EST+1 Hello @Murugappan try this query Select tab.Marks From(SELECT p.Marks, @curRank := @curRank + 1 AS rank FROM tTable-Name p, (SELECT @curRank := 0) r)Tab Where Tab.rank<=5;+1
Rajeev PunhaniRank #551 · 4.4K reputation10y agoPosted Feb 9, 2016, 8:09 AM EST+1Hi Murugappan, Please try the following query. SET @row_number = 0;SELECT *,(@row_number:=@row_number + 1) AS num FROM tablename WHERE @row_number<=5 If the above solution is helpful then,accept the answer. +1Rajeev PunhaniRank #551 · 4.4K reputation10y agoPosted Feb 10, 2016, 8:44 AM EST+1Hi Murugappan, Please check the following querySET @row_number = 0;SET @row_number1 = 0;SELECT DISTINCT(salary) FROM(SELECT DISTINCT(salary),(@row_number1:=@row_number1 + 1) FROM(SELECT total_bill,(@row_number:=@row_number + 1) AS num FROM tablename WHERE @row_number<=5 ORDER BY salary DESC )a WHERE @row_number1<=5) b If the above solution is helpful then,accept the answer.+1CMCassie ModRank #3,821 · 387 reputation10y agoPosted Feb 9, 2016, 5:31 AM EST0 use something like this: select MAX('columnname') from table order by value desc limit N 0MMMurugappan Mn37 reputation10y agoPosted Feb 9, 2016, 7:36 PM EST0This one is showing first 6 records(if we use less than it will be solved).I need to show top 5 salaries.So, I have to use order by then use this code.Do you have any idea how to do that 0Pankaj Kumar ChoudharyRank #80 · 24.4K reputation10y agoPosted Feb 9, 2016, 8:25 PM EST0 Try This Select Tab.col1 From(SELECT *, @curRank := @curRank + 1 AS rank FROM table_ p, (SELECT @curRank := 0) r)TabWhere Tab.rank<5Order BY Tab.col1 0MMMurugappan Mn37 reputation10y agoPosted Feb 9, 2016, 8:46 PM EST0I'm not getting it I have table with name Employee, columns-Id,Name and Salary.I have to show top 5 salary(we have to use order by desc)0MMMurugappan Mn37 reputation10y agoPosted Feb 10, 2016, 6:11 PM EST0Thank you Rajeev, your code also worked for me...0MMMurugappan Mn37 reputation10y agoPosted Feb 11, 2016, 6:54 PM EST0I came across another code select * from Employee e1 where 5>(select count(distinct(e2.salary)) from Employee e2 where e2.salary> e1.salary) order by salary desc;0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajeev PunhaniRank #551 · 4.4K reputation10y agoPosted Feb 10, 2016, 8:44 AM EST+1Hi Murugappan, Please check the following querySET @row_number = 0;SET @row_number1 = 0;SELECT DISTINCT(salary) FROM(SELECT DISTINCT(salary),(@row_number1:=@row_number1 + 1) FROM(SELECT total_bill,(@row_number:=@row_number + 1) AS num FROM tablename WHERE @row_number<=5 ORDER BY salary DESC )a WHERE @row_number1<=5) b If the above solution is helpful then,accept the answer.+1CMCassie ModRank #3,821 · 387 reputation10y agoPosted Feb 9, 2016, 5:31 AM EST0 use something like this: select MAX('columnname') from table order by value desc limit N 0MMMurugappan Mn37 reputation10y agoPosted Feb 9, 2016, 7:36 PM EST0This one is showing first 6 records(if we use less than it will be solved).I need to show top 5 salaries.So, I have to use order by then use this code.Do you have any idea how to do that 0Pankaj Kumar ChoudharyRank #80 · 24.4K reputation10y agoPosted Feb 9, 2016, 8:25 PM EST0 Try This Select Tab.col1 From(SELECT *, @curRank := @curRank + 1 AS rank FROM table_ p, (SELECT @curRank := 0) r)TabWhere Tab.rank<5Order BY Tab.col1 0MMMurugappan Mn37 reputation10y agoPosted Feb 9, 2016, 8:46 PM EST0I'm not getting it I have table with name Employee, columns-Id,Name and Salary.I have to show top 5 salary(we have to use order by desc)0MMMurugappan Mn37 reputation10y agoPosted Feb 10, 2016, 6:11 PM EST0Thank you Rajeev, your code also worked for me...0MMMurugappan Mn37 reputation10y agoPosted Feb 11, 2016, 6:54 PM EST0I came across another code select * from Employee e1 where 5>(select count(distinct(e2.salary)) from Employee e2 where e2.salary> e1.salary) order by salary desc;0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
CMCassie ModRank #3,821 · 387 reputation10y agoPosted Feb 9, 2016, 5:31 AM EST0 use something like this: select MAX('columnname') from table order by value desc limit N 0
MMMurugappan Mn37 reputation10y agoPosted Feb 9, 2016, 7:36 PM EST0This one is showing first 6 records(if we use less than it will be solved).I need to show top 5 salaries.So, I have to use order by then use this code.Do you have any idea how to do that 0
Pankaj Kumar ChoudharyRank #80 · 24.4K reputation10y agoPosted Feb 9, 2016, 8:25 PM EST0 Try This Select Tab.col1 From(SELECT *, @curRank := @curRank + 1 AS rank FROM table_ p, (SELECT @curRank := 0) r)TabWhere Tab.rank<5Order BY Tab.col1 0
MMMurugappan Mn37 reputation10y agoPosted Feb 9, 2016, 8:46 PM EST0I'm not getting it I have table with name Employee, columns-Id,Name and Salary.I have to show top 5 salary(we have to use order by desc)0
MMMurugappan Mn37 reputation10y agoPosted Feb 10, 2016, 6:11 PM EST0Thank you Rajeev, your code also worked for me...0
MMMurugappan Mn37 reputation10y agoPosted Feb 11, 2016, 6:54 PM EST0I came across another code select * from Employee e1 where 5>(select count(distinct(e2.salary)) from Employee e2 where e2.salary> e1.salary) order by salary desc;0
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Feb 10, 2016, 1:22 AM EST
(SELECT Employee .Salary, @curRank := @curRank + 1 AS rank FROM Employee , (SELECT @curRank := 0) r
Where Tab.rank<5
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Feb 9, 2016, 6:20 AM EST
(SELECT p.Marks, @curRank := @curRank + 1 AS rank FROM tTable-Name p, (SELECT @curRank := 0) r)Tab
Rajeev PunhaniRank #551 · 4.4K reputationPosted Feb 9, 2016, 8:09 AM EST
If the above solution is helpful then,accept the answer.
Rajeev PunhaniRank #551 · 4.4K reputationPosted Feb 10, 2016, 8:44 AM EST
Cassie ModRank #3,821 · 387 reputationPosted Feb 9, 2016, 5:31 AM EST
Murugappan Mn37 reputationPosted Feb 9, 2016, 7:36 PM EST
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Feb 9, 2016, 8:25 PM EST
(SELECT *, @curRank := @curRank + 1 AS rank FROM table_ p, (SELECT @curRank := 0) r)Tab
Where Tab.rank<5
Order BY Tab.col1
Murugappan Mn37 reputationPosted Feb 9, 2016, 8:46 PM EST
Murugappan Mn37 reputationPosted Feb 10, 2016, 6:11 PM EST
Murugappan Mn37 reputationPosted Feb 11, 2016, 6:54 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts