GimmeJob
Sign in
Databases, SQL & BI · SQL · Chapter 14 / 17

SQL learning path

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

SQL
SQLite · sample DB · browser sandbox
Result
Run the query to see the result.

Always define a deterministic tie-breaker.

Detecting duplicates

SQL
SQLite · sample DB · browser sandbox
Result
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

SQL
SQLite · sample DB · browser sandbox
Result
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

SQL
SQLite · sample DB · browser sandbox
Result
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.

SQL
SQLite · sample DB · browser sandbox
Result
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.

Source registry

3 chapter references · Under review
PostgreSQL 18 — Queries

Detailed reference for FROM/WHERE/GROUP BY/HAVING, set operations, sorting, LIMIT/OFFSET, CTEs, recursive queries, and query processing.

PostgreSQL Global Development Group · official documentation
Source ↗
PostgreSQL 18 — Window Functions

Primary reference for OVER, PARTITION BY, window ordering, ranking, running calculations, and the difference between windows and aggregation.

PostgreSQL Global Development Group · official documentation
Source ↗
SQLite — SELECT

Runtime-aligned reference for SELECT grammar and behavior in the in-browser SQLite practice environment.

SQLite · official documentation
Source ↗