SQL GROUP BY Keyword
The GROUP BY keyword is used to group rows that share the same value in specified columns. It’s commonly paired with aggregate functions like COUNT(), SUM(), AVG(), MIN(), and MAX() to summarize data.
SQL GROUP BY Syntax
SQL
x
1
SELECT column_name, aggregate_function(column_name)
2
FROM table_name
3
GROUP BY column_name;
SQL GROUP BY Patients Gender Example
This query lists how many patients are grouped by gender:
SQL
1
1
SELECT gender, COUNT(patient_id)
2
FROM Patients
3
GROUP BY gender;

SQL GROUP BY Doctors Specialty Example
This query counts how many doctors belong to each specialty in the Doctors table and lists them from highest to lowest count:
SQL
1
1
SELECT specialty, COUNT(doctor_id)
2
FROM Doctors
3
GROUP BY specialty
4
ORDER BY COUNT(doctor_id) DESC;
