COALESCE: Handle Missing Composers
Many tracks have NULL composers. Use COALESCE to provide a default value when the Composer is missing. Write a query...
Fix missing values, duplicates and messy formatting before you analyse.
Real tables arrive with missing values, duplicate rows and inconsistent formatting, and cleaning them is usually the first half of any analysis. NULL is not zero and not an empty string, so it needs IS NULL and COALESCE rather than an equality test, and duplicates are removed most reliably by ranking rows with ROW_NUMBER and keeping the first of each key. These questions give you dirty seeded data and ask for the clean result set.
Many tracks have NULL composers. Use COALESCE to provide a default value when the Composer is missing. Write a query...
Some customers don't have a company. Use COALESCE to show 'Individual' for customers without a company. Write a query...
Find pairs of customers who live in the same city. This can help with organizing local meetups or referral programs....
An INNER JOIN only returns matching rows. Use LEFT JOIN to include ALL artists, even those without any albums. Write a...
The customer success team wants a quick contact list of customers from North America, showing each as a single readable...
The analytics team wants a quick view of customer email providers, alongside each customer's full name and an...
FAQ
Short answers to what people get stuck on most. Tap a question to expand it.
NULL means unknown, so NULL = NULL is not true. Test with IS NULL instead.
SELECT COALESCE(NULLIF(TRIM(phone), ''), 'unknown') AS phone
FROM customers;Rank the rows inside each duplicate group with ROW_NUMBER and keep number 1.
WITH ranked AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY email ORDER BY updated_at DESC
) AS rn
FROM users
)
SELECT * FROM ranked WHERE rn = 1;Normalise text before you group, join or compare it.
SELECT LOWER(TRIM(city)) AS city, COUNT(*) AS customers
FROM customers
GROUP BY LOWER(TRIM(city));Track your streak & earn XP
Sign up free to unlock your progress dashboard, daily streaks, and leaderboard ranking.
10 role-based tests for Data Analysts, SQL Developers & Data Engineers with instant scorecards.