Rakesh
Rakesh

Reputation: 4262

List Customers who have paid for all the orders or have not placed order

I am looking for most efficient query to fetch Customer name who have either paid for all the orders they have placed with us or have not placed order at all.Following is the table structure:

Customer to Order is one to many relationship.

Payment status can only be PAID OR UNPAID.

Following is sample data and result which I am expecting. Thanks alot! enter image description here

Upvotes: 0

Views: 856

Answers (1)

shawnt00
shawnt00

Reputation: 17915

select *
from customer c
where not exists (
    select 1 from "order" o
    where o.cust_id = c.cust_id and payment_status = 'UNPAID'
)

Upvotes: 3

Related Questions