T-SQL Tutorial

SQL Left Join


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_IDNAMEYEAR
1STUDENT_1I
2STUDENT_2II
3STUDENT_3III
4STUDENT_4IV
5STUDENT_5I
6STUDENT_6IV

Books table:

BOOK_IDSTUDENT_IDPRICE
1140
2250
3370
41100
52120
6490
73200
82150

Left Join Result:

STUDENT_IDNAMEPRICE
1STUDENT_140
1STUDENT_1100
2STUDENT_250
2STUDENT_2120
2STUDENT_2150
3STUDENT_370
3STUDENT_3200
4STUDENT_490
5STUDENT_5
6STUDENT_6