SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

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