SQL (Core) - Multi-table queries with JOINs
Course Syllabus
Multi-table queries with JOINs
Real-world relational databases normalize data into separate specialized tables to eliminate redundancy. To query data that spans across multiple tables, we use the JOIN operator using Primary Key and Foreign Key constraints.
1. Database Normalization & Primary/Foreign Keys
Database tables are connected through key relationships. A Primary Key (PK) is a column that uniquely identifies each row in its own table (e.g. movies.id). A Foreign Key (FK) is a column in another table that references that Primary Key (e.g. boxoffice.movie_id).
2. Inner Join Syntax
SELECT title, domestic_sales, international_sales FROM movies JOIN boxoffice ON movies.id = boxoffice.movie_id;
The INNER JOIN (or simply JOIN) matches rows in both tables where the ON expression condition evaluates to TRUE.
Table Aliasing Tip
You can alias long table names to keep multi-table queries short and concise: FROM movies AS m JOIN boxoffice AS b ON m.id = b.movie_id.
🗄️ Schema Explorer SELECTABLE
🎯 Exercises Checklist:
💡 Hint:
Loading hint...
🔑 Solution SQL:
SELECT * FROM movies; Data Output
No results. Run query to fetch data.