Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:55:32+00:00 2026-06-17T22:55:32+00:00

I have an SQL statement, SELECT foo FROM bar WHERE id IN %s .

  • 0

I have an SQL statement, SELECT foo FROM bar WHERE id IN %s. I have a list of integers, e.g. [1, 2, 3] and I’d like this to get turned into a SQL statement that looks like SELECT foo FROM bar WHERE id IN (1, 2, 3).

I use SQLAlchemy Core for its connection pooling and for making some inserts with multiple VALUES clauses easier to write and maintain. I prefer to write most of my queries in raw SQL.

To do this in Pyscopg2 I do cursor.execute('SELECT .. WHERE IN %s', (tuple(my_list),)). I can’t manage to make this work in SQLAlchemy, however.

engine.execute('SELECT ... WHERE IN %s', tuple(my_list)) raises an exception: TypeError: not all arguments converted during string formatting. This same exception is raised if I pass just the list, not wrapped in a tuple.

If I use named parameters like engine.execute('SELECT ... WHERE id IN :ids', ids=my_list) I get a ProgrammingError exception because SQLAlchemy creates incorrect SQL: SELECT * FROM foo WHERE id IN :ids (it doesn’t substitute the :ids value for my variable). This same exception is raised if I pass a tuple.

How can I use a WHERE IN() clause using raw SQL in SQLAlchemy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:55:34+00:00Added an answer on June 17, 2026 at 10:55 pm

    This is an unusual format supported only by some DBAPIs, in that it renders a tuple of items as individual SQL expressions, including that it renders the comma and such in between parameters, so a statement like execute("select * from table where value in %s", (somelist, )) expands out at the database level into select * from table where value in (1, 2, 3).

    SQLAlchemy is not expecting this format – it already does some inspection of the incoming parameters as it is concerned with routing the parameters into either the DBAPI execute() or executemany() methods, and also accepts a few different styles, and the outcome of this conversion is that the tuple here gets flattened out. You can sneak your tuple past this parsing by adding one more tuple:

    from sqlalchemy import create_engine
    
    engine = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
    
    with engine.connect() as conn:
        trans = conn.begin()
    
    
        conn.execute("create table test (data integer)")
        conn.execute(
                "insert into test (data) values (%s)",
                [(1, ), (2, ), (3, ), (4, ), (5, )]
            )
    
        result = conn.execute(
                    "select * from test where data in %s",
                    (
                        ((1, 2, 3),),
                    )
                )
    
        print result.fetchall()
    

    The above style only works for some DBAPIs. A quick test confirms it works for psycopg2 and MySQLdb, but not on sqlite3. It has more to do with the underlying system which the DBAPI uses to send bound parameters to the database; psycopg2 and MySQLdb both do Python string interpolation and their own escaping, but systems like cx_oracle will pass the parameters individually to OCI, so this kind of thing wouldn’t work in that case.

    SQLAlchemy of course offers the in_() operator when using SQL expression constructs but this doesn’t apply to straight strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0;
this SQL statement select firstname,lastname from users where lastname in('foo','bar') group by firstname it
I have this SQL statement: SELECT * FROM history WHERE (fk_person = 2119) AND
I have the following SQL statement: SELECT ID, NAME FROM myTable WHERE ID LIKE
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
I have a SQL statement like the following: select A from table1, (select B
I have this SQL-Statement: SELECT Geburtsdatum FROM Kunde WHERE Geburtsdatum BETWEEN '1993-01-01' AND '2000-01-01'
I have this sql statement: SELECT * from tBooks where BookNbr = '20111122-001' BookNbr
I have this sql statement SELECT userID from users WHERE (name='name1' AND username='username1') OR
I have this sql statement: SELECT game.game_id FROM game, userGame WHERE userGame.user_id = 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.