Написано: 26.03.2023

183. Клиенты без заказов (Customers Who Never Order)

easy

SQL schema

Table: customer

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table indicates the ID and name of a customer.

Table: orders

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| customerId  | int  |
+-------------+------+
id is the primary key column for this table.
customerId is a foreign key of the ID from the Customers table.
Each row of this table indicates the ID of an order and the ID of the customer who ordered it.

Задание.

Напишите SQL-запрос, чтобы сообщить обо всех клиентах, которые никогда ничего не заказывают.

Верните таблицу результатов в любом порядке.

Формат результата запроса приведен в следующем примере.

Пример 1.

Входные данные:

Customers table:
+----+-------+
| id | name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders table:
+----+------------+
| id | customerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

Результат:

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

Решение.

/* Write your PL/SQL query statement below */
SELECT name AS customers FROM customers WHERE id NOT IN (SELECT DISTINCT customerId FROM orders)