Using "distinct" beats 99% of the solutions


  • 3
    W
    select 
    Name 
    from 
    (
        select 
        c.Name, 
        o.CustomerId
        from Customers c 
        left join (select distinct CustomerId from Orders) o 
        on c.Id=o.CustomerId 
    ) t 
    where t.CustomerId is null
    ;

  • 0
    I

    Good job! Beating 98%, confirmed!


  • 2
    I

    minor improvement

    select 
    c.Name
    from Customers c 
    left join (select distinct CustomerId from Orders) o 
    on c.Id=o.CustomerId 
    where o.CustomerId is null;

  • 0
    N

    Pre distinct accelerate the join. Good job!


  • 0
    B

    I'm confused...First of all, when I copy and paste your solution, it fails. If I alias the column as Customers, like so:

    select 
    Name as Customers
    from 
    (
        select 
        c.Name, 
        o.CustomerId
        from Customers c 
        left join (select distinct CustomerId from Orders) o 
        on c.Id=o.CustomerId 
    ) t 
    where t.CustomerId is null
    ;
    

    Then it works, but it only beat 33% of the solutions (623 ms). When you ran it was it faster than 623 ms?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.