FAQ: Multiple Tables - Combining Tables with SQL

This community-built FAQ covers the “Combining Tables with SQL” 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 Combining Tables with SQL

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!

For step 3.2 of 10 (using JOIN to write a second query looking just for ‘Fashion Magazine’), is there a difference in performance between:

SELECT *
FROM orders
JOIN subscriptions
	ON orders.subscription_id = subscriptions.subscription_id 
    AND subscriptions.description = 'Fashion Magazine';

and the answer in the Hint:

SELECT *
FROM orders
JOIN subscriptions
	ON orders.subscription_id = subscriptions.subscription_id 
WHERE subscriptions.description = 'Fashion Magazine';

I’m assuming WHERE is faster, but unsure.

3 Likes

Hey people,

I don’t know what i’m doing wrong…
Could someone help me?

Here is my code:

SELECT *
FROM orders
JOIN subscriptions
ON orders.subscription_id = subscriptions.subscription_id
WHERE subscriptions.description = ‘Fashion Magazine’;

1 Like

It took a little while to figure this one out. I think your query is correct but the code editor wants both queries to be in the editor at the same time like this:

4 Likes

This is misleading and not a practical step in my opinion. I don’t know why you would realistically keep the first section, and then repeat it below with an added WHERE clause.

After rereading the instructions it does say to “add a second query”, but within the flow of creating queries, I interpreted that as “keep the query you wrote, and now add a WHERE clause”.

2 Likes

I tested it, and you’re correct. You need both queries in the Codecademy environment for step 2 to work. This is probably designed to get us to type it all out again for additional practice.

2 Likes

hi, guys this is from 3/10. I am trying to be one step ahead by trying to add the ‘customer’ table and the a particular ‘customer’

I probably would have know how before this session ends but, still wanted to put my attempt out here and see if I can get a response soon. Thank you

When we do this and we join tables using the customer_id for example, are we actually matching up the customer ids even if they aren’t int he same order? Its not just equating the first row in one table with the first row in another table?

I feel like it must be matching but just want to check that is correct?

I think the verbage was a little confusing. I understood it wanted a second query. But it could also say, created a second query with another table. Basically, stacking the table instead of just joining the tables column to column.

1 Like

Does it matter what table you SELECT from and what table you JOIN? If they are being joined by the same column, would this

SELECT *
FROM orders
JOIN subscriptions
ON orders.subscription_id = subscriptions.subscription_id;

Be the same as this—
SELECT *
FROM subscriptions
JOIN orders
ON orders.subscription_id = subscriptions.subscription_id;

1 Like

добавил решение
SELECT *

FROM orders

JOIN subscriptions

ON orders.subscription_id = subscriptions.subscription_id;

SELECT *

FROM orders

JOIN subscriptions

ON orders.subscription_id = subscriptions.subscription_id

WHERE description = ‘Fashion Magazine’;

но переход к следующему заданию не проходит
пишет Did you remember to combine orders and subscriptions?

I came here to ask the exact same question!

How do I know which table I am in when working with multiple tables? In the “WHERE” statement, could I use orders.description = ‘Fashion Magazine’;?

SELECT *

FROM orders

JOIN subscriptions

ON orders.subscription_id

= subscriptions.subscription_id

WHERE subscriptions.description

= ‘Fashion Magazine’;

Hello,

On exercise 3/10, I’m noticing that the subscription_id column is duplicated. What is causing this? Is there a way to avoid it when joining tables? Thanks!

2 Likes

I also had this duplication of the subscription_id column. How do I make the duplicate columns actually join into the same column?

Something I don’t yet grasp is the hierarchy between joined tables.

When the exercise asks:

“Add a second query after your first one that only selects rows from the join where description is equal to ‘Fashion Magazine”,

and the query is:

“WHERE subscriptions.description = ‘Fashion Magazine’;”

It implies that the ‘subscription’s’ table is the title of the joined one, as in orders + subscriptions = subscriptions. Is this true?

Wouldn’t it make more sense to have WHERE joined.description = ‘Fashion Magazine’, or something along this?

I have the same question. It seems to me that the default hierarchy between tables follows FROM to JOIN, in other words, whatever you’re selecting from table FROM will be combined to table JOIN, so now JOIN is the reference table for the previous two separate tables.

In 3/10 on step 2 - Add a second query after your first one that only selects rows from the join where description is equal to ‘Fashion Magazine’.

I wanted to try to see if I cold only select rows that were ‘Fashion Magazine’ and ‘Sports Magazine’ (for example) and was not sure how to do this.

My original lines of code were correct and these were:

SELECT *
FROM orders
JOIN subscriptions
ON orders.subscription_id = subscriptions.subscription_id
WHERE subscriptions.description = ‘Fashion Magazine’
;

I played around with what I could add so that it selected rows from more than just ‘Fashion Magazine’ using AND and OR but was not getting any results. How would I go about specifying that I want to join the results from two different rows and not just one while using a WHERE command?

Thanks!