Test whether a subquery returns at least one row.
SELECT c.id FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);Test whether a subquery returns at least one row.
WHERE EXISTS (SELECT 1 FROM ... WHERE correlated_condition)Test whether a subquery returns at least one row.
SELECT c.id FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);SQLite-oriented example; consult the relevant dialect manual.
SELECT c.id FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);| Option / clause | Description |
|---|---|
SELECT / expression | Choose output columns, expressions, aliases or functions. |
FROM / JOIN | Choose source tables and combine related rows. |
WHERE | Filter rows before grouping; use IS NULL for null checks. |
GROUP BY / HAVING | Group rows and filter aggregate groups. |
ORDER BY | Sort the result; ASC is commonly the default and DESC reverses it. |
LIMIT / OFFSET / FETCH | Limit or skip rows; syntax differs by database dialect. |
Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.