MODE
Developers8 min read

Building a SQL IN Clause From a Spreadsheet Column: Quoting, Limits and Safer Alternatives

How to turn a list of values into a correct IN clause, with the pitfalls that cause wrong results: quotes, NULL in NOT IN, type mismatches, list size limits and injection risk.

Published September 7, 2026 · By Sudip Bhowmick

You have a column of customer IDs in a spreadsheet and you need every matching row from a database. The quick route is to turn the column into a comma separated list and paste it into an IN clause. That works, until a value contains an apostrophe, a leading zero disappears or the list is too long for the database. Here is how to do it correctly and when to choose a different method.

The Quick Conversion

Copy the column from the spreadsheet and paste it into the List Converter. Choose to wrap each item in single quotes for text values, or none for numbers, join with a comma and a space and wrap the whole list in parentheses. Turn on removing duplicates and trimming. The result, such as ('A100', 'A101', 'A102'), can go straight after the word IN. The same tool produces square bracket arrays for code and other separators for other uses.

Pitfall 1: Quotes Inside Values

Standard SQL escapes a single quote inside a string by doubling it, so O'Brien is written as O''Brien. The List Converter protects quote characters with a backslash, which some databases accept only in particular modes, and standard SQL does not. If any value contains an apostrophe, replace each single quote with two single quotes before wrapping, or use a method that does not require manual quoting at all, as described below.

Pitfall 2: Types and Hidden Characters

  • ▸Numbers stored as text and the reverse. A column of text IDs with leading zeros, such as 00123, must be quoted. If the spreadsheet already dropped the zeros, the values are wrong before you start.
  • ▸Non-breaking spaces and trailing spaces copied from the spreadsheet make values that look right but never match. Trim and normalize first.
  • ▸Mixed case values in a case sensitive comparison. Make the comparison explicit, for example by comparing lowercase on both sides.
  • ▸Dates in the wrong format. Use ISO format strings, and let the database convert them.

Pitfall 3: NOT IN and NULL

If the list or the subquery for a NOT IN contains a NULL, the whole condition can evaluate to unknown and return no rows at all. That is a classic source of wrong results. When using NOT IN with a subquery that might return NULL, filter out NULL values, or use NOT EXISTS, which behaves as most people expect. A literal list with a NULL inside has the same effect.

Pitfall 4: List Size Limits

  • ▸Oracle limits an IN list to 1000 expressions and raises error ORA-01795 above that. Split the list into groups joined with OR, or use another method.
  • ▸SQL Server has a limit on the number of parameters per query, 2100 in the common case, and very long literal lists slow parsing.
  • ▸PostgreSQL and MySQL accept long lists, but performance drops, planning takes longer and the statement can hit a maximum packet size.
  • ▸As a rule of thumb, beyond a few hundred values choose a join instead.

Better Options for Larger Lists

  • ▸Load the values into a temporary table or a table variable and join to it. This is faster, has no length limit and avoids quoting problems.
  • ▸Import the spreadsheet into the database with its import tool, then join.
  • ▸Pass the list as an array parameter where the database supports it, for example with ANY on an array in PostgreSQL.
  • ▸Use a common table expression with a VALUES list for short lists you want to keep inside the query.

Pitfall 5: Injection

If the values come from a person or an untrusted file, pasting them into SQL text is an injection risk, because a value such as a quote followed by a command changes the query. Manual conversion is fine for your own trusted one-off lookups in a console. For application code, always use parameterized queries and let the driver handle the values, never string concatenation.

Conclusion

Turning a column into an IN list takes seconds, but correctness takes care: quote text, double embedded apostrophes, keep leading zeros, trim hidden spaces, watch for NULL in NOT IN and respect list size limits. For large or untrusted lists, use a temporary table, a join or parameters instead of pasting values into the query text.

Free Tool

Open the List Converter

Try It Free →