Sql Query To Use a ' SELECT ' statement with GROUP BY Clause


THE ' GROUP BY ' clause is used to group records having similar values. It is used with aggregate functions like count, sum, avg, min, max.

Some of the examples are-

  1. Calculate the number of employees in each department. (here we will use count()).
  2. Find the maximum salary of the employee in each department. (here we will use max()).
  3. Find the minimum salary of the employee in each department. (here we will use min()).
  4. Calculate the average salary of the employee in each department. (here we will use avg()).
  5. Calculate the sum of the salary of the employee in each department. (here we will use sum()).
Enter - Select * from table_name;
SQL Query
SELECT 'Calculate the number of employees in each department. (here we will use count())'; SELECT COUNT(employee_id) FROM employees GROUP BY department_id; SELECT 'Find the maximum salary of the employee in each department. (here we will use max()))'; SELECT MAX(salary) FROM employees GROUP BY department_id; SELECT 'Find the minimum salary of the employee in each department. (here we will use min())'; SELECT MIN(salary) FROM employees GROUP BY department_id; SELECT 'Calculate the average salary of the employee in each department. (here we will use avg())'; SELECT AVG(salary) FROM employees GROUP BY department_id; SELECT 'Calculate the sum of the salary of the employee in each department. (here we will use sum()).'; SELECT SUM(salary) FROM employees GROUP BY department_id;
Output