Resell services with the EthicalSMM API

SQL IN Clause Generator

Turn a pasted list of IDs or values into a SQL IN clause, quoted and de-duplicated, ready for a WHERE condition.

Was this tool helpful?

Thanks for the feedback!

This tool turns a pasted list of IDs or values into a SQL IN (...) clause, quoted, escaped and with duplicates removed, ready to drop into a WHERE condition. It saves analysts and developers from adding commas and quotes by hand when someone sends a column of order numbers from Excel. It can also split long lists into groups of 1,000 for Oracle.

The fields

FieldChoices and effect
Column name, optionalIf filled, the output starts WHERE column IN (. If blank, it is just IN (...).
QuotesAutomatic (numbers bare, text quoted), Always quote, or Never quote
DuplicatesRemove duplicates, or Keep duplicates
More than 1,000 valuesKeep one IN list, or Split into OR groups of 1,000 (Oracle)

How the list is read

Values are split on new lines, commas and tabs. Spaces do not split, so "New Delhi" stays one value. Each value is trimmed, one pair of matching quotes around it is removed, and empty entries are dropped. Duplicate removal keeps the first copy and is case-sensitive, so Pune and pune are both kept.

With Automatic quotes, the tool checks whether every value is a plain number (digits, an optional minus sign and an optional decimal part). If they all are, it leaves them bare. If even one is not, it quotes all of them. Inside quoted values, every apostrophe is doubled, which is how SQL escapes it. The stats show the number of values, how many duplicates were removed and whether quoting was used.

Worked examples

A list of five customer IDs with one repeat, column customer_id:

1001
1002
1003
1002
1004
WHERE customer_id IN (1001, 1002, 1003, 1004)

The stats show 4 values and 1 duplicate removed. A list of cities with column city, where one value is already in double quotes and one contains an apostrophe:

Pune
Mumbai
O'Neil
"Delhi"
Pune
WHERE city IN ('Pune', 'Mumbai', 'O''Neil', 'Delhi')

Leading zeros, NULL and other traps

  • Codes with leading zeros: Automatic mode sees 007, 008, 009 as numbers and writes IN (007, 008, 009), which the database reads as 7, 8 and 9. If the column is text, choose Always quote to get IN ('007', '008', '009').
  • In a mixed list, 101, 102 and ABC-7 are all quoted in Automatic mode, because one value is not a number. Never quote writes them bare, so use it only when every value is a number or an expression.
  • IN never matches rows where the column is NULL, and a NOT IN list containing NULL returns no rows at all. Handle NULLs with a separate IS NULL condition.
  • A comma inside a value always splits it in two, so add any value such as "Sharma, R." to the clause by hand.

An empty box gives "Paste a list of values, one per line or separated by commas."

Oracle's 1,000-item limit

Oracle Database rejects an IN list with more than 1,000 expressions, with the error ORA-01795: maximum number of expressions in a list is 1000. Choose "Split into OR groups of 1,000 (Oracle)" and any list longer than 1,000 is wrapped like this:

WHERE (order_id IN (100001, 100002, ...)
   OR order_id IN (101001, 101002, ...)
   OR order_id IN (102001, 102002, ...))

That is what the tool produces for 2,500 order IDs: three groups of 1,000, 1,000 and 500. If you leave the column name blank, it uses the placeholder column_name, which you must replace. Lists of 1,000 or fewer are never split. Other databases have no fixed limit this small, but very long lists make queries slow and hard to read, so for many thousands of values a temporary table and a join is usually better.

To clean a messy list first, the duplicate line remover helps. To tidy the full query, use the SQL formatter.

Frequently asked questions

How do I turn an Excel column into a SQL IN clause?
Copy the column in Excel and paste it into the tool, since each cell arrives on its own line. Add the column name and the tool writes WHERE column IN (...) with commas and quotes in place.
What is the maximum number of values in a SQL IN clause?
Oracle allows at most 1,000 expressions in one IN list and raises ORA-01795 above that, which the split option handles. Other databases have no such small fixed limit, though very long lists slow queries down.
How do I escape an apostrophe in a SQL IN list?
Double it. The tool does this automatically, so O'Neil is written as 'O''Neil'.
Should numbers be quoted in an IN clause?
Not for numeric columns. For text columns that hold codes with leading zeros, such as 007, choose Always quote so the zeros are kept.
Does the tool remove duplicate values?
Yes, by default it keeps the first copy of each value and reports how many were removed. The match is case-sensitive, so Pune and pune are treated as different.
Can my values contain spaces?
Yes. The tool splits only on new lines, commas and tabs, so a value like New Delhi stays in one piece.

Related guides

Related tools

People also used

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.