I have two tables, customers (with columns A, B) and orders (with columns C,D,A; the last one a foreign key).
This query returns the data I’m interested in:
SELECT customers.A, customers.B, orders.C, orders.D
FROM customers, orders
WHERE customers.A = orders.A AND customers.B < 5 AND orders.D < 5
If I add LIMIT 10, I will get first ten 10 results (basically 10 orders), but what I want to do is to limit output based on the number of unique customers. So there would be exactly 10 unique customers in the result and therefore at least 10 – but likely more – orders. I think it’s possible to do that with a subquery but I can’t figure it out.
Something like this:
(Verified that sqlite successfully parses
LIMITin subqueries. Hopefully it doesn’t ignore thisLIMITsilently).