Image of SQL joins in 2 minutes

ADVERTISEMENT

Introduction

What is a join in SQL (Structured Query Language)? It is a SQL clause used to combine data from two different database tables. Through combining the tables, there is more data to draw conclusions from. This means that it's possible to find answers to more complicated questions when querying your dataset.

An example

For example, if we want to query a hotel database to find out how many transactions a guest has made inside the hotel, we can perform a JOIN on the guest table and the transactions table. Once we have a combined table, we can query the "guest number" or guest_id to find out the number of transactions.

Let's take a look at what this might look like when programming the SQL query:

select number_of_transactions
from guests
join transactions
   on guests.guest_id = transactions.guest_id
where guest_id = 2000

The above code returns the number of transactions from the guest inside the hotel. This is done by joining on the primary key guest_id between the two tables, setting them where condition (guest id = 2000), and then selecting the number_of_transactions field.

Combining data is powerful

SQL Joins are very powerful in finding out information. They are often used in the field of Data Science, Software development, and can be used to gather Business Intelligence (BI). There various types of joins that combine your data in different ways. W3Schools do a fantastic job of showing the different types of joins, providing examples to get your hands dirty while learning.

Remember, SQL can be only used to manipulate data inside a Relational Database Management Systems (RDBMS). The JOIN clause is unavailable if you are using an object database to hold your data. MySQL is a great choice when selecting a relational database, as it is trusted and used by major companies, Facebook, Google, and Adobe. These companies operate at a large scale and still see benefits in relational databases.

Summary

In this article, we discussed how joins work in SQL.

Next Steps

If you're interested in learning more about the basics of SQL, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.