"Leetcode" 183. Customers who never order

Source: Internet
Author: User

183. Customers who never order

Suppose a Web site contains two tables, Customers tables and Orders tables. Write an SQL statement that finds all customers who never order anything.


tables and data SQL used:

Create table If not Exists Customers (idint, Name varchar (255));
Create table If not Exists Orders (Id int,customerid int);
Truncate table Customers;
Insert into Customers (Id, Name) VALUES (' 1 ', ' Joe ');
Insert into Customers (Id, Name) VALUES (' 2 ', ' Henry ');
Insert into Customers (Id, Name) VALUES (' 3 ', ' Sam ');
Insert into Customers (Id, Name) VALUES (' 4 ', ' Max ');
Truncate table Orders;
Insert into Orders (Id, CustomerId) VALUES (' 1 ', ' 3 ');
Insert into Orders (Id, CustomerId) VALUES (' 2 ', ' 1 ');

Answer:

This problem is very simple, directly let two tables left outer connection, and then as long as the right side of the CustomerID is a NULL customer is not placed orders of customers.

Select C.name Customers from
Customers C-left
join Orders o ' O.customerid = c.id
where O.customerid is Nu ll

Write more rough, there is no understanding can scan two-dimensional code plus QQ Group to find my answer.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.