Everyone knows “item-based collaborative filtering” (ItemCF): Amazon recommendations, YouTube watch-next, etc. Here’s how to implement it in PostgreSQL using the MovieLens dataset. No Python, just SQL.
Theory in one minute
ItemCF recommends items similar to what a user already likes. You need:
A user–item rating log (user_id, item_id, rating). Behavior logs (view, click, favorite, purchase) can be weighted into pseudo-ratings.
An item–item similarity matrix. For items i and j:
wij=∣N(i)∣∣N(j)∣∣N(i)∩N(j)∣
where N(i) is the set of users who liked i. If many users like both, the items are similar. Represent the matrix as a table of triples (i, j, similarity).
To predict user u’s preference for item j:
puj=i∈N(u)∑wjirui
In practice we limit the sum to the top-K similar items per item.
That’s it: a basic ItemCF pipeline entirely inside PostgreSQL. From here you can add time decay, normalize ratings, or materialize similarity tables per business need, but the foundation is just a handful of SQL statements.