×

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 ><>=<==, !=LIKEBETWEEN, etc., and Logical Operators are ANDOR, 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

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 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 CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

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.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.