PURE SQL RECOMMENDER SYSTEMS · NO PYTHON

Recommendation is a ranking decision—not a list of popular things.

A recommender connects evidence about users, items, time and context. SQL can construct those relationships directly: self-joins create graphs, aggregates learn statistics, windows rank candidates, and CTEs preserve the reasoning path.

01 · CANDIDATEWhat is eligible?
02 · EVIDENCEWhose behavior matters?
03 · SCOREHow is relevance estimated?
04 · RANKWhat constraints reshape the list?

SEVEN LIVE METHODS

One target user. Seven definitions of “relevant.”

User 1 has rated Space Odyssey = 5, Robot City = 4 and Quiet Letters = 1. Every method ranks the same three unseen candidates, so students can compare evidence rather than datasets.

Bayesian Popularity

GLOBAL · SHRINKAGE · COLD START

Shrink item means toward the global mean according to evidence count.

target user = 1
EXECUTED SQL
Select Run recommender SQL.

Co-occurrence Graph

BASKET · EDGE · TRANSITION

Modernizes the original KeyValue and probview flow into a positive-interaction graph.

target user = 1
EXECUTED SQL
Select Run recommender SQL.

Item-based Collaborative Filtering

ITEM × ITEM · ADJUSTED COSINE

Learns adjusted-cosine item similarity from co-rating users.

target user = 1
EXECUTED SQL
Select Run recommender SQL.

User-based Collaborative Filtering

USER × USER · NEIGHBOR DEVIATION

Finds similar users on overlapping items and aggregates neighbor deviations.

target user = 1
EXECUTED SQL
Select Run recommender SQL.

Content-based Profile

FEATURE VECTOR · COSINE

Builds a rating-weighted genre profile and scores candidates with cosine similarity.

target user = 1
EXECUTED SQL
Select Run recommender SQL.

Time-decayed Ranking

RECENCY · HALF-LIFE · DRIFT

Applies exponential decay when taste or inventory changes over time.

target user = 1
EXECUTED SQL
Select Run recommender SQL.

Hybrid Rank

POPULARITY × CONTENT · WEIGHTED RANK

Combines normalized popularity and content ranks with transparent weights.

target user = 1
EXECUTED SQL
Select Run recommender SQL.

SHARED TEACHING DATA

Small enough to audit every score. Rich enough to disagree.

The dataset is intentionally tiny. A student should be able to trace one recommendation back through every join and aggregate before scaling the pattern to retail events or MovieLens.

UserItemRating
115
124
131
214
225
241
335
344
354
412
434
455
524
545
564
ItemTitleContent features
1Space Odysseysci-fi, drama
2Robot Citysci-fi, action
3Quiet Lettersdrama
4Fast Trackaction
5Family Tabledrama, comedy
6Cosmic Jokesci-fi, comedy

KeyValue → graphview → probview → recommendation

The supplied retail SQL first reduces transactions to unique CustomerID–StockCode pairs. A self-join counts co-occurring products; item occurrence converts counts to conditional probabilities; the target customer’s owned products activate outgoing edges, and unseen destinations are ranked by summed probability.

What remains valuable

The graph is explainable: every recommendation has a path from something the customer already selected.

What the new page adds

Explicit positive-event policy, evidence count, modern CTEs, alternative models and ranking evaluation.

SYSTEM ARCHITECTURE

A model score is only one stage in a recommendation system.

A useful list also needs eligibility, deduplication, inventory or policy constraints, diversity, exploration and logging. Leaving these out creates a model demo—not a recommender system.

EVENTSview · rate · buy
CANDIDATESavailable · unseen
FEATURESuser · item · context
RANKERscore · blend · constrain
FEEDBACKimpression · action

EVALUATION & RESPONSIBILITY

A recommender changes what can be discovered.

Therefore evaluation must include ranking quality, system behavior and human consequences. High click-through can coexist with low diversity, fatigue or unfair exposure.

Offline ranking

TEMPORAL HOLDOUT

Precision@K · Recall@K · MAP · NDCG · MRR

Catalog behavior

BEYOND RELEVANCE

Coverage · diversity · novelty · serendipity · popularity concentration

Online outcomes

EXPERIMENT

CTR · conversion · revenue · retention · hide/dismiss rate

Guardrails

RESPONSIBLE RANKING

Exposure fairness · frequency caps · safety · inventory · latency

Three mistakes SQL cannot prevent for us

  • Randomly splitting future interactions into training leaks information; use a temporal cutoff.
  • Missing impressions make “not clicked” ambiguous: the user may never have seen the item.
  • Optimizing clicks alone can reinforce popularity and narrow the catalog users encounter.

CONTINUE

Understand ranking in SQL, then decide where it should operate.

Connect this lesson to Pure SQL ML, Declarative ML and Data Systems Labs.