Skip to content

Commit 96f890e

Browse files
authored
Merge pull request #3717 from Apoorva-Atre/newbranch
Added SQL-Left-Join.md in SQL under docs
2 parents bbd3d0a + b86f7f5 commit 96f890e

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/SQL/SQL-Left-Join.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
id: sql-left-join
3+
title: Left Join in SQL
4+
sidebar_label: Left Join
5+
sidebar_position: 13
6+
tags: [sql, database, operation]
7+
description: In this tutorial, we will learn about left joins in sql.
8+
---
9+
10+
## What is a left join?
11+
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.
12+
13+
## Syntax
14+
15+
```sql
16+
select *
17+
from table_1 left join table_2
18+
on table_1.col=table_2.col;
19+
```
20+
21+
##### Note that the columns of table_1 and table_2 in the on clause must be the same attribute.
22+
23+
## Example
24+
25+
Consider the following tables:
26+
27+
```sql
28+
select * from students;
29+
+---------+-----------+
30+
| stud_id | stud_name |
31+
+---------+-----------+
32+
| 101 | Shreeya |
33+
| 102 | Aakash |
34+
| 103 | Mansi |
35+
| 104 | Aditya |
36+
+---------+-----------+
37+
38+
select * from grades;
39+
+---------+-------+
40+
| stud_id | grade |
41+
+---------+-------+
42+
| 101 | A |
43+
| 104 | A+ |
44+
+---------+-------+
45+
```
46+
47+
Now , lets try to obtain a result using left join.
48+
49+
```sql
50+
select s.stud_id, s.stud_name, g.grade
51+
from students s left outer join grades g
52+
on s.stud_id=g.stud_id;
53+
54+
Output:
55+
+---------+-----------+-------+
56+
| stud_id | stud_name | grade |
57+
+---------+-----------+-------+
58+
| 101 | Shreeya | A |
59+
| 102 | Aakash | NULL |
60+
| 103 | Mansi | NULL |
61+
| 104 | Aditya | A+ |
62+
+---------+-----------+-------+
63+
```
64+
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.
65+
66+
## Conclusion
67+
In this tutorial, we learnt how to use the left outer join with an example.
68+
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.

0 commit comments

Comments
 (0)