Question: Needing some help with assignment 2 part of this assignment the data would all be used in SQL databae queries Users Table: CREATE TABLE Users

Needing some help with assignment 2 part of this assignment the data would all be used in SQL databae queries Users Table: CREATE TABLE Users ( user_id INT PRIMARY KEY, username VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL ); Tweets Table: CREATE TABLE Tweets ( tweet_id INT PRIMARY KEY, user_id INT NOT NULL, tweet_text VARCHAR(280) NOT NULL, timestamp DATETIME NOT NULL, FOREIGN KEY (user_id) REFERENCES Users(user_id) ); Followers Table: CREATE TABLE Followers ( follower_id INT PRIMARY KEY, follower_user_id INT NOT NULL, following_user_id INT NOT NULL, FOREIGN KEY (follower_user_id) REFERENCES Users(user_id), FOREIGN KEY (following_user_id) REFERENCES Users(user_id) ); Insert Data Let's insert five random users to the Users table: INSERT INTO Users (user_id, username, password) VALUES (1, 'JohnDoe', 'P@ssW0rd'), (2, 'JaneDoe', 'AVerySecurePassword'), (3, 'BobSmith', 'ILoveChicago'), (4, 'AliceJohnson', 'TestTest!'), (5, 'SamWilson', 'password2023'); INSERT INTO Tweets (tweet_id, user_id, tweet_text, timestamp) VALUES (1, 1, 'Just had the best burger ever! #yum #foodie', NOW()), (2, 1, 'Excited to see my favorite band in concert tonight!', NOW()), (3, 2, 'Working on a new project today. #programming #coding', NOW()), (4, 2, 'Just finished a great workout at the gym.', NOW()), (5, 3, 'Relaxing at the beach with a good book. #beachlife', NOW()), (6, 3, 'Trying out a new recipe for dinner tonight.', NOW()); INSERT INTO Followers (follower_id, follower_user_id, following_user_id) VALUES (1, 1, 2), (2, 1, 3), (3, 2, 1), (4, 2, 3), (5, 3, 1), (6, 4, 1), (7, 4, 3), (8, 5, 2), (9, 5, 4);

Query Data Ok, now that we have a few data points in our database, let's write a query to return all the tweets made by the user with the username of BobSmith:

SELECT t.tweet_id, u.username, t.tweet_text, t.timestamp FROM Tweets t INNER JOIN Users u ON t.user_id = u.user_id WHERE u.username = 'BobSmith'; Next, let's return all the users who follow BobSmith. Use the following query to list all the followers of BobSmith: SELECT u.username AS follower_username FROM Users u INNER JOIN Followers f ON u.user_id = f.follower_user_id INNER JOIN Users u2 ON u2.user_id = f.following_user_id WHERE u2.username = 'BobSmith'; This query joins the Users, Followers, and Users tables to retrieve the usernames of all the users who are following the user with the username 'BobSmith'. The query first joins the Users and Followers tables using the follower_user_id and user_id columns, then joins the resulting table with the Users table again using the following_user_id and user_id columns. The WHERE clause filters the results to only include the followers of the user with the username 'BobSmith'. The SELECT statement specifies which column to return: the username of the follower. Lab Assignment 2: Modify the above query to return all the people BobSmith follows. That would practically be the list of BobSmith's followees. Submit the query as part of your lab submission.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!