DEV Community

Wendy Calderon
Wendy Calderon

Posted on ā€¢ Edited on

8 3

SQL: Inner Join

In SQL we have various powerful clauses that make managing databases possible, but one that usually brings confusion along with it's functionality, is the JOIN clause.

The JOIN clause is used in a relational database to select records from two or more tables that share at least one column in common. There are different ways of joining tables, and in this post we will be discussing, through the use of an example, the one corresponding to the INNER JOIN.

INNER JOIN will compare the specified tables and select those rows that have matching values. It could be considered the default JOIN type, since we would get the same result without specifying the keyword INNER.

It's syntax is the following:

Inner Join syntax

In the SELECT clause, we specify the columns we want to select, by chaining them to the table they correspond. Pro-tip: we only do this for columns that are not specific to one table. If we want to select a column that is specific to just one table, we don't have to specify the table name.

Now let's explain how it works, based on an example. The following image shows the tables to be considered.
Alt Text
In this example, we see that in the pets table we have a pet_id column that is common to the checkups table. The pet_id column in the pets table is referred to as Primary Key, and the one in the checkups table as Foreign Key.

The next image graphically explains the result of joining these two tables with the INNER clause, referencing the Venn Diagram.

Alt Text

Now, considering the tables above, if we wanted to get our pets names, their species and the date they had a checkup, the query would look like this:

Example query

Let's break it down: We select the columns we want, and since they are unique columns to each of the tables, we don't have to chain them to their tables. We select them from pets, and join the checkups table on the column both tables have in common (remember, the INNER keyword is optional). We could've selected from checkups and joined pets, as long as we join them on their common field, we would be getting the same result.

In a following post, we'll discuss the other ways available to join tables. See you there!

E.T.A.: The following post, where we discuss Outer Joins, can be found here.

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry šŸ‘€

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more ā†’

Top comments (4)

Collapse
 
katnel20 profile image
Katie Nelson ā€¢

What if both tables had a column called 'name'. How would you select the name of the checkups table?

Collapse
 
wendisha profile image
Wendy Calderon ā€¢

Good question Katie.
When a column name is not unique to a table, you would chain it to it's table name, so it would be SELECT checkups.name FROM ...

Collapse
 
katnel20 profile image
Katie Nelson ā€¢

Makes perfect sense. Thanks Wendy.

Thread Thread
 
wendisha profile image
Wendy Calderon ā€¢

Glad to help! :)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

šŸ‘‹ Kindness is contagious

Please leave a ā¤ļø or a friendly comment on this post if you found it helpful!

Okay