Course contentsShow
← Computer Science
Lesson 1754 of 2,87239. NoSQL and Modern DatabasesFree lesson

Social Network and Recommendation Queries

Modeling and querying friend networks, followers, and recommendation engines using graph traversals.

Social Network and Recommendation Queries

What you'll learn: How to model and query friend networks, followers, and recommendation systems using graph database traversals.

Social networks are perfect graph database use cases. Each person is a node, and relationships like "follows," "friends_with," or "likes" are edges. Graph databases excel at answering questions that would require complex, slow joins in relational databases.

Friend-of-Friend Queries

Finding "people you may know" means traversing relationships two or three hops away. In Cypher (from earlier lessons), you can express this naturally:

MATCH (me:User {id: 123})-[:FRIENDS_WITH]->(friend)-[:FRIENDS_WITH]->(fof)
WHERE NOT (me)-[:FRIENDS_WITH]->(fof)
RETURN fof.name, COUNT(*) AS mutualFriends
ORDER BY mutualFriends DESC
LIMIT 10

This finds friends-of-friends you're not already connected to, ranked by mutual connections—a classic recommendation pattern.

Follower Networks

Directional relationships model followers (asymmetric, unlike friendship). Finding influencers or discovering trending content uses path traversal to count incoming edges or measure reach:

MATCH (user:User)-[:FOLLOWS]->(influencer)
RETURN influencer.name, COUNT(user) AS followerCount
ORDER BY followerCount DESC

Content Recommendations

Recommendation engines combine traversals with scoring. "Users who liked X also liked Y" queries traverse from item → users → other items, counting co-occurrences:

MATCH (me)-[:LIKED]->(item)<-[:LIKED]-(other)-[:LIKED]->(recommendation)
WHERE NOT (me)-[:LIKED]->(recommendation)
RETURN recommendation, COUNT(*) AS score
ORDER BY score DESC

This collaborative filtering approach finds items popular among users with similar tastes—naturally expressed as graph patterns.

Key Takeaway: Graph databases turn complex social network and recommendation problems into intuitive traversal queries, efficiently exploring multi-hop relationships that would be prohibitively expensive in relational systems.