Data Science Explorer

[Basic Grammar] SQL LEFT JOIN 본문

SQL

[Basic Grammar] SQL LEFT JOIN

grace21110 2023. 10. 18. 20:25
반응형

The LEFT JOIN returns all records from the left table (table 1). In other words, if there is no match with table 2, the result is 0 records from the right side. 

 

  • Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

 

Example 

SELECT Brands.BrandName, Orders.OrderID 
FROM Brands 
LEFT JOIN Orders ON Brands.BrandsID = Orders.OrderID 
ORDER BY Brands.BrandName;

 

Exercises 

Write an SQL query to retrieve a list of all employees and their respective departments. Use a LEFT JOIN to include employees who may not yet be assigned to a department.

SELECT Employees.EmployeeName, Department.DepartmentName 
FROM Employees 
LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

'SQL' 카테고리의 다른 글

[Basic Grammar] SQL FULL OUTER JOIN  (0) 2023.10.18
[Basic Grammar] SQL RIGHT JOIN  (0) 2023.10.18
[Basic Grammar] SQL INNER JOIN  (0) 2023.10.18
[Basic Grammar] SQL Aliases  (0) 2023.10.17
[Basic Grammar] SQL BETWEEN Operator  (0) 2023.10.16