Free Study Material
Every query so far read from one single table. This closing chapter covers combining two related tables into one result — starting with what happens when you don't tell SQL how they're related, and what changes once you do.
4-row table joined this way with a 3-row table produces 4 × 3 = 12 rows, almost all of them meaningless.SELECT * FROM STUDENT, RESULT;
SELECT STUDENT.Name, RESULT.Marks
FROM STUDENT, RESULT
WHERE STUDENT.RollNo = RESULT.RollNo;
The first query — naming two tables in FROM with no WHERE — produces the full Cartesian Product: every STUDENT row paired with every RESULT row, regardless of whether they actually belong together at all. The second query adds WHERE STUDENT.RollNo = RESULT.RollNo, keeping only the row-pairs where the RollNo genuinely matches between both tables — an Equi-Join, returning exactly one meaningful combined row per real student.
Warning: Forgetting the WHERE condition when combining two tables silently produces a Cartesian Product instead of the intended join — technically valid SQL, but a very common, easily-missed mistake that returns far too many, mostly meaningless rows.
An Equi-Join is really a Cartesian Product with a filter applied — the same WHERE-based filtering idea from every earlier chapter, here used specifically to discard every mismatched pair the full Cartesian Product would otherwise include. This is precisely the same operation pd.merge() performed automatically, back in the Pandas unit — a join is just that same idea, written directly in SQL.
A Cartesian Product pairs every row of one table with every row of another, with no condition — almost always far too many rows to be useful. An Equi-Join adds an equality condition on a shared column (WHERE table1.col = table2.col), keeping only genuinely matching pairs — the SQL equivalent of pd.merge().
System Update: SQL Engine Ready.
Action: Run this block to create TWO tables: STUDENT and STREAM. We will try to connect them.
/* ==================================================
SYSTEM SETUP (Two Related Tables)
Table 1: STUDENT (Holds Name and StreamID)
Table 2: STREAM (Holds StreamID and StreamName)
================================================== */
DROP TABLE IF EXISTS STUDENT;
DROP TABLE IF EXISTS STREAM;
CREATE TABLE STREAM (Code INT, StreamName VARCHAR(20));
INSERT INTO STREAM VALUES (1, 'Science');
INSERT INTO STREAM VALUES (2, 'Commerce');
INSERT INTO STREAM VALUES (3, 'Humanities');
CREATE TABLE STUDENT (Name VARCHAR(20), Code INT);
INSERT INTO STUDENT VALUES ('Amit', 1); -- Amit is in Science
INSERT INTO STUDENT VALUES ('Ravi', 2); -- Ravi is in Commerce
INSERT INTO STUDENT VALUES ('Pooja', 1); -- Pooja is in Science
If you try to select from two tables without a condition, SQL creates a Cartesian Product (also called a Cross Join).
SELECT * FROM STUDENT, STREAM;
/* ==================================================
TASK: The Mistake (Cartesian Product)
Count the rows. 3 Students x 3 Streams = 9 Rows.
================================================== */
SELECT * FROM STUDENT, STREAM;
To get meaningful data, we must tell SQL how the tables are related. We do this using a WHERE clause that matches the common column.
This is called an Equi-Join because we use the Equal sign (=).
SELECT Name, StreamName
FROM STUDENT, STREAM
WHERE STUDENT.Code = STREAM.Code;
Logic: "Only join rows where the Code in Student matches the Code in Stream."
/* ==================================================
TASK: Correct Join
We use the common column 'Code' to link them.
================================================== */
SELECT
STUDENT.Name,
STREAM.StreamName
FROM STUDENT, STREAM
WHERE STUDENT.Code = STREAM.Code;