0
votes

I have a Table that displays a teams salaries and I want to display their minimum, maximum and average salary for each team for each year.

My table looks like:

Table description

I run the following SQL:

SELECT MIN(salary), MAX(salary), AVG(salary), teamID, yearID FROM salaries; 

But get the following error:

ERROR: column "salaries.teamid" must appear in the GROUP BY clause or be used in an aggregate function

What does it mean?

2
You'll need to add the non aggregate columns in a group by clause, i.e. add GROUP BY teamID, yearID to the end of your query. - StuartLC
@StuartLC so I'm just curious -- why comment and not an answer? - MK.
@StuardLC: I think a comment is appropriate, because I don't even regard this as a "question". Was there a question being asked? Looks more like a report of some observed behavior, and a comment is appropriate when we don't know what question is being asked, e.g. "What causes MySQL to return this error?" - spencer7593
I would argue this still should be an answer because until it is answered and an answer is accepted, it will show up in searches as unanswered. Need closure. - MK.
You actually got this error in mysql? By default, mysql lets you get away with abominations like this. - pala_

2 Answers

-2
votes
SELECT MIN(salary), MAX(salary), AVG(salary), teamID, yearID 
FROM salaries
GROUP BY teamID, yearID;
0
votes

Try this

SELECT MIN(salary), MAX(salary), AVG(salary), teamID, yearID FROM 
salaries group by teamID,yearID