SQL JOIN Types Explained

Written by Coursera Staff • Updated on

The SQL JOIN is a command clause that combines records from two or more tables in a database. It is a means of combining data in fields from two tables by using values common to each table.

[Featured Image] SQL Developer in front of several monitors.

If you work with databases, you'll likely need to use SQL Joins to retrieve data from multiple tables at some point in your work. These impactful clauses allow you to get information from separate tables so that you get the right information you need to make the best possible decision.

In this guide, you'll learn more about SQL Joins, including its different types and use cases in the real world.

What is a Join in SQL?

In Structure Query Language (SQL), a Join is used to connect two or more records within a relational database. As their name suggests, relational databases organize data based on pre-established relationships, which define how data contained in one table relates to data contained within another (or several others).

The Join clause is used to retrieve data from related tables in a database. Because it retrieves data from multiple tables, however, the SQL Join clause is more complex than a simple query that retrieves data from a single table. 

Read more: Relational vs. Non-relational Database: The Difference Explained

Types of SQL Joins

There are many different use cases for SQL Joins, and they are crucial when mapping out relationships between tables in your database. There are four primary types of SQL Joins that you can use: Inner Join, Left Outer Join, Right Outer Join, and Full Outer Join. Take a look at how each works, along with some sample SQL Join clauses below:

1. Inner Join

Inner Joins combine two tables based on a shared key. For example, if you have a table with a column called "user id" and each user id is unique to each user, this you could join that table to another table with a "user id" column to find the information associated with each user. This example shows how to use an Inner JOIN clause to join two tables:

SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;

2. Left Outer Join

Left Outer Joins return all rows from the first table and only the rows in the second table that match. This example shows how to use a Left Outer Join clause to join two tables:

SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.id = table2.user_id

3. Right Outer Join

Right Joins are logically the opposite of Left Joins—they return all rows from the second table, and only the rows in the first table that match. This example shows how to use a Right Outer Join clause to join two tables:

SELECT * FROM table1 RIGHT OUTER JOIN table2 ON table1.id = table2.user_id

4. Full Outer Join

Full Joins combine both left and right joins by returning all rows from both tables, as long as there is at least one match between them. This example shows how to use a Full Outer Join clause to join two tables:

SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.user_id

What is Cross Join in SQL?

In SQL, Cross Join returns every possible combination of rows contained within one table with all of those contained within another table (producing what is known as a "Cartesian Product"). While Cross Join queries are effective for relatively small data sets, they can be overwhelming and inefficient with larger datasets.

Placeholder

SQL JOIN example uses

SQL Joins are critical to retrieving relevant information from relational databases. Perhaps unsurprisingly, then, there are countless ways that SQL Join clauses can be used to provide workers with actionable information when they need it. Below, we explore some real-world examples:

1. Employee database

Imagine a table that stores personal information (name, address, phone number) and another table that stores information related to employee job positions. Suppose each row on the employee table represents a single employee. In that case, it makes sense to store the employees' personal data in another table since an individual may be represented more than once (one row per position as they change roles).

Let's say that you need to write an application that shows employee names and addresses along with their current position, previous positions, and hire date. To retrieve this data from the database, you need to join these two tables together using some attributes common between them (such as Employee ID).

2. E-commerce sales tracking

Imagine now that you have an online store and want to know which products were bought by your customers. You would have two tables: one containing information about your customers and another containing information about your products. You can use an Inner Join to retrieve all the records that appear in both of these tables using the following syntax:

Select * from customers Inner JOIN orders on customers.id = orders.customer_id;

3. Student grade database

Consider a situation where you have two database tables, one called “Students” and the other called “Grades.” The “Students” table contains one record for each student: their ID number, name, major, and so on. The “Grades” table contains one record for each student's grade on different courses: their student ID number, the course they took, and their grade in the course.

In SQL, you would write a query to find the names of all students who have received a grade of 100 as follows:

  • SELECT Students.StudentName FROM Students.

  • JOIN Grades ON Students.StudentID=Grades.StudentID.

  • WHERE Grades.Grade=100.

Combining JOINs 

There are many ways to combine results from two or more queries. Here are the most common:

- Use a JOIN statement to combine data from multiple tables in one SELECT statement.

- Use a subquery to retrieve data from one table based on values from another table.

- Use a UNION statement to combine multiple tables (or queries) data.

- A JOIN statement can be used with any other type of statement that SQL supports, including UPDATE and DELETE.

Placeholder

Tips for learning more about SQL JOINs

If you're looking to do SQL projects or to get a job using SQL, you may need to build your knowledge and skills. Make sure you learn from reliable material, verify that your instructor has advanced competencies in SQL, and read reviews to see how others feel about the course. Here are some of the ways you might consider learning more about SQL:

1. Tutorials 

Many tutorials are available on the internet that can help you learn SQL. These tutorials are often free and provided by competent people in their field. Learning through tutorials requires some planning. If you choose this route, make sure you follow a logical learning structure to learn all the foundational building blocks for working with SQL. For example, you will need a solid understanding of databases.

2. Online courses

There are many online courses with which you can learn SQL. Some of these courses are free, and some charge a fee. Some of the paid courses are comprehensive and offer value for money. Courses provide you with a structured learning process and can be an excellent way to build knowledge. 

3. Certifications 

There are plenty of SQL certifications for you to choose from. Certificates allow you to demonstrate to employers that you have passed an examination testing your SQL knowledge and can be particularly helpful if your resume doesn't contain much SQL experience.

Start learning SQL on Coursera

If you want to learn more about SQL, consider taking one of the many courses, Specializations, and Professional Certificates on Coursera.

In the University of Michigan's Introduction to Structured Query Language (SQL) course, you'll learn how to create a MySQL database step-by-step and explore SQL in greater depth. In Google's Data Analytics Professional Certificate, meanwhile, you'll learn key analytical skills and tools like SQL, R programming, and Tableau.

Keep reading

Updated on
Written by:

Editorial Team

Coursera’s editorial team is comprised of highly experienced professional editors, writers, and fact...

This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.