wasAsked.

What are the different types of JOINs in SQL?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · SQL

Community answers

Posted by wasAsked editorial collection

A JOIN combines rows from two or more tables based on a related column. Example tables: Employees(Id, Name, DepartmentId) Departments(Id, Name) 1. INNER JOIN Returns only rows with matching values in both tables. SELECT e.Name, d.Name AS Department FROM Employees e INNER JOIN Departments d ON e.DepartmentId = d.Id; Employees without a department, and departments without employees, are excluded. 2. LEFT JOIN (LEFT OUTER JOIN) Returns all rows from the left table plus matching rows from the right; non-matches have NULLs. SELECT e.Name, d.Name AS Department FROM Employees e LEFT JOIN Departments d ON e.DepartmentId = d.Id; Every employee appears; those without a department show NULL. Finding rows with no match (anti-join): SELECT e.Name FROM Employees e LEFT JOIN Departments d ON e.DepartmentId = d.Id WHERE d.Id IS NULL; (NOT EXISTS is an equally good, often clearer alternative.) 3. RIGHT JOIN (RIGHT OUTER JOIN) All rows from the right table plus matching rows from the left. SELECT e.Name, d.Name FROM Employees e RIGHT JOIN Departments d ON e.DepartmentId = d.Id; Every department appears, even with no employees. In practice, many teams rewrite right joins as left joins by swapping table order for readability. 4. FULL JOIN (FULL OUTER JOIN) All rows from both tables; NULLs where there is no match on either side. SELECT e.Name, d.Name FROM Employees e FULL OUTER JOIN Departments d ON e.DepartmentId = d.Id; Useful for reconciliation reports (finding records missing on either side). MySQL does not support FULL JOIN directly; combine LEFT and RIGHT joins with UNION. 5. CROSS JOIN Cartesian product: every row of the first table combined with every row of the second. SELECT s.Size, c.Color FROM Sizes s CROSS JOIN Colors c; Useful for generating combinations (all size and colour variants); dangerous on large tables (rows multiply). 6. SELF JOIN A table joined to itself, using aliases — for example employees and their managers: SELECT e.Name AS Employee, m.Name AS Manager FROM Employees e LEFT JOIN Employees m ON e.ManagerId = m.Id; Related concepts - CROSS APPLY and OUTER APPLY (SQL Server) or LATERAL joins (PostgreSQL): join each row to the result of a table-valued expression, such as "top 3 orders per customer". - Equi-joins use equality; non-equi joins use other conditions (BETWEEN for salary bands). - Natural join (joining automatically on columns with the same name) exists in some databases but is risky; explicit ON conditions are preferred. Interview pitfalls - Filtering the right table in WHERE turns a LEFT JOIN into an inner join; put such conditions in the ON clause instead: LEFT JOIN Orders o ON o.CustomerId = c.Id AND o.Status = 'Paid' - Joining on non-unique columns multiplies rows unexpectedly. - Index foreign key columns to keep joins fast.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics