SQL Having

The HAVING clause in SQL is used to filter records that are grouped by the GROUP BY clause. Unike the WHERE clause, which operates on individual rows, the HAVING clause works on groups (or aggregates) of rows.

SQL Having Syntax

SELECT column1, column2, aggregate_function(column3)
FROM table_name
GROUP BY column1, column2
HAVING condition;



SQL Having Basic Example

This query groups the result by doctor’s first and last name, counts how many appointments each doctor has, and filter out those with 2 or fewer appointments.

SELECT d.first_name, d.last_name, COUNT(a.appointment_id) AS total_appointments
FROM Doctors d
JOIN Appointments a ON d.doctor_id = a.doctor_id
GROUP BY d.first_name, d.last_name
HAVING COUNT(a.appointment_id) > 2;




SQL Having Visual Diagram




SQL Having Labs

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top