The GROUP BY clause is used in SQL to arrange identical data into groups.
It is commonly used with aggregate functions like COUNT, SUM, AVG, MAX, and MIN to perform calculations on each group.
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;
SELECT specifies the columns you want to retrieve.GROUP BY determines how the rows are grouped together.HAVING speciifiesin the GROUP BY to display specific valuesSELECT department, AVY(salary) as average salary
FROM employees
GROUP BY department;
HAVING AVG(salary) > 50000
SELECT list that is not inside an aggregate function must appear in the GROUP BY.GROUP BY happens after the WHERE clause but before ORDER BY.GROUP BY can group by one or more columns.You can use the ORDER BY clause along with GROUP BY to sort the grouped data based on specific criteria, making it easier to analyze the results. For example:
SELECT product_category, COUNT(*) AS product_count
FROM products
GROUP BY product_category
ORDER BY product_count DESC;
This query will group products by their categories and display the product count in descending order, showing the most popular categories first.
SELECT city, AVG(salary)
FROM employees
GROUP BY city;
Retrieve the department and the total salary expenses (sum of salaries) as "TotalSalaryExpenses" for each department. Display the results in descending order based on the "TotalSalaryExpenses".
Table: employees
ColumnNames DataType
employee_id int
first_name varchar
last_name varchar
department varchar
salary decimal
manager_id int
Sample Input:
| employees |
|---|
| employee_id | first_name | last_name | department | salary | manager_id |
|---|---|---|---|---|---|
| 1 | John | Doe | Sales | 65000.00 | 3 |
| 2 | Jane | Smith | Finance | 75000.00 | 4 |
| 3 | Mike | Johnson | Sales | 80000.00 | 5 |
| 4 | Emily | Davis | IT | 70000.00 | 5 |
| 5 | Robert | Brown | HR | 90000.00 | NULL |
| 6 | Maria | Garcia | Finance | 78000.00 | 4 |
| 7 | Alex | Lee | HR | 85000.00 | 5 |
Constraints: The input table employees contains valid data.
Output Format: A result set with columns: department (varchar) TotalSalaryExpenses (decimal)
Sample Output:
| department | TotalSalaryExpenses |
|---|---|
| HR | 175000.00 |
| Finance | 153000.00 |
| Sales | 145000.00 |
| IT | 70000.00 |
-- Write your SQL code attempt or solution related to SQL COMMAND
SELECT department,SUM(salary) AS TotalSalaryExpenses
FROM employees
GROUP BY department
The results include the department and the total salary expenses (sum of salaries) as "TotalSalaryExpenses" for each department. The output is ordered in descending order based on the "TotalSalaryExpenses."