I have noticed that all the columns in a SQL Select Query should be in a aggregate function, because otherwise the output value for group would be arbitrary.This makes sense. But when the other columns are same in value, it doesn't matter which is returned. Is there a way to specify this or just use MAX() or MIN() on varchar columns?
Return multiple columns with SQL where GroupBy is on one column
sql
If you don't want to aggregate one or more columns that need to be displayed, include them in the column list of your query and the columns to group by list too.SELECT Col1, Col2, SUM(Col3) AS Col3TotalFROM TableGROUP BY Col1, Col2ORDER BY Col1, Col2;In the above example, data from Col3 is summed using the combined data of Col1 and Col2 to determine where the aggregation is segregated. In other words, when the value of either Col1 or Col2 changes, a new bucket for a running sum of Col3 is created. You output data could look like this:Col1  Col2  Col3Total====  ====  =========A     A     4A     B     97B     A     1B     B     NULL - assumes a NULL value was found in the Col3 dataB     C     2If you want to assume that Col2 has a single matching value per distinct Col1, it would like something like this:Col1  Col2  Col3Total====  ====  =========A     Y     101B     Z     NULL - assumes a NULL value was found in the Col3 dataMicrosoft TSQL SUM functionOracle SQL Aggregate functionsMySQL SQL Aggregate functions