SQL GROUP BY Clause
SQL GROUP BY as its name imply, returns group of rows. SQL GROUP BY clause divides a table into sets and it usually use with SQL aggregate functions which produces summary value for each set. Let take a look at this example to see how SQL GROUP BY clause works.
SELECT department_id,
count(employee_id) AS employee_count
FROM employees
GROUP BY department_id
department_id employee_count
------------- --------------
(NULL) 1
1 1
2 1
The query example above returns a summary of how many employee in each department. If you want to see it in a visual way, you can combine with SQL JOIN clause to see the department name.
SELECT D.name as department_name,
count(employee_id) as employee_count
FROM employees AS E
INNER JOIN departments AS D
ON D.department_id = E.department_id
GROUP BY department_name
department_name employee_count
-------------------- --------------
Marketing 1
Sale 2
Software Engineering 2
Be noted that you can only make a group of things you select.