SQL Challenger
0 of 250 complete

Lessons

0 of 250 complete

Saved in this browser

Lesson 71 of 250

Moderate

Keep customers with no refunds

Your task

Return customer ID, name, and refunded_order_id for every customer. Show one row per refunded order, or one NULL order ID if the customer has none. Sort by customer ID then refunded order ID.

A report can show only matching orders and still keep every customer.

Sample data

Choose a table
idnamesignup_datesource
1Avery2025-01-02organic
2Casey2025-01-05email
3Jordan2025-01-12organic
4Morgan2025-02-01email
5Riley2025-02-10social
6Sam2025-02-20organic
7Taylor2025-03-01social
8Devon2025-02-07email

Expected output

customer_idnamerefunded_order_id
1AveryNULL
2CaseyNULL
3Jordan204
4MorganNULL
5RileyNULL
6SamNULL
7TaylorNULL
8DevonNULL
Show a hint

Put the refunded-status test in the LEFT JOIN’s ON clause.

Joins and set operations guide

Match rows by a key

A join combines columns horizontally. The condition links sales.album_id to albums.id. If an album has three sale records, a join can return its title three times.

SELECT s.id AS sale_id, a.title, s.quantity
FROM sales AS s
JOIN albums AS a ON a.id = s.album_id;

Preserve rows without a match

A LEFT JOIN retains each album. The quantity condition belongs in ON because it defines which sales qualify as matches. Placing it in WHERE would also remove albums whose sale columns are NULL.

SELECT a.title, s.id AS large_sale_id
FROM albums AS a
LEFT JOIN sales AS s
  ON s.album_id = a.id
 AND s.quantity >= 3;

Combine result sets vertically

UNION stacks compatible result sets and removes duplicate rows. UNION ALL keeps every row. INTERSECT keeps shared rows, and EXCEPT keeps rows unique to the first result. With set operations, both inputs must return the same number of columns.

SELECT title FROM albums WHERE genre = 'Jazz'
UNION
SELECT title FROM albums WHERE price < 20;
SQLiteCtrl / ⌘ + Enter

Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.

Results

Loading SQLite…