Overview
The Group BY clause is utilized in a SELECT statement to group rows into a set of summary rows by values of one or more columns. It is regularly utilized in conjunction with aggregate functions such as Count, MAX, MIN, Sum, AVG, etc. The Group BY clause takes after the WHERE clause in a SELECT statement and goes before the Order BY clause.
The SQL GROUP BY
An online retail store has recently started selling products online, and they want to analyze customer orders to understand its customer base better. The store owner has turned to SQL to help them do this. They want to group customer orders by product category to understand better which products their customers buy. To do this, they use the GROUP BY clause to group customer orders by product category. This allows them to quickly identify the most popular product categories and decide how to serve their customers better. Unfortunately, they weren't aware of how to progress. Let's help them.
Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
The SELECT clause helps specify the columns selected from the table. The WHERE clause helps to restrict the result set by including a condition. The GROUP BY clause is utilized to group the selected rows into groups with the same values for the specified column(s). The ORDER BY clause helps to sort the result set based on the specified column(s).
SQL GROUP BY Example
Table Name: customers
customer_id | country |
---|---|
1 | USA |
2 | UK |
3 | USA |
4 | Germany |
SELECT COUNT(customer_id), country
FROM customers
GROUP BY country;
This SQL statement would return the number of customers in each country. The result would be two columns - one containing the count of customers, and the second containing the country
GROUP BY With JOIN Example
Employees Table
emp_no | last_name |
---|---|
1 | Smith |
2 | Johnson |
3 | Williams |
4 | Jones |
Salaries Table
emp_no | salary |
---|---|
1 | 1000 |
2 | 2000 |
3 | 3000 |
4 | 4000 |
SELECT employees.last_name, SUM(salaries.salary)
FROM employees
INNER JOIN salaries ON employees.emp_no = salaries.emp_no
GROUP BY employees.last_name;
This query uses a GROUP BY clause with a JOIN to select the last name of every employee and the total salary of all salaries associated with that employee. It does this by joining the employees and salaries tables on the emp_no column and grouping the result by last_name. The SUM function is then used to calculate the total salary for each employee.
Conclusion
The online retail store utilized SQL and the Group BY clause to analyze client orders and gain a better understanding. By gathering client orders by item category, they could rapidly recognize the most prevalent item categories and choose how to serve their clients better. This permitted the store to better get its client base and make educated choices on how to better serve them.
Key takeaways
Quiz
Answer: a. To group results by a specified field
Answer: d. All of the above
Answer: a. The HAVING clause can be used with aggregate functions
Answer: b. To group results by a specified field
Top Tutorials
Related Articles