Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GROUP BY one column; pick arbitrary value for another

Tags:

People also ask

Can we use SELECT * with GROUP BY?

You can use a SELECT command with a GROUP BY clause to group all rows that have identical values in a specified column or combination of columns, into a single row.

Can we use GROUP BY with aggregate function?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

Does GROUP BY return unique values?

The group by can also be used to find distinct values as shown in below query. The two queries return same result.

Can you GROUP BY coalesce?

Because the COALESCE is an expression, you can use it in any clause that accepts an expression such as SELECT , WHERE , GROUP BY , and HAVING .


I am trying to select one row per user. I don't care which image I get. This query works in MySQL, but not in SQL Server:

SELECT user.id, (images.path + images.name) as 'image_path'
FROM users
JOIN images ON images.user_id = users.id
GROUP BY users.id