183. Customers Who Never Order

Difficulty:
Related Topics:
    Similar Questions:

      Problem

      Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

      Table: Customers.

      +----+-------+
      | Id | Name  |
      +----+-------+
      | 1  | Joe   |
      | 2  | Henry |
      | 3  | Sam   |
      | 4  | Max   |
      +----+-------+
      

      Table: Orders.

      +----+------------+
      | Id | CustomerId |
      +----+------------+
      | 1  | 3          |
      | 2  | 1          |
      +----+------------+
      

      Using the above tables as example, return the following:

      +-----------+
      | Customers |
      +-----------+
      | Henry     |
      | Max       |
      +-----------+
      

      Solution 1

      # Write your MySQL query statement below
      select a.Name as Customers
        from Customers a
        where a.Id not in (
          select b.CustomerId from Orders b
        )
      

      Explain:

      nope.

      Complexity:

      Solution 2

      # Write your MySQL query statement below
      select a.Name as Customers
        from Customers a
        where not exists (
          select Id from Orders b where a.Id = b.CustomerId
        )
      

      Explain:

      nope.

      Complexity:

      Solution 3

      # Write your MySQL query statement below
      select a.Name as Customers
        from Customers a
        left join Orders b
        on a.Id = b.CustomerId
        where b.CustomerId is null
      

      Explain:

      nope.

      Complexity: