일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- variables
- matplotlib.pyplot
- line width
- train/test
- Python
- multiple lines
- Default X points
- data distribution
- MySQL
- For loops
- Else
- Github
- __init__
- PANDAS
- Text mining
- polynomial regression
- start exercise
- machine learning
- line color
- PROJECT
- SQL
- AS
- matplotlib
- continue
- error
- self parameter
- pie charts
- break
- Text Analytics
- iterates
- Today
- Total
Data Science Explorer
[Basic Grammar] SQL GROUP BY Statement 본문
The GROUP BY statement groups rows that have the same values and it is usually used in aggregate functions ( COUNT (), MAX(), MIN(), SUM(), AVG()).
- GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example
Write an SQL query to find the total number of orders placed by each customer in the "Orders" table. Group the results by customer, and display the customer's name along with the count of their orders.
SELECT CustomerID, COUNT (OrderID) AS OrderCount
FROM Orders
GROUP BY CustomerID;
- GROUP BY with JOIN
Example
Write an SQL query to find the total sales amount for each product category in the "Products" table. Join the "Products" and "Categories" tables to retrieve the product category names and their corresponding sales amounts. Group the results by the category name and calculate the total sales amount within each category.
SELECT Categories.CategoryName, SUM(Products.SalesAmount) AS TotalSales
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY Categories.CategoryName;
Exercises
Write an SQL query to find the average salary for each department in a company. Join the "Employees" and "Departments" tables to retrieve employee salaries and their respective departments. Group the results by department, and calculate the average salary for each department.
SELECT Departments.DepartmentName, AVG(Employees.Salary) AS AverageSalary
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID
GROUP BY Departments.DepartmentName;
'SQL' 카테고리의 다른 글
[Basic Grammar] SQL EXISTS (0) | 2023.10.23 |
---|---|
[Basic Grammar] SQL HAVING Clause (0) | 2023.10.19 |
[Basic Grammar] SQL UNION Operator (0) | 2023.10.19 |
[Basic Grammar] SQL FULL OUTER JOIN (0) | 2023.10.18 |
[Basic Grammar] SQL RIGHT JOIN (0) | 2023.10.18 |