FAQ: Multiple Tables - Union

This community-built FAQ covers the “Union” 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 Union

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!

About the condition with the same amount of columns in the tables: what if we only choose 2 columns per table to union but the original tables don’t have the same amount of columns, will it work?
let’s say the table newspaper has 5 columns and the table subscription has 3. if i only pick 2 per table for the union is that possible?
like:
select id, first_name
from newspaper
union
select id, start_month
from subscription;

1 Like

Can someone please clarify for me the following: Are all of the queries in this exercise just that “queries” or do some actually change the tables, columns or data? Such as cross join or union, etc…

About the rules-

  • Tables must have the same number of columns.
  • The columns must have the same data types in the same order as the first table.

Both tables must also have the same column names?

1 Like

What if you want to ‘Union’ multiple tables, considering all tables meet requirements, do you have to type the ‘union’ title of each table?

This might be a stupid question but why is it that when we JOIN two tables it is sufficient to simply SELECT * to display the resultant joined table, like so:

SELECT *
FROM table1
INNER JOIN table 2

However for a union, the method changes such that we have to specify SELECT * twice, like this:

SELECT *
FROM table1
UNION
SELECT *
FROM table2

If SELECT can be used to display the resultant joined table, why can it be used similarly like so:

SELECT *
FROM table1
UNION table2

This would follow the syntax that we use in JOIN.

1 Like

What is the difference between Union and Cross Join? They both look the same to me but they were introduced as different things so I know they are not supposed to be the same.

1 Like

How would we create a new column with all the same values before we UNION the two data sets?

In this example, we might want to know whether the subscription was online or newspaper, so how would we add a new column called subscription_type to newspaper and label every row as “newspaper” in that column; and in the online table, add a column where every row has an “online” label?

That way, when you UNION both data sets, we know which data came from where in case you need to reference it later. It seems like we should use this statement:

ALTER TABLE table_name
ADD COLUMN subscription_type text;

And then can you UPDATE the table to have all values of ‘Online’ for subscription_type? I feel like I have done this before and I can’t quite remember how to do it.

I have the same question. Is this simply a syntactic quirk of SQL? Or is there some particular reason the JOIN syntax would cause problems? For instance, it’s not quite clear to me what the difference is between a UNION and a CROSS JOIN. Although given that this question was asked 3 years ago I wonder if there will be an answer…

Were you ever able to find an answer to this question?

Queries such as Select generate a result that is held in memory. The source data tables are not affected. Certain SQL commands, such as Insert, Update, Delete do change the source data table.