Skip to main content
Beginnersql query Verified

What are the different types of joins in SQL? Explain each with an example.

ReportedSource: GeeksforGeeks interview experience: Deloitte Analyst on-campus 2022-23

Quick answer

INNER (matches only), LEFT/RIGHT OUTER (all of one side plus matches), FULL OUTER (all of both), CROSS (Cartesian product), SELF (table with itself). Watch row multiplication and WHERE filters negating LEFT JOINs.

Constraints

  • Input is setup SQL: CREATE TABLE customers (id INTEGER, name TEXT) and CREATE TABLE orders (id INTEGER, customer_id INTEGER, amount REAL), plus INSERT statements.
  • Output columns: name, order_count, total_amount, ordered by name ascending. Customer names are unique.
  • Orders whose customer_id does not exist in customers are ignored.
  • The query must run on SQLite.

Sample I/O

in: CREATE TABLE customers (id INTEGER, name TEXT); CREATE TABLE orders (id INTEGER, customer_id INTEGER, amount REAL); INSERT INTO customers VALUES (1,'Asha'),(2,'Bala'),(3,'Chitra'); INSERT INTO orders VALUES (10,1,100),(11,1,50),(12,3,70);

out: Asha 2 150 Bala 0 0 Chitra 1 70

in: CREATE TABLE customers (id INTEGER, name TEXT); CREATE TABLE orders (id INTEGER, customer_id INTEGER, amount REAL); INSERT INTO customers VALUES (1,'Zoya'),(2,'Amit');

out: Amit 0 0 Zoya 0 0

in: CREATE TABLE customers (id INTEGER, name TEXT); CREATE TABLE orders (id INTEGER, customer_id INTEGER, amount REAL); INSERT INTO customers VALUES (1,'Dev'),(2,'Esha'); INSERT INTO orders VALUES (1,1,10.5),(2,2,20),(3,2,5.25);

out: Dev 1 10.5 Esha 2 25.25

sql
Loading editor…
Result

Sign up to run your code in the sandbox.

Sign up to run code and get an AI review

You can read and edit the challenge freely — running it in the sandbox and getting AI feedback need a free account.

Sign up free

Hints

Reference solution

Using customers(id, name) and orders(id, customer_id, amount):

INNER JOIN. Only rows with a match on both sides: customers who placed orders.

query.sql
SELECT c.name, o.amount FROM customers c JOIN orders o ON o.customer_id = c.id;

LEFT (OUTER) JOIN. All left rows plus matches; NULLs where no match: every customer, with orders if any. Add WHERE o.id IS NULL to find customers who never ordered.

RIGHT (OUTER) JOIN. Mirror of LEFT: all orders, even if the customer row is missing (rarely used; swap the tables and use LEFT instead).

FULL OUTER JOIN. All rows from both sides, matched where possible (not in MySQL; emulate with LEFT UNION RIGHT).

CROSS JOIN. Every combination of rows (Cartesian product), e.g. all sizes x all colours.

SELF JOIN. A table joined to itself with aliases, e.g. employee to manager.

Pitfalls. Joining on non-unique keys multiplies rows (double-counted sums); putting a right-table filter in WHERE turns a LEFT JOIN into an inner join (put it in ON instead).

See the concept: Join fan-out

Join fan-out
order_lines: one row per line
O101line 1₹300
payments: one row per attempt
O101P101FAILED
O101P102SUCCESS
lines JOIN payments ON order_id
line 1P101₹300
line 1P102₹300
SUM(line_revenue)₹600

Wrong: the single ₹300 line is counted once per payment attempt.

Fix: aggregate payments to one row per order first

line 1, paid, ₹300, so SUM = ₹300

One order line, two payment attempts: a direct join doubles the revenue. Aggregating payments to order grain first keeps one row per line.

Discussion