SQLancer
LESSON 6

SQL (Core) - Multi-table queries with JOINs

Progress: 0%

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

movies
|

🎯 Exercises Checklist:

1. Find the domestic and international sales for each movie
2. Show the sales numbers for each movie that did better internationally rather than domestically
3. List all the movies by their ratings in descending order
⚡ Practice Console

Data Output

No results. Run query to fetch data.

Current Task Target

5 rows