Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Select top 3 rows with highest value in a certain column

Hi Stackoverflow users,

I'm currently working on a small website and I need a SQL query that selects 3 rows with the most 'Likes'. I've tried using max and top 3 but nothing seem to work for me. I would appreciate some help from you guys! Thanks in advance.

enter image description here

like image 223
CodingLegend Avatar asked Nov 24 '25 10:11

CodingLegend


1 Answers

Using TOP won't work with MySQL, because that is SQL Server (or maybe Access) syntax. You probably want LIMIT here:

SELECT *
FROM yourTable
ORDER BY Likes DESC
LIMIT 3;

We could have also used:

LIMIT 3, OFFSET 0;  -- three records with no offset
LIMIT 0, 3          -- same as above
like image 84
Tim Biegeleisen Avatar answered Nov 27 '25 00:11

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!