Bird
Raised Fist0
DBMS Theoryknowledge~6 mins

Joins in SQL in DBMS Theory - Full Explanation

Choose your learning style10 modes available
Introduction
Imagine you have two lists of information, like one with customer names and another with their orders, but you want to see them together in one view. Joins in SQL solve this problem by combining data from two or more tables based on related columns.
Explanation
Inner Join
Inner Join returns only the rows where there is a match in both tables based on a common column. It filters out rows that do not have matching values in both tables.
Inner Join shows only the matching data from both tables.
Left Join (Left Outer Join)
Left Join returns all rows from the left table and the matched rows from the right table. If there is no match, the result will have NULLs for the right table's columns.
Left Join keeps all data from the left table, adding matches or NULLs from the right.
Right Join (Right Outer Join)
Right Join returns all rows from the right table and the matched rows from the left table. If there is no match, the left table's columns will have NULLs.
Right Join keeps all data from the right table, adding matches or NULLs from the left.
Full Join (Full Outer Join)
Full Join returns all rows when there is a match in one of the tables. It combines Left and Right Joins, showing all data from both tables with NULLs where there is no match.
Full Join shows all data from both tables, matching where possible and filling NULLs otherwise.
Cross Join
Cross Join returns the Cartesian product of the two tables, meaning every row from the first table is combined with every row from the second table. It does not require a matching column.
Cross Join pairs every row of one table with every row of the other.
Real World Analogy

Imagine two lists: one of students and another of clubs they might join. Inner Join is like listing only students who have joined clubs. Left Join is like listing all students and showing their clubs if any, otherwise blank. Right Join is like listing all clubs and showing members if any, otherwise blank. Full Join is like listing all students and all clubs, matching where possible. Cross Join is like pairing every student with every club to see all possible combinations.

Inner Join → Students who have actually joined clubs, showing only those with matches
Left Join → All students listed, with their clubs if they joined any, else blank
Right Join → All clubs listed, with members if any, else blank
Full Join → All students and all clubs listed, matched where possible, blanks where not
Cross Join → Every student paired with every club, showing all possible pairs
Diagram
Diagram
┌─────────────┐       ┌─────────────┐
│  Table A    │       │  Table B    │
│ (Students)  │       │  (Clubs)    │
└─────┬───────┘       └─────┬───────┘
      │                     │
      │                     │
      │    ┌─────────────┐  │
      ├────│ Inner Join  │──┤
      │    └─────────────┘  │
      │                     │
      │    ┌─────────────┐  │
      ├────│ Left Join   │  │
      │    └─────────────┘  │
      │                     │
      │    ┌─────────────┐  │
      ├────│ Right Join  │  │
      │    └─────────────┘  │
      │                     │
      │    ┌─────────────┐  │
      ├────│ Full Join   │  │
      │    └─────────────┘  │
      │                     │
      │    ┌─────────────┐  │
      └────│ Cross Join  │──┘
           └─────────────┘
Diagram showing two tables and the five types of joins connecting them.
Key Facts
Inner Join → Returns rows with matching values in both tables only.
Left Join → Returns all rows from the left table and matched rows from the right.
Right Join → Returns all rows from the right table and matched rows from the left.
Full Join → Returns all rows from both tables, matching where possible.
Cross Join → Returns all possible combinations of rows from both tables.
Code Example
DBMS Theory
CREATE TABLE Students (ID INT, Name TEXT);
CREATE TABLE Clubs (ID INT, ClubName TEXT);

INSERT INTO Students VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');
INSERT INTO Clubs VALUES (1, 'Chess'), (2, 'Music');

-- Inner Join: Students who joined clubs
SELECT Students.Name, Clubs.ClubName
FROM Students
INNER JOIN Clubs ON Students.ID = Clubs.ID;

-- Left Join: All students with their clubs if any
SELECT Students.Name, Clubs.ClubName
FROM Students
LEFT JOIN Clubs ON Students.ID = Clubs.ID;

-- Cross Join: Every student with every club
SELECT Students.Name, Clubs.ClubName
FROM Students
CROSS JOIN Clubs;
OutputSuccess
Common Confusions
Believing Left Join and Right Join return the same results.
Believing Left Join and Right Join return the same results. Left Join keeps all rows from the left table; Right Join keeps all rows from the right table, so results differ depending on which table is on which side.
Thinking Full Join only returns matched rows.
Thinking Full Join only returns matched rows. Full Join returns all rows from both tables, including unmatched rows with NULLs where no match exists.
Assuming Cross Join requires a matching column.
Assuming Cross Join requires a matching column. Cross Join does not use any matching column; it pairs every row from one table with every row from the other.
Summary
Joins combine rows from two tables based on related columns to show combined data.
Inner Join shows only matching rows; Left and Right Joins keep all rows from one table and match from the other.
Full Join shows all rows from both tables; Cross Join pairs every row from one table with every row from the other.

Practice

(1/5)
1. What does an INNER JOIN do in SQL?
easy
A. Returns all rows from the right table, and matched rows from the left
B. Returns all rows from the left table, and matched rows from the right
C. Returns only rows with matching values in both tables
D. Returns all rows from both tables, matching where possible

Solution

  1. Step 1: Understand INNER JOIN behavior

    An INNER JOIN returns rows where there is a match in both tables based on the join condition.
  2. Step 2: Compare with other join types

    LEFT JOIN returns all from left, RIGHT JOIN all from right, FULL JOIN all from both. INNER JOIN only matched rows.
  3. Final Answer:

    Returns only rows with matching values in both tables -> Option C
  4. Quick Check:

    INNER JOIN = matched rows only [OK]
Hint: INNER JOIN keeps only matching rows from both tables [OK]
Common Mistakes:
  • Confusing INNER JOIN with LEFT JOIN
  • Thinking INNER JOIN returns unmatched rows
  • Mixing up LEFT and RIGHT JOIN definitions
2. Which of the following is the correct syntax for a LEFT JOIN in SQL?
easy
A. SELECT * FROM table1 LEFT JOIN table2 USING (id);
B. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id;
C. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id;
D. SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;

Solution

  1. Step 1: Recall LEFT JOIN syntax

    The correct syntax is: SELECT columns FROM table1 LEFT JOIN table2 ON condition;
  2. Step 2: Check each option

    SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; matches correct syntax. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id; has wrong order. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON. SELECT * FROM table1 LEFT JOIN table2 USING (id); uses correct syntax with parentheses.
  3. Final Answer:

    SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option D
  4. Quick Check:

    LEFT JOIN syntax = FROM table1 LEFT JOIN table2 ON condition [OK]
Hint: LEFT JOIN uses ON, not WHERE, for join condition [OK]
Common Mistakes:
  • Using WHERE instead of ON for join condition
  • Incorrect order of JOIN keywords
  • Confusing USING with ON without matching columns
3. Given two tables:
Employees(emp_id, name, dept_id)
Departments(dept_id, dept_name)
What will this query return?
SELECT e.name, d.dept_name FROM Employees e INNER JOIN Departments d ON e.dept_id = d.dept_id;
medium
A. All employees with their department names, only if dept_id matches
B. All employees, even if they have no department
C. All departments with employees, including departments with no employees
D. All employees and departments, matched or not

Solution

  1. Step 1: Analyze INNER JOIN condition

    The INNER JOIN returns rows where Employees.dept_id matches Departments.dept_id.
  2. Step 2: Understand result rows

    Only employees with a matching department will appear, showing employee name and department name.
  3. Final Answer:

    All employees with their department names, only if dept_id matches -> Option A
  4. Quick Check:

    INNER JOIN filters to matching dept_id rows [OK]
Hint: INNER JOIN shows only matching rows from both tables [OK]
Common Mistakes:
  • Assuming employees without departments appear
  • Confusing INNER JOIN with LEFT JOIN
  • Thinking unmatched departments appear
4. Identify the error in this SQL join query:
SELECT * FROM Orders o LEFT JOIN Customers c ON o.customer_id = c.id WHERE c.country = 'USA';
medium
A. The WHERE clause filters out unmatched rows, defeating LEFT JOIN purpose
B. The ON clause should use = instead of LIKE
C. LEFT JOIN should be RIGHT JOIN here
D. Missing GROUP BY clause for aggregation

Solution

  1. Step 1: Understand LEFT JOIN with WHERE filter

    LEFT JOIN keeps all rows from Orders, matching Customers or NULL if no match.
  2. Step 2: Effect of WHERE on joined table

    WHERE c.country = 'USA' removes rows where c is NULL (no match), so unmatched rows are excluded, negating LEFT JOIN effect.
  3. Final Answer:

    The WHERE clause filters out unmatched rows, defeating LEFT JOIN purpose -> Option A
  4. Quick Check:

    WHERE on joined table filters NULLs, breaks LEFT JOIN [OK]
Hint: Use ON for join filters, WHERE filters after join [OK]
Common Mistakes:
  • Filtering joined table in WHERE instead of ON
  • Confusing LEFT JOIN with RIGHT JOIN
  • Adding unnecessary GROUP BY
5. You want to list all products and their suppliers, including products without suppliers and suppliers without products. Which join should you use?
hard
A. LEFT JOIN
B. FULL OUTER JOIN
C. RIGHT JOIN
D. INNER JOIN

Solution

  1. Step 1: Understand join requirements

    We want all products and all suppliers, even if no matching row exists in the other table.
  2. Step 2: Match join type to requirement

    INNER JOIN excludes unmatched rows. LEFT or RIGHT JOIN include unmatched rows from one side only. FULL OUTER JOIN includes unmatched rows from both tables.
  3. Final Answer:

    FULL OUTER JOIN -> Option B
  4. Quick Check:

    FULL OUTER JOIN returns all rows from both tables [OK]
Hint: Use FULL OUTER JOIN to include unmatched rows from both tables [OK]
Common Mistakes:
  • Choosing INNER JOIN which excludes unmatched rows
  • Using LEFT or RIGHT JOIN which include only one side unmatched
  • Confusing FULL OUTER JOIN syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard