Posts

Showing posts from March, 2024

Leetcode SQL 50 - Another two

I had so much fun doing that first easy one, I did two more! First I tackled 584. Find Customer Referee The problem: Find the names of the customer that are not referred by the customer with id = 2. Return the result table in any order. The solution: select name from Customer where referee_id != 2 or referee_id is null; Pretty self explanatory. Since there were nulls in the referee_id column, we needed to also allow if there were nulls. Then I did 1148. Article Views I The problem: Write a solution to find all the authors that viewed at least one of their own articles. Return the result table sorted by id in ascending order. The solution: select distinct author_id as id from Views where author_id = viewer_id order by author_id; Of course I missed the order requirement the first time around, but luckily it was easily fixed the second time it was run. I use distinct since there were possible duplicates, though didn't test to see if that was the case. That will make it slower, so only...

Leetcode SQL 50

I picked a nice easy SQL one today, and it was far easier than I expected! 1757. Recyclable and Low Fat Products The problem: Write a solution to find the ids of products that are both low fat and recyclable. Return the result table in any order.  The solution: select product_id from Products where low_fats = 'Y' and recyclable='Y'; Not much else to say, except I wish SQL was always this easy!