The challenge involves a table,
The goal is to write an SQL query to report the names of the customers that were not referred by the customer with id = 2.
Here's an example for better understanding:
Will, Jane, Bill, and Zack were not referred by the customer with id = 2.
We'll explore two SQL solutions that solve this problem with subtle differences. We'll discuss their differences, strengths, weaknesses, and structures.
The first solution selects customers whose referee_id is not 2 or is null.
SELECT name FROM Customer WHERE referee_id != 2 OR referee_id IS NULL
This solution has a runtime of 740ms, outperforming 91.47% of other submissions.
The second solution closely resembles the first. The only difference is the order of conditions in the WHERE clause. It first checks for null referee_ids before checking for referee_ids not equal to 2.
SELECT name FROM Customer WHERE referee_id IS NULL OR referee_id != 2
The runtime for this solution is 741ms, beating 91.35% of other submissions.
Both solutions yield the desired outcome, but the first solution performs marginally better. Consequently, the ranking of solutions based on overall performance, from best to worst, is as follows: Source Code 1 > Source Code 2.
Your choice should depend on your specific requirements and performance expectations.
You can find the original problem at LeetCode.
For more insightful solutions and tech-related content, connect with me on my Beacons page.