Key Concepts

Review core concepts you need to learn to master this subject

Inner Join

SELECT * FROM books JOIN authors ON books.author_id = authors.id;

The 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 ON clause. INNER JOIN is the default JOIN and it will only return results matching the condition specified by ON.

Outer Join

SELECT * FROM books JOIN authors ON books.author_id = authors.id;

An outer join will combine rows from different tables even if the join condition is not met. 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.

Primary Key in SQL

SELECT * FROM books JOIN authors ON books.author_id = authors.id;

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 JOIN operations.

Foreign Key in SQL

SELECT * FROM books JOIN authors ON books.author_id = authors.id;

A foreign key is a reference in one table’s records to the primary key from 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

SELECT * FROM books JOIN authors ON books.author_id = authors.id;

The 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 shirt_color and pants_color columns from the result set, which will contain all combinations of combining the rows in the shirts and 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

SELECT * FROM books JOIN authors ON books.author_id = authors.id;

The 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

SELECT * FROM books JOIN authors ON books.author_id = authors.id;

The 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 WITH keyword.

Multiple Tables
Lesson 1 of 1
  1. 1
    In 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 …
  2. 2
    Let’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…
  3. 3
    Combining 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…
  4. 4
    Let’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…
  5. 5
    What 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…
  6. 6
    Let’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…
  7. 7
    So 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…
  8. 8
    Sometimes 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…
  9. 9
    Often 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…
  10. 10
    In 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…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo