capypad
0 day streak
sql / expert
Snippet

Multi-Column Comparison with Row Value Constructors

Row Value Constructors (Tuples) allow for comparing multiple columns as a single unit. This prevents the 'logic leak' where individual column matches (A or B) create false positives that a combined check (A and B) avoids.

snippet.sql
sql
1
2
3
4
5
SELECT *
FROM inventory_moves
WHERE (warehouse_id, shelf_id) IN (
SELECT w_id, s_id FROM critical_zones
);
Breakdown
1
WHERE (warehouse_id, shelf_id)
Defines a row constructor consisting of two columns to be treated as a composite value.
2
IN (SELECT w_id, s_id ...)
Checks for the existence of the composite pair within the result set of the subquery.