Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two rows from same table while sorting in SQL?

I want to sort all data in custom manner with conditional statement. Firstly, I will check if column1 & column1 are same. If they are same then I will compare based on column3.

Example

id total_score mcq_score
1 50 30
2 50 40
3 50 20

Expected output

id total_score mcq_score
2 50 40
1 50 30
3 50 20
like image 953
Evan Shareef Avatar asked Sep 16 '26 16:09

Evan Shareef


1 Answers

Specify both columns in ORDER BY. If the values in first column have a tie, the second column is considered (and so on):

ORDER BY total_score DESC, mcq_score DESC
like image 65
Salman A Avatar answered Sep 19 '26 06:09

Salman A