+91 81900 98289

MySQL Join

This MySQL Join lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A join combines rows from two tables that share a key. An inner join keeps only pairs that match.

The example attaches a batch city to each learner with that batch id.

Example 1: Start here

JOIN batch ON learner.batch_id = batch.id is the SQL shape.

learners = [{"name": "Meera", "batch_id": 1}] # a list; the values stay in this order batches = [{"id": 1, "city": "Chennai"}] # a list; the values stay in this order print([{"name": learner["name"], "city": batch["city"]} for learner in learners for batch in batches if learner["batch_id"] == batch["id"]]) # keep the items that pass the test

Example 2: Filter the rows

MySQL Join is easier to see as a list of records. The filter keeps every row whose seats value is at least 10.

rows = [{"topic": "MySQL Join", "seats": 29}, {"topic": "SQL", "seats": 12}] # a list; the values stay in this order print([row["topic"] for row in rows if row["seats"] >= 10]) # keep the items that pass the test

Example 3: Update one field

An update changes one field and leaves the rest of the record alone. On a database you would run an UPDATE or an update_one for the same change.

row = {"topic": "MySQL Join", "seats": 29} # a dictionary; each key has one value row["seats"] = row["seats"] + 1 # change this one field print(row)

Example 4: Count the matches

Counting matches is the idea behind COUNT(*). Here the number 29 appears twice.

rows = [29, 4, 29] # a list; the values stay in this order print(sum(1 for value in rows if value == 29)) # keep the items that pass the test
JOIN batch ON learner.batch_id = batch.id is the SQL shape.
Call Now 81900 98289