FAQ: Multiple Tables - Primary Key vs Foreign Key

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Primary Key vs Foreign Key” exercise from the lesson “Multiple Tables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise Primary Key vs Foreign Key

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why I have to use JOIN and not LEFT JOIN?

Do you understand the difference between (inner) join and left join? Knowing the differences can help

left join selects all rows from the left and table and the matching records from the right table
inner join select all rows which match from both tables

Translating to the exercise at hand: left join would give all classes, even the classes with no students, while inner join while only give the classes with students (at least one student has to be in the class for the row to be returned by the query)

1 Like

It’s inconvenient when you enter a simple query to see what’s on the tables (if they don’t automatically appear in the viewer), follow that with what the instructions request, are returned an error saying that you didn’t follow the instructions, take 30 minutes trying to figure out what you did wrong without using the hint, only to realize that you were correct on the first attempt yet was told you were incorrect because you still had the initial query up.

You’d think that the system wouuld be optimized in a way that acknowledges your correct input while ignoring irrelevant (albeit correctly typed) queries that came before it.

1 Like

Sorry, mistake on my part on a bug. Not a bug.

In the example, Why are we using classes.id = students.class_id?
Instead of classes.class_id = students.class_id ? Is this because class_id in the students table is a foreign key and has to be more descriptive?

classes table doesn’t have a class_id column?

including the table name makes the code more readable in my opinion.

is the primary key always the left most column in a table? is there any way to recognize a primary key 100%?

Although a common practice, based on position is not a good way to determining primary key. Simple connect to the database and run a command to get the primary key or use GUI tool, they often can also display which column is marked as primary

Hello, i have a question,
the lesson’s description says that:

Primary keys have a few requirements:

  1. None of the values can be NULL.

and

  1. Each value must be unique (i.e., you can’t have two customers with the same customer_id in the customers table).

But i’ve tried adding some code here and have a different result for this description.
I added and had:

INSERT INTO students (id, class_id)
VALUES (NULL, 999);

INSERT INTO classes (id)
VALUES (999), (999);


SELECT *
FROM students
JOIN classes
  ON students.class_id = classes.id;

and at the end of the table it outputs:

So i’ve got the NULL in the “students.id” and same values in “classes.id” in their tables where they are primary keys.

Why is that, is it because we work in SQLite or something?

Thank you!

1 Like

Probably not strictly related to what was being discussed … but I feel this is a useful add for anyone who might be a better visual learner here’s a really simple primer on Primary and Foreign key … This guy does amazing math explanations too !!!

6 Likes

Yes, this is definitely a useful video so thanks for posting, @ngwolfhare.

I was actually looking for a video to better explain this process as I’m having a bit of challenge trying to relate (contextualize) the concepts I’m learning in the SQL track, and this video provides some clarity and context here.

2 Likes

Sometimes text just doesn’t do it justice … Glad wasn’t alone :partying_face: it was also after being a little bit of a YouTube coding tutorial nerd it was certainly the best explained.

1 Like

Why in the exercise says that id its primary key of classes table and then says that ‘class_id’ is the primary key of classes?

This exercise could be bit more consistent with wording/phrasing.

id is generally the name of the primary key, while class_id is generally the foreign key name. So you know which table the foreign key is referencing.

2 Likes

i learned more in that sentence than the entire lessons phrasing and wording. thank you.

1 Like

First they told us, that primary keys in both tables are called “id” but then when I try to join it this way I get an error. I checked the solution and now it tells me that the primary key in students is called class_id. What the heck, why is it so inconsistent? Why does it suddenly change names?

If we look at the columns in students table, we see the following:

id 	first_name 	last_name 	email 	class_id

id is still the primary key, but the table also has a foreign key (class_id)

the foreign key often reference a primary key on a different table.

Also, he refers to a schema here as a schematic diagram and suddenly it made sense to me, whereas the word schema meant nothing…

@xalava i believe yours being an interesting question. have you already figured out why that´s happening ?
does anybody more expert know the answer?