Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

ELECT 2: Technical Elective

DATABASE MANAGEMENT SYSTEM


ENGR. JONICIO A. DACUYA
Instructor

CHAPTER 7
Chapter VII

Basic Functions

Function in its simplest definition is simple a small program that performs a certain and specific
task. It works like mathematical functions which transform values into another result.

Example 1:
Display how many rows are there in a row set of an Employee1 table. Use the COUNT() function.
Here is the syntax of COUNT() function:

SELECT COUNT(*) FROM TableName;

Where:
COUNT(*) returns a count of the number of rows in the table.

Solution:
SELECT COUNT(*) AS 'Row Count' FROM Employee1;
+-----------+
| Row Count |
+-----------+
| 12 |
+-----------+

Explanation: As what the output says, there are 12 records (rows) in our Employee1 table. Let us
see if this is true by issuing the following SQL query:

SELECT * FROM EMPLOYEE1;


+----------------+-----------------+----------+---------------------+
| employeenumber | name | salary | address |
+----------------+-----------------+----------+---------------------+
| 1001 | Juan Cruise | 20500.00 | Project 8, Q.C. |
| 1002 | Gabriela Silang | 19500.00 | Project 4, Q.C. |
| 1003 | Diego Silang | 29300.00 | Project 4, Q.C. |
| 1004 | Pedro Cruise | 16200.00 | Project 2, Q.C. |
| 1006 | Adres Bonifacio | 28600.00 | Project 4, Q.C. |
| 1007 | Jose Rizal | 27700.00 | Calamba, Laguna |
| 1008 | Terio Balutlot | 23100.00 | Cubao, Q.C. |
| 1009 | Gavino Purisa | 23100.00 | SSS Vill., Marikina |
| 1010 | Lenny Panogalog | 21800.00 | San Mateo, Rizal |
| 1005 | Lolit Bando | NULL | Parang, Marikina |
| 1011 | Chona Hermosa | NULL | San Mateo, Rizal |
| 1099 | Pedro Pendoku | 51876.00 | Naval, Biliran |
+----------------+-----------------+----------+---------------------+
See? There were 12 Employees recorded at Employee1 table.

Example 2:
Count the number of salary in the Salary column of Employee1 table. Use the COUNT() function.

Solution:
SELECT COUNT(Salary) AS 'Salary Count' FROM Employee1;
+--------------+
| Salary Count |
+--------------+
| 10 |
+--------------+
Explanation:
Though our table has 12 employees recorded on it, the Salary column has NULL values which are
not counted by the COUNT function. Specifically, Salary column has two NULL values on it that
was not counted.
Note: The SQL will not allow us to count two or more columns at the same time. Take the following
query as example:

SELECT COUNT(Name, Salary) AS 'Name and Salary' FROM Employee1;

ERROR 1064 (42000): You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the
right syntax to use near ' Salary) AS 'Name and Salary' FROM
Employee1' at line 1.

Example 3:
Increase the salary of all employees with an additional 50 pesos. Use the Employee1 table. Display
only the name, the old salary, and the new salary.

Solution:
SELECT Name, Salary, (Salary + 50) AS 'New Salary' FROM
Employee1;
+-----------------+----------+------------+
| Name | Salary | New Salary |
+-----------------+----------+------------+
| Juan Cruise | 20500.00 | 20550.00 |
| Gabriela Silang | 19500.00 | 19550.00 |
| Diego Silang | 29300.00 | 29350.00 |
| Pedro Cruise | 16200.00 | 16250.00 |
| Adres Bonifacio | 28600.00 | 28650.00 |
| Jose Rizal | 27700.00 | 27750.00 |
| Terio Balutlot | 23100.00 | 23150.00 |
| Gavino Purisa | 23100.00 | 23150.00 |
| Lenny Panogalog | 21800.00 | 21850.00 |
| Lolit Bando | NULL | NULL |
| Chona Hermosa | NULL | NULL |
| Pedro Pendoku | 51876.00 | 51926.00 |
+-----------------+----------+------------+

Explanation:
We can see that all employees had a 50 salary increase except for those who has a NULL value
salary.

Example 4:
Increase the salary of all employees with an additional 10 percent. Use the Employee1 table.
Display only the name, the old salary and the new salary.

Solution:
SELECT Name, Salary, (Salary + Salary * 0.10) AS 'New Salary'
FROM Employee1;
+-----------------+----------+------------+
| Name | Salary | New Salary |
+-----------------+----------+------------+
| Juan Cruise | 20500.00 | 22550.0000 |
| Gabriela Silang | 19500.00 | 21450.0000 |
| Diego Silang | 29300.00 | 32230.0000 |
| Pedro Cruise | 16200.00 | 17820.0000 |
| Adres Bonifacio | 28600.00 | 31460.0000 |
| Jose Rizal | 27700.00 | 30470.0000 |
| Terio Balutlot | 23100.00 | 25410.0000 |
| Gavino Purisa | 23100.00 | 25410.0000 |
| Lenny Panogalog | 21800.00 | 23980.0000 |
| Lolit Bando | NULL | NULL |
| Chona Hermosa | NULL | NULL |
| Pedro Pendoku | 51876.00 | 57063.6000 |
+-----------------+----------+------------+

Explanation:
As we can see the salary of all the employees had an increase of 10 percent except those who has
a NULL value salary.

Example 5:
List the top 5 Commission in the Salesman1 table. Use the LIMIT() function. We need to create
the Salesman1 table first. Use the following SQL commands to create and populate the saleman1
table:

CREATE TABLE Salesman1 (Name CHAR(20), Commission DECIMAL(8,2));

INSERT INTO Salesman1 VALUES('Jacky Gates', 30000);


INSERT INTO Salesman1 VALUES('Oliver Homes', 40000);
INSERT INTO Salesman1 VALUES('Thomas Jefferun', 60000);
INSERT INTO Salesman1 VALUES('Harry Truboy', 20000);
INSERT INTO Salesman1 VALUES('Grace Aguilou', 25000);
INSERT INTO Salesman1 VALUES('Olivia Newtune', 35000);
INSERT INTO Salesman1 VALUES('Ruby Ramin', 50000);
INSERT INTO Salesman1 VALUES('Mia Fratz', 40000);
INSERT INTO Salesman1 VALUES('Lucy Florez', 55000);
INSERT INTO Salesman1 VALUES('Sophie Lorenz', 20000);
INSERT INTO Salesman1 VALUES('Ella Encante', 60000);
INSERT INTO Salesman1 VALUES('Ethan DelaCruz', 45000);
INSERT INTO Salesman1 VALUES('Daniel Schmidt', 50000);
INSERT INTO Salesman1 VALUES('James Alline', 48000);
INSERT INTO Salesman1 VALUES('Emily Lorin', 57000);
INSERT INTO Salesman1 VALUES('Amelia Kennycy', 55000);
INSERT INTO Salesman1 VALUES('Lily Cross', 70000);
INSERT INTO Salesman1 VALUES('Alfie Alasdo', 65000);
INSERT INTO Salesman1 VALUES('Joshua Fernadis', 80000);
INSERT INTO Salesman1 VALUES('Jessica Farker', 70000);

Solution:
SELECT * FROM Salesman1 ORDER BY Commission DESC LIMIT 5;

+-----------------+------------+
| Name | Commission |
+-----------------+------------+
| Joshua Fernadis | 80000.00 |
| Jessica Farker | 70000.00 |
| Lily Cross | 70000.00 |
| Alfie Alasdo | 65000.00 |
| Ella Encante | 60000.00 |
+-----------------+------------+

Example 6:
List the bottom 5 salesman in Salesman1 table. Apply the LIMIT function.
Solution:
SELECT * FROM Salesman1 ORDER BY Commission ASC LIMIT 5;
+----------------+------------+
| Name | Commission |
+----------------+------------+
| Sophie Lorenz | 20000.00 |
| Harry Truboy | 20000.00 |
| Grace Aguilou | 25000.00 |
| Jacky Gates | 30000.00 |
| Olivia Newtune | 35000.00 |
+----------------+------------+

Example 7:
List the top 20 percent (%) salesmen in Salesman1 table. Solution:
SELECT * FROM Salesman1 WHERE Commission > (SELECT
AVG(Commission) * 0.80 FROM Salesman1) ORDER BY Commission DESC
LIMIT 5;

+-----------------+------------+
| Name | Commission |
+-----------------+------------+
| Joshua Fernadis | 80000.00 |
| Jessica Farker | 70000.00 |
| Lily Cross | 70000.00 |
| Alfie Alasdo | 65000.00 |
| Thomas Jefferun | 60000.00 |
+-----------------+------------+

Example 8:
Display all the commissions but eliminate the duplicates. Use the Salesman1 table. Apply the
DISTINCT() function.

Solution:
SELECT DISTINCT(Commission) FROM Salesman1;
+------------+
| Commission |
+------------+
| 30000.00 |
| 40000.00 |
| 60000.00 |
| 20000.00 |
| 25000.00 |
| 35000.00 |
| 50000.00 |
| 55000.00 |
| 45000.00 |
| 48000.00 |
| 57000.00 |
| 70000.00 |
| 65000.00 |
| 80000.00 |
+------------+
Creating a View

A view looks like a table and works almost like a table. It is regarded a name SELECT
statement that produces a result set that we can further work on. The SELECT statement that is
used to create a view can be from one or more underlying table of from other views in the current
or other databases. We create a view as our way of hiding the logic that created the joined table
just displayed. Here is the general syntax in creating a view.

CREATE VIEW View_Name AS SELECT


...

Example 9:
Create a simple view from two tables where you list both the supplier and its respective item
supplied. Use the Supplier1 and Item1 tables.

Solution:
CREATE VIEW Item1_Supplier AS SELECT Item1.Descri,
Supplier1.SName FROM Item1, Supplier1 WHERE Item1.SCode =
Supplier1.Scode;

First we have to know first the data inside Item1 table by issuing the following SQL query:
SELECT * FROM Item1;
+-------+-------+----------------+-------+
| icode | scode | descri | price |
+-------+-------+----------------+-------+
| 101 | A2 | Hamburger | 20.00 |
| 102 | A2 | Cheesburger | 25.00 |
| 103 | A2 | Spaghetti | 30.00 |
| 104 | B3 | Coke(Large) | 17.00 |
| 105 | B3 | Sprite(Medium) | 15.00 |
+-------+-------+----------------+-------+

Then we have to display the data inside Supplier1 table by issuing the following SQL query:

SELECT * FROM Supplier1;


+-------+-------------------+-------------+
| SCode | SName | CityAdd |
+-------+-------------------+-------------+
| A2 | Del Monte Farms | Quezon City |
| B3 | Coca-Cola Bottles | Makati City |
+-------+-------------------+-------------+

This time, let us see the data inside the view we created, by issuing the following SQL query:
SELECT * FROM Item1_Supplier;
+----------------+-------------------+
| Descri | SName |
+----------------+-------------------+
| Hamburger | Del Monte Farms |
| Cheesburger | Del Monte Farms |
| Spaghetti | Del Monte Farms |
| Coke(Large) | Coca-Cola Bottles |
| Sprite(Medium) | Coca-Cola Bottles |
+----------------+-------------------+

Explanation:
See? Truly a view almost works and looks like a table! Isn’t it? With the data above, we
were able to extract the information we need to know what are the respective supplier for each
item listed. And at the same, time, we were able to put them in a view that works and looks like
a table.
Our view above contains specific data which we had extracted from two different tables,
namely the SUpplier1 and Item1. In this way, we can easily build complex reports out from two or
more tables.

Example 10:
Create a simple view from two tables where you list both the supplier and its respective
item supplied. Use Supplier1 and Item1 table. This time, we have to apply the column alias with
view. The alias we will assign for Descri column is Description while the alias for SName is Supplier
Name.

Explanation:
Previously, without using the column alias command, our output looks like the following
+----------------+-------------------+
| Descri | SName |
+----------------+-------------------+
| Hamburger | Del Monte Farms |
| Cheesburger | Del Monte Farms |
| Spaghetti | Del Monte Farms |
| Coke(Large) | Coca-Cola Bottles |
| Sprite(Medium) | Coca-Cola Bottles |
+----------------+-------------------+
where the columns are abbreviated,.

Now, with the use of column alias command, we have the complete column name as we issue the
following SQL query:

SELECT * From Item2_Supplier;


+----------------+-------------------+
| Description | Supplier Name |
+----------------+-------------------+
| Hamburger | Del Monte Farms |
| Cheesburger | Del Monte Farms |
| Spaghetti | Del Monte Farms |
| Coke(Large) | Coca-Cola Bottles |
| Sprite(Medium) | Coca-Cola Bottles |
+----------------+-------------------+
This is the beauty of using the column alias command. It makes our output more readable.
The Pareto Principle (80/20 Rule)

Mini Case Study Presentation

The Pareto Principle states that for many events, 20% of the causes produce 80% of the
effects. This principle is based on the observation of the Italian economist and sociologist Vilfredo
Pareto that according to him, 20% of the population in Italy owns 80% of the nation’s wealth. He
created the 80/20 Rule economic formula in 1906.

In our mini case study, we will apply this economic formula to manage effectively the
stocks and sales transaction of our hypothetical store which we call "Pareto Mart". In the Pareto
Mart, we will design and develop an SQL script that will be able to monitor and manage the sales
and stocks of products which we firmly believed as vital to the stores high profitability.

We theorize that only 20% of the most saleable products generate 80% of the entire
Pareto Marts profitability. Thus, monitoring and managing them effectively and efficiently is
critical to the marts survival and high profitability. We have to reconsider also that in real-life,
these saleable products are the ones mostly experiencing an "out-of-stock" problem. Therefore,
it is of utmost importance that we can monitor and manage their stock level as well their high
availability too, with respect to business transaction. Remember, that the moment these saleable
products are not available to customers when they buy them (due to "out of stocks problem"),
this means a financial loss to the company. In the days, weeks or months that these "out-of-stock"
products are unavailable, literally means a stop to business transaction that pertains to these
saleable products. So it is critical to monitor and manage them well.

Here in this mini case study, we need two tables - the stocks and sales table. We are to
create these two tables and put some data to work on. In our stocks table, we will input 20
products as our data to monitor and manage. Then, in our sales table, we need 60 various
products as sold. Our first objective is to be able to list the products with their respective total
sales. Then, our second objective is to be able to get the list of the 20% most saleable products,
based of its total sales. The first thing to do is to create a table for Stocks1 table. Let us begin now.

CREATE TABLE Stocks1(ItemNumber SMALLINT, Description


VARCHAR(20), Price Decimal(8,2), QuantityOnHand TINYINT);

After successfully creating the table for Stocks1, we need to input the 20 products that
we would like to monitor and manage. So, we have to do the next step.

INSERT INTO Stocks1 VALUES(1001, 'Agedashi', 95, 100);


INSERT INTO Stocks1 VALUES(1002, 'Kushi', 170, 100);
INSERT INTO Stocks1 VALUES(1003, 'Gyoza', 120, 100);
INSERT INTO Stocks1 VALUES(1004, 'Tonkatsu', 145, 100); INSERT
INTO Stocks1 VALUES(1005, 'Karaage', 145, 100);
INSERT INTO Stocks1 VALUES(1006, 'Sukiyaki', 300, 100);
INSERT INTO Stocks1 VALUES(1007, 'Ebi', 200, 100);
INSERT INTO Stocks1 VALUES(1008, 'Ika', 180, 100);
INSERT INTO Stocks1 VALUES(1009, 'Sakana', 205, 100);
INSERT INTO Stocks1 VALUES(1010, 'Wasabi', 95, 100);
INSERT INTO Stocks1 VALUES(1011, 'Wonton', 95, 100);
INSERT INTO Stocks1 VALUES(1012, 'Shishamo', 205, 100);
INSERT INTO Stocks1 VALUES(1013, 'Tataki', 205, 100);
INSERT INTO Stocks1 VALUES(1014, 'Maguro', 175, 100);
INSERT INTO Stocks1 VALUES(1015, 'Shake', 185, 100);
INSERT INTO Stocks1 VALUES(1016, 'Kani', 135, 100);
INSERT INTO Stocks1 VALUES(1017, 'Tamago', 95, 100);
INSERT INTO Stocks1 VALUES(1018, 'Uni', 175, 100);
INSERT INTO Stocks1 VALUES(1019, 'Sashimi', 290, 100);
INSERT INTO Stocks1 VALUES(1020, 'Moriawase', 290, 100);

This time, we will create the SALES1 table by issuing the following SQL script:
CREATE TABLE Sales1(ItemNumber SMALLINT, QuantityBought TINYINT);

We will assume that there are already 60 various products that are already sold in a
week. So, we need to input them on the Sales1 table. Input the following data values by using
the INSERT command.

INSERT INTO Sales1 VALUES (1019, 1);


INSERT INTO Sales1 VALUES (1010, 1);
INSERT INTO Sales1 VALUES (1002, 2);
INSERT INTO Sales1 VALUES (1009, 1);
INSERT INTO Sales1 VALUES (1004, 1);
INSERT INTO Sales1 VALUES (1007, 1);
INSERT INTO Sales1 VALUES (1003, 1);
INSERT INTO Sales1 VALUES (1018, 1);
INSERT INTO Sales1 VALUES (1017, 1);
INSERT INTO Sales1 VALUES (1016, 1);
INSERT INTO Sales1 VALUES (1010, 1);
INSERT INTO Sales1 VALUES (1011, 2);
INSERT INTO Sales1 VALUES (1005, 1);
INSERT INTO Sales1 VALUES (1006, 2);
INSERT INTO Sales1 VALUES (1009, 1);
INSERT INTO Sales1 VALUES (1007, 1);
INSERT INTO Sales1 VALUES (1008, 1);
INSERT INTO Sales1 VALUES (1004, 1);
INSERT INTO Sales1 VALUES (1005, 1);
INSERT INTO Sales1 VALUES (1006, 1);
INSERT INTO Sales1 VALUES (1010, 1);
INSERT INTO Sales1 VALUES (1011, 1);
INSERT INTO Sales1 VALUES (1012, 1);
INSERT INTO Sales1 VALUES (1013, 1);
INSERT INTO Sales1 VALUES (1019, 1);
INSERT INTO Sales1 VALUES (1020, 1);
INSERT INTO Sales1 VALUES (1014, 1);
INSERT INTO Sales1 VALUES (1015, 1);
INSERT INTO Sales1 VALUES (1016, 1);
INSERT INTO Sales1 VALUES (1001, 2);
INSERT INTO Sales1 VALUES (1002, 1);
INSERT INTO Sales1 VALUES (1010, 1);
INSERT INTO Sales1 VALUES (1011, 1);
INSERT INTO Sales1 VALUES (1013, 1);
INSERT INTO Sales1 VALUES (1009, 1);
INSERT INTO Sales1 VALUES (1003, 1);
INSERT INTO Sales1 VALUES (1004, 1);
INSERT INTO Sales1 VALUES (1019, 1);
INSERT INTO Sales1 VALUES (1020, 1);
INSERT INTO Sales1 VALUES (1005, 1);
INSERT INTO Sales1 VALUES (1006, 1);
INSERT INTO Sales1 VALUES (1008, 1);
INSERT INTO Sales1 VALUES (1007, 1);
INSERT INTO Sales1 VALUES (1001, 1);
INSERT INTO Sales1 VALUES (1020, 1);
INSERT INTO Sales1 VALUES (1019, 1);
INSERT INTO Sales1 VALUES (1011, 1);
INSERT INTO Sales1 VALUES (1012, 1);
INSERT INTO Sales1 VALUES (1014, 1);
INSERT INTO Sales1 VALUES (1008, 1);
INSERT INTO Sales1 VALUES (1013, 1);
INSERT INTO Sales1 VALUES (1014, 2);
INSERT INTO Sales1 VALUES (1019, 3);
INSERT INTO Sales1 VALUES (1001, 1);
INSERT INTO Sales1 VALUES (1002, 2);
INSERT INTO Sales1 VALUES (1003, 1);
INSERT INTO Sales1 VALUES (1019, 3);
INSERT INTO Sales1 VALUES (1020, 1);
INSERT INTO Sales1 VALUES (1018, 1);
INSERT INTO Sales1 VALUES (1017, 1);

This time, we have to learn how many sales are there for a product which item number is
1001. Let us issue the following SQL query.

SELECT * FROM Sales1 WHERE ItemNumber = 1001;


+------------+----------------+
| ItemNumber | QuantityBought |
+------------+----------------+
| 1001 | 2 |
| 1001 | 1 |
| 1001 | 1 |
+------------+----------------+

With the output above, we learned that there are probably 3 customers who bought the
product with item number is 1001. The first customer, bought 2 quantity of the product, while
the second and third customers bought only 1. However, we cannot tell what this product is. We
need to know the description of the product. Since the description of the product can be found
at Stocks1 table, we have to access that table. To accomplish this, we have to issue the following
SQL query:

SELECT Description FROM Stocks1 WHERE ItemNumber = 1001;


+-------------+
| Description |
+-------------+
| Agedashi |
+-------------+

So this time, we know that, the product which item number is 1001 is Agedashi. However,
we issue two SQL query to accomplish what we need. The question is, can we design one SQL
query that will produce the two outputs above in one single output? And if so, how? Here is now
our SQL query to accomplish it:

SELECT Sales1.ItemNumber, Stocks1.Description,


Sales1.QuantityBought FROM Sales1, Stocks1 WHERE
Sales1.ItemNumber = Stocks1.ItemNumber AND Sales1.ItemNumber =
1001;
+------------+-------------+----------------+
| ItemNumber | Description | QuantityBought |
+------------+-------------+----------------+
| 1001 | Agedashi | 2 |
| 1001 | Agedashi | 1 |
| 1001 | Agedashi | 1 |
+------------+-------------+----------------+
We were able to do so by querying two tables at once. What if we want to display a
summary of the total quantity bought for all products sold, as well as their respective number of
buyers? What should we do? Here is the following SQL query to accomplish the task:

SELECT Stocks1.Description, SUM(Sales1.QuantityBought) AS 'Total


Quantity Bought', COUNT(Sales1.ItemNumber) AS 'Number of Buyers'
FROM Stocks1, Sales1 WHERE Stocks1.ItemNumber = Sales1.ItemNumber
GROUP BY Stocks1.Description; +-------------+--------------------
---+------------------+
| Description | Total Quantity Bought | Number of Buyers |
+-------------+-----------------------+------------------+
| Agedashi | 5 | 3 |
| Ebi | 3 | 3 |
| Gyoza | 3 | 3 |
| Ika | 3 | 3 |
| Kani | 2 | 2 |
| Karaage | 3 | 3 |
| Kushi | 5 | 3 |
| Maguro | 4 | 3 |
| Moriawase | 4 | 4 |
| Sakana | 3 | 3 |
| Sashimi | 10 | 6 |
| Shake | 1 | 1 |
| Shishamo | 2 | 2 |
| Sukiyaki | 4 | 3 |
| Tamago | 2 | 2 |
| Tataki | 3 | 3 |
| Tonkatsu | 3 | 3 |
| Uni | 2 | 2 |
| Wasabi | 4 | 4 |
| Wonton | 5 | 4 |
+-------------+-----------------------+------------------+

This Time we know the total quantity bought for each product as well as the probable
buyers who bought them. This time, how could we list all the products with their respective total
sales? First, we have to know the list of the sales transactions that were stored at Sales1 table. Let
us issue the following SQL query:

SELECT * FROM Sales1;


+------------+----------------+
| ItemNumber | QuantityBought |
+------------+----------------+
| 1019 | 1 |
| 1010 | 1 |
| 1002 | 2 |
| 1009 | 1 |
| 1004 | 1 |
| 1007 | 1 |
| 1003 | 1 |
| 1018 | 1 |
| 1017 | 1 |
| 1016 | 1 |
| 1010 | 1 |
| 1011 | 2 |
| 1005 | 1 |
| 1006 | 2 |
| 1009 | 1 |
| 1007 | 1 |
| 1008 | 1 |
| 1004 | 1 |
| 1005 | 1 |
| 1006 | 1 |
| 1010 | 1 |
| 1011 | 1 |
| 1012 | 1 |
| 1013 | 1 |
| 1019 | 1 |
| 1020 | 1 |
| 1014 | 1 |
| 1015 | 1 |
| 1016 | 1 |
| 1001 | 3 |
| 1002 | 1 |
| 1010 | 1 |
| 1011 | 1 |
| 1013 | 1 |
| 1009 | 1 |
| 1003 | 1 |
| 1004 | 1 |
| 1019 | 1 |
| 1020 | 1 |
| 1005 | 1 |
| 1006 | 1 |
| 1008 | 1 |
| 1007 | 1 |
| 1001 | 1 |
| 1020 | 1 |
| 1019 | 1 |
| 1011 | 1 |
| 1012 | 1 |
| 1014 | 1 |
| 1008 | 1 |
| 1013 | 1 |
| 1014 | 2 |
| 1019 | 3 |
| 1001 | 1 |
| 1002 | 2 |
| 1003 | 1 |
| 1019 | 3 |
| 1020 | 1 |
| 1018 | 1 |
| 1017 | 1 |
+------------+----------------+
60 rows in set (0.00 sec)

So, there are 60 sales transaction that were stored at Sales1 table. We need to create a
view to easily create the sales report that we need. Here is now our SQL script;

CREATE VIEW TotalSales1 AS SELECT Stocks1.Description,


SUM(Sales1.QuantityBought) AS 'Total Quantity Bought',
COUNT(Sales1.ItemNumber) AS 'Number of Buyers' FROM Stocks1,
Sales1 WHERE Stocks1.ItemNumber = Sales1.ItemNumber GROUP BY
Stocks1.Description;
Now, with the total quantity bought is summarized for each product, it is now easier for
us to produce the sales report that list the product and its total sales. Here is now our solution to
accomplish the objective of this mini case study:

SELECT TotalSales1.Description, TotalSales1.`Total Quantity


Bought` * Stocks1.Price AS 'Total Sold' FROM TotalSales1, Stocks1
WHERE TotalSales1.Description = Stocks1.Description; +-----------
--+------------+
| Description | Total Sold |
+-------------+------------+
| Agedashi | 475.00 |
| Kushi | 850.00 |
| Gyoza | 360.00 |
| Tonkatsu | 435.00 |
| Karaage | 435.00 |
| Sukiyaki | 1200.00 |
| Ebi | 600.00 |
| Ika | 540.00 |
| Sakana | 615.00 |
| Wasabi | 380.00 |
| Wonton | 475.00 |
| Shishamo | 410.00 |
| Tataki | 615.00 |
| Maguro | 700.00 |
| Shake | 185.00 |
| Kani | 270.00 |
| Tamago | 190.00 |
| Uni | 350.00 |
| Sashimi | 1740.00 |
| Moriawase | 1160.00 |
+-------------+------------+ 20
rows in set (0.00 sec)
The product and its total sales table.

To list all the products ordered by its total sales.

SELECT TotalSales1.Description, TotalSales1.`Total Quantity


Bought` * Stocks1.Price AS 'Total Sold' FROM TotalSales1, Stocks1
WHERE TotalSales1.Description = Stocks1.Description ORDER BY
`Total Sold` DESC;
+-------------+------------+
| Description | Total Sold |
+-------------+------------+
| Sashimi | 1740.00 |
| Sukiyaki | 1200.00 |
| Moriawase | 1160.00 |
| Kushi | 850.00 |
| Maguro | 700.00 |
| Sakana | 615.00 |
| Tataki | 615.00 |
| Ebi | 600.00 |
| Ika | 540.00 |
| Agedashi | 475.00 |
| Wonton | 475.00 |
| Tonkatsu | 435.00 |
| Karaage | 435.00 |
| Shishamo | 410.00 |
| Wasabi | 380.00 |
| Gyoza | 360.00 |
| Uni | 350.00 |
| Kani | 270.00 |
| Tamago | 190.00 |
| Shake | 185.00 |
+-------------+------------+
20 rows in set (0.05 sec)

Using TotalSales1.`Total Quantity Bought` * Stocks1.Price also work.

SELECT TotalSales1.Description, TotalSales1.`Total Quantity


Bought` * Stocks1.Price AS 'Total Sold' FROM TotalSales1, Stocks1
WHERE TotalSales1.Description = Stocks1.Description ORDER BY
TotalSales1.`Total Quantity Bought` * Stocks1.Price DESC;
+-------------+------------+
| Description | Total Sold |
+-------------+------------+
| Sashimi | 1740.00 |
| Sukiyaki | 1200.00 |
| Moriawase | 1160.00 |
| Kushi | 850.00 |
| Maguro | 700.00 |
| Sakana | 615.00 |
| Tataki | 615.00 |
| Ebi | 600.00 |
| Ika | 540.00 |
| Agedashi | 475.00 |
| Wonton | 475.00 |
| Tonkatsu | 435.00 |
| Karaage | 435.00 |
| Shishamo | 410.00 |
| Wasabi | 380.00 |
| Gyoza | 360.00 |
| Uni | 350.00 |
| Kani | 270.00 |
| Tamago | 190.00 |
| Shake | 185.00 |
+-------------+------------+
20 rows in set (0.00 sec)

Now, that the data are well-arranged, we can easily get the 20% most saleable products
out of 20 various items. We know that 20 percent of 20 is 4, thus the top 4 products listed are the
20% most saleable products.

PREPARE stmt1 FROM "SELECT TotalSales1.Description,


TotalSales1.`Total Quantity Bought` * Stocks1.Price AS 'Total
Sold' FROM TotalSales1, Stocks1 WHERE TotalSales1.Description =
Stocks1.Description ORDER BY TotalSales1.`Total Quantity Bought`
* Stocks1.Price DESC";

Query OK, 0 rows affected (0.07 sec)


Statement prepared
EXECUTE stmt1;
+-------------+------------+
| Description | Total Sold |
+-------------+------------+
| Sashimi | 1740.00 |
| Sukiyaki | 1200.00 |
| Moriawase | 1160.00 |
| Kushi | 850.00 |
| Maguro | 700.00 |
| Sakana | 615.00 |
| Tataki | 615.00 |
| Ebi | 600.00 |
| Ika | 540.00 |
| Agedashi | 475.00 |
| Wonton | 475.00 |
| Tonkatsu | 435.00 |
| Karaage | 435.00 |
| Shishamo | 410.00 |
| Wasabi | 380.00 |
| Gyoza | 360.00 |
| Uni | 350.00 |
| Kani | 270.00 |
| Tamago | 190.00 |
| Shake | 185.00 |
+-------------+------------+
20 rows in set (0.00 sec)

PREPARE STM FROM "SELECT TotalSales1.Description,


TotalSales1.`Total Quantity Bought` * Stocks1.Price AS 'Total
Sold' FROM TotalSales1, Stocks1 WHERE TotalSales1.Description =
Stocks1.Description ORDER BY TotalSales1.`Total Quantity Bought`
* Stocks1.Price DESC LIMIT ?";

Query OK, 0 rows affected (0.00 sec)


Statement prepared

SELECT COUNT(DESCRIPTION) * 0.20 FROM totalsales1;


+---------------------------+
| count(description) * 0.20 |
+---------------------------+
| 4.00 |
+---------------------------+
1 row in set (0.00 sec)

SET @foo1 = (SELECT COUNT(Description) * 0.20 FROM TotalSales1);


Query OK, 0 rows affected (0.00 sec)

mysql> EXECUTE STM USING @foo1; +-------------+------------+


| Description | Total Sold |
+-------------+------------+
| Sashimi | 1740.00 |
| Sukiyaki | 1200.00 |
| Moriawase | 1160.00 |
| Kushi | 850.00 |
+-------------+------------+
4 rows in set (0.04 sec)

Finally, we were able to provide the solution in our second objective. With our SQL script
above, we can get the top 20 percent of any range of products. For example, if the Pareto Mart
has 1,000 products to sell and it wants to monitor and manage its 20 percent most saleable
products, we can simply apply the SQL above to solve the problem. To compute mentally, the 20
percent of 1000 is 200. Thus, there are only 200 products that are most saleable, and in turn
generates 80 percent of the profitability of the mart. How about if the Pareto Mart has 1000000
products? How many saleable products are there? It’s easy, we will simple compute it mentally
and we will arrive to the answer of 200000 - since this is the 20 percent of 1000000. Even in the
range of millions of products, our SQL script above can handle such range of products without any
problem.
I think we got a bright idea in relating Pareto Principle to how a business operation can
achieve high profitability. Historically, Dr. Joseph M. Juran, a pioneer in quality management
coined the term "Pareto Principle". Dr. Juran uses Pareto Principle heavily in his quality
management lectures in Japan. Dr. Juan together with Dr. Edward Demings were credited in
revolutionizing the Japanese high products and services. Twenty years after their historic lectures
and quality management consultancy in Japanese manufacturers, Japan became the world’s
number one in quality. This quality hallmark of Japan is still alive and thriving until this day. And it
just started with a very sound principle – the Pareto Principle.
Chapter VII Activities

1. Display how many rows are there in a row set of Sales1 table. Use the COUNT() function.
2. Count the number of the quantity on hand in the QuantityOnHand column of the STOCKS1
table. Use the COUNT() function.

3. Increase the salary of all employees with an additional 100 pesos. Use the employee 1 table.
Display only the name, the old salary and the new salary.
4. Increase the salary of all employees with an additional 20 percent. Use the Employee1 table.
Display only the name, the old salary and the new salary.
5. List the top 3 salesman in Salesman1 table. Apply the LIMIT clause.
6. List the bottom top 3 salesman in Salesman1 table. Apply the Limit clause.
7. Using the Stocks1 and Sales1 tables, produce the following results:
+-------------+----------------+-----------+------------+
| Description | TotalQtyBought | QtyOnHand | QtyRemains |
+-------------+----------------+-----------+------------+
| Agedashi | 5 | 100 | 95 |
| Ebi | 3 | 100 | 97 |
| Gyoza | 3 | 100 | 97 |
| Ika | 3 | 100 | 97 |
| Kani | 2 | 100 | 98 |
| Karaage | 3 | 100 | 97 |
| Kushi | 5 | 100 | 95 |
| Maguro | 4 | 100 | 96 |
| Moriawase | 4 | 100 | 96 |
| Sakana | 3 | 100 | 97 |
| Sashimi | 6 | 100 | 94 |
| Shake | 1 | 100 | 99 |
| Shishamo | 2 | 100 | 98 |
| Sukiyaki | 4 | 100 | 96 |
| Tamago | 2 | 100 | 98 |
| Tataki | 3 | 100 | 97 |
| Tonkatsu | 3 | 100 | 97 |
| Uni | 2 | 100 | 98 |
| Wasabi | 4 | 100 | 96 |
| Wonton | 5 | 100 | 95 |
+-------------+----------------+-----------+------------+
20 rows in set (0.00 sec)
Where:
QtyRemains = QtyOnHand – TotalQtyBought

You might also like