Ctr

Group Chat

For this challenge, suppose we are working on an app similar to Facebook Messenger, called "Footbook Messenger".

The Footbook users are in a table called fb_users. Of course, people can add other users as friends. The friendships are a junction table called friendships.


fb_users
column_name type
id
INTEGER
name
VARCHAR
username
VARCHAR
email
VARCHAR
phone_number
VARCHAR

friendships
column_name type reference
friend_id_1
INTEGER
fb_users.id
friend_id_2
INTEGER
fb_users.id

One important difference for this junction table compared to our Tweeter example is that friendships are always mutual. For Tweeter, people followed one another. One person may follow another without them following back. If they do follow back, that is a separate row in the junction table.

Now, consider friendships. If user ID 100 is friends with user ID 101, whose ID goes in the friend_id_1 column and whose in friend_id_2? When you join the tables, you need to know which ID goes where to write your join condition. There are multiple ways to solve this. One simple way is to just add both rows. So, when user 100 friends 101, just add both (100, 101) and (101, 100) as rows in the friendships table. This makes writing queries easier. That's how this example is done.

We have yet another table for our app: phone_contacts. These are for the imported contacts that the Messenger app asks for when you install it on your phone.


phone_contacts
column_name type reference
user_id
INTEGER
fb_users.id
contact_name
VARCHAR
NULL
phone_number
VARCHAR
NULL

We are working on building a group chat feature for our Messenger app. The feature allows you to have group chats with your friends on Footbook and also your contacts on your phone. The app will reach your Footbook friends via their username, and your phone contacts via their phone number.

Write a query that will fetch a list of potential group chat members for the user with username Reyes22. You'll need to fetch both her friends from the fb_users table and the contacts in the phone_contacts table. Your results should have 2 columns:

  1. A column called contact_info which is the username if the candidate is a Footbook Friend or a phone number if it's a phone contact.
  2. A column called source which is the name of the table each candidate came from, 'friends' or 'phone_contacts'.

© 2022 Andrew Carlson. All rights reserved.

contact_info
VARCHAR
source
TEXT
1
David_Gislason
friends
2
1-866-534-1692
phone_contacts
3
242-671-8306
phone_contacts
4
1-576-432-0145
phone_contacts
5
509-775-6829
phone_contacts
6
Edgardo.Turcotte27
friends
7
Maximillia3
friends
7 rows

You haven't solved this challenge yet!
Are you sure you want to reveal the answer?