1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

Joining Tables in MySQL guide with examples

  • ScientificTools.org
  • November 23, 2023 at 9:55 AM
  • 951 times read
Contents [hideshow]
  1. Understanding Joins in MySQL
  2. 1. INNER JOIN
    1. Example: Joining Two Tables
  3. 2. LEFT JOIN (LEFT OUTER JOIN)
    1. Example: Users and Their Orders
  4. 3. RIGHT JOIN (RIGHT OUTER JOIN)
    1. Example: Orders and Users
  5. 4. FULL OUTER JOIN
    1. Example: Simulating FULL OUTER JOIN
  6. 5. CROSS JOIN
    1. Example: Combining Products and Stores

Joins are one of MySQL's useful functions. This guide provides a complete overview of different types of joins in MySQL including INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN. For each type, we provide a practical example.

Understanding Joins in MySQL

A join in MySQL is used to combine rows from two or more tables based on a related column between them. The most common types of joins are:

  1. INNER JOIN
  2. LEFT JOIN (or LEFT OUTER JOIN)
  3. RIGHT JOIN (or RIGHT OUTER JOIN)
  4. FULL OUTER JOIN (not supported directly in MySQL)
  5. CROSS JOIN

1. INNER JOIN

An INNER JOIN selects records with matching values in both tables.

Example: Joining Two Tables

Imagine two tables: Users and Orders. To list all users who have placed an order:

SQL
SELECT Users.Name, Orders.OrderID
FROM Users
INNER JOIN Orders ON Users.UserID = Orders.UserID;

This query combines rows from Users and Orders where the UserID matches in both tables.

2. LEFT JOIN (LEFT OUTER JOIN)

A LEFT JOIN returns all records from the left table, and the matched records from the right table. If there's no match, the result is NULL on the right side.

Example: Users and Their Orders

To list all users and their orders, including users who haven't placed any orders:

SQL
SELECT Users.Name, Orders.OrderID
FROM Users
LEFT JOIN Orders ON Users.UserID = Orders.UserID;

Users without orders will show NULL for OrderID.

3. RIGHT JOIN (RIGHT OUTER JOIN)

A RIGHT JOIN returns all records from the right table, and the matched records from the left table. If there's no match, the result is NULL on the left side.

Example: Orders and Users

To list all orders and the users who placed them, including orders not yet linked to a user:

SQL
SELECT Users.Name, Orders.OrderID
FROM Users
RIGHT JOIN Orders ON Users.UserID = Orders.UserID;

Orders without a user will show NULL for Name.

4. FULL OUTER JOIN

MySQL doesn't natively support FULL OUTER JOIN, but you can simulate it using a combination of LEFT JOIN and RIGHT JOIN.

Example: Simulating FULL OUTER JOIN

To list all users and all orders, including unmatched records:

SQL
SELECT Users.Name, Orders.OrderID
FROM Users
LEFT JOIN Orders ON Users.UserID = Orders.UserID
UNION
SELECT Users.Name, Orders.OrderID
FROM Users
RIGHT JOIN Orders ON Users.UserID = Orders.UserID;

5. CROSS JOIN

A CROSS JOIN returns a Cartesian product of the two tables, meaning it combines each row of the first table with all rows in the second table.

Example: Combining Products and Stores

If you have Products and Stores tables and want to list all possible combinations:

SQL
SELECT Products.ProductID, Stores.StoreID
FROM Products
CROSS JOIN Stores;

This query combines each product with each store.

  • Previous Article Guide to GROUP BY and HAVING clauses in MySQL with examples
  • Next Article Practical guide for UNION and UNION ALL in MySQL with examples

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2026. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™