Social Network Relationship Analysis with SQL
This challenge asks you to simulate graph database queries using standard SQL. Social networks are inherently graph-structured, with users as nodes and relationships (friendships, follows, etc.) as edges. You'll be given a relational database schema representing a simplified social network and tasked with writing SQL queries to answer common graph-related questions.
Problem Description
You are provided with a relational database schema representing a social network. The schema consists of two tables: Users and Relationships. Your goal is to write SQL queries to retrieve information about the network, mimicking the functionality of a graph database.
Tables:
- Users:
user_id(INTEGER, PRIMARY KEY): Unique identifier for each user.name(VARCHAR): User's name.
- Relationships:
source_user_id(INTEGER, FOREIGN KEY referencing Users.user_id): The user initiating the relationship.target_user_id(INTEGER, FOREIGN KEY referencing Users.user_id): The user being targeted by the relationship.relationship_type(VARCHAR): The type of relationship (e.g., 'friend', 'follows', 'blocked').
What needs to be achieved:
You need to write SQL queries to answer the following questions, which are analogous to graph database traversals:
- Find all friends of a given user: Given a
user_id, return the names of all users who are friends of that user. - Find all users who follow a given user: Given a
user_id, return the names of all users who follow that user. - Find the mutual friends of two users: Given two
user_ids, return the names of all users who are friends with both of them. - Find the shortest path (in terms of relationship hops) between two users: Given two
user_ids, return a list ofuser_ids representing the shortest path between them. If no path exists, return an empty list. Assume only 'friend' relationships are used for path finding. - Find users who are 'friends' with a user who is 'friends' with another user: Given two
user_ids (user1 and user2), find all users who are friends with a user who is also friends with both user1 and user2.
Key Requirements:
- Queries must be valid SQL and executable against a standard relational database (e.g., PostgreSQL, MySQL, SQLite).
- Queries should be efficient and avoid unnecessary joins or subqueries where possible.
- The queries should accurately reflect the graph relationships defined in the schema.
Expected Behavior:
- Queries should return the correct results based on the provided input data.
- Queries should handle edge cases gracefully (e.g., user not found, no relationships, no path between users).
- The shortest path query should return the shortest path, not just any path.
Edge Cases to Consider:
- Users with no friends or followers.
- Users who are friends with themselves (self-loops).
- Cycles in the graph (e.g., A is friends with B, B is friends with C, C is friends with A).
- No path exists between two users.
- Duplicate relationships (e.g., two entries with the same source and target user IDs).
Examples
Example 1:
Input: user_id = 1
Query: Find all friends of user 1.
Output: [name1, name2]
Explanation: User 1 has 'friend' relationships with users with IDs name1 and name2.
Example 2:
Input: user_id = 3
Query: Find all users who follow user 3.
Output: [name3, name4]
Explanation: Users with IDs name3 and name4 have 'follows' relationships with user 3.
Example 3:
Input: user_id1 = 1, user_id2 = 2
Query: Find the mutual friends of users 1 and 2.
Output: [name5]
Explanation: User 5 is a friend of both user 1 and user 2.
Example 4:
Input: user_id1 = 1, user_id2 = 4
Query: Find the shortest path between users 1 and 4.
Output: [1, 3, 4]
Explanation: The shortest path from user 1 to user 4 is 1 -> 3 -> 4 (assuming user 3 is a friend of both 1 and 4).
Example 5:
Input: user_id1 = 1, user_id2 = 5
Query: Find users who are 'friends' with a user who is 'friends' with both user1 and user2.
Output: [name6]
Explanation: User 3 is friends with both user 1 and user 2. User 6 is friends with user 3.
Constraints
user_idwill always be a positive integer.relationship_typewill be one of: 'friend', 'follows', 'blocked'.- The database will contain at least 10 users and 20 relationships.
- The shortest path will be no longer than 10 hops.
- Queries should execute within 5 seconds on a moderately sized dataset (up to 1000 users and 5000 relationships).
Notes
- You are not required to create the database schema or populate it with data. Assume the schema exists and is populated.
- Focus on writing efficient and correct SQL queries.
- Consider using Common Table Expressions (CTEs) to improve query readability and modularity, especially for the shortest path query.
- The shortest path query is the most challenging and may require a recursive approach or iterative search. There are multiple valid approaches.
- For the shortest path, assume that the relationship type 'friend' is the only type that can be used to traverse the graph. Other relationship types are ignored.