Left Join Example:
SELECT s.student_id, s.name, b.price
FROM students s LEFT JOIN books b
ON s.student_id = b.student_id
ORDER BY s.student_id ;
Students table:
STUDENT_ID | NAME | YEAR |
1 | STUDENT_1 | I |
2 | STUDENT_2 | II |
3 | STUDENT_3 | III |
4 | STUDENT_4 | IV |
5 | STUDENT_5 | I |
6 | STUDENT_6 | IV |
Books table:
BOOK_ID | STUDENT_ID | PRICE |
1 | 1 | 40 |
2 | 2 | 50 |
3 | 3 | 70 |
4 | 1 | 100 |
5 | 2 | 120 |
6 | 4 | 90 |
7 | 3 | 200 |
8 | 2 | 150 |
Left Join Result:
STUDENT_ID | NAME | PRICE |
1 | STUDENT_1 | 40 |
1 | STUDENT_1 | 100 |
2 | STUDENT_2 | 50 |
2 | STUDENT_2 | 120 |
2 | STUDENT_2 | 150 |
3 | STUDENT_3 | 70 |
3 | STUDENT_3 | 200 |
4 | STUDENT_4 | 90 |
5 | STUDENT_5 | |
6 | STUDENT_6 | |