Reputation: 2975
I'm confused about this example in this tutorial page. http://www.postgresqltutorial.com/postgresql-subquery/
SELECT
first_name,
last_name
FROM
customer
WHERE
EXISTS (
SELECT
1
FROM
payment
WHERE
payment.customer_id = payment.customer_id
);
Could you please tell me the point of the subquery?
I understand that EXISTS converts the result set to a boolean "true" if there's at least one result returned from the subquery. But in the WHERE clause of the sub query, it would seem like it would always be "true", so a row will always be selected, so the EXISTS will always be "true".
Was that example meant to do this in the subquery?
WHERE
customer.customer_id = payment.customer_id
Also, I assume that a subquery that is part of the WHERE will run once for every "customer". Is that right?
Thanks.
Upvotes: 2
Views: 273
Reputation: 43434
You are absolutely right. That seems to be a typo in the PostgreSQL documentation... and quite a confusing one, by the way.
Regarding the last question, thinking of it running for each customer is a good approach too.
Upvotes: 4