Lab-lecture-week-04(2)
Lab-lecture-week-04(2)
Input Output
Function
arg n
Two Types of SQL Functions
Functions
Single-row Multiple-row
functions functions
Single-row functions:
– Manipulate data items
– Accept arguments and return one value
– Act on each row that is returned
– Return one result per row
– May modify the data type
– Can be nested
– Accept arguments that can be a column or an
expression.
function_name [(arg1, arg2,...)]
Single-Row Functions
Character
Single-row
General Number
functions
Conversion Date
Character Functions
Character
functions
Case-manipulation Character-manipulation
functions functions
LOWER CONCAT
UPPER SUBSTR
INITCAP LENGTH
INSTR
LPAD | RPAD
TRIM
REPLACE
Case-Manipulation Functions
These functions convert case for character strings:
Function Result
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
Using Case-Manipulation Functions
Function Result
MONTHS_BETWEEN 19.6774194
('01-SEP-95','11-JAN-94')
ADD_MONTHS ('11-JAN-94',6) '11-JUL-94'
NEXT_DAY ('01-SEP-95','FRIDAY') '08-SEP-95'
LAST_DAY ('01-FEB-95') '28-FEB-95'
Data Type Conversion
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
Using the TO_CHAR Function with Dates
TO_CHAR(date, 'format_model')
Element Result
YYYY Full year in numbers
YEAR Year spelled out (in English)
MM Two-digit value for month
MONTH Full name of the month
MON Three-letter abbreviation of the month
DY Three-letter abbreviation of the day of the
week
DAY Full name of the day of the week
DD Numeric day of the month
Using the TO_CHAR Function with Dates
SELECT last_name,
TO_CHAR(hire_date, 'fmDD Month YYYY')
AS HIREDATE
FROM employees;
…
Using the TO_CHAR Function with Numbers
TO_CHAR(number, 'format_model')
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
Nesting Functions
SELECT last_name,
UPPER(CONCAT(SUBSTR (LAST_NAME, 1, 8), '_US'))
FROM employees
WHERE department_id = 60;
What Are Group Functions?
• Group functions operate on sets of rows to
give one result per group.
EMPLOYEES
DEPTNO SAL
--------- ---------
10 2450
10 5000
10 1300
20 800
20 1100
20 3000 “maximum MAX(SALARY)
20 3000 salary in ---------
20 2975 the EMP table” 5000
30 1600
30 2850
30 1250
30 950
30 1500
30 1250
Types of SQL Group Functions
• COUNT(*)
• COUNT(Cust_Area_Code)
• AVG (Prod_Amt)
• SUM(Prod_Amt)
• MIN (Prod_Amt)
• MAX (Prod_Amt)
Using the COUNT Function
• COUNT(*) returns the number of rows in a table
– Includes duplicates & nulls
SELECT COUNT(*)
FROM EMPLOYEES
WHERE DEPARTMENT_NO = 30;
COUNT(*)
---------
6
Using the COUNT Function
• COUNT(expr) returns the number of non-null
rows.
– Includes duplicates but not nulls
SELECT COUNT(commission_pct)
FROM EMPLOYEES
WHERE deptno = 30;
COUNT(COMM)
-----------
4
Using the COUNT Function
• COUNT(expr) includes duplicates
• Use DISTINCT to eliminate duplicates
SELECT COUNT(department_no)
FROM EMPLOYEES;
COUNT(DEPARTMENT_NO)
--------------------
14
COUNT(DISTINCTDEPTNO)
---------------------
3 26
Using AVG and SUM Functions
• You can use AVG and SUM for numeric
data.
SELECT AVG(salary), MAX(salary),
MIN(salary), SUM(salary)
FROM EMPLOYEES
WHERE job LIKE 'SALES%';
MIN(HIRED MAX(HIRED
--------- ---------
17-DEC-80 12-JAN-83
Group Functions and Null Values
AVG(COMM)
---------
550
Using the NVL Function
with Group Functions
• The NVL function forces group functions to
include null values.
SELECT AVG(NVL(commission_oct,0))
FROM EMPLOYEES;
AVG(NVL(COMM,0))
----------------
157.14286
Creating Groups of Data
EMPLOYEES
DEPTNO SAL
--------- ---------
10 2450
10 5000 2916.6667
10 1300
20 800 “average DEPTNO AVG(SAL)
20 1100 salary ------- ---------
20 3000 2175 in EMP
10 2916.6667
20 3000 table
20 2975 for each 20 2175
30 1600 department” 30 1566.6667
30 2850
30 1250 1566.6667
30 950
30 1500
30 1250
Creating Groups of Data:
GROUP BY Clause
• Divide rows in a table into smaller groups
by using the GROUP BY clause.
DEPTNO AVG(SAL)
--------- ---------
10 2916.6667
20 2175
30 1566.6667
Using the GROUP BY Clause
• The GROUP BY column does not have to
be in the SELECT list.
SELECT AVG(salary)
FROM EMPLOYEES
GROUP BY department_no;
AVG(SAL)
---------
2916.6667
2175
1566.6667
Grouping by More Than One Column
EMPLOYEES
DEPTNO JOB SAL
--------- --------- ---------
10 MANAGER 2450
DEPTNO JOB SUM(SAL)
10 PRESIDENT 5000
-------- --------- ---------
10 CLERK 1300
10 CLERK 1300
20 CLERK 800 “sum salaries in 10 MANAGER 2450
20 CLERK 1100 the EMP table 10 PRESIDENT 5000
20 ANALYST 3000 for each job,
20 ANALYST 6000
20 ANALYST 3000 grouped by
20 CLERK 1900
20 MANAGER 2975 department”
20 MANAGER 2975
30 SALESMAN 1600
30 CLERK 950
30 MANAGER 2850
30 MANAGER 2850
30 SALESMAN 1250
30 SALESMAN 5600
30 CLERK 950
30 SALESMAN 1500
30 SALESMAN 1250
Using the GROUP BY Clause on
Multiple Columns
SELECT department_no, job_id, sum(salary)
FROM EMPLOYEES
GROUP BY department_no, job_id;
DEPTNO MAX(SAL)
--------- ---------
10 5000
20 3000
Using the HAVING Clause
JOB_ID PAYROLL
--------- ---------
ANALYST 6000
MANAGER 8275
Nesting Group Functions
• Display the maximum average salary.
SELECT max(avg(salary))
FROM EMPLOYEES
GROUP BY department_no;
MAX(AVG(SALARY))
-------------
2916.6667
Order of precedence
• Order of evaluation of the clauses:
– WHERE clause
– GROUP BY clause
– HAVING clause