For working professionals
For fresh graduates
More
SQL Tutorial: Learn Structured…
1. SQL Tutorial
2. The Essential Guide To Understanding SQL In DBMS
3. SQL Commands
4. SQL Data Types
5. SQL Aliases
6. SQL INSERT INTO With Examples
7. Master SQL Update
8. SQL Delete Statement: A Complete Overview with Examples
9. SQL Delete Statement Example
10. SQL WHERE Clause
11. SQL AND Operator
12. SQL NOT Operator: A Comprehensive Guide
13. SQL Like
14. SQL Between Operator: A Complete Overview with Examples
15. Difference Between SQL and MySQL: Get to Know Your DBMS
16. MySQL Workbench
17. A Comprehensive Guide to MySQL Workbench Installation and Configuration
18. Mastering SQL: Your Comprehensive Guide to Becoming an SQL Developer
19. SQL CREATE TABLE With Examples
20. How To Add Columns In SQL: A Step-By-Step Guide
21. Drop Column in SQL: Everything You Need to Know
22. Index in SQL
23. Constraints in SQL: A Complete Guide with Examples
24. Schema in SQL
25. Entity Relationship Diagram (ERD) - A Complete Overview
26. Foreign Key in SQL with Examples
27. An Ultimate Guide to Understand all About Composite Keys in SQL
28. Normalization in SQL
29. Better Data Management: The Efficiency of TRUNCATE in SQL
30. Difference Between DELETE and TRUNCATE in SQL
31. SQL ORDER BY
32. SQL Not Equal Operator
33. SQL Intersect Operator: A Comprehensive Guide
34. SQL Union: Explained with Examples
35. SQL Case Statement Explained with Examples
36. Unleashing the CONCAT Function In SQL: String Manipulation Made Easy
37. Understanding and Mastering COALESCE in SQL
38. NVL in SQL
39. Understanding SQL Date Formats and Functions
40. DateDiff in SQL: A Complete Guide in 2024
41. SQL Wildcards
42. SQL DISTINCT: A Comprehensive Guide
43. LIMIT in SQL: A Comprehensive Tutorial
44. SQL Aggregate Functions
45. GROUP BY in SQL
46. SQL HAVING
47. EXISTS in SQL
48. SQL Joins
49. Inner Join in SQL
50. Left Outer Join in SQL
51. Full Outer Join in SQL
52. Cross Join in SQL
53. Self Join SQL
54. Left Join in SQL
Now Reading
55. Mastering SQL Substring
56. Understanding the ROW_NUMBER() Function in SQL
57. Cursor in SQL
58. Triggers In SQL
59. Stored Procedures in SQL
60. RANK Function in SQL
61. REPLACE in SQL
62. How to Delete Duplicate Rows in SQL
63. Transact-SQL
64. INSTR in SQL
65. PostgreSQL vs MySQL: Explore Key Differences
66. Mastering SQL Server Management Studio (SSMS): A Comprehensive Guide
67. Auto-Increment in SQL
68. Unveiling the Power of SQL with Python
69. SQL Vs NoSQL: Key Differences Explained
70. Advanced SQL
71. SQL Subquery
72. Second Highest Salary in SQL
73. Database Integrity Constraints: Everything You Need to Know
74. Primary Key In SQL: A Complete Guide in 2024
75. A Comprehensive Guide on View in SQL
76. Understanding PostgreSQL: A Complete Tutorial
77. SQL Injection Attack
78. MySQL database
79. What is SQLite
80. SQLite
81. ALTER Command in SQL
In the realm of SQL, Joining data from multiple tables is one of the most important features and hence, joins play a pivotal role in combining data from multiple tables. One of the most commonly used joins is the left join. This article explorates the left join in SQL in detail, covering its definition, syntax, practical examples, and usage scenarios.
A left join, also known as a left outer join, is a join operation that returns all records from left table and the matched records from right table. If there is no match found in the right table, NULL values are returned for the columns of the right table.
The LEFT JOIN command in SQL is a specific type of JOIN operation. The way a LEFT JOIN works is it returns all the rows from the left table (i.e., the table mentioned first in JOIN clause) and any matching row from the right table (i.e., the table mentioned second in the JOIN clause). If there is no match, the result is NULL on the right side.
Components of a Left Join:
There are several key components to understand when performing a LEFT JOIN operation:
Consider a real-world scenario: You manage a large department store, and you have two tables - 'Employees' and 'Sales.' The 'Employees' table contains information about all your employees, and the 'Sales' table contains information about all sales transactions.
Now, suppose you want to see a list of all employees and any sales they've made, but you also want to include employees who haven't made any sales. A LEFT JOIN operation would be the perfect solution in this case.
Let’s dive into the syntax of Left Join in SQL
Here is the basic syntax for performing an inner join in SQL:
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
In this syntax:
Examples
In this example, we will alias the tables and perform a left join to retrieve the names of employees and their corresponding department names. Let's consider two tables, 'Employees' and 'Departments', with the following data.
Employees Table
EmployeeID | Name | DepartmentID |
---|---|---|
1 | John Doe | 101 |
2 | Jane Smith | 102 |
3 | Mary Johnson | 103 |
4 | James Brown | NULL |
Departments Table
DepartmentID | DepartmentName |
---|---|
101 | HR |
102 | IT |
103 | Sales |
104 | Marketing |
Now, let's say you want to create a list of all employees along with their department names. For employees who don't belong to any department, the department name should be NULL. Here, you would use a LEFT JOIN operation with aliases for table names.
SELECT E.Name, D.DepartmentName
FROM Employees AS E
LEFT JOIN Departments AS D
ON E.DepartmentID = D.DepartmentID;
Output:
In this query, 'E' and 'D' are aliases for the 'Employees' and 'Departments' tables, respectively. This makes the query more concise and easier to read. The result of this query would be:
Name | DepartmentName |
---|---|
John Doe | HR |
Jane Smith | IT |
Mary Johnson | Sales |
James Brown | NULL |
The WHERE clause in SQL is used to filter records, selecting only those that meet certain criteria. The WHERE clause can be used with SQL commands like SELECT, UPDATE, DELETE, etc. When combined with the LEFT JOIN operation, the WHERE clause can be used to further refine the results of the join.
Let's continue with the 'Employees' and 'Departments' tables from the previous example. Let's say you want to create a list of all employees who either belong to the 'Sales' department or do not belong to any department. Here, you would use a LEFT JOIN operation along with a WHERE clause.
The SQL query could look something like this:
SELECT E.Name, D.DepartmentName
FROM Employees AS E
LEFT JOIN Departments AS D
ON E.DepartmentID = D.DepartmentID
WHERE D.DepartmentName = 'Sales' OR D.DepartmentName IS NULL;
Output:
In this query, the LEFT JOIN operation combines rows from 'Employees' and 'Departments' tables based on 'DepartmentID' column. The WHERE clause then filters the results, selecting only those rows where the 'DepartmentName' is either 'Sales' or NULL.
Name | DepartmentName |
---|---|
Mary Johnson | Sales |
James Brown | NULL |
Left join in SQL is a powerful tool for retrieving data from multiple tables, especially when it's necessary to include all records from the left table regardless of whether there are matching records in the right table. By understanding its syntax, examples, and application scenarios, database professionals can effectively utilize left joins to meet their data retrieval needs.
Let's discuss some frequently asked questions:
Q. What is the difference between left and right join in SQL?
A. The difference lies in the handling of unmatched rows. A left join returns all rows from left table and the matching rows from right table, whereas a right join returns all rows from right table and matching rows from the left table.
Q. What is the left outer join (+) in SQL?
A. The left outer join (+) in SQL is used to return all rows from left table and the matched rows from right table. If there is no match, the result is NULL on the right side.
Q. What is the difference between join and left join?
A. A join, also known as inner join, returns only the matching rows from both right and left tables, whereas a left join returns all the rows from the left table, whether they have a match in the right table or not.
Q. Why LEFT join is used?
A. LEFT JOIN is used when you need to return all the records from the left table and the matched records from the right table, even if there are no matches in the right table.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.