Unisys Interview Question

What are the different types of joins in SQL?

Interview Answer

Anonymous

Nov 30, 2024

In SQL, joins are used to combine rows from two or more tables based on a related column. The common types of joins are: INNER JOIN: Returns records that have matching values in both tables. Example: SELECT * FROM TableA INNER JOIN TableB ON TableA.id = TableB.id LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table, and the matched records from the right table. Unmatched rows from the right table will have NULL values. Example: SELECT * FROM TableA LEFT JOIN TableB ON TableA.id = TableB.id RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table, and the matched records from the left table. Unmatched rows from the left table will have NULL values. Example: SELECT * FROM TableA RIGHT JOIN TableB ON TableA.id = TableB.id FULL JOIN (or FULL OUTER JOIN): Returns all records when there is a match in either the left or right table. Unmatched rows from both tables will have NULL values. Example: SELECT * FROM TableA FULL JOIN TableB ON TableA.id = TableB.id CROSS JOIN: Returns the Cartesian product of the two tables, meaning every row in the first table is combined with every row in the second table. Example: SELECT * FROM TableA CROSS JOIN TableB SELF JOIN: A table is joined with itself to compare rows within the same table. Example: SELECT A.*, B.* FROM TableA A, TableA B WHERE A.id = B.parent_id