I would first figure out how you would do this in SQL, and then
translate that to SQLAlchemy. In this case, the EXISTS operator might
work:
SELECT *
FROM department
WHERE EXISTS (
SELECT 1
FROM employee
WHERE employee.department_id = department.id
AND employee.name IN (...)
)
Using ORM querying what is the best practice for limiting the output to a
given number of resulting *entities*?
Consider this model:
from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import