Advanced query patterns
Advanced SQL is usually a combination of fundamentals rather than a new language. The key is to define the desired grain and tie-breaking rules explicitly.
Latest row per entity
Run the query to see the result.
Always define a deterministic tie-breaker.
Detecting duplicates
Run the query to see the result.
A duplicate query is only meaningful when the business key is correctly defined.
Keeping one duplicate and identifying extras
Run the query to see the result.
Do not delete duplicate-looking rows until the survivor rule and dependent relationships are understood.
Detecting sequence gaps
Run the query to see the result.
This is useful for event streams, imported records, telemetry, and ordered processing pipelines.
Keyset pagination
OFFSET pagination asks the database to skip rows and becomes unstable when data changes between pages. Keyset pagination says “give me the next rows after the last key I saw.”
For descending (created_at, id), the next-page predicate compares against both values. Exact tuple-comparison syntax is engine-specific, but the core rule is to preserve the complete sort key.
Expected versus actual reconciliation
EXCEPT can identify missing/mismatched expected rows and the reverse EXCEPT can identify unexpected actual rows.
Run the query to see the result.
Running state from an event/ledger stream
Window SUM, LAG/LEAD, and cumulative counts let you derive state while retaining every event. This pattern appears in financial ledgers, monitoring, test execution history, and audit trails.
Relational division / “has all required things”
Problems such as “customers who bought every required category” can be solved with nested NOT EXISTS or grouping/count comparisons. The important step is to define the required set and prove there are no missing members.
Practice: find the latest event per user rather than per entity, then compare the result with latest per entity and explain why the grains differ.