Not sure how you get 2 queries, but this seems like it gives right answer. class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) items = relationship('Item') class Item(Base): __tablename__ = 'items' id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey(User.id))
q1 = session.query(User.id, func.count(Item.id)).\ join('items').\ group_by(User.id).having(func.count(Item.id)>5) Check out the documentation for Query.join for alternatives on the join parameter http://www.sqlalchemy.org/docs/06/orm/query.html#sqlalchemy.orm.query.Query.join for example: if the "items" relationship isn't declared .join((Item,User.id==Item.user_id)) generates the same SQL -- Mike Conley -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.