SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL OPERATORS SQL COMMANDS SQL Queries

SQL Table

SQL TABLE SQL CREATE TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL UPDATE TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT INTO Statement

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join

SQL OPERATOR

SQL Comparison SQL LOGICAL 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

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 KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter

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

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



ADVERTISEMENT
ADVERTISEMENT