Microsoft SQL Server GROUP BY Simple Grouping

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Orders Table

CustomerIdProductIdQuantityPrice
125100
132200
141500
21450
356700

When grouping by a specific column, only unique values of this column are returned.

SELECT customerId
FROM orders
GROUP BY customerId;

Return value:

customerId
1
2
3

Aggregate functions like count() apply to each group and not to the complete table:

SELECT customerId, 
       COUNT(productId) as numberOfProducts,
       sum(price) as totalPrice
FROM orders
GROUP BY customerId;

Return value:

customerIdnumberOfProductstotalPrice
13800
2150
31700


Got any Microsoft SQL Server Question?