SQL Challenges-1: Find the order id and the item name for all companies who are not registered with the distributor
SQL Challenges-1: Exercise-50 with Solution
From the following table write a SQL query to find the order id and the item name for all companies who are not registered with the distributor. Return the result table in any order.
Input:
Table: company_info
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
company_id | int(11) | NO | PRI | ||
company_name | varchar(25) | YES |
Data:
company_id | company_name |
---|---|
5001 | Intel |
5002 | Kingston |
5003 | Dell |
5004 | Sony |
5005 | Iball |
5006 | Canon |
Table: orders
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
order_id | int(11) | YES | |||
item_name | varchar(25) | YES | |||
company_id | int(11) | YES |
Data:
order_id | item_name | company_id |
---|---|---|
101 | mother board | 5001 |
102 | RAM | 5002 |
103 | printer | 5006 |
104 | keyboard | 5005 |
105 | mouse | 6051 |
106 | speaker | 6009 |
107 | web cam | 5005 |
108 | hard disk | 5002 |
109 | monitor | 5003 |
110 | scanner | 7023 |
Sample Solution:
SQL Code(MySQL):
CREATE TABLE company_info (company_id int not null unique, company_name varchar(25));
INSERT INTO company_info VALUES(5001,'Intel');
INSERT INTO company_info VALUES(5002,'Kingston');
INSERT INTO company_info VALUES(5003,'Dell');
INSERT INTO company_info VALUES(5004,'Sony');
INSERT INTO company_info VALUES(5005,'Iball');
INSERT INTO company_info VALUES(5006,'Canon');
CREATE TABLE orders (order_id int, item_name varchar(25), company_id int);
INSERT INTO orders VALUES(101 ,'mother board ',5001);
INSERT INTO orders VALUES(102 ,'RAM ',5002);
INSERT INTO orders VALUES(103 ,'printer ',5006);
INSERT INTO orders VALUES(104 ,'keyboard ',5005);
INSERT INTO orders VALUES(105 ,'mouse',6051);
INSERT INTO orders VALUES(106 ,'speaker',6009);
INSERT INTO orders VALUES(107 ,'web cam ',5005);
INSERT INTO orders VALUES(108 ,'hard disk ',5002);
INSERT INTO orders VALUES(109 ,'monitor ',5003);
INSERT INTO orders VALUES(110 ,'scanner',7023);
SELECT orders.order_id, orders.item_name
FROM orders
Left Join company_info
on orders.company_id = company_info.company_id
WHERE company_info.company_name IS NULL;
Sample Output:
order_id|item_name| --------|---------| 105|mouse | 106|speaker | 110|scanner |
SQL Code Editor:
Contribute your code and comments through Disqus.
Previous: List the items sold out within a specific period.
Next: Salesman who makes Largest Number of transactions.
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-50.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics