Customers Not Referred by Customer 2
Find the names of customers who were not referred by the customer with id = 2. Handle NULL values correctly: customers without a referrer must also be included.
Get comfortable retrieving exactly the data you need with SQL SELECT exercises. Practice choosing columns, filtering rows with conditions, sorting results, and limiting the output. Each query runs in a real database and is checked against expected results using hidden data. Problems in this collection: 224, free: 127.
Problems in this set: 224, free: 127
Start with a free problemFind the names of customers who were not referred by the customer with id = 2. Handle NULL values correctly: customers without a referrer must also be included.
Find all authors who viewed at least one of their own articles. Return each author’s identifier (author_id) in a result column named id. Sort the result in ascending order.
HR is awarding a special bonus equal to 100% of salary to employees whose employee_id is odd and whose name does not start with M. Everyone else receives a bonus of 0. Return employee_id and bonus.
The data quality team is checking user email addresses before sending a campaign.
The warehouse team is checking the quality of product serial number data. Find products whose entire serial_number matches the pattern SN- followed by exactly 4 digits, such as SN-1234.
The platform team is looking for "zombie sessions" — sessions that last more than a day and were probably not closed properly. Find sessions whose duration is greater than 24 hours.
A warehouse reconciliation is looking for products whose stock has fallen below zero. These products need to be reported to the inventory team promptly. Return the id of each product where stock < 0.
The support team needs a list of guest orders from orders so it can separately review requests from customers without registered accounts. Find rows where user_id is not specified.
Each order needs a Russian text label in the form Заказ N пользователя U. If user_id is missing, the user part of the label must be гость. Return id and label, sorted by id.
The operations team needs a list of all orders, sorted with higher user_id values first and guest orders without a user_id last.
The analysts are compiling a master list of statuses found in orders and subscriptions. Combine the status values from orders and subscriptions without duplicates.
An operational report based on orders needs to show order IDs and user IDs in a consistent format. If a guest order has no user_id, show 0 instead.
AND has higher precedence than OR in SQL. Reading a condition from left to right can lead to a different interpretation than the database uses. Break complex filters into logical parts, add parentheses where needed, and test rows that meet only some of the conditions.
LIMIT restricts how many rows are returned, but it does not choose the newest, cheapest, or highest-ranked records. Without ORDER BY, the result order is not guaranteed. If a problem asks for top or latest entries, identify the sorting rule before applying LIMIT.
NULL represents a missing value, so = NULL does not work like an ordinary equality check. The condition may silently exclude records you expected to see. Use the appropriate NULL check and consider whether records with missing values should be included or excluded.
Values stored as text follow string sorting rules, which can produce a different order from numeric sorting. Dates stored as strings can cause similar surprises. Check the column's data type and inspect the first few returned rows before assuming the ordering is correct.
Yes. Free exercises in this collection: 127. With the free plan, you can sign in and solve up to 3 new problems per day without paying. Premium exercises remain visible in the catalog but require a subscription to solve.
Your SQL runs in a real database, using PostgreSQL, MySQL, ClickHouse, or BigQuery depending on the problem. The result is automatically compared with an expected result using hidden data. This makes it worth checking the selected columns, returned rows, and required ordering carefully.
WHERE determines which rows qualify for the result. ORDER BY determines their sequence. Sorting does not remove unwanted records, and filtering does not guarantee their order. Read the selection and sorting requirements separately, then make sure your query handles both.
Without ORDER BY, a database does not guarantee the order of returned records. LIMIT simply takes a portion of that result. If you need a predictable set of first rows, specify the sorting criteria. When values tie, an additional sorting column may be necessary.
Yes. Understanding tables, rows, columns, and basic filtering is enough to begin. SELECT exercises help you practice retrieving and organizing data from individual tables. Once those operations feel familiar, combining multiple tables with JOIN becomes much easier to reason about.