In: Computer Science
Answer)
In relational databases, SQL is used for performing the databases
operations on the database. The grouping in a database query is
used for arranging the identical data into the groups with the help
of the functions.
Basically, the group by a statement in SQL is used to do the same.
When the particular column has the same values in many rows of the
table then we can arrange the rows in a grouping mechanism and thus
view it as a short summary of the content of the table.
For example:
SELECT DEPT, SUM(SALARY) FROM Employee
GROUP BY DEPT;
The above SQL query is used to find out the department and the sum of all salaries of the employee who are in that department. Thus the sum function is used in this SQL query from the Employee table in the database, grouped by the DEPT column.
Another example can be:
SELECT DEPT, AVG(SALARY) FROM Employee
GROUP BY DEPT;
The above query is similar to the previous one, here the average of the salary is queried from the table Employee of employees who belong to the same department. Thus each department and average salaries are displayed using this query.