What does the following query pattern detect? SELECT * FROM orders o WHERE NOT EXISTS (SELECT 1 FROM returns r WHERE r.order_id = o.order_id)

SQL Data Analyst Medium

SQL Data Analyst — Medium

What does the following query pattern detect? SELECT * FROM orders o WHERE NOT EXISTS (SELECT 1 FROM returns r WHERE r.order_id = o.order_id)

Key points

  • The query uses a subquery to search for orders with no matching returns.
  • The NOT EXISTS clause filters out orders that have a corresponding return entry.
  • This helps identify orders that have not been returned.

Ready to go further?

Related questions