280 likes | 440 Views
Functions. Oracle Labs 5 & 6. SQL Functions. Input. Function. Output. arg 1. arg 2. Resulting Value. arg n. SQL Functions. Single-Row Functions Multiple-Row Functions. Single-Row Functions. Acts on each row Can accept multiple arguments Column name Variable name Expression
E N D
Functions Oracle Labs 5 & 6
SQL Functions Input Function Output arg 1 arg 2 Resulting Value . . . arg n Adapted from Introduction to Oracle: SQL and PL/SQL
SQL Functions • Single-Row Functions • Multiple-Row Functions Adapted from Introduction to Oracle: SQL and PL/SQL
Single-Row Functions • Acts on each row • Can accept multiple arguments • Column name • Variable name • Expression • Constant • Returns a single value for each row Adapted from Introduction to Oracle: SQL and PL/SQL
Single-Row Function Use • Select • Where • Order By • Any where a column name can be used Adapted from Introduction to Oracle: SQL and PL/SQL
Single-Row Function Types • Character • Number • Date • Conversion • General Adapted from Introduction to Oracle: SQL and PL/SQL
LOWER UPPER INITCAP CONCAT SUBSTR LENGTH INSTR LPAD RPAD TRIM REPLACE Character functions Case conversion functions Character manipulation functions Character Functions Adapted from Introduction to Oracle: SQL and PL/SQL
Character Functions Adapted from Introduction to Oracle: SQL and PL/SQL
Character Functions Oracle Examples • Accepts character Input • Returns a single value • Value can be • Character • Numeric Function_name (column|expression, [arg1, arg2,…]) Adapted from Introduction to Oracle: SQL and PL/SQL
Other Single Row Functions • Number functions • Date functions • Format functions • Conversion functions Adapted from Introduction to Oracle: SQL and PL/SQL
NVL FunctionConverts Null to a Value • Syntax NVL (expr1, expr2) • expr1 • Source value or expression • that may contain null • expr2 value to replace null Adapted from Introduction to Oracle: SQL and PL/SQL
NVL Function • Datatypes could be • Date • Character • Number • Datatypes must match. • NVL(comm, 0) • NVL(hiredate, ’01-Jan-97’) • NVL(job, ‘No Job Yet’) Adapted from Introduction to Oracle: SQL and PL/SQL
Good Things to Know • Whole number too large • For format model • Replaced by string of pound signs (#) • Decimal value too large • For format model • Replaced by rounded value Adapted from Introduction to Oracle: SQL and PL/SQL
Multiple-Row Functions Or Group Functions
Multiple Rows Single Result ENAME SAL ---------- ---------- SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300 MAX(SAL) ---------- 5000 Adapted from Introduction to Oracle: SQL and PL/SQL
Types of Group Functions • AVG • COUNT • MAX • MIN • STDDEV • SUM • VARIANCE Adapted from Introduction to Oracle: SQL and PL/SQL
SELECT [column,] groupfunction(column)FROM table[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY column] Adapted from Introduction to Oracle: SQL and PL/SQL
Functions for Only Numeric Data • AVG • SUM • VARIANCE • STDDEV • MAX & MIN used for any datatype Adapted from Introduction to Oracle: SQL and PL/SQL
Examples Adapted from Introduction to Oracle: SQL and PL/SQL
The Count Function • COUNT(*) • Number of rows in the table • Including • Duplicate rows • Null Values • Example Adapted from Introduction to Oracle: SQL and PL/SQL
The Count Function • COUNT(expr) • Number of nonnull rows in • The column • Identified by expr • Example Adapted from Introduction to Oracle: SQL and PL/SQL
Group Functions & Null Values • Null values ignored in calculations • Use NVL function to ‘work-around’ • Examples Adapted from Introduction to Oracle: SQL and PL/SQL
DISTINCT Option • Consideration of only non-duplicate values • Default – ALL • Example Adapted from Introduction to Oracle: SQL and PL/SQL
Groups Within Groups • Multiple columns in Group By clause • Top-level group listed first • Example Adapted from Introduction to Oracle: SQL and PL/SQL
ProblemDisplay the deptno and average salary of all departments that have an average salary greater than 2000. Adapted from Introduction to Oracle: SQL and PL/SQL
HAVING Clause • Restricts • groups of rows • Based on group conditions • Like Where clause • Filters • Based on single-row conditions • After GROUP BY • Before ORDER BY Adapted from Introduction to Oracle: SQL and PL/SQL
Having Clause Oracle Server Steps • Group rows. • Apply group function. • Filter • by matching criteria • in HAVING clause EXAMPLE PROBLEM Adapted from Introduction to Oracle: SQL and PL/SQL