Cập nhật Sql Left Join Keyword – A Comprehensive Guide To Left Join In Sql mới nhất

The SQL LEFT JOIN joins two tables based on a common column, and selects records that have matching values in these columns and remaining rows from the left table.

Bạn đang xem: Sql left join

Bạn Đang Xem: Cập nhật Sql Left Join Keyword – A Comprehensive Guide To Left Join In Sql mới nhất

Example

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersLEFT JOIN OrdersON Customers.customer_id = Orders.customer;
Here”s how this code works:

*

Example: SQL LEFT JOIN

Here, the SQL command selects customer_id and first_name columns (from the Customers table) and the amount column (from the Orders table).

And, the result set will contain those rows where there is a match between customer_id (of the Customers table) and customer (of the Orders table) along with all the remaining rows from the Customers table.

Syntax of LEFT JOIN

The syntax of LEFT JOIN is:

SELECT columnsFROM table1LEFT JOIN table2ON table1.column_name = table2.column_name;

LEFT JOIN With WHERE Clause

The SQL command can have an optional WHERE clause with the LEFT JOIN statement. For example,

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersLEFT JOIN OrdersON Customers.customer_id = Orders.customerWHERE Orders.amount >= 500;Here, the SQL command joins two tables and selects rows where the amount is greater than or equal to 500.

SQL LEFT JOIN With AS Alias

We can use AS aliases inside LEFT JOIN to make our snippet short and clean. For example,

SELECT C.cat_name, P.prod_titleFROM Categories1 AS CLEFT JOIN Products AS PON C.cat_id= P.cat_id;Here, the SQL command selects common rows between Category and Products table.

Xem Thêm : Cập nhật Kinh nghiệm chọn ngân hàng hỗ trợ cho vay vốn khởi nghiệp tốt nhất mới nhất

Xem thêm: Nhuộm Ombre Không Tẩy Tóc – 18 Màu Tóc Nhuộm Ombre Cho Nữ Đẹp Nhất Hiện Nay

Left Join Vs Other Joins

The LEFT JOIN selects the common rows as well as all the remaining rows from the left table. Whereas the INNER JOIN selects only the common rows between two tables.

Let”s take a look at example,

LEFT JOIN

Xem Thêm : Rất Hay: Mỹ phẩm Phương Anh

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersLEFT JOIN OrdersON Customers.customer_id = Orders.customer;Output

Example: SQL LEFT JOIN Output

INNER JOIN

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersINNER JOIN OrdersON Customers.customer_id = Orders.customer;Output

*

Example: SQL INNER JOIN Output

The LEFT JOIN selects the common rows as well as all the remaining rows from the left table. Whereas the RIGHT JOIN selects the common rows as well as all the remaining rows from the right table.

Let”s take a look at example,

LEFT JOIN

Xem Thêm : Rất Hay: Mỹ phẩm Phương Anh

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersLEFT JOIN OrdersON Customers.customer_id = Orders.customer;Output

Example: SQL LEFT JOIN Output

RIGHT JOIN

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersRIGHT JOIN OrdersON Customers.customer_id = Orders.customer;Output

*

Example: SQL RIGHT JOIN Output

The LEFT JOIN selects the common rows as well as all the remaining rows from the left table. Whereas the FULL OUTER JOIN selects all the rows from both the tables.

Let”s take a look at example,

LEFT JOIN

Xem Thêm : Rất Hay: Mỹ phẩm Phương Anh

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersLEFT JOIN OrdersON Customers.customer_id = Orders.customer;Output

Example: SQL LEFT JOIN Output

FULL OUTER JOIN

SELECT Customers.customer_id, Customers.first_name, Orders.amountFROM CustomersFULL OUTER JOIN OrdersON Customers.customer_id = Orders.customer;Output

Nguồn: https://longchien.vn
Danh mục: Thủ Thuật

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *