Loading lesson...
SQL operators nobody warned you about
SQL operators nobody warned you about
Topics covered: Correlated subqueries, EXCEPT and EXCEPT ALL, INTERSECT and INTERSECT ALL, UNNEST for arrays, SELECT Without FROM
The defining characteristic is the column reference that crosses query boundaries. The inner query references a column from the outer table alias, creating a dependency: the database cannot execute the subquery independently because it needs context from each outer row. Think of it as a nested loop in code, where the inner loop body runs once per iteration of the outer loop. Understanding Correlated Subqueries The two examples below show how the inner query can reference the outer row: first to
EXCEPT Usage Examples Basic Example When to Use EXCEPT Real-World Applications EXCEPT Scenarios Financial reconciliation is one of the most demanding use cases for set operations because regulatory requirements demand a complete and auditable account of every discrepancy between trading systems. Indexing the correlation columns used in reconciliation queries is critical because the queries run once per row and a missing index can turn a seconds-long query into a minutes-long one on large trade t
Basic Example Practical Usage When to Use INTERSECT Real-World Use Cases Finding users who exist in both your production database and your analytics warehouse (data validation). Identifying devices that appear in both active monitoring and incident reports (cross-system analysis). Discovering which product IDs exist in both inventory and sales logs (stock reconciliation). Any time you need "show me what's in both places" without complex join logic.
Unnesting Arrays Basic Example Input table with arrays stored in a column: Why We Need UNNEST Handling Edge Cases UNNEST with NULL/Empty UNNEST with String Data
Basic Example This performs simple math. No table needed. SQL acts like a calculator. Result: 6. This retrieves metadata from the database server itself; the current timestamp. Useful for logging when queries run or timestamping data pipeline operations. Practical Applications Why This Matters Which approach returns the current date in a standard format? This pattern has been available in SQL for decades, though it wasn't always so simple.