Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
INTERSECT is similar to an Inner Join. As with a UNION, they must have the same columns in both the left and right side of the SQL operation.
As with the other topics we’ve covered in this series, there is a wealth of information available on the topic of sets, databases, and SQL.
Check out this great read on Set Theory and SQL: “SQL-99 Complete, Really”, by Peter Guzman & Trudy Pelzer hosted on MariaDB
The link above contains more than just UNION, INTERSECT and EXCEPT, but there is a section towards the end that applies those operations to the larger discussion. This is a wonderful article!
Other good reads about Set Operations include:
Union and Union All:
See all of the SQL used in Querying Relational Databases in the Querying Relational Databases Cheat Sheet.
So far, we've seen the union and union all operations.
In this video, we're going to learn about the intersect operator.
Instead of bringing back all rows from both subsets like the union.
The intersect is only going to return the rows that exist in both.
Let's see our sample union again.
Here, it takes all products from the electronics and furnished tables and
creates one result cell.
If we perform an intersect on the same two tables, we get no results back.
[SOUND] This is because that is no overlap.
There's other piece of furniture that we might want to market
along with electronics.
The TV stand now if we perform an inspection operation.
We'll get the TV stand as the only row in our results set.
It's the only record that exists in both tables.
The Venn diagram of an intersection looks like this.
Now, let's perform an intersect operation in our car DB.
Remember, we have car manufacturers that live in two different tables.
Make and ForeignMake.
Let's see, if we can find the makes that exist in both tables.
We can do that by using the INTERSECT keyword.
Let's order them by name.
And for fun, let's see if we can order that in descending order.
What if we wanted to see the ID's along with the MakeNames
We get no records back, because with the ID's included, the rows no longer match.
Remember, that set operations use all the columns supplied within the query.
Since the ID values are different for each make from one to table to another.
The database treats the rows as different.
Like with unions, intersect statements include 2 sequel queries
with the keyword INTERSECT in between.
You need to sign up for Treehouse in order to download course files.Sign up