

AA
Joins are one of the most common SQL interview topics because they directly affect which rows appear in a result set. A wrong join choice can silently exclude important records or include unexpected NULL values.
Explain the difference between INNER JOIN and LEFT JOIN in SQL. In your answer, cover:
customers and ordersLEFT JOINThe interviewer is looking for a practical explanation, not just definitions. You should be able to describe the result set behavior clearly, explain how NULLs appear in a LEFT JOIN, and connect each join type to realistic business questions such as "customers with orders" versus "all customers, including those with no orders."
INNER JOIN returns only rows where the join condition matches in both tables. If a row from either side has no match, it is excluded from the result.
SELECT c.customer_id, c.customer_name, o.order_id
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;
LEFT JOIN returns all rows from the left table and matching rows from the right table. If there is no match on the right side, the right-table columns are returned as NULL.
SELECT c.customer_id, c.customer_name, o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id;
The main behavioral difference is how unmatched rows are treated. INNER JOIN drops them, while LEFT JOIN preserves left-table rows and fills right-table columns with NULL.
SELECT c.customer_name, o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
Use INNER JOIN when you only care about records with valid relationships in both tables. Use LEFT JOIN when the absence of related data is meaningful and should still appear in the output.
A common mistake is putting a filter on the right table in the WHERE clause after a LEFT JOIN, which can effectively turn it into an INNER JOIN. If you want to preserve unmatched left rows, place the filter in the ON clause when appropriate.
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
AND o.order_status = 'completed';