Skip to content

Added SQL-Left-Join.md in SQL under docs #3717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions docs/SQL/SQL-Left-Join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
id: sql-left-join
title: Left Join in SQL
sidebar_label: Left Join
sidebar_position: 13
tags: [sql, database, operation]
description: In this tutorial, we will learn about left joins in sql.
---

## What is a left join?
A left join of two tables, say table 1 and table 2, would return all the rows from the left table and matched values from table 2. If for a particular row in table 1 there is no matching entry in table 2, 'null' is returned.

## Syntax

```sql
select *
from table_1 left join table_2
on table_1.col=table_2.col;
```

##### Note that the columns of table_1 and table_2 in the on clause must be the same attribute.

## Example

Consider the following tables:

```sql
select * from students;
+---------+-----------+
| stud_id | stud_name |
+---------+-----------+
| 101 | Shreeya |
| 102 | Aakash |
| 103 | Mansi |
| 104 | Aditya |
+---------+-----------+

select * from grades;
+---------+-------+
| stud_id | grade |
+---------+-------+
| 101 | A |
| 104 | A+ |
+---------+-------+
```

Now , lets try to obtain a result using left join.

```sql
select s.stud_id, s.stud_name, g.grade
from students s left outer join grades g
on s.stud_id=g.stud_id;

Output:
+---------+-----------+-------+
| stud_id | stud_name | grade |
+---------+-----------+-------+
| 101 | Shreeya | A |
| 102 | Aakash | NULL |
| 103 | Mansi | NULL |
| 104 | Aditya | A+ |
+---------+-----------+-------+
```
Here we can see that the output contains the entry of student id's 102 and 103 even though they are not assigned any grade, i.e., they are not present in the 'grades' table.

## Conclusion
In this tutorial, we learnt how to use the left outer join with an example.
Left outer joins are used when we want to retrieve all the rows from the left(1st) table, irrespective of it being in the right(2nd) table.
Loading