0
0
SQLquery~10 mins

INNER JOIN with table aliases in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - INNER JOIN with table aliases
Start with two tables
Assign aliases to tables
Write INNER JOIN using aliases
Match rows where join condition is true
Combine matched rows into result
Return joined rows as output
We start with two tables, give them short names (aliases), then join rows where a condition matches, producing combined rows.
Execution Sample
SQL
SELECT a.id, a.name, b.order_id
FROM customers AS a
INNER JOIN orders AS b ON a.id = b.customer_id;
This query joins customers and orders tables using aliases 'a' and 'b' to match customer IDs.
Execution Table
StepActionTable AliasRow from customers (a)Row from orders (b)Join Condition (a.id = b.customer_id)Output Row
1Check first row of customersa{id:1, name:'Alice'}
2Check first row of ordersb{order_id:101, customer_id:1}1 = 1 (True){id:1, name:'Alice', order_id:101}
3Check second row of ordersb{order_id:102, customer_id:2}1 = 2 (False)
4Check second row of customersa{id:2, name:'Bob'}
5Check first row of ordersb{order_id:101, customer_id:1}2 = 1 (False)
6Check second row of ordersb{order_id:102, customer_id:2}2 = 2 (True){id:2, name:'Bob', order_id:102}
7No more rows in customers
💡 All rows checked; join condition matched only when a.id equals b.customer_id
Variable Tracker
VariableStartAfter Step 1After Step 4Final
a (customer row)none{id:1, name:'Alice'}{id:2, name:'Bob'}none (end)
b (order row)none{order_id:101, customer_id:1}{order_id:102, customer_id:2}none (end)
Output Rowsempty[{id:1, name:'Alice', order_id:101}][{id:1, name:'Alice', order_id:101}, {id:2, name:'Bob', order_id:102}]final two joined rows
Key Moments - 2 Insights
Why do we use aliases like 'a' and 'b' instead of full table names?
Aliases make the query shorter and easier to read, especially when joining tables. In the execution_table, you see 'a' and 'b' used to refer to customers and orders rows.
What happens if the join condition is false for some rows?
Rows that do not meet the join condition are skipped and not included in the output. For example, in steps 3 and 5, the condition is false, so no output row is created.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the output row?
A{id:1, name:'Alice', order_id:102}
B{id:1, name:'Alice', order_id:101}
C{id:2, name:'Bob', order_id:101}
DNo output row
💡 Hint
Check the 'Output Row' column in step 2 of the execution_table.
At which step does the join condition first become false?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Join Condition' column in the execution_table rows.
If we changed the join condition to a.id = b.order_id, how would the output change?
AOnly rows with matching IDs would join as before
BAll rows would join
CNo rows would join because customer IDs don't match order IDs
DQuery would error out
💡 Hint
Compare the join condition column and think about matching values.
Concept Snapshot
INNER JOIN combines rows from two tables where a condition matches.
Use aliases (short names) to simplify table references.
Syntax: FROM table1 AS a INNER JOIN table2 AS b ON a.col = b.col
Only rows matching the condition appear in the result.
Aliases help avoid repeating long table names.
Full Transcript
This visual execution shows how INNER JOIN works with table aliases. We start with two tables, customers and orders, and give them short names 'a' and 'b'. The query joins rows where a.id equals b.customer_id. Step by step, each row from customers is checked against each row from orders. When the join condition is true, the rows combine into one output row. Rows that don't match are skipped. The output includes only matched rows. Using aliases makes the query easier to write and read. The execution table tracks each step, showing which rows are compared and when output rows are created.