Lab 5 SQL SELECT With Functions, Aggregate Functions
Lab 5 SQL SELECT With Functions, Aggregate Functions
Lab-04
SQL SELECT WITH FUNCTION
Lab 4: SQL SELECT WITH FUNCTION
Table of Contents
1. Introduction 60
4. Concept Map 61
4.5.1. AVG() 64
4.5.3. SUM() 65
5. Procedure& Tools 65
6.1. Tools 66
6. Practice Tasks 67
8. Evaluation criteria 68
9. Further Reading 68
10. Books 68
b) Slides 68
11. REFERENCES: 68
Date Functions are used to manipulate the display format of a date as well as calculate time.
i.e. Formatting the date using DATE_FORMAT() function, or getting year from date using
YEAR() function.
String Functions can format or manipulate a text string. i.e. Adding text to an existing value
using CONCAT() function or getting a substring from text using RIGHT, LEFT, or MID
functions.
Numeric Functions can format or manipulate figures/numbers. i.e. getting floor or ceiling
value using FLOOR() or CEILING() functions respectively.
Aggregate/Summarizing Functions are used to get a summarized/aggregate result from a
query. i.e. counting the number of rows in the ResultSet of a query using COUNT() function.
There are also Control Functions that can be used to give conditionality to queries. In this lab,
you can learn about the select command using functions.
• To get familiar with querying a database using SELECT statement with functions.
4. Concept Map
In this section, a brief overview of the concepts is presented, those will be used in this lab
afterwards. MySQL has many built in functions that can transform data to meet our
requirements. These include:
Date Functions
String Functions
Numeric Functions
Aggregate/Summarizing Functions
NOW: returns the current date and time in the format of 'YYYY-MM-DD HH:MM:SS'.
CURDATE (or CURRENT_DATE(), or CURRENT_DATE): returns the current date in the
format of 'YYYY-MM-DD'.
CURTIME (or CURRENT_TIME(), or CURRENT_TIME): returns the current time in the
format of 'HH:MM:SS'.
CONCAT(string1,string2,...)
Thus we can take an existing value (say string2) and place a new value (string1) at the
beginning to get string1string2.
For example: concat employee name and job title of employees
REPLACE(whole_string,to_be_replaced,replacement)
For Example: Replace the name of Salman to Hasnat then can use Replace Function for replace
the result set string.
LOCATE(substring,string)
Operator Function
+ Add
- Subtract
* Multiply
/ Divide
Examples:
COUNT(field)
For Example: If count the all rows of employee table then use count function
4.5.1. AVG()
The next function we are going to look at is the AVG() which unsurprisingly is the average
function.
Syntax:
AVG(field)
For Example: Show the average salary of employees which is given in Figure 6
MIN(field)
MAX(field)
For Example: show the min and max salary of employee which is given in Figure 7
4.5.3. SUM()
The final summary function that we will look at is the SUM() function which adds rows of one
field in the results set together.
Syntax:
SUM(field)
For Example: Show the sum of all salary which is given in Figure 8
The selected row will be ordered according to the values in columnA, in either ascending (ASC)
(default) or descending (DESC) order. If several rows have the same value in columnA, it will be
For Example, to select officeCode, city, and country of all offices and sort the result set by
country in ascending order, the following statement can be written:
+------------+--------------+-----------+
| officecode | city | country |
+------------+--------------+-----------+
|6 | Sydney | Australia |
|4 | Paris | France |
|5 | Tokyo | Japan |
|7 | London | UK |
|2 | Boston | USA |
|3 | NYC | USA |
|1 | San Francisco | USA |
+------------+---------------+-----------+
Offset is the index of record in the result set, indexes starts from zero (0).
For Example, to show office code, city, and country of first three offices of the result set, the
following statement can be written:
+------------+-----------------+-----------+
| officecode | city | country |
+------------+-----------------+-----------+
|1 | San Francisco | USA |
|2 | Boston | USA |
|3 | NYC | USA |
+------------+-----------------+-----------+
To show office code, city, and country of 2 nd to 4th offices of the result set, an offset can be
defined along with required number of rows, the following statement can be written:
+------------+-----------------+-----------+
| officecode | city | country |
+------------+-----------------+-----------+
|2 | Boston | USA |
|3 | NYC | USA |
|4 | Paris | France |
+------------+-----------------+-----------+
We can see duplicated in price column price. To get only the unique elements write the
following statement.
You must solve the following problems at home before the lab.
5. Procedure& Tools
In this section, you will study how to make and run a customized exception.
6.1. Tools
6. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Teacher Name\Introduction To Databases\Lab4
8. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
9. Further Reading
This section provides the references to further polish your skills.
10. Books
a) Text Book:
Database Systems, A practical approach to design, implementation and management
by by Thomas Connolly, Carolyn Begg, Addison Wesley , Fifth Edition,
b) Slides
The slides and reading material can be accessed from the folder of the class instructor
available at \\dataserver\jinnah$\
11. REFERENCES:
SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer.
More examples for the SELECT command:
http://dev.mysql.com/doc/mysql/en/select.html
MySQL operators:
http://dev.mysql.com/doc/mysql/en/non-typed_operators.html
Built-in functions:
http://dev.mysql.com/doc/mysql/en/functions.html
Joining tables:
http://www.melonfire.com/community/columns/trog/article.php?id=148
Using subqeries:
http://www.melonfire.com/community/columns/trog/article.php?id=204