0% found this document useful (0 votes)
53 views20 pages

MIT 1203 - Database Systems Assignment On SQL: Index No: 19550359 Registration No: 2019/MIT/035 Name: S.D. Jalath

The document describes S.D. Jalath creating tables, inserting data, and performing SQL queries for an assignment on database systems. Tables were created for Customer, Sales, Vendor_Info, Products, and Vendors. Data was then inserted into these tables. Queries were run to view the structure and data of each table.

Uploaded by

Shehan Jayalath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views20 pages

MIT 1203 - Database Systems Assignment On SQL: Index No: 19550359 Registration No: 2019/MIT/035 Name: S.D. Jalath

The document describes S.D. Jalath creating tables, inserting data, and performing SQL queries for an assignment on database systems. Tables were created for Customer, Sales, Vendor_Info, Products, and Vendors. Data was then inserted into these tables. Queries were run to view the structure and data of each table.

Uploaded by

Shehan Jayalath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Index No: 19550359

Registration No: 2019/MIT/035


Name: S.D. Jalath

MIT 1203 – Database Systems


Assignment on SQL

mysql> TEE /Users/SDJalath/Desktop/SQLAssignmnet/19550359/19550359.txt

01).Create tables and insert data

mysql> CREATE DATABASE assignment_2;


Query OK, 1 row affected (0.00 sec)

Create tables

Table: Customer PK: CustomerId

mysql> CREATE TABLE Customer (


-> CustomerId int NOT NULL,
-> CustomerName varchar(50) NOT NULL,
-> CustomerCity varchar(50) NOT NULL,
-> PRIMARY KEY (CustomerId)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> DESCRIBE Customer;


+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| CustomerId | int | NO | PRI | NULL | |
| CustomerName | varchar(50) | NO | | NULL | |
| CustomerCity | varchar(50) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
Table: Sales PK: OrderId FK: CustomerId

mysql> CREATE TABLE Sales (


-> OrderId int NOT NULL,
-> OrderDate date NOT NULL,
-> OrderPrice int NOT NULL,
-> OrderQuantity int NOT NULL,
-> CustomerId int NOT NULL,
-> PRIMARY KEY (OrderId),
-> FOREIGN KEY (CustomerId) REFERENCES Customer(CustomerId)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> DESCRIBE Sales;


+---------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------+------+-----+---------+-------+
| OrderId | int | NO | PRI | NULL | |
| OrderDate | date | NO | | NULL | |
| OrderPrice | int | NO | | NULL | |
| OrderQuantity | int | NO | | NULL | |
| CustomerId | int | NO | MUL | NULL | |
+---------------+------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Table: Vendor_Info PK: VendorId

mysql> CREATE TABLE Vendor_Info (


-> VendorId int NOT NULL,
-> Vendor_Name varchar(50) NOT NULL,
-> PRIMARY KEY (VendorId)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> DESCRIBE Vendor_Info;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| VendorId | int | NO | PRI | NULL | |
| Vendor_Name | varchar(50) | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
Table: Products PK: ProductId FK: OrderId, VendorId
mysql> CREATE TABLE Products (
-> ProductId varchar(50) NOT NULL,
-> OrderId int NOT NULL,
-> ManufactureDate date NOT NULL,
-> RawMaterial varchar(50) NOT NULL,
-> VendorId int NOT NULL,
-> PRIMARY KEY (ProductId),
-> FOREIGN KEY (OrderId) REFERENCES Sales(OrderId),
-> FOREIGN KEY (VendorId) REFERENCES Vendor_Info(VendorId)
-> );
Query OK, 0 rows affected (0.02 sec)

mysql> DESCRIBE Products;


+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| ProductId | varchar(50) | NO | PRI | NULL | |
| OrderId | int | NO | MUL | NULL | |
| ManufactureDate | date | NO | | NULL | |
| RawMaterial | varchar(50) | NO | | NULL | |
| VendorId | int | NO | MUL | NULL | |
+-----------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Table: Products PK: VendorId, RawMaterial (Composite Key)


mysql> CREATE TABLE Vendors (
-> RawMaterial varchar(50) NOT NULL,
-> Vendors varchar(50) NOT NULL,
-> VendorId int,
-> PRIMARY KEY (VendorId, RawMaterial),
-> FOREIGN KEY (VendorId) REFERENCES Vendor_Info(VendorId)
-> );
Query OK, 0 rows affected (0.09 sec)

mysql> DESCRIBE Vendors;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| RawMaterial | varchar(50) | NO | PRI | NULL | |
| Vendors | varchar(50) | NO | | NULL | |
| VendorId | int | NO | PRI | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
ALL tables
mysql> SHOW TABLES;
+------------------------+
| Tables_in_assignment_2 |
+------------------------+
| Customer |
| Products |
| Sales |
| Vendor_Info |
| Vendors |
+------------------------+
5 rows in set (0.01 sec)

Insert Data

Table: Customer

mysql> INSERT INTO Customer (CustomerId, CustomerName, CustomerCity)


-> VALUES
-> (1, "Smith", "Toronto"),
-> (2, "Baldwin", "Ottawa"),
-> (3, "Johnson", "Victoria"),
-> (4, "Wood", "Vancouver");
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM Customer;


+------------+--------------+--------------+
| CustomerId | CustomerName | CustomerCity |
+------------+--------------+--------------+
| 1 | Smith | Toronto |
| 2 | Baldwin | Ottawa |
| 3 | Johnson | Victoria |
| 4 | Wood | Vancouver |
+------------+--------------+--------------+
4 rows in set (0.00 sec)
Table: Sales
mysql> INSERT INTO Sales (OrderId, OrderDate, OrderPrice, OrderQuantity,
CustomerId)
-> VALUES
-> (1, STR_TO_DATE ("22/12/2005", "%d/%m/%Y"), 160, 2, 1),
-> (2, STR_TO_DATE ("10/08/2005", "%d/%m/%Y"), 190, 2, 3),
-> (3, STR_TO_DATE ("13/07/2005", "%d/%m/%Y"), 500, 5, 2),
-> (4, STR_TO_DATE ("15/07/2005", "%d/%m/%Y"), 420, 2, 1),
-> (5, STR_TO_DATE ("22/12/2005", "%d/%m/%Y"), 1000, 4, 4),
-> (6, STR_TO_DATE ("02/10/2005", "%d/%m/%Y"), 820, 4, 1),
-> (7, STR_TO_DATE ("03/11/2005", "%d/%m/%Y"), 2000, 2, 2),
-> (8, STR_TO_DATE ("22/12/2002", "%d/%m/%Y"), 1000, 4, 4),
-> (9, STR_TO_DATE ("29/12/2004", "%d/%m/%Y"), 5000, 4, 1);
Query OK, 9 rows affected (0.01 sec)
Records: 9 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM Sales;


+---------+------------+------------+---------------+------------+
| OrderId | OrderDate | OrderPrice | OrderQuantity | CustomerId |
+---------+------------+------------+---------------+------------+
| 1 | 2005-12-22 | 160 | 2 | 1 |
| 2 | 2005-08-10 | 190 | 2 | 3 |
| 3 | 2005-07-13 | 500 | 5 | 2 |
| 4 | 2005-07-15 | 420 | 2 | 1 |
| 5 | 2005-12-22 | 1000 | 4 | 4 |
| 6 | 2005-10-02 | 820 | 4 | 1 |
| 7 | 2005-11-03 | 2000 | 2 | 2 |
| 8 | 2002-12-22 | 1000 | 4 | 4 |
| 9 | 2004-12-29 | 5000 | 4 | 1 |
+---------+------------+------------+---------------+------------+
9 rows in set (0.00 sec)

Table: Vendor_Info

mysql> INSERT INTO Vendor_Info (VendorId, Vendor_Name)


-> VALUES
-> (1, “Smith"),
-> (2, “Wills"),
-> (3, “Johnson"),
-> (4, “Roger”);
Query OK, 4 rows affected (0.00 sec)

mysql> SELECT * FROM Vendor_Info;


+----------+-------------+
| VendorId | Vendor_Name |
+----------+-------------+
| 1 | Smith |
| 2 | Wills |
| 3 | Johnson |
| 4 | Roger |
+----------+-------------+
4 rows in set (0.00 sec)
Table: Products

mysql> INSERT INTO Products (ProductId, OrderId, ManufactureDate, RawMaterial,


VendorId)
-> VALUES
-> ("AZ145", 2, STR_TO_DATE("23/12/2005", "%d/%m/%Y"), "Steel", 1),
-> ("CS784", 4, STR_TO_DATE("28/11/2005", "%d/%m/%Y"), "Plastic", 2),
-> ("AZ147", 6, STR_TO_DATE("15/08/2002", "%d/%m/%Y"), "Steel", 3),
-> ("FD344", 3, STR_TO_DATE("03/11/2005", "%d/%m/%Y"), "Milk", 1),
-> ("GR233", 3, STR_TO_DATE("30/11/2005", "%d/%m/%Y"), "Pulses", 2),
-> ("FD123", 2, STR_TO_DATE("03/10/2005", "%d/%m/%Y"), "Milk", 2),
-> ("CS783", 1, STR_TO_DATE("03/11/2004", "%d/%m/%Y"), "Plastic", 2),
-> ("CS435", 5, STR_TO_DATE("04/11/2001", "%d/%m/%Y"), "Steel", 1),
-> ("GR567", 6, STR_TO_DATE("03/09/2005", "%d/%m/%Y"), "Pulses", 2),
-> ("FD267", 5, STR_TO_DATE("21/03/2002", "%d/%m/%Y"), "Bread", 4),
-> ("FD333", 9, STR_TO_DATE("12/12/2001", "%d/%m/%Y"), "Milk", 1),
-> ("CS321", 7, STR_TO_DATE("03/09/2004", "%d/%m/%Y"), "Steel", 3),
-> ("GR489", 8, STR_TO_DATE("24/12/2005", "%d/%m/%Y"), "Pulses", 2);
Query OK, 13 rows affected (0.01 sec)
Records: 13 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM Products;


+-----------+---------+-----------------+-------------+----------+
| ProductId | OrderId | ManufactureDate | RawMaterial | VendorId |
+-----------+---------+-----------------+-------------+----------+
| AZ145 | 2 | 2005-12-23 | Steel | 1 |
| AZ147 | 6 | 2002-08-15 | Steel | 3 |
| CS321 | 7 | 2004-09-03 | Steel | 3 |
| CS435 | 5 | 2001-11-04 | Steel | 1 |
| CS783 | 1 | 2004-11-03 | Plastic | 2 |
| CS784 | 4 | 2005-11-28 | Plastic | 2 |
| FD123 | 2 | 2005-10-03 | Milk | 2 |
| FD267 | 5 | 2002-03-21 | Bread | 4 |
| FD333 | 9 | 2001-12-12 | Milk | 1 |
| FD344 | 3 | 2005-11-03 | Milk | 1 |
| GR233 | 3 | 2005-11-30 | Pulses | 2 |
| GR489 | 8 | 2005-12-24 | Pulses | 2 |
| GR567 | 6 | 2005-09-03 | Pulses | 2 |
+-----------+---------+-----------------+-------------+----------+
13 rows in set (0.00 sec)
Table: Vendors
mysql> INSERT INTO Vendors (RawMaterial, Vendors, VendorId)
-> VALUES
-> ("Bread", "Roger", 4),
-> ("Bread", "Wills", 2),
-> ("Milk", "Smith", 1),
-> ("Milk", "Wills", 2),
-> ("Plastic", "Wills", 2),
-> ("Pulses", "Wills", 2),
-> ("Steel", "Johnson", 3),
-> ("Steel", "Smith", 1)
-> ;
Query OK, 8 rows affected (0.02 sec)
Records: 8 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM Vendors;


+-------------+---------+----------+
| RawMaterial | Vendors | VendorId |
+-------------+---------+----------+
| Milk | Smith | 1 |
| Steel | Smith | 1 |
| Bread | Wills | 2 |
| Milk | Wills | 2 |
| Plastic | Wills | 2 |
| Pulses | Wills | 2 |
| Steel | Johnson | 3 |
| Bread | Roger | 4 |
+-------------+---------+----------+
8 rows in set (0.00 sec)
02). SQL queries

- For some questions I tried a few alternative queries.


- Assumptions are made and wrote prior to the query
relevant to the question
- Here I have edited the file generated by the “TEE”
command to make assumptions and comments

mysql> -- SQL queries

01).
mysql> SELECT p.ProductId, p.ManufactureDate, s.OrderDate, p.RawMaterial,
YEAR(p.ManufactureDate) as Year
-> FROM Products p
-> INNER JOIN Sales s
-> ON p.OrderId = s.OrderId
-> WHERE YEAR(p.ManufactureDate) = YEAR(s.OrderDate);
+-----------+-----------------+------------+-------------+------+
| ProductId | ManufactureDate | OrderDate | RawMaterial | Year |
+-----------+-----------------+------------+-------------+------+
| AZ145 | 2005-12-23 | 2005-08-10 | Steel | 2005 |
| FD123 | 2005-10-03 | 2005-08-10 | Milk | 2005 |
| FD344 | 2005-11-03 | 2005-07-13 | Milk | 2005 |
| GR233 | 2005-11-30 | 2005-07-13 | Pulses | 2005 |
| CS784 | 2005-11-28 | 2005-07-15 | Plastic | 2005 |
| GR567 | 2005-09-03 | 2005-10-02 | Pulses | 2005 |
+-----------+-----------------+------------+-------------+------+
6 rows in set (0.00 sec)

An alternative query
mysql> SELECT p.*
-> FROM Products p, Sales s
-> WHERE YEAR(p.ManufactureDate) = YEAR(s.OrderDate) AND p.OrderId = s.OrderId;
+-----------+---------+-----------------+-------------+----------+
| ProductId | OrderId | ManufactureDate | RawMaterial | VendorId |
+-----------+---------+-----------------+-------------+----------+
| AZ145 | 2 | 2005-12-23 | Steel | 1 |
| FD123 | 2 | 2005-10-03 | Milk | 2 |
| FD344 | 3 | 2005-11-03 | Milk | 1 |
| GR233 | 3 | 2005-11-30 | Pulses | 2 |
| CS784 | 4 | 2005-11-28 | Plastic | 2 |
| GR567 | 6 | 2005-09-03 | Pulses | 2 |
+-----------+---------+-----------------+-------------+----------+
6 rows in set (0.00 sec)
02).
mysql> SELECT p.ProductId, p.ManufactureDate, s.OrderDate, p.RawMaterial,
v.Vendor_Name, YEAR(p.ManufactureDate) as Year
-> FROM ((Products p
-> INNER JOIN Sales s
-> ON p.OrderId = s.OrderId)
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> WHERE YEAR(p.ManufactureDate) = YEAR(s.OrderDate) AND v.Vendor_Name =
"Smith";
+-----------+-----------------+------------+-------------+-------------+------+
| ProductId | ManufactureDate | OrderDate | RawMaterial | Vendor_Name | Year |
+-----------+-----------------+------------+-------------+-------------+------+
| AZ145 | 2005-12-23 | 2005-08-10 | Steel | Smith | 2005 |
| FD344 | 2005-11-03 | 2005-07-13 | Milk | Smith | 2005 |
+-----------+-----------------+------------+-------------+-------------+------+
2 rows in set (0.02 sec)

An alternative query
mysql> SELECT p.*
-> FROM Products p, Sales s
-> WHERE YEAR(p.ManufactureDate) = YEAR(s.OrderDate) AND p.OrderId = s.OrderId
and p.VendorId = (
-> SELECT VendorId FROM Vendor_Info WHERE Vendor_Name = 'Smith');
+-----------+---------+-----------------+-------------+----------+
| ProductId | OrderId | ManufactureDate | RawMaterial | VendorId |
+-----------+---------+-----------------+-------------+----------+
| AZ145 | 2 | 2005-12-23 | Steel | 1 |
| FD344 | 3 | 2005-11-03 | Milk | 1 |
+-----------+---------+-----------------+-------------+----------+
2 rows in set (0.02 sec)
03).
Assumption: The number of orders is determined by the number of OrderQuantity
mysql> SELECT YEAR(OrderDate) AS Year, SUM(OrderQuantity) AS
Total_number_of_order_placed
-> FROM Sales
-> GROUP BY YEAR(OrderDate);
+------+------------------------------+
| Year | Total_number_of_order_placed |
+------+------------------------------+
| 2005 | 21 |
| 2002 | 4 |
| 2004 | 4 |
+------+------------------------------+
3 rows in set (0.00 sec)

Assumption: That there is one order number per order


mysql> SELECT YEAR(OrderDate) AS Year, COUNT(OrderId) AS
Total_number_of_orders_placed
-> FROM Sales
-> GROUP BY YEAR(OrderDate);
+------+-------------------------------+
| Year | Total_number_of_orders_placed |
+------+-------------------------------+
| 2005 | 7 |
| 2002 | 1 |
| 2004 | 1 |
+------+-------------------------------+
3 rows in set (0.00 sec)

Assumption: That there is one order number per order and That the order quantity
contains the product quantity
mysql> SELECT YEAR(s.OrderDate) AS Year, COUNT(s.OrderId) AS
Total_number_of_orders_placed
-> FROM (Sales s INNER JOIN Products p
-> ON s.OrderId = p.OrderId)
-> GROUP BY YEAR(s.OrderDate);
+------+-------------------------------+
| Year | Total_number_of_orders_placed |
+------+-------------------------------+
| 2005 | 11 |
| 2002 | 1 |
| 2004 | 1 |
+------+-------------------------------+
3 rows in set (0.00 sec)
04).
mysql> SELECT v.Vendor_Name, YEAR(s.OrderDate) AS Year, COUNT(s.OrderId) AS
Total_number_of_orders_placed
-> FROM ((Sales s
-> INNER JOIN Products p
-> ON s.OrderId = p.OrderId)
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> WHERE v.Vendor_Name = "Wills"
-> GROUP BY YEAR(s.OrderDate);
+-------------+------+-------------------------------+
| Vendor_Name | Year | Total_number_of_orders_placed |
+-------------+------+-------------------------------+
| Wills | 2005 | 5 |
| Wills | 2002 | 1 |
+-------------+------+-------------------------------+
2 rows in set (0.00 sec)

05).
Assumption: vendors and customers with similar names are considered as the same
person
mysql> SELECT v.Vendor_Name AS vendor_and_customer_both, v.VendorId, c.CustomerId
-> FROM Vendor_Info v
-> INNER JOIN Customer c
-> ON v.Vendor_Name = c.CustomerName
-> WHERE v.Vendor_Name = c.CustomerName;
+--------------------------+----------+------------+
| vendor_and_customer_both | VendorId | CustomerId |
+--------------------------+----------+------------+
| Smith | 1 | 1 |
| Johnson | 3 | 3 |
+--------------------------+----------+------------+
2 rows in set (0.01 sec)

An alternative query (Using EXISTS)


mysql> SELECT c.CustomerName AS vendor_and_customer_both
-> FROM Customer c
-> WHERE
-> EXISTS (
-> SELECT v.Vendor_Name
-> FROM Vendor_Info v
-> WHERE c.CustomerName=v.Vendor_Name);
+--------------------------+
| vendor_and_customer_both |
+--------------------------+
| Smith |
| Johnson |
+--------------------------+
2 rows in set (0.01 sec)
06).
Assumption: That only "Milk", "Pulses", and "Bread" are used for food production,
that “Steel”, and “Plastic” is not used even for covers.
mysql> SELECT YEAR(s.OrderDate) AS Year, COUNT(p.RawMaterial) AS
Total_number_of_food_items
-> FROM Sales s
-> INNER JOIN Products p
-> ON s.OrderId = p.OrderId
-> WHERE p.RawMaterial IN ("Milk", "Pulses", "Bread")
-> GROUP BY YEAR(s.OrderDate);
+------+----------------------------+
| Year | Total_number_of_food_items |
+------+----------------------------+
| 2005 | 5 |
| 2004 | 1 |
| 2002 | 1 |
+------+----------------------------+
3 rows in set (0.03 sec)

Assumption: That all products ordered are food products


mysql> SELECT YEAR(OrderDate) AS Year, SUM(OrderQuantity) AS
Total_number_of_food_items
-> FROM Sales
-> GROUP BY YEAR(OrderDate);
+------+----------------------------+
| Year | Total_number_of_food_items |
+------+----------------------------+
| 2005 | 21 |
| 2002 | 4 |
| 2004 | 4 |
+------+----------------------------+
3 rows in set (0.00 sec)
07).
mysql> SELECT YEAR(s.OrderDate) AS Year, p.RawMaterial, SUM(s.OrderQuantity) AS
Total_number_of_food_items
-> FROM Sales s
-> INNER JOIN Products p
-> ON s.OrderId = p.OrderId
-> WHERE p.RawMaterial = "Bread"
-> GROUP BY YEAR(s.OrderDate);
+------+-------------+----------------------------+
| Year | RawMaterial | Total_number_of_food_items |
+------+-------------+----------------------------+
| 2005 | Bread | 4 |
+------+-------------+----------------------------+
1 row in set (0.00 sec)

An alternative query
mysql> SELECT YEAR(OrderDate) AS YEAR, SUM(OrderQuantity) AS
Total_number_of_food_items
-> FROM Sales
-> WHERE OrderId IN (
-> SELECT OrderId
-> FROM Products
-> WHERE RawMaterial = "Bread")
-> GROUP BY YEAR(OrderDate);
+------+----------------------------+
| YEAR | Total_number_of_food_items |
+------+----------------------------+
| 2005 | 4 |
+------+----------------------------+
1 row in set (0.00 sec)

08).
mysql> SELECT p.ProductId, v.Vendor_Name, c.CustomerName
-> FROM (((Products p
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> INNER JOIN Sales s
-> ON p.OrderId = s.OrderId)
-> INNER JOIN Customer c
-> ON s.CustomerId = c.CustomerId)
-> WHERE v.Vendor_Name != c.CustomerName;
+-----------+-------------+--------------+
| ProductId | Vendor_Name | CustomerName |
+-----------+-------------+--------------+
| AZ145 | Smith | Johnson |
| CS435 | Smith | Wood |
| FD344 | Smith | Baldwin |
| CS783 | Wills | Smith |
| CS784 | Wills | Smith |
| FD123 | Wills | Johnson |
| GR233 | Wills | Baldwin |
| GR489 | Wills | Wood |
| GR567 | Wills | Smith |
| AZ147 | Johnson | Smith |
| CS321 | Johnson | Baldwin |
| FD267 | Roger | Wood |
+-----------+-------------+--------------+
12 rows in set (0.00 sec)
09).
Assumption: Smith is treated as a vendor as well as a customer, and keeps track of
the goods he sells as a business. Or Having a vendor and customer named Smith

mysql> SELECT c.CustomerName, p.RawMaterial, vi.Vendor_Name


-> FROM (((Products p
-> INNER JOIN Vendor_Info vi
-> ON p.VendorId=vi.VendorId)
-> INNER JOIN Sales s
-> ON p.OrderId=s.OrderId)
-> INNER JOIN Customer c
-> ON s.CustomerId=c.CustomerId)
-> WHERE p.RawMaterial="Milk" AND vi.Vendor_Name="Smith";
+--------------+-------------+-------------+
| CustomerName | RawMaterial | Vendor_Name |
+--------------+-------------+-------------+
| Smith | Milk | Smith |
| Baldwin | Milk | Smith |
+--------------+-------------+-------------+
2 rows in set (0.00 sec)

Assumption: Smith cannot pretend to be a vendor and a customer at the same time
mysql> SELECT c.CustomerName, p.RawMaterial, vi.Vendor_Name
-> FROM (((Products p
-> INNER JOIN Vendor_Info vi
-> ON p.VendorId=vi.VendorId)
-> INNER JOIN Sales s
-> ON p.OrderId=s.OrderId)
-> INNER JOIN Customer c
-> ON s.CustomerId=c.CustomerId)
-> WHERE p.RawMaterial="Milk" AND vi.Vendor_Name="Smith" AND c.CustomerName !=
vi.Vendor_Name;;
+--------------+-------------+-------------+
| CustomerName | RawMaterial | Vendor_Name |
+--------------+-------------+-------------+
| Baldwin | Milk | Smith |
+--------------+-------------+-------------+
1 row in set (0.00 sec)
10).
Assumption: The number of orders is determined by the number of OrderQuantity
mysql> SELECT vi.Vendor_Name, year(s.OrderDate) AS YEAR, SUM(s.OrderQuantity) AS
Total_number_of_orders
-> FROM Products p
-> NATURAL JOIN Vendor_Info vi NATURAL JOIN Sales s
-> GROUP BY Vendor_Name, YEAR(OrderDate);
+-------------+------+------------------------+
| Vendor_Name | YEAR | Total_number_of_orders |
+-------------+------+------------------------+
| Smith | 2005 | 11 |
| Smith | 2004 | 4 |
| Wills | 2005 | 15 |
| Wills | 2002 | 4 |
| Johnson | 2005 | 6 |
| Roger | 2005 | 4 |
+-------------+------+------------------------+
6 rows in set (0.01 sec)

Assumption: That there is one order number per order


mysql> SELECT v.Vendor_Name, YEAR(s.OrderDate) AS YEAR, COUNT(s.OrderId) AS
Total_number_of_orders
-> FROM ((Vendor_Info v
-> INNER JOIN Products p
-> ON v.VendorId = p.VendorId)
-> INNER JOIN Sales s
-> ON p.OrderId = s.OrderId)
-> GROUP BY v.VendorId, YEAR(s.OrderDate);
+-------------+------+------------------------+
| Vendor_Name | YEAR | Total_number_of_orders |
+-------------+------+------------------------+
| Smith | 2005 | 3 |
| Smith | 2004 | 1 |
| Wills | 2005 | 5 |
| Wills | 2002 | 1 |
| Johnson | 2005 | 2 |
| Roger | 2005 | 1 |
+-------------+------+------------------------+
6 rows in set (0.00 sec)
11).
Assumption: Total_Amount was taken as the sum of (OrderPrice * OrderQuantity)
mysql> SELECT Vendor_Name, year(OrderDate) AS Year, sum(OrderPrice*OrderQuantity)
as TotalAmount
-> FROM Products p
-> NATURAL JOIN Vendor_Info vi
-> NATURAL JOIN Sales s group by vi.Vendor_Name, YEAR(OrderDate)
-> HAVING SUM(s.OrderPrice * s.OrderQuantity) > 200;
+-------------+------+-------------+
| Vendor_Name | Year | TotalAmount |
+-------------+------+-------------+
| Smith | 2005 | 6880 |
| Smith | 2004 | 20000 |
| Wills | 2005 | 7320 |
| Wills | 2002 | 4000 |
| Johnson | 2005 | 7280 |
| Roger | 2005 | 4000 |
+-------------+------+-------------+
6 rows in set (0.01 sec)

Assumption: Order_price_total was taken as the sum of (OrderPrice) and Only the
total amount(Order_price_total) over 200 will be considered.
mysql> SELECT YEAR(s.OrderDate) AS Year, v.Vendor_Name, SUM(s.OrderPrice) AS
Order_price_total
-> FROM ((Sales s
-> INNER JOIN Products p
-> ON s.OrderId = p.OrderId)
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> GROUP BY YEAR(s.OrderDate), v.VendorId
-> HAVING Order_price_total > 200;
+------+-------------+-------------------+
| Year | Vendor_Name | Order_price_total |
+------+-------------+-------------------+
| 2005 | Smith | 1690 |
| 2004 | Smith | 5000 |
| 2005 | Wills | 2090 |
| 2002 | Wills | 1000 |
| 2005 | Johnson | 2820 |
| 2005 | Roger | 1000 |
+------+-------------+-------------------+
6 rows in set (0.07 sec)
Assumption: Order_price_total was taken as the sum of (OrderPrice) and Only the
OrderPrice over 200 will be considered.
mysql> SELECT YEAR(s.OrderDate) AS Year, v.Vendor_Name, SUM(s.OrderPrice) AS
Order_price_total
-> FROM ((Sales s
-> INNER JOIN Products p
-> ON s.OrderId = p.OrderId)
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> WHERE s.OrderPrice > 200
-> GROUP BY YEAR(s.OrderDate), v.VendorId;
+------+-------------+-------------------+
| Year | Vendor_Name | Order_price_total |
+------+-------------+-------------------+
| 2005 | Smith | 1500 |
| 2005 | Wills | 1740 |
| 2005 | Roger | 1000 |
| 2005 | Johnson | 2820 |
| 2002 | Wills | 1000 |
| 2004 | Smith | 5000 |
+------+-------------+-------------------+
6 rows in set (0.00 sec)
12).
mysql> SELECT v.Vendor_Name, c.CustomerCity AS City
-> FROM (((Customer c
-> INNER JOIN Sales s
-> ON c.CustomerId = s.CustomerId)
-> INNER JOIN Products p
-> ON s.OrderId = p.OrderId)
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> WHERE c.CustomerCity = "Vancouver";
+-------------+-----------+
| Vendor_Name | City |
+-------------+-----------+
| Smith | Vancouver |
| Roger | Vancouver |
| Wills | Vancouver |
+-------------+-----------+
3 rows in set (0.00 sec)

13).
mysql> SELECT s.OrderId, p.RawMaterial, (s.OrderPrice / OrderQuantity) AS
Unit_price
-> FROM Products p
-> INNER JOIN Sales s
-> ON p.OrderId = s.OrderId
-> ORDER BY Unit_price DESC;
+---------+-------------+------------+
| OrderId | RawMaterial | Unit_price |
+---------+-------------+------------+
| 9 | Milk | 1250.0000 |
| 7 | Steel | 1000.0000 |
| 5 | Steel | 250.0000 |
| 5 | Bread | 250.0000 |
| 8 | Pulses | 250.0000 |
| 4 | Plastic | 210.0000 |
| 6 | Steel | 205.0000 |
| 6 | Pulses | 205.0000 |
| 3 | Milk | 100.0000 |
| 3 | Pulses | 100.0000 |
| 2 | Steel | 95.0000 |
| 2 | Milk | 95.0000 |
| 1 | Plastic | 80.0000 |
+---------+-------------+------------+
13 rows in set (0.00 sec)
14).
Assumption: Consider only the raw material sold by one vendor
mysql> SELECT Vendors, COUNT(RawMaterial) AS Raw_materials_count
-> FROM Vendors
-> GROUP BY Vendors
-> HAVING Raw_materials_count > 1;
+---------+---------------------+
| Vendors | Raw_materials_count |
+---------+---------------------+
| Smith | 2 |
| Wills | 4 |
+---------+---------------------+
2 rows in set (0.00 sec)

Assumption: The fact that a product may have several Raw Materials and that it may
increase with the number of product orders
mysql> SELECT v.Vendors, COUNT(v.RawMaterial) AS Raw_materials_count
-> FROM ((Vendors v
-> INNER JOIN Products p
-> ON v.vendorId = p.vendorId)
-> INNER JOIN Sales s
-> ON p.orderId = p.OrderId)
-> GROUP BY v.vendors
-> HAVING Raw_materials_count > 1;
+---------+---------------------+
| Vendors | Raw_materials_count |
+---------+---------------------+
| Smith | 72 |
| Wills | 216 |
| Johnson | 18 |
| Roger | 9 |
+---------+---------------------+
4 rows in set (0.00 sec)
15). Without “OrderId” Field
mysql> SELECT c.CustomerName, v.Vendor_Name, p.RawMaterial, s.OrderQuantity
-> FROM (((Sales s
-> INNER JOIN Customer c
-> ON s.CustomerId = c.CustomerId)
-> INNER JOIN Products p
-> On s.OrderId = p.OrderId)
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> WHERE s.OrderQuantity > 1
-> ORDER BY s.OrderQuantity ASC;
+--------------+-------------+-------------+---------------+
| CustomerName | Vendor_Name | RawMaterial | OrderQuantity |
+--------------+-------------+-------------+---------------+
| Smith | Wills | Plastic | 2 |
| Johnson | Smith | Steel | 2 |
| Johnson | Wills | Milk | 2 |
| Smith | Wills | Plastic | 2 |
| Baldwin | Johnson | Steel | 2 |
| Wood | Smith | Steel | 4 |
| Wood | Roger | Bread | 4 |
| Smith | Johnson | Steel | 4 |
| Smith | Wills | Pulses | 4 |
| Wood | Wills | Pulses | 4 |
| Smith | Smith | Milk | 4 |
| Baldwin | Smith | Milk | 5 |
| Baldwin | Wills | Pulses | 5 |
+--------------+-------------+-------------+---------------+
13 rows in set (0.00 sec)

With “OrderId” Field


mysql> SELECT s.OrderId, c.CustomerName, v.Vendor_Name, p.RawMaterial,
s.OrderQuantity
-> FROM (((Sales s
-> INNER JOIN Customer c
-> ON s.CustomerId = c.CustomerId)
-> INNER JOIN Products p
-> On s.OrderId = p.OrderId)
-> INNER JOIN Vendor_Info v
-> ON p.VendorId = v.VendorId)
-> WHERE s.OrderQuantity > 1
-> ORDER BY s.OrderQuantity ASC;
+---------+--------------+-------------+-------------+---------------+
| OrderId | CustomerName | Vendor_Name | RawMaterial | OrderQuantity |
+---------+--------------+-------------+-------------+---------------+
| 1 | Smith | Wills | Plastic | 2 |
| 2 | Johnson | Smith | Steel | 2 |
| 2 | Johnson | Wills | Milk | 2 |
| 4 | Smith | Wills | Plastic | 2 |
| 7 | Baldwin | Johnson | Steel | 2 |
| 5 | Wood | Smith | Steel | 4 |
| 5 | Wood | Roger | Bread | 4 |
| 6 | Smith | Johnson | Steel | 4 |
| 6 | Smith | Wills | Pulses | 4 |
| 8 | Wood | Wills | Pulses | 4 |
| 9 | Smith | Smith | Milk | 4 |
| 3 | Baldwin | Smith | Milk | 5 |
| 3 | Baldwin | Wills | Pulses | 5 |
+---------+--------------+-------------+-------------+---------------+
13 rows in set (0.00 sec)

You might also like