AlgoMaster Logo

Exercise: FULL OUTER JOIN

Last Updated: May 12, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Using FULL OUTER JOIN, list every user paired with every payment, showing usernamepayment_id, and amount. Users without payments and payments without users should both appear (with NULLs on the missing side). Order by username (NULLs last), then payment_id.

SQL

Expected Query

Expected Output

Exercise 2

Easy

Using FULL OUTER JOIN on tracks and streams, list every track id along with its stream count (include tracks with zero streams). Show track_id (via COALESCE) and stream_count. Order by stream_count descending, then track_id ascending.

SQL

Expected Query

Expected Output

Exercise 3

Medium

Using FULL OUTER JOIN on users and payments, return every user's username along with their total amount paid. Include users with no payments (treat as 0) and orphan payments (collapse them into one row labeled '(orphan)'). Show username and total_paid. Order by username, with the orphan row placed last using NULLS LAST on the underlying u.username.

SQL

Expected Query

Expected Output

Exercise 4

Hard

Using FULL OUTER JOIN on users and payments, list every distinct country that appears on either side, along with the count of distinct users and the count of payments from that country. Treat a NULL country (orphan payment with no user) as 'Unknown'. Show countryuser_count, and payment_count. Order by country.

SQL

Expected Query

Expected Output