GROUP BY

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.

Tutorials dojo strip

SQL GROUP BY Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;

SQL GROUP BY Patients Gender Example

This query lists how many patients are grouped by gender:

SELECT gender, COUNT(patient_id)
FROM Patients
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:

SELECT specialty, COUNT(doctor_id)
FROM Doctors
GROUP BY specialty
ORDER BY COUNT(doctor_id) DESC;

SQL GROUP BY Labs

Tutorials dojo strip
Scroll to Top