The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of some functions. The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns. GROUP BY clause is used with the SELECT statement. In the query, GROUP BY clause is placed after the WHERE clause. In the query, GROUP BY clause is placed before ORDER BY clause if used.
GROUP BY Syntax:
SELECT column(s), function_name(column)
FROM table_name
[WHERE condition]
GROUP BY column1, column2
[ORDER BY column1, column2];
Example: Consider employees table
The following query
SELECT dept_id, SUM(salary) AS total_salaries FROM employees GROUP BY dept_id;
gives the output
0 comments:
Post a Comment