Intermediate Joins · Three-table JOIN
16. Plan behind each purchase
Concept: Three-table JOINshow
You can chain JOINs: each one adds a table and its own ON condition. Join every table on the column that really links them (a foreign key like user_id points to users.id), not on matching id columns that just happen to look alike.
Example (different data):
SELECT s.name, c.title, e.grade FROM students s JOIN enrollments e ON e.student_id = s.id JOIN courses c ON c.id = e.course_id;
Your task
Finance is reconciling purchases against plans and needs each buyer's username, plan and purchase date in one list.
Return username, plan and purchase_date (the date of the user's purchase event) for every user who has both a subscription and a purchase event. Sort by purchase_date.
Solution unlocks after 3 more tries
Starting Postgres in your browser…
query.sql
Output
Write a query and press Run Query (Ctrl/⌘ + Enter).