Professional Documents
Culture Documents
ELECT 2: Technical Elective Database Management System Instructor
ELECT 2: Technical Elective Database Management System 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:
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:
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:
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:
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.
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:
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:
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.
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.
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.
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.
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:
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:
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:
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;
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.
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