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. Learn more details and tips for using SQL JOIN types.
If you work with databases, you will likely need to use SQL JOINs at some point. This guide offers a quick overview of SQL JOINs and introduces you to some of the common types of JOINs.
Let's start with an overview of what a database is. A database is a collection of different tables storing different types of information. The JOIN clause retrieves data from related tables in a database. The SQL JOIN clause is more complex than a simple query that retrieves data from a single table because it retrieves data from multiple tables.
Depending on your desired results, you can choose among four types of SQL JOINs: 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:
Inner JOINs combine two tables based on a shared key. For example, if you had a table with a column called 'user id' and each user id was unique to a user, 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;
Left JOINs return all rows from the first table and only those 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
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
Full JOINs combine left and right joins by returning all rows from both tables as long as at least one match exists 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
There are many cases where SQL JOINs are crucial when mapping out relationships between tables in your database.
Imagine a table that stores personal information (name, address, phone number) and another that stores information about employee job positions. Suppose each row on the employee table represents a single employee. In that case, storing the employees' personal data in another table makes sense since an individual may be represented more than once (one row per position as they change roles).
You need to write an application that shows employee names and addresses, current and previous positions, and hire dates. To retrieve this data from the database, you need to join these two tables using some common attributes (such as Employee ID).
Imagine now that you have an online store and want to know which products your customers bought. You would have two tables: one containing information about your customers and another about your products. You can use an Inner JOIN to retrieve all the records that appear in both these tables using the following syntax:
Select * from the customer's Inner JOIN orders on customers.id = orders.customer_id;
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 of 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.
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 data tables (or queries).
A JOIN statement can be used with any other type of statement that SQL supports, including UPDATE and DELETE.
If you want to do SQL projects or get a job using SQL, you need to build your knowledge and skills. Make sure you learn from reliable materials. Check that the trainer or instructor has advanced competencies in SQL. Read reviews and analyse the coursework or learning structure.
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, 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.
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 a structured learning process and can be an excellent way to build knowledge.
There are plenty of SQL certifications for you to choose from. Certifications allow you to demonstrate to employers that you have passed an examination testing your SQL knowledge and can be particularly helpful if your CV doesn't contain much SQL experience.
If you want to learn more about SQL, consider taking one of the courses on Coursera. The Introduction to Structured Query Language (SQL) course offered by the University of Michigan is a good place to start your journey. By taking this course, you can learn how to create a MySQL database step by step and learn more about the SQL language.
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.