Khan Academy "Project: App Impersonator" Help
(1) By jevonco on 2022-09-29 19:25:19 [link] [source]
Greetings!
I'm learning SQL/Database basics, I'm on the last project of the Khan Academy course and I'm looking for help on Joins.
I created two tables, and tried to join them based off of a related row. I'm confused on what makes a row "related". For instance, I thought the following SELECT would bring all of the rows together neatly and associated with each other, but it didn't:
SELECT * FROM tik_tok JOIN data ON tik_tok.id = data.account_id; Instead , the following SELECT statement worked:
SELECT * FROM tik_tok JOIN data ON tik_tok.id = data.id; I can't figure out why the second statement worked and did what I wanted over the first, and if my flawed code/logic would cause issue down the road. I'm adding the code here below:
CREATE TABLE tik_tok (ID INTEGER PRIMARY KEY AUTOINCREMENT, Username TEXT, age INTEGER);
CREATE TABLE data (ID INTEGER PRIMARY KEY AUTOINCREMENT, account_id INTEGER, followers INTEGER, following INTEGER, posts INTEGER, country TEXT);
INSERT INTO tik_tok (username, age) VALUES ("itzmctryhard", 24), ("KBS", 29), ("CrossPOINT", 19), ("narutoGoD", 39), ("plumbShER", 57), ("CHILLBOX", 85), ("Mdenise", 17); INSERT INTO data (account_id, followers, following, posts, country) VALUES (123, 987, 745, 12, "USA"), (124, 898, 2588, 34, "UK"), (125, 234, 876, 985, "Germany"), (126, 4869, 8574, 25897, "USA"), (127, 8574, 887, 78, "Germany"), (128, 5269, 8542, 8521, "Italy"), (129, 524128, 967452, 25241, "Canada"); SELECT * FROM tik_tok JOIN data ON tik_tok.id = data.id; SELECT * FROM tik_tok JOIN data ON tik_tok.id = data.account_id;
Can anyone help explain where I went wrong with my assumption that, "....ON tik_tok.id = data.account_id;" would bring up the results?
(2) By Chris Locke (chrisjlocke1) on 2022-09-29 20:58:49 in reply to 1 [link] [source]
In short, the numbers have to match. So in 'data', the account_id (123,124,etc) doesn't match any value in tik_tok. However, in 'data', the id (1,2,3...etc) matches the id in tik_tok.
This isn't exactly the right way to do it, but it does work.
Ideally, the tik_tok table would have an account_id field to, and its this that would link the two tables (both having the same values in them).
Happy to assist further ... welcome to your SQLite journey!
(3) By Keith Medcalf (kmedcalf) on 2022-09-29 21:00:29 in reply to 1 [source]
This (SQLite) is a RELATIONAL DATABASE. This means that tables which are "related" have the "same data duplicated" between the tables.
In the olden days (more than a century ago) relationships where maintained by having a "pointer" that "pointed" to the related thing. However, this means that if your "pointer"s got corrupted, there was no way to reconstruct the data.
As computers got more powerful and capable of storing more information (about a century ago) the RELEATIONAL data model was "invented" wherein, instead of using a "pointer" between related items, the data was duplicated.
So the question you need to consider is:
Does "tik_tok.id" contain exactly the same duplicated data as "data.account_id", such that the duplication indicates that the tik_tok row is "related" to the data row?
You seemed to be assuming that they are, but discovered that they are not.
The source of your assumption is probably the problem.