SQL Challenges-1: Sales Person
SQL Challenges-1: Exercise-22 with Solution
From the following tables find those customers who did not make any order to the supplier 'DCX LTD'. Return customers name.
Input:
Table: customers
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
customer_id | int(11) | YES | |||
customer_name | varchar(255) | YES | |||
customer_city | varchar(255) | YES | |||
avg_profit | int(11) | YES |
Data:
customer_id | customer_name | customer_city | avg_profit |
---|---|---|---|
101 | Liam | New York | 25000 |
102 | Josh | Atlanta | 22000 |
103 | Sean | New York | 27000 |
104 | Evan | Toronto | 15000 |
105 | Toby | Dallas | 20000 |
Table: supplier
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
supplier_id | int(11) | YES | |||
supplier_name | varchar(255) | YES | |||
supplier_city | varchar(255) | YES |
Data:
supplier_id | supplier_name | supplier_city |
---|---|---|
501 | ABC INC | Dallas |
502 | DCX LTD | Atlanta |
503 | PUC ENT | New York |
504 | JCR INC | Toronto |
Table: orders
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
order_id | int(11) | YES | |||
customer_id | int(11) | YES | |||
supplier_id | int(11) | YES | |||
order_date | date | YES | |||
order_amount | int(11) | YES |
Data:
order_id | customer_id | supplier_id | order_date | order_amount |
---|---|---|---|---|
401 | 103 | 501 | 2012-03-08 | 4500 |
402 | 101 | 503 | 2012-09-15 | 3650 |
403 | 102 | 503 | 2012-06-27 | 4800 |
404 | 104 | 502 | 2012-06-17 | 5600 |
405 | 104 | 504 | 2012-06-22 | 6000 |
406 | 105 | 502 | 2012-06-25 | 5600 |
Sample Solution:
SQL Code(MySQL):
DROP TABLE if exists customers;
CREATE TABLE customers (customer_id int, customer_name varchar(255), customer_city varchar(255), avg_profit int);
INSERT INTO customers VALUES ('101', 'Liam','New York',25000);
INSERT INTO customers VALUES ('102', 'Josh','Atlanta',22000);
INSERT INTO customers VALUES ('103', 'Sean','New York',27000);
INSERT INTO customers VALUES ('104', 'Evan','Toronto',15000);
INSERT INTO customers VALUES ('105', 'Toby','Dallas',20000);
CREATE TABLE supplier (supplier_id int, supplier_name varchar(255), supplier_city varchar(255));
INSERT INTO supplier VALUES ('501', 'ABC INC','Dallas');
INSERT INTO supplier VALUES ('502', 'DCX LTD','Atlanta');
INSERT INTO supplier VALUES ('503', 'PUC ENT','New York');
INSERT INTO supplier VALUES ('504', 'JCR INC','Toronto');
CREATE TABLE orders (order_id int, customer_id int, supplier_id int, order_date Date, order_amount int);
INSERT INTO orders VALUES (401, 103,501,'2012-03-08','4500');
INSERT INTO orders VALUES (402, 101,503,'2012-09-15','3650');
INSERT INTO orders VALUES (403, 102,503,'2012-06-27','4800');
INSERT INTO orders VALUES (404, 104,502,'2012-06-17','5600');
INSERT INTO orders VALUES (405, 104,504,'2012-06-22','6000');
INSERT INTO orders VALUES (406, 105,502,'2012-06-25','5600');
SELECT * FROM customers;
SELECT * FROM supplier;
SELECT * FROM orders;
SELECT cus.customer_name
FROM customers cus
WHERE cus.customer_id
NOT IN (SELECT ord.customer_id
FROM orders ord
LEFT JOIN supplier sup
ON ord.supplier_id = sup.supplier_id
WHERE sup.supplier_name = 'DCX LTD');
Sample Output:
customer_name| -------------| Liam | Josh | Sean |
SQL Code Editor:
Contribute your code and comments through Disqus.
Previous: Consecutive Availability of a doctor in a clinic.
Next: Highest Single marks.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql-exercises/challenges-1/sql-challenges-1-exercise-22.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics