KnowledgeBoat Logo
|

Informatics Practices

How is a left join different from a natural join ? Give example.

SQL Joins & Grouping

1 Like

Answer

In a LEFT JOIN, all rows from the left table are included along with matching rows from the right table, and NULL values are used for non-matching rows in the right table. Conversely, a natural join is a join where only one of the identical columns from the joined tables exists.

1. LEFT JOIN Example:

SELECT name, lastname
FROM empl LEFT JOIN dept ON empl.id = dept.id;

The result of this query would return name and lastname values from the empl table and all available values from the dept table. NULL is returned for non-existing values in dept table.

2. NATURAL JOIN Example:

SELECT * FROM empl NATURAL JOIN dept;

The query returns all columns from empl and dept tables, with rows having matching values in their common columns.

Answered By

3 Likes


Related Questions