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 use if needed!

SQL is second nature to me now, and I've been writing it for a while. I'm eager to see how the difficulty ramps up.


Comments

Popular posts from this blog

Leetcode 75: 1768. Merge Strings Alternately

Defending Against Ettercap Attacks: A Brief Overview

Leetcode Two Sum Problem in three different languages