MySQL Joins - Explained with Examples

Demystify MySQL Joins! Elevate your database querying skills with practical examples and clear explanations, mastering the art of combining data tables seamlessly.

MySQL Joining Tables

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Let's look at a selection from the "Orders" table:

OrderIDCustomerIDOrderDate
1030821996-09-18
10309371996-09-19
10310771996-09-20

Then, look at a selection from the "Customers" table:

CustomerIDCustomerNameContactNameCountry
1Alfreds FutterkisteMaria AndersGermany
2Ana Trujillo Emparedados y heladosAna TrujilloMexico
3Antonio Moreno TaqueríaAntonio MorenoMexico

Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.

Then, we can create the following SQL statement (that contains an INNER JOIN), that selects records that have matching values in both tables:

Example

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID; 

and it will produce something like this:

OrderIDCustomerNameOrderDate
10308Ana Trujillo Emparedados y helados9/18/1996
10365Antonio Moreno Taquería11/27/1996
10383Around the Horn12/16/1996
10355Around the Horn11/15/1996
10278Berglunds snabbköp8/12/1996

Supported Types of Joins in MySQL

  • INNER JOIN: Returns records that have matching values in both tables
  • LEFT JOIN: Returns all records from the left table, and the matched records from the right table
  • RIGHT JOIN: Returns all records from the right table, and the matched records from the left table
  • CROSS JOIN: Returns all records from both tables

MySQL INNER JOIN  MySQL LEFT JOIN  MySQL RIGHT JOIN  MySQL CROSS JOIN

#mysql #sql 

MySQL Joins - Explained with Examples
6.90 GEEK