×

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any similar records. If similar records are not found in the right table, the NULL keyword is returned in the row of the left table.

Syntax of the SQL Left Join

SELECT Column_Name1, Column_Name2, Column_Name3, Column_Name4 FROM Table_1 LEFT JOIN Table_2 ON Expression;  

Column_Name1, Column_Name2, Column_Name3, Column_Name4 columns from both the table.

Left Table: Table_1, Right Table: Table_2.

Expression: Condition expression with ON clause.

This conditional expression is a comparison condition of comparison operators and logical operators of SQL. Remember that Comparison Operators are >, <, >=, <=, =, !=, LIKE, BETWEEN, etc., and Logical Operators are AND, OR, and NOT.

There is one too many relationships between the Student and the Course table. One to Many relationships means that one Student can apply for one course or more than one course. When we execute the SQL Left Join query on these tables on Students_Id and Course_Id, all the records of the student table are displayed in the result set with similar records in the Course records.

Students who have not applied for any courses will be in the result set with the NULL keyword from the right side table.  

Example of Left Join in Structured Query Language:

Table Number 1: Student

Student_IdStudent_NameCityAge
1Pratik SrivastavPune23
2Utkarsh RokadeMumbai22
3Sourabh ChougaleNashik23
4Prateek ZimbrePune24
5Sakshi PatilAurangabad22
6Shruti SharmaMumbai21
7Pranoti ShendeAurangabad23
8Harshada DhanwatNashik24
9Tejas BairagiNashik21
10Nikhil PatilPune24
11Samaira SharmaMumbai22
12Anushka SenAurangabad23

Table Number 2: Course

Course_IdCourse_NameStudent_IdDuration
11Cloud Computing23
12SQL Database11
13Advance Java43
14Data Structures76
15AWS82
16Angular Js103
17Oracle Integration Cloud116
18Python61
19ReactJs13
20Computing64

Example 1: Execute a left joins query on the Student and Course table name.

SELECT S.Student_Id, S.Student_Name, S.City, S.Age, C.Course_Id, C.Course_Name, C.Duration FROM Student S LEFT JOIN Course C ON S.Student_Id = C.Student_Id;

The above left join query joins the employees' table and course table and retrieve the data from the tables where S.Student_Id = C.Student_Id.

The output of the above query is as follows:

Student_IdStudent_NameCityAgeCourse_IdCourse_NameDuration
1Pratik SrivastavPune2312SQL Database1
1Pratik SrivastavPune2319ReactJs3
2Utkarsh RokadeMumbai2211Cloud Computing3
3Sourabh ChougaleNashik23NULLNULLNULL
4Prateek ZimbrePune2413Advance Java3
5Sakshi PatilAurangabad22NULLNULLNULL
6Shruti SharmaMumbai2118Python1
6Shruti SharmaMumbai2120Computing4
7Pranoti ShendeAurangabad2314Data Structures6
8Harshada DhanwatNashik2415AWS2
9Tejas BairagiNashik21NULLNULLNULL
10Nikhil PatilPune2416Angular Js3
11Samaira SharmaMumbai2217Oracle Integration Cloud6
12Anushka SenAurangabad23NULLNULLNULL
SQL Left Join

Example 2: Execute a left join query on the Student and Course table name using the WHERE Clause.

SELECT S.Student_Id, S.Student_Name, S.City, S.Age, C.Course_Id, C.Course_Name, C.Duration FROM Student S LEFT JOIN Course C ON S.Student_Id = C.Student_Id WHERE City IN ('Pune', 'Mumbai', 'Aurangabad');

The above left join query joins the employees' table and manager table and retrieve the data from the tables where S.Student_Id = C.Student_Id. The Student records show students who reside in the 'Pune', 'Mumbai' and 'Aurangabad' cities. We have applied the WHERE clause condition on the City of the Student table.

The output of the above query is as follows:

Student_IdStudent_NameCityAgeCourse_IdCourse_NameDuration
1Pratik SrivastavPune2312SQL Database1
1Pratik SrivastavPune2319ReactJs3
2Utkarsh RokadeMumbai2211Cloud Computing3
4Prateek ZimbrePune2413Advance Java3
5Sakshi PatilAurangabad22NULLNULLNULL
6Shruti SharmaMumbai2118Python1
6Shruti SharmaMumbai2120Computing4
9Tejas BairagiNashik21NULLNULLNULL
10Nikhil PatilPune2416Angular Js3
11Samaira SharmaMumbai2217Oracle Integration Cloud6
12Anushka SenAurangabad23NULLNULLNULL
SQL Left Join

Example 3: Execute a left join query on the Student and Course table name using the ORDER BY Clause.

SELECT S.Student_Id, S.Student_Name, S.City, S.Age, C.Course_Id, C.Course_Name, C.Duration FROM Student S LEFT JOIN Course C ON S.Student_Id = C.Student_Id ORDER BY Student_Name;

The above left join query joins the employees' table and manager table and retrieves the data from the tables where S.Student_Id = C.Student_Id and the Student's records will display in the ascending order as we used the ORDER BY clause in the query.

The output of the above query is as follows:

Student_IdStudent_NameCityAgeCourse_IdCourse_NameDuration
12Anushka SenAurangabad23NULLNULLNULL
8Harshada DhanwatNashik2415AWS2
10Nikhil PatilPune2416Angular Js3
7Pranoti ShendeAurangabad2314Data Structures6
4Prateek ZimbrePune2413Advance Java3
1Pratik SrivastavPune2312SQL Database1
1Pratik SrivastavPune2319ReactJs3
5Sakshi PatilAurangabad22NULLNULLNULL
11Samaira SharmaMumbai2217Oracle Integration Cloud6
6Shruti SharmaMumbai2118Python1
6Shruti SharmaMumbai2120Computing4
3Sourabh ChougaleNashik23NULLNULLNULL
9Tejas BairagiNashik21NULLNULLNULL
2Utkarsh RokadeMumbai2211Cloud Computing3
SQL Left Join

Related Topics

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.