Rullst AcademyRullst Academy/Lesson
Databases and SQL I · Lesson 07 / 12

Join without duplicating conclusions

Use keys, INNER JOIN and LEFT JOIN and recognize multiplicity.

Learning paths18 min

Let's discover together

  1. 1

    loans.kit_id refers to kits.id: many loans to one material. An enabled FK guarantees the material exists, not that loan totals fit stock. That rule requires additional logic.

  2. 2

    JOIN combines rows through ON. INNER JOIN shows matches. Material 1 has two loans and appears twice: that is model multiplicity. A wrong condition may multiply unrelated rows.

  3. 3

    LEFT JOIN keeps materials without loans, with NULL on the right. To keep all materials and match only open loans, put l.returned IS NULL in ON. WHERE may remove rows after joining; also test a material with only returned loans.

  4. 4

    Aliases k and l show column origins. Predict why Nova has a null loan_id. Replace LEFT JOIN with JOIN, run and explain what disappeared.

Your data workbench

Our model: kits.id ← loans.kit_id. One material can have many loans; names may repeat. We use fictional data only.

Predict before opening the reference. It shows verified output from the original example, not execution of your text. Run your changes with the kit on your computer.

-- Join without duplicating conclusions
SELECT k.id, k.label, l.id AS loan_id FROM kits AS k LEFT JOIN loans AS l ON l.kit_id=k.id ORDER BY k.id, l.id;
Compare with reference output
[
  {
    "columns": [
      "id",
      "label",
      "loan_id"
    ],
    "rows": [
      [
        1,
        "Orbit",
        1
      ],
      [
        1,
        "Orbit",
        2
      ],
      [
        2,
        "Sprout",
        3
      ],
      [
        3,
        "Orbit",
        4
      ],
      [
        4,
        "Nova",
        null
      ]
    ]
  }
]

Run the command in the extracted folder’s terminal. Use python3 on macOS/Ubuntu or py if that is your Windows launcher. Every run starts in memory; read README.md for saving files and recovery practice.

python lab.py run lesson.sql
My files and GitHub

The editor saves your .sql draft in this browser. Download it and replace lesson.sql in the kit; for the final project, availability.sql. The ZIP remains the original template. GitHub publishing sends only the confirmed file, without executing SQL or sending a database.

Official links and next step

✦
YOUR MISSION

Use keys, INNER JOIN and LEFT JOIN and recognize multiplicity.

Now it's your turn

Discovery workshop

Read, predict and check

Use keys, INNER JOIN and LEFT JOIN and recognize multiplicity.
Build your answers and check when you are ready.
A moment to understand

Which join keeps materials without loans?

I need a hint

JOIN combines rows through ON. INNER JOIN shows matches. Material 1 has two loans and appears twice: that is model multiplicity. A wrong condition may multiply unrelated rows.

Loading your journey…

Take it with you

Return id 1’s loans in the same run and compare ON and WHERE filters. Save an example explaining the difference.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.