This directory contains examples of SQL set operations: UNION, INTERSECT, and EXCEPT.
Demonstrates various set operations:
- UNION - Combine results removing duplicates
- UNION ALL - Combine results keeping duplicates
- INTERSECT - Find common rows between queries
- EXCEPT - Find rows in first query but not in second
- Multiple UNION operations
- UNION with aggregations
- UNION with WHERE filters
# Run all examples
php 01-set-operations.php
# Or use the test script
cd ../..
./scripts/test-examples.sh- MySQL 8.0+: Full support
- PostgreSQL: Full support
- SQLite 3.8.3+: Full support
Combines results from two queries and removes duplicates:
$db->find()
->from('table1')
->union(fn($qb) => $qb->from('table2'))
->get();Combines results keeping all rows including duplicates:
$db->find()
->from('table1')
->unionAll(fn($qb) => $qb->from('table2'))
->get();Returns only rows that appear in both queries:
$db->find()
->from('table1')
->intersect(fn($qb) => $qb->from('table2'))
->get();Returns rows from first query that don't appear in second:
$db->find()
->from('table1')
->except(fn($qb) => $qb->from('table2'))
->get();- ORDER BY, LIMIT, and OFFSET apply to the final combined result
- All queries must have the same number of columns
- Column types should be compatible across queries