Search code examples
sqlsql-servercountselect-query

sql select 5 higest values


I'm trying to select the 5 rows with the highest count value

This is my query:

string sql = "SELECT   top 5  count FROM  Likes  ORDER BY COUNT(*) DESC";

It's just throwing an error code that

Column 'Likes.count' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

It's for a project I've got to present tomorrow... enter image description here


Solution

  • On SQL Server, simply do this:

     SELECT TOP 5 * FROM Likes ORDER BY [Count] DESC
    

    This assumes that your Likes-table already contains a column named [Count] meaning that you don't need to count the records yourself (which is what COUNT(*) does).