Skip to content Skip to sidebar Skip to footer

Sql Query That Relies On Count From Another Table? - Sqlalchemy

I need a query that can return the records from table A that have greater than COUNT records in table B. The query needs to be able to go in line with other filters that might be a

Solution 1:

Use a subquery:

SELECT * from person
WHERE PersonID IN 
  (SELECT PersonId FROM appointments
   GROUP BY PersonId
   HAVING COUNT(*) >= 5)
AND dob > 25

Solution 2:

SELECT Person.PersonID, Person.Name
FROM Person INNER JOIN Appointment
ON Person.PersonID = Appointment.PersonID
GROUP BY Person.PersonID, Person.Name
HAVING COUNT(Person.PersonID) >= 5

Post a Comment for "Sql Query That Relies On Count From Another Table? - Sqlalchemy"