I have two raw Postgresql statements.
q1 = "SELECT name FROM table_a"
q2 = "SELECT name FROM table_b"
How can I query the intersection of them using SQLAlchemy.
This is more or less what you could do:
from sqlalchemy import create_engine, text
engine = create_engine('postgresql://user:password@localhost/somedb')
q1 = "SELECT name FROM table_a intersect SELECT name FROM table_b"
q1res = engine.execute(text(q1)).fetchall()
# or
q2res = engine.execute(text(q1)).fetchone()
Depending on your needs. Ditch fetchone and fetchall if you intend to make inserts or updates.