Reading:  

Beginners guide to MySQL and MariaDB


JOINS in MySQL

In MySQL JOINS are used for joining two more tables to get result as single table. The JOINS can be used with SELECT, UPDATE and DELETE statement.

Up till now we have seen selecting the data from a single table, but in many cases you would want to get data from multiple tables with one query.

MySQL joins can be very complex depending on how you are storing your data. There are multiple type of joins such as 

  1. INNER JOIN
  2. OUTER JOIN
  3. LEFT JOIN
  4. STRAIGHT JOIN etc

More details on MySQL joins can be found from this page

But let's briefly check what a JOIN can do

INNER JOIN

Let's take a looks at couple of tables below

First table is an Orders table with data below

OrderIDCustDOrderDateProduct
10308 1 2014-09-18 iPad 2
10309 1 2015-01-19 iPad 3
10310 2 2015-06-20 iPhone 6 Plus

Second table is our Customers table with data shown bleow

CustIDNameCountry
1 Alex Germany
2 David New Zealand
3 Scott Australia

 

As you can see that Customer and Orders table has One to Many relationship, what that means is that One customer may have more than One orders, in our case Alex has 2 orders. 

If you want to get Customer Orders, this is where you would want to JOIN 2 tables.

Query

SELECT Customers.CustID, Customers.Name, Orders.OrderID, Orders,Product 
FROM  Orders INNER JOIN Customers
ON Orders.CustID=Customers.CustID;

Above query will return the following output

CustIDNameOrderIDProduct
1 Alex 1 iPad 2
1 Alex 2 iPad 3
2 David 3 iPhone 6 Plus

Above is an example for INNER JOIN. INNER JOIN return all rows from multiple tables where the join condition is met.

LEFT JOIN

The LEFT JOIN returns all rows from the left table, In the query below, left table is Customers, with the matching rows in the right table, In our case the right table is Orders. If there is no match to right table then NULL is returned instead

Here is a our query to explain the logic

Query

SELECT Customers.CustID, Customers.Name, Orders.OrderID, Orders,Product 
FROM  Customers LEFT JOIN Orders
ON Orders.CustID=Customers.CustID;

Above query will return the following data

CustIDNameOrderIDProduct
1 Alex 1 iPad 2
1 Alex 2 iPad 3
2 David 3 iPhone 6 Plus
3 Scott NULL NULL

This is especially helpful if you want to check in one hit if certain customer ever purchased from your store. 

Description

In this tutorial, we will cover few topics that will give you a heads on start to build your knowledge on. Topics that we will cover briefly but still providing enough information are listed below

  • Overview
  • Installing on Linux and Windows
  • Some useful admin queries for starters
  • Connection
  • Create Database
  • Drop Database
  • Select Database
  • Data Type
  • Create Table
  • Drop Table
  • Inserting and Selecting data
  • Where Clause
  • Updating and deleting data
  • Like Clause
  • Sorting Result
  • Using Joins
  • Brief introduction to Regex, Transactions and Indexes
  • Alter Command
  • Temporary Tables
  • Database Info
  • Using Sequence
  • Database Export and Import
  • Resetting MySQL/MariaDB Administrator password


Audience

Absolute beginners looking to get a sneak peak into what MySQL. Please remember that this is not a full on guide but a quick introduction to the subject.

Learning Objectives

Get to know MySQL and MariaDB

Author: Subject Coach
Added on: 23rd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!