ARJUN
ARJUN

Reputation: 59

Unable to run query with group by

I was trying to execute the following command:

SELECT name,sum(mark) 
FROM students 
group by name order by mark

but I get the error.

Column "students.mark" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

I am unable to understand the error.

Upvotes: 0

Views: 79

Answers (3)

Faizan Younus
Faizan Younus

Reputation: 803

Try using this

SELECT name,sum(mark) FROM students group by name order by sum(mark)

Hope this helps

Upvotes: 3

Panda
Panda

Reputation: 2520

This should work for you,

SELECT name,sum(mark) FROM students group by name order by sum(mark)

You need to specify sum aggregate in orderby

Upvotes: 0

Pரதீப்
Pரதீப்

Reputation: 93754

Move the SUM aggregate to Order by

SELECT name,sum(mark) FROM students group by name order by sum(mark)

or use Alias name

SELECT name,sum(mark) as Mark FROM students group by name order by mark

Upvotes: 3

Related Questions