All courses
Intermediate

Existence, sets and safe NULLs

Answer yes-or-no questions across tables with EXISTS, find missing relationships, combine lists with UNION, and keep missing values from corrupting reports.

6 exercises · 30 minutes · Data: Online shop, Company team
Start course
SQL
01
→ 02
→ 03

Your exercises

01
Business customers with no orders

Sales wants to check on business customers who have never placed any order, regardless of status. Return their name and email, sorted by name A to Z. Use the customer ID relationship, not a guess based on their email or signup date.

5 min
02
Customers who experienced a refund

Customer success wants one row per customer who has at least one refunded order. Return customer name and country, sorted by name A to Z. A customer with several refunds must still appear only once.

5 min
03
Combine two outreach lists

Marketing has two overlapping target lists: business customers, and customers in Germany. Combine them into one list with customer name and email, sorted by name A to Z. Anyone in both lists should appear once, not twice.

5 min
04
Combine paid and refunded order counts

Operations wants a two-row scorecard with status and orders: one row for paid orders and one for refunded orders. Include their actual counts, sorted by status A to Z. Use UNION ALL to combine the two independently counted groups.

5 min
05
Fill missing cities for an export

The CRM export cannot accept blank city values. Return each customer name and their city, replacing a NULL city with the text 'Unknown'. Sort the 20 customers by name A to Z, then by customer ID to keep same-name rows stable.

4 min
06
Department budget use, including empty teams

Finance needs every department, including Research with no employees. Return department name, budget, total employee salaries (0 for an empty department), and salary spend as a percentage of budget rounded to 1 decimal. Sort by department name A to Z.

6 min
Queries run on sample company data in SQLite. Core SELECT, JOIN and GROUP BY patterns transfer to other SQL engines; functions, dates and types can differ.