The answer is yes, but only in a very certain way.
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
Let's imagine that this is your current query:
select foo , sum(bar) as sumbar from qux group by foo
Now, we want to add two more columns—let's call them other1 and other2—but we don't want to add them to the GROUP BY, because we still want only one row per foo in the results.
The very certain and only way that this will be allowed is by ensuring that the two new columns are inside aggregate functions.
Here's one example of many possibilities:
select foo , sum(bar) as sumbar , max(other1) as maxcol1 , avg(other2) as avgcol2 from qux group by foo
What is NOT ALLOWED is to put "naked" columns (not inside aggregate functions) into the SELECT clause but leave them out of the GROUP BY clause:
select foo , sum(bar) as sumbar , other1 , other2 from qux group by foo
This is invalid syntax, and only MySQL will run it (although they do warn you, in GROUP BY and HAVING with Hidden Fields, that results can be unpredictable).
Related Q&A from Rudy Limeback
Read an example of an SQL case expression from our SQL expert Rudy Limeback.continue reading
Read SQL expert Rudy Limeback's advice for counting combinations in a table with SQL's GROUP BY clausecontinue reading
Read about the Mimer Validator, a tool used to verify your SQL code, in this tip from SQL expert Rudy Limeback.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.