Definition: Returns only the rows where there is a match in both tables.
Use Case: When you only need data that exists in both tables.
SELECT p.firstName, p.lastName, a.cityFROM Person pINNER JOIN Address a ON p.PersonId = a.PersonId;
2. LEFT JOIN (LEFT OUTER JOIN)
Definition: Returns all rows from the left table and the matched rows from the right table. If there is no match, NULL is returned for columns from the right table.
Use Case: When you need all data from the left table regardless of matches in the right table.
SELECT p.firstName, p.lastName, a.cityFROM Person pLEFT JOIN Address a ON p.PersonId = a.PersonId;
3. RIGHT JOIN (RIGHT OUTER JOIN)
Definition: Returns all rows from the right table and the matched rows from the left table. If there is no match, NULL is returned for columns from the left table.
Use Case: When you need all data from the right table regardless of matches in the left table.
SELECT p.firstName, p.lastName, a.cityFROM Person pRIGHT JOIN Address a ON p.PersonId = a.PersonId;
4. FULL OUTER JOIN
Definition: Returns all rows from both tables. When there is no match, NULL is returned for columns from the table without a match.
Use Case: When you need all records from both tables, whether or not they have matches.
SELECT p.firstName, p.lastName, a.cityFROM Person pFULL OUTER JOIN Address a ON p.PersonId = a.PersonId;
5. CROSS JOIN
Definition: Returns the Cartesian product of the two tables, meaning every row from the first table is joined with every row from the second table.
Use Case: Rarely used, but useful for generating combinations of rows from two tables.
SELECT p.firstName, a.cityFROM Person pCROSS JOIN Address a;
6. SELF JOIN
Definition: Joins a table to itself, which can be useful for comparing rows within the same table.
Use Case: When you need to find relationships within a single table.