Lab 9 SQL SELECT With Functions, Aggregate Functions
Lab 9 SQL SELECT With Functions, Aggregate Functions
Contents
1. Introduction 3
4. Concept Map 4
4.5.1. AVG() 6
4.5.3. SUM() 7
5. Procedure& Tools 7
6.1. Tools 7
6. Practice Tasks 11
8. Evaluation criteria 12
9. Further Reading 12
10. Books 12
b) Slides 12
11. REFERENCES: 13
• 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.
a) Text Book: Java: Text Book: Database Systems, A practical approach to design,
implementation and management by Thomas Connolly, Carolyn Begg, Addison
Wesley, Fifth Edition,
1. Read URL:
• https://www.w3schools.com/sql/sql_min_max.asp
• https://www.w3schools.com/sql/sql_count_avg_sum.asp
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'.
For Example:We use the above built in function in a query like given below:
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: Replace the name of Steven to Steven hock in employee table. For this we have
used replace function which temporally replace the name.
SELECT REPLACE(employees.first_name, ‘Steven’, ‘Steven hock’) from employees where
employee_id=100
LOCATE(substring, string)
For Example: locate the string king in employee last name attribute so
SELECT LOCATE ('King', employees.last_name) from employees;
4.3. NumericFunctions
Before talking about the specific numeric functions, it is probably worth mentioning that MySQL
can perform simple math functions using mathematical operators.
Operator Function
+ Add
- Subtract
* Multiply
/ Divide
Examples:
SELECT 6/2, 6*8, 6+3, 6-5;
etc. An aggregate function ignores NULL values when it performs calculation except for the
COUNT function.COUNT()
This counts the number of times a row (or field) is returned.
Syntax:
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 paid by department whose number is 90.
SELECT AVG(salary) from employees WHERE department_id=90;
MIN(field)
MAX(field)
For Example:Show the Min salary paid by department whose number is 90.
SELECT MIN(salary) from employees WHERE department_id=90;
For Example: Show the MAX salary paid by department whose number is 90.
SELECT MAX(salary) from employees WHERE department_id=90;
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 Total salary paid by department whose number is 90.
SELECT SUM(salary) from employees WHERE department_id=90;
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
In this section tools installation and setup is defined.
For Example: Replace the name of Steven to Steven hock in employee table. For this we have
used replace function which temporally replace the name.
SELECT REPLACE(employees.first_name, ‘Steven’, ‘Steven hock’) from employees where
employee_id=100
For Example: Show the MAX salary paid by department whose number is 90.
SELECT MAX(salary) from employees WHERE department_id=90;
For Example: Show the Total salary paid by department whose number is 90.
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:
\\fs\assignments$
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 \\fs\lectures$
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