Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Is it normal to have more than 2100 items in a whereIn clause?

I am using MS SQL and I got an error because my ->whereIn('id', $ids) has an array of more than 2100 ids in it.

If I got to this point of the WHERE IN limit - does it mean I'm doing something wrong?

If not - is there a way to split it in Laravel so that it would work? Or I will need to set it up in the DB?

Edit: It's a query to fetch data:

$data = MyModel::whereIn('id', $ids)->get();

Ty

0 likes
7 replies
Snapey's avatar

if it worked, what are you going to do with 2100+ records?

1 like
Ligonsker's avatar

@Snapey Earlier I got this data with where() clause, then I wanted another query to first get the ids for the another query so I changed it to whereIn

I need to display these records in a table in the frontend, using pagination

Ligonsker's avatar

@Snapey I am trying to make it work with the old query then. But just curious - if the normal where already fetches thousands of records with no issue, why is it a problem to just swap it with whereIn instead?

Tray2's avatar
Tray2
Best Answer
Level 73

There is a limit on how many values can be in an IN clause, but if you use a subquery like this

SELECT * 
FROM table1
WHERE some_id IN (SELECT id 
					FROM  table2
					WHERE someother_column = 'soma value');

It might be better to use EXISTS instead of in though, it coulb be faster.

1 like
Ligonsker's avatar

@Tray2 Oh this is a good option, I just wonder why there is this limit if the where clause works without problems - is it because the WHERE IN is "heavier"?

regarding EXISTS, something like this?

SELECT *
FROM table1
WHERE EXISTS
(SELECT id FROM table2 WHERE ...);
Tray2's avatar

@Ligonsker Not sure as to why you can't have more than x in an IN statement, but my guess it has to do with memory usage.

And yes something like that.

1 like

Please or to participate in this conversation.