Master SQL Inner Join 3 Tables with Where Clause for Data Filtering

SQL inner joins are a fundamental part of data manipulation in relational databases. They allow us to combine rows from two or more tables, based on a related column between them. But what happens when you need to join three tables with additional filtering criteria? That's where the INNER JOIN combined with the WHERE clause comes in handy. Let's explore this through a practical example.

Give me 2 mins—I'll teach you how SQL JOINs work: The Join clause lets… | Neo Kim | 82 comments
Give me 2 mins—I'll teach you how SQL JOINs work: The Join clause lets… | Neo Kim | 82 comments

Consider you have three tables: 'Customers', 'Orders', and 'OrderItems'. You want to find all orders that have ordered more than 10 'ProductX'. To achieve this, we'll use an INNER JOIN to combine the tables and a WHERE clause to filter the results.

three vennuous circles with the words right table, left table and right table
three vennuous circles with the words right table, left table and right table

Setting up the Tables

The first step is to understand the structure of your tables. Let's assume your tables have the following structure:

SQL Joins Explained with Examples | Beginner SQL Guide
SQL Joins Explained with Examples | Beginner SQL Guide
Customers Orders OrderItems
CustomerID (PK) OrderID (PK) OrderID (FK)
CustomerName CustomerID (FK) ProductID (FK)
OrderDate Quantity
ProductName

Identifying the Relationships

SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners

The 'Orders' table acts as a connecting bridge between 'Customers' and 'OrderItems'. It references the 'CustomerID' from the 'Customers' table and has a one-to-many relationship with 'OrderItems' using 'OrderID'.

Now, let's move on to writing the SQL query.

Writing the SQL Query

SQL JOINS
SQL JOINS

The SQL query will look like this:

```sql SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN OrderItems ON Orders.OrderID = OrderItems.OrderID WHERE OrderItems.ProductName = 'ProductX' GROUP BY Orders.OrderID, Customers.CustomerName HAVING COUNT(OrderItems.ProductName) > 10; ```

Here's what the query does:

  • It joins the 'Orders', 'Customers', and 'OrderItems' tables.
  • The WHERE clause filters the results to only include rows where the 'ProductName' is 'ProductX'.
  • It groups the results by 'OrderID' and 'CustomerName'.
  • The HAVING clause filters the groups, only including those with more than 10 'ProductX'.
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples

The result of this query is a list of orders that have ordered more than 10 'ProductX', along with the customer's name.

Real World Applications

Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
an image of a notebook with the text 6 soljoins you should know
an image of a notebook with the text 6 soljoins you should know
SQL - Using where clause with not and between operators
SQL - Using where clause with not and between operators
SQL Joins Explained with Examples | Inner Join, Left Join, Right Join & Full Join
SQL Joins Explained with Examples | Inner Join, Left Join, Right Join & Full Join
SQL INNER JOIN Explained with Examples | Beginner SQL Tutorial
SQL INNER JOIN Explained with Examples | Beginner SQL Tutorial
a poster with the names and symbols for different types of letters
a poster with the names and symbols for different types of letters
How to Learn SQL JOINs
How to Learn SQL JOINs
What are Semi, Anti, and Natural Joins in SQL?
What are Semi, Anti, and Natural Joins in SQL?
SQL INNER JOIN Explained
SQL INNER JOIN Explained
Here's my Ultimate SQL Joins Cheatsheet: (Save this post now for your interview prep) Most candidates learn JOINs by memorizing patterns: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN. But… | Sohan Sethi
Here's my Ultimate SQL Joins Cheatsheet: (Save this post now for your interview prep) Most candidates learn JOINs by memorizing patterns: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN. But… | Sohan Sethi
a blue and white poster with the names of different types of letters on it's side
a blue and white poster with the names of different types of letters on it's side
What is a join in SQL?
What is a join in SQL?
SQL JOINs
SQL JOINs
SQL Joins Cheat Sheet Every Developer Should Save
SQL Joins Cheat Sheet Every Developer Should Save
Table join
Table join
Inner Join - Animated
Inner Join - Animated
SQL JOIN Cheat Sheet
SQL JOIN Cheat Sheet
SQL Joins Explained Better Than Most Tutorials
SQL Joins Explained Better Than Most Tutorials

Queries like this are incredibly useful in real-world scenarios. They allow you to analyze sales patterns, target marketing campaigns, and understand your customer's preferences.

However, remember that the ORDER BY clause is not included in our query. Depending on your needs, you might want to add it to sort the results by a specific column, such as 'OrderDate' or 'CustomerName'.

Always keep in mind that performance can be an issue with complex joins and aggregations. Make sure to create appropriate indexes on your tables to ensure your queries run efficiently.

In the ever-evolving world of data management, staying proficient in SQL skills is not just an advantage, it's a necessity. Happy coding!

Related Articles

W3 Sql Create Function W3 Sql Delete Column What Does Except Do In Sql W3 Sql Datediff When To Use Left Join And When To Use Inner Join W3 Sql Select Distinct W3 Sql Delete Row Join 3 Tables In Sql W3schools How To Use Except In Sql W3 Process