MIT 1203 - Database Systems Assignment On SQL: Index No: 19550359 Registration No: 2019/MIT/035 Name: S.D. Jalath
MIT 1203 - Database Systems Assignment On SQL: Index No: 19550359 Registration No: 2019/MIT/035 Name: S.D. Jalath
Create tables
Insert Data
Table: Customer
Table: Vendor_Info
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 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
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
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: 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)