AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

SQL LEFT JOIN


<< Previous Page
sql tutorial
Next Page >>
sql tutorial

SQL Tutorial » SQL LEFT JOIN

What is a SQL LEFT join?

The LEFT JOIN returns all records from the left table (table1), even if there are no matches in the right table.

This means that a left join returns all the values from the left table, plus matched values from the right table or NULL in case of no matching join predicate.

Syntax:

SELECT table_1.column1, table_2.column_2...
FROM table_1
LEFT JOIN table_2
ON table_1.common_field = table_2.common_field;

 

Let's look at the Table1 "Customer" table: We will have four columns as: "Customer_ID", "Name", "Age" and "Value_salary"

Customer_ID Name Age Value_Salary
1 Kent Ford 27 1900
2 Joveau Pierre 41 2200
3 Anderson Nime 34 1800
4 Martínez Alexandro 26 2700
5 Parker Table 31 1700

 

Table 2 − "Orders" Table is as follows.:

Orders_ID Date Customer_ID Amount
101 2019-10-08 12:14:00 1 2300
102 2019-10-08 12:22:00 3 2400
103 2019-10-08 12:31:00 4 1900
104 2019-10-08 12:37:00 4 1600
105 2019-10-08 01:20:00 4 2000

 

How do I join two tables with LEFT join? - Examples

Now, let us join these two tables Customer and Orders using the LEFT JOIN as follows:

SQL> SELECT Customer_ID, NAME, AMOUNT, DATE
FROM CUSTOMER
LEFT JOIN ORDERS
ON CUSTOMER.Customer_ID = ORDERS.Customer_ID;

Output displayed:

Order_ID Name Amount Date
1 Kent Ford 1900 2019-10-08 12:14:00
2 Joveau Pierre NULL NULL
3 Anderson Nime 2300 2019-10-08 12:22:00
4 Martínez Alexandro 2200 2019-10-08 12:31:00
4 Martínez Alexandro 2200 2019-10-08 12:37:00
4 Martínez Alexandro 2200 2019-10-08 01:20:00
5 Parker Table NULL NULL

Related subjects:
How to create database phpmyadmin? Select data form database Insert into SQL SQL update PSql delete row

Tags: SQL LEFT JOIN: example, vs right join, select, nultiple tables, vs left outer join, vs inner join, on multiple columns, with where clause, right join, duplicate row,
What is a SQL LEFT join?
Is Left join the same as left outer join?
How do I join two tables with LEFT join?
When to use left join vs Right join?

 

SQL operators to JOIN tables

Several operators can be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all be used to join tables.

 

SQL JOIN - WHERE clause vs. ON clause

WHERE clause and ON clause are not the same.

WHERE
SELECT *
FROM Orders
LEFT JOIN OrderLines ON OrderLines.OrderID=Orders.ID
WHERE Orders.ID = 12345

AND

SELECT *
FROM Orders
LEFT JOIN OrderLines ON OrderLines.OrderID=Orders.ID
AND Orders.ID = 12345

The first will return an order and its lines, if any, for order number 12345. The second will return all orders, but only order 12345 will have any lines associated with it.

 



SQL LEFT JOIN: example, vs right join, select, nultiple tables, vs left outer join, vs inner join, on multiple columns, with where clause, right join, duplicate row,
What is a SQL LEFT join?
Is Left join the same as left outer join?
How do I join two tables with LEFT join?
When to use left join vs Right join?
SQL LEFT JOIN - sql tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home - Database
SQL - Database
SQL Syntax
SQL Select Data
SQL Insert Data
SQL Update
SQL Delete Row
PHP mysqli_query()
SQL order
SQL Create Database
SQL Display Data
SQL Count()
SQL mysqli_connect
SQL create table
SQL Where And & Or
SQL like Syntax
SQL Between Operator
SQL IN Operator
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...