A Left Join in SQL may be a type of join that's used to recover information from two or more tables when there's a relationship between the tables. It is additionally known as a Left Outer Join. The LEFT Join returns all the rows from the left table (table1) and the matching rows from the proper table (table2).
Definition
Hari, a computer program engineer, was asked to create a web application for a huge company. The application was to store and recover client information from a database. The client information was stored in two separate tables - one for client data, and one for client orders. Since the client information was put away in two separate tables, he required to utilize a Left Join in SQL to recover the client information. The Left Join would return all the records from the left table (the client data table) indeed in the event that there were no matches within the right table (the client orders table). Let's offer assistance to him. A LEFT Join may be a sort of join in SQL that's utilized to recover information from two or more tables when there's a relationship between the tables. It is additionally known as a Left Outer Join.
Syntax:
SELECT table1.column1, table2.column2
FROM table1
LEFT JOIN table2
ON table1.column1 = table2.column2;
Example
Suppose we have two tables, ‘students’ and ‘courses’, where ‘students’ table has a column ‘course_id’ with a foreign key pointing to the ‘courses’ table. Now, we want to get the list of all the students along with the course name they have taken.
We can use LEFT JOIN to get this information. The following query will return the list of all the students along with the course name they have taken:
students.name | courses.name |
---|---|
John | Math |
Jane | Biology |
Alex | Chemistry |
SELECT [students.name](<http://students.name/>), [courses.name](<http://courses.name/>)
FROM students
LEFT JOIN courses
ON students.course_id = [courses.id](<http://courses.id/>);
Benefits:
Limitations:
Alternatives:
Common Mistakes:
Conclusion
Hari was able to successfully complete the web application he was tasked with developing. He utilized a Left Join in SQL to recover the client information from two separate tables and show it within the web application. The Left Join was essential for the application to operate appropriately, because it returned all the records from the cleared out table indeed in case there were no matches within the right table. With the successful usage of the Left Join, Hari was able to fulfill his assignment of creating the web application.
Key Takeaways:
Quiz
Answer: b. LEFT JOIN
Answer: c. RIGHT JOIN
Answer: D. FULL OUTER JOIN
Answer: a. INNER JOIN
Top Tutorials
Related Articles