JOIN clause allows for the return of results from more than one table by joining them together with other results based on common column values specified using an
INNER JOIN is the default
JOIN and it will only return results matching the condition specified by
An outer join will combine rows from different tables even if the join condition is not met. In a
LEFT JOIN, every row in the left table is returned in the result set, and if the join condition is not met, then
NULL values are used to fill in the columns from the right table.
A primary key column in a SQL table is used to uniquely identify each record in that table. A primary key cannot be
NULL. In the example,
customer_id is the primary key. The same value cannot re-occur in a primary key column. Primary keys are often used in
A foreign key is a reference in one table’s records to the primary key of another table. To maintain multiple records for a specific row, the use of foreign key plays a vital role. For instance, to track all the orders of a specific customer, the table
order (illustrated at the bottom of the image) can contain a foreign key.
CROSS JOIN Clause
CROSS JOIN Clause
CROSS JOIN clause is used to combine each row from one table with each row from another in the result set. This
JOIN is helpful for creating all possible combinations for the records (rows) in two tables.
The given query will select the
pants_color columns from the result set, which will contain all combinations of combining the rows in the
pants tables. If there are 3 different shirt colors in the
shirts table and 5 different pants colors in the
pants table then the result set will contain 3 x 5 = 15 rows.
UNION clause is used to combine results that appear from multiple
SELECT statements and filter duplicates.
For example, given a
first_names table with a column
name containing rows of data “James” and “Hermione”, and a
last_names table with a column
name containing rows of data “James”, “Hermione” and “Cassidy”, the result of this query would contain three
names: “Cassidy”, “James”, and “Hermione”.
WITH clause stores the result of a query in a temporary table (
temporary_movies) using an alias.
Multiple temporary tables can be defined with one instance of the
- 1In order to efficiently store data, we often spread related information across multiple tables. For instance, imagine that we’re running a magazine company where users can have different types of …
- 2Let’s return to our magazine company. Suppose we have the three tables described in the previous exercise – shown in the browser on the right (we are going to try something new!): - orders - subsc…
- 3Combining tables manually is time-consuming. Luckily, SQL gives us an easy sequence for this: it’s called a JOIN. If we want to combine orders and customers, we would type: SELECT * FROM orders JO…
- 4Let’s revisit how we joined orders and customers. For every possible value of customer_id in orders, there was a corresponding row of customers with the same customer_id. What if that wasn’t true…
- 5What if we want to combine two tables and keep some of the un-matched rows? SQL lets us do this through a command called LEFT JOIN. A left join will keep all rows from the first table, regardles…
- 6Let’s return to our example of the magazine subscriptions. Recall that we had three tables: orders, subscriptions, and customers. Each of these tables has a column that uniquely identifies each ro…
- 7So far, we’ve focused on matching rows that have some information in common. Sometimes, we just want to combine all rows of one table with all rows of another table. For instance, if we had a tab…
- 8Sometimes we just want to stack one dataset on top of the other. Well, the UNION operator allows us to do that. Suppose we have two tables and they have the same columns. table1: | pokemon | ty…
- 9Often times, we want to combine two tables, but one of the tables is the result of another calculation. Let’s return to our magazine order example. Our marketing department might want to know a b…
- 10In this lesson, we learned about relationships between tables in relational databases and how to query information from multiple tables using SQL. Let’s summarize what we’ve learned so far: - JOI…
Multiple Tables with REBU
It's time to build fluency in SQL fundamentals. In this next Pro Project, we're going to practice using multiple tables in SQL so you can hone your skills and feel confident taking them to the real world. Why? It's important to understand how to interact with multiple tables in a single query. What's next? A data analyst, ride sharing, more SQL. You got this!