By 25/02/2016 1 Comments

What Is GROUP BY Statement In SQL?

  • SQL GROUP BY statement usually used with the multi-row functions.
  • When you use a multi-row function the SQL GROUP BY statement groups the result-set by one or more columns in a table according to that multi-row function.

Syntax:

SELECT column_name, multi-row function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Example: Use of SUM() function

SELECT b.buyer_name, SUM(p.price) "Total price"
FROM buyers b
INNER JOIN products p
ON b.buyer_id = p.buyer_id
GROUP BY p.buyer_id;
  • In above example the GROUP BY statement groups the result by column buyer_id in table (products) and returns the total price of each product which is bought by each buyer.

Example: Use of COUNT() function

SELECT b.buyer_name, COUNT(p.price) "Item Numbers"
FROM buyers b
INNER JOIN products p
ON b.buyer_id = p.buyer_id
GROUP BY p.buyer_id;
  • In above example the GROUP BY statement groups the result by column buyer_id in table (products) and returns the total number of each product which is bought by each buyer.

Example: Use GROUP BY with multiple columns

SELECT b.buyer_name, p.product_name, COUNT(p.price) "Item Number(s)"
FROM buyers b
INNER JOIN products p
ON b.buyer_id = p.buyer_id
GROUP BY p.buyer_id, p.product_name;
  • In above example two columns (buyer_id) and (product_name) are grouped by GROUP BY statement.






← Previous Tutorial


Posted in: Sql

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .