0% found this document useful (0 votes)
166 views3 pages

SQL Query Lecture Group by PDF

The GROUP BY clause is used in a SELECT statement to group the results by one or more columns. It is followed by aggregate functions like COUNT, SUM, AVG, MIN, and MAX to operate on grouped data. Example syntax is provided to GROUP BY region and return the customer count, GROUP BY product_id to SUM the quantity sold, and GROUP BY customer_id to return MIN, MAX, AVG, and SUM of sales values.

Uploaded by

Tauseef khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views3 pages

SQL Query Lecture Group by PDF

The GROUP BY clause is used in a SELECT statement to group the results by one or more columns. It is followed by aggregate functions like COUNT, SUM, AVG, MIN, and MAX to operate on grouped data. Example syntax is provided to GROUP BY region and return the customer count, GROUP BY product_id to SUM the quantity sold, and GROUP BY customer_id to return MIN, MAX, AVG, and SUM of sales values.

Uploaded by

Tauseef khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

GROUP BY

GROUP BY clause is used in a SELECT statement to group the results by one or more columns.

SELECT "column_name1", "function type" ("column_name2")


FROM "table_name"
Syntax GROUP BY "column_name1";

Start-Tech Academy
GROUP BY
GROUP BY clause is used in a SELECT statement to group the results by one or more columns.

SELECT region, COUNT (customer_id) AS customer_count


FROM customer GROUP BY region;

SELECT product_id, SUM (quantity) AS quantity_sold FROM sales


GROUP BY product_id ORDER BY quantity_sold DESC;

SELECT customer_id,
Example MIN(sales) AS min_sales,
MAX(sales) AS max_sales,
AVG(sales) AS Average_sales,
SUM(sales) AS Total_sales
FROM sales
GROUP BY customer_id
ORDER BY total_sales DESC
LIMIT 5;

Start-Tech Academy

You might also like