Skip to content
Advertisement

LIMIT and Contains in a single JSONB Query

I have 2 queries that separately run perfectly:

This one limits the results:

This one correctly runs a ‘contains’ filter:

How do I combine these so that I can “select data where the first name contains M, and limit the result to X rows”?

Here is a SQLFiddle with fake data to test it on. I am unsure how to proceed.

Advertisement

Answer

Use limit a subquery before aggregating:

Note: you probably want to add an order by clause in the subquery; without it, it is not possible to predict which 3 records will be picked.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement