I would like to construct a SQL statement with an IN operator that works on a list of arbitrary length. I am working with python, pandas, and sqlalchemy.
For example, If the query I'd like to execute is
SELECT * FROM users WHERE age IN (25, 26, 27)"
I have tried:
import pandas as pd
from sqlalchemy.sql import text
ages = (25, 26, 27)
query = text("SELECT * FROM users WHERE age IN (:x)")
df = pd.read_sql_query(query, conn, params={"x":ages})
But this results in errors. How can I construct the query properly? I would like the solution to work in the case that there is only one value in the list for the IN operator, i.e. ages = tuple(25).