Split SQL IN Lists into Chunks of 1,000 in JavaScript
sql-in-chunker is a free npm package that splits long SQL IN lists into chunks of 1,000 joined with OR, so Oracle never throws ORA-01795.
To avoid ORA-01795: maximum number of expressions in a list is 1000, split the values into groups of at most 1,000 and join the IN conditions with OR. sql-in-chunker is a free, MIT-licensed JavaScript package that does this in one call, including quoting, escaping and the outer parentheses people forget. Get it from npm or read the source on GitHub.
The usual scenario: someone sends you a spreadsheet with 2,500 order numbers and asks for their status. You paste the column into a query and Oracle rejects it. Cutting the list by hand every thousand lines is slow, and it is easy to drop a value at a boundary.
What does the chunked query look like?
For 2,500 values the package produces three chunks:
SELECT *
FROM orders o
WHERE (o.order_no IN ('1', '2', ..., '1000')
OR o.order_no IN ('1001', ..., '2000')
OR o.order_no IN ('2001', ..., '2500'));
No single list exceeds 1,000 expressions, and the result set is the same as for one long list.
Install and build the WHERE clause
npm install sql-in-chunker
import { parseList, buildInClause } from "sql-in-chunker";
// a column pasted from a spreadsheet, one value per line
const orderNumbers = parseList(pastedText, { unique: true });
const where = buildInClause("o.order_no", orderNumbers);
const sql = `SELECT * FROM orders o WHERE ${where}`;
parseList splits on newlines, commas, semicolons and tabs, trims every value and skips empty lines. The unique option drops duplicates, which show up in spreadsheet exports more often than you would expect.
Why are the chunks wrapped in parentheses?
This is the most common bug in hand-written chunking. In SQL, AND binds tighter than OR, so adding another condition without parentheses changes the result:
-- wrong: status = 'NEW' applies only to the first chunk
WHERE status = 'NEW'
AND id IN (...1,000 values...)
OR id IN (...more values...)
-- right: the whole IN condition is grouped
WHERE status = 'NEW'
AND (id IN (...1,000 values...)
OR id IN (...more values...))
The first query runs without an error. It returns rows from the second chunk with any status, and the mistake often goes unnoticed until production. When there is more than one chunk, buildInClause always adds the outer parentheses, so you can combine its output with other conditions.
How does it handle NOT IN, empty lists and quotes?
| Input | buildInClause output |
|---|---|
| 2 values | id IN ('1', '2') |
| 2,500 values | 3 chunks joined with OR, wrapped in parentheses |
not: true |
NOT IN chunks joined with AND |
| empty list | 1 = 0 (matches nothing) |
empty list with not: true |
1 = 1 (matches everything) |
the value O'Brien |
'O''Brien' |
An empty list is an easy edge case to miss. IN () is a syntax error in Oracle, so the package returns a condition that is always false or always true instead.
Pro-Tip: keep leading zeros quoted. The
quoteoption takes"always"(default),"never"or"auto". Inautomode values are left unquoted only when every value is a plain number, and007stays quoted. If you compare aVARCHAR2column with numbers, Oracle implicitly converts the column values to numbers. A regular index on that column is then skipped, and a single non-numeric row stops the query with ORA-01722: invalid number.
How to read an IN list back into an array
Sometimes you need the opposite: take an IN list from a log file and turn it back into a list of values. parseInList handles escaped quotes and throws a SyntaxError on malformed input:
import { parseInList } from "sql-in-chunker";
parseInList("('A-1', 'O''Brien', 'A-3')"); // ["A-1", "O'Brien", "A-3"]
When should you not use it?
sql-in-chunker is built for ad-hoc queries, reports, migration scripts and admin tools. In application code that handles user input, use bind variables. For tens of thousands of values, load them into a temporary table and JOIN instead. How to fix ORA-01795 compares these workarounds, including the tuple trick. The column name is inserted as-is, so pass only identifiers you wrote yourself.
To format a short list without installing anything, paste it into the SQL IN list formatter on WuseBox. It runs in your browser and does not upload your data. For lists over 1,000 items, use the package.
The companion package pl-identifiers validates and generates Polish PESEL, NIP and REGON numbers.
Frequently Asked Questions (FAQ)
Does the 1,000 limit apply to bind variables?
Yes. Oracle counts expressions in the list, and each bind variable such as :1, :2 is one expression. The limit does not apply to a subquery like IN (SELECT id FROM staging_table).
Does sql-in-chunker work with PostgreSQL and MySQL?
Yes. It produces standard SQL. PostgreSQL and MySQL have no 1,000-item limit, but you can set your own chunkSize if your statements get too long.
Does chunking make the query faster?
No. It removes ORA-01795, but the database still has to process every value. For very large sets, a temporary table with a JOIN is faster.