Databases
Joining Tables (JOIN)
O(n × m) time with a simple nested-loop match. O(n + m) if you build an index (a hash map) on the join key first. This is what real databases do.
The idea, in plain English
Real data usually lives in separate tables, to avoid repeating itself. For example, one table for users and one for orders. A JOIN is like matching two stacks of index cards by a shared ID number. This lets you read 'Ana bought a Book' instead of a lonely user_id next to an item.
How it works
- 1Pick the shared column that connects the two tables, such as users.id and orders.user_id.
- 2For each row in one table, find the matching row or rows in the other table, where the shared column matches.
- 3Combine each matched pair into one wider row, with columns from both tables.
When you'd use it
Use JOIN whenever the data you need is spread across more than one table. For example: showing a customer's name next to their order, or a product's name next to a sale.
Common beginner mistakes
- If you join on the wrong column, you can silently get garbage matches or far too many rows.
- A plain JOIN drops any row with no match on either side. A LEFT JOIN keeps the unmatched rows too.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
SQL: SELECT users.name, orders.item, orders.amount FROM users JOIN orders ON users.id = orders.user_id
Ana Book 15
Bilal Pen 2
Ana Lamp 25
Cara Chair 40Not sure this is the right topic? See the learning paths → or where this leads →