SQL Challenges-1: List the items sold out within a specific period
SQL Challenges-1: Exercise-49 with Solution
From the following tables write a SQL query to get the description of items with 50 or more quantities sold out within January and February of 2020. Return item description and sale quantity.
Input:
Table: item
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
item_code | int(11) | NO | PRI | ||
item_desc | varchar(255) | YES | |||
cost | int(11) | YES |
Data:
item_code | item_desc | cost |
---|---|---|
101 | mother board | 2700 |
102 | RAM | 800 |
103 | key board | 300 |
104 | mouse | 300 |
Table: sales_info
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
distributor_id | int(11) | YES | |||
item_code | int(11) | YES | |||
retailer_id | int(11) | YES | |||
date_of_sell | date | YES | |||
quantity | int(11) | YES | |||
total_cost | int(11) | YES |
Data:
distributor_id | item_code | retailer_id | date_of_sell | quantity | total_cost |
---|---|---|---|---|---|
5001 | 101 | 1001 | 2020-01-12 | 30 | 8100 |
5001 | 103 | 1002 | 2020-01-15 | 25 | 4500 |
5002 | 101 | 1001 | 2019-01-30 | 25 | 5400 |
5001 | 104 | 1003 | 2019-02-17 | 75 | 2400 |
5003 | 101 | 1003 | 2020-03-07 | 55 | 13500 |
5003 | 104 | 1002 | 2020-05-27 | 100 | 3000 |
5002 | 102 | 1001 | 2020-05-18 | 65 | 9600 |
5002 | 103 | 1004 | 2020-01-30 | 45 | 2400 |
5003 | 103 | 1001 | 2020-03-12 | 30 | 900 |
Sample Solution:
SQL Code(MySQL):
CREATE TABLE item (item_code int not null unique, item_desc varchar(255), cost int);
INSERT INTO item VALUES(101,'mother board', 2700);
INSERT INTO item VALUES(102,'RAM', 800);
INSERT INTO item VALUES(103,'key board',300);
INSERT INTO item VALUES(104,'mouse',300);
CREATE TABLE sales_info (distributor_id int, item_code int, retailer_id int, date_of_sell date, quantity int, total_cost int);
INSERT INTO sales_info VALUES(5001,101,1001,'2020-01-12',30,8100);
INSERT INTO sales_info VALUES(5001,103,1002,'2020-01-15',25,4500);
INSERT INTO sales_info VALUES(5002,101,1001,'2019-01-30',25,5400);
INSERT INTO sales_info VALUES(5001,104,1003,'2019-02-17',75,2400);
INSERT INTO sales_info VALUES(5003,101,1003,'2020-03-07',55,13500);
INSERT INTO sales_info VALUES(5003,104,1002,'2020-05-27',100,3000);
INSERT INTO sales_info VALUES(5002,102,1001,'2020-05-18',65,9600);
INSERT INTO sales_info VALUES(5002,103,1004,'2020-01-30',45,2400);
INSERT INTO sales_info VALUES(5003,103,1001,'2020-03-12',30,900);
select item_desc, sum(quantity) as sale_quantity
from item a join sales_info b
on a.item_code = b.item_code
where month(date_of_sell) between 1 and 2
group by item_desc
having sale_quantity >= 50
Sample Output:
item_desc |sale_quantity| ------------|-------------| key board | 70| mother board| 55| mouse | 75|
SQL Code Editor:
Contribute your code and comments through Disqus.
Previous: Show running quantiry for each unit type of item.
Next: Find the order id and the item name for all companies who are not registered with the distributor.
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-49.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics