Top Banner

of 62

Introduction to Oracle Functions (1)

Apr 14, 2018

Download

Documents

Nikhil Malik
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/29/2019 Introduction to Oracle Functions (1)

    1/62

    Introduction to Oracle

    Functions and Group ByClause

  • 7/29/2019 Introduction to Oracle Functions (1)

    2/62

    Introduction to OracleFunctions

    Functions make the result of the queryeasier and are used to manipulate the datavalues. Functions can accept any number ofconstant values or variables. Thesevariables or constant are called asarguments. SQL functions can performdifferent types of operations such as

    modify individual data items, convertcolumn data types, format dates andnumbers etc.

  • 7/29/2019 Introduction to Oracle Functions (1)

    3/62

    Categories of Functions

    Oracle functions are categorized into two categories: Single Row/Scalar Functions Group/Aggregate Functions

    Functions, which operate on single rows and return onevalue per row, are called as Scalar functions or SingleRow Functions.

    On the other hand functions, which operate on set ofvalues to give one result, are called as Group Functionsor Aggregate Functions.

  • 7/29/2019 Introduction to Oracle Functions (1)

    4/62

    Single-Row Functions (Scalar functions)

    These functions act on each row of the table andreturn a single value for each row selected. Thesefunctions may accept one or more arguments and canreturn a value of different data type than that it

    has accepted.

  • 7/29/2019 Introduction to Oracle Functions (1)

    5/62

    Classification of Single Row Functions

    Single Row Functions can be classified into the following

    categories:

    (i) Character

    (ii) Number

    (iii) Date

    (iv) Conversion

    (v) General

  • 7/29/2019 Introduction to Oracle Functions (1)

    6/62

    Character Functions

  • 7/29/2019 Introduction to Oracle Functions (1)

    7/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    8/62

    length(x)

    It returns the length of the string x.

    Example:

    SQL> Select LENGTH ('Ipso Facto') ergo FROM dual;

  • 7/29/2019 Introduction to Oracle Functions (1)

    9/62

    ltrim(string[,char(s)])

    It removes all the blank spaces from the left side of thestring if no char is specified. If we give a char, then itremoves the leading occurrences of that character from thestring.

  • 7/29/2019 Introduction to Oracle Functions (1)

    10/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    11/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    12/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    13/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    14/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    15/62

    Translate(char,find,new)

    This function is used to find a charand replace it with newcharacter. All occurrences of find are replaced by the

    character in new.

  • 7/29/2019 Introduction to Oracle Functions (1)

    16/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    17/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    18/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    19/62

    floor(x)Where x is a number. This function returns the largest integer that is less than or equal to n. FLOOR round

    down to a whole number.

  • 7/29/2019 Introduction to Oracle Functions (1)

    20/62

    round(x[,y])It rounds off x to the decimal precision of y. If y is negative, rounds to the precision of yplaces to the left of the

    decimal point.

  • 7/29/2019 Introduction to Oracle Functions (1)

    21/62

    sqrt(x)This function returns the square root of the given number x. If the given number x is negative or NULL then the result is

    NULL.Example:SQL>select sqrt(36) as square_root from dual;

  • 7/29/2019 Introduction to Oracle Functions (1)

    22/62

    Date Functions

  • 7/29/2019 Introduction to Oracle Functions (1)

    23/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    24/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    25/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    26/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    27/62

    SQL> SELECT TO_CHAR(SYSDATE,'HH') HOUR,TO_CHAR(SYSDATE,'MI') MIN,TO_CHAR(SYSDATE,'SS')SEC FROM DUAL;

    The output is:

    HO MI SE

    -- -- --

    03 01 16

    SQL> SELECT TO_DATE('15-MAR-1999','DD-MON-YYYY')FROM DUAL;

    SQL>SELECT TO_NUMBER('49583') FROM DUAL;

  • 7/29/2019 Introduction to Oracle Functions (1)

    28/62

    General functions

    SQL>Select greatest(-2 ,10,7) from dual ;

    SQL>Select least(ABCD,abcd,xyz)from dual ;

  • 7/29/2019 Introduction to Oracle Functions (1)

    29/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    30/62

    SQL>Select user from dual;

  • 7/29/2019 Introduction to Oracle Functions (1)

    31/62

    Aggregate Functions (GroupFunctions)

    These functions are used to produce summarizedresults. They are applied on set of rows to give yousingle value as a result. A group function allows you toperform a data operation on several values in a columnof data as though the column was one collective groupof data. These functions are called group functionsalso, because they are often used in a special clause of

    select statements called a group by clause.

  • 7/29/2019 Introduction to Oracle Functions (1)

    32/62

    COUNT (x)

    This function returns the number of rows or non-null valuesfor column x. When we use * in place of x, it returns thetotal number of rows in the table.

    Syntax:count([distinct|all]column name)

    Example:1. Count the number of employees in the emp table.

    SQL>Select count(empno) from emp;

    The Output is:COUNT(EMPNO)

    ------------16

    L h b f d ff h bl

  • 7/29/2019 Introduction to Oracle Functions (1)

    33/62

    List the number of different names in the emp table.

    SQL>Select count (distinct ename) from emp;

    The output is:COUNT(DISTINCTENAME)--------------------

    16List the number of departments in the employee table .

    SQL>Select count( distinct deptno) from emp;

    The output is:

    COUNT(DISTINCTDEPTNO)---------------------3

  • 7/29/2019 Introduction to Oracle Functions (1)

    34/62

    SUM(x)

    This function returns the sum of values for thecolumn x. This function is applied on columns having numericdatatype and it returns the numeric value.

    syntax : sum([distinct|all]column name)

    Example:

    List the total salary paid to the employees in the emp table.

    SQL>select sum(sal) from emp ;

    The output is:SUM(SAL)---------

    29025

  • 7/29/2019 Introduction to Oracle Functions (1)

    35/62

    AVG(x)

    This function returns the average of values forthe column x. This function is applied on columns havingnumeric datatype and it returns the numeric value. It ignoresthe null values in the column x.

    syntax : avg([distinct|all]column name)

    Example:

    List the average salary and the number of employees in theemp table .

    SQL>select avg(sal) ,count(sal) from emp ;

  • 7/29/2019 Introduction to Oracle Functions (1)

    36/62

    MIN(x)

    This function returns the minimum of values for the columnx for all the rows .this function can be applied on anydatatype .

    syntax : min([distinct|all]column name)

    Example:

    List the minimum salary in the emp table .SQL>select min(sal) from emp ;The output is:

    MIN(SAL)--------800

  • 7/29/2019 Introduction to Oracle Functions (1)

    37/62

    MAX(x)

    This function returns the maximum of values forthe column x for all the rows .this function can be applied onany datatype.

    syntax : max([distinct|all]column name)

    Example:List the maximum salary and commission in the emp table .

    SQL>select max(sal) ,max(comm) from emp ;

  • 7/29/2019 Introduction to Oracle Functions (1)

    38/62

    Note : The avg() and sum() functions will always be appliedon numeric datatype while min() and max() functions can beapplied on any datatype.

    Example

    SQL>select avg(sal),sum(sal),min(ename),max(ename) fromemp ;

  • 7/29/2019 Introduction to Oracle Functions (1)

    39/62

    Exercise :

    list the names of the employees earning minimum salary .list the names of the employees earning second highest

    salary .list the details of the employees who earn salary greaterthan the average salary . also count their number .count the number of employees whose salary is equal tothe highest salary .list the number of employees ,their average salary,minimum salary and maximum salary in the employee

    table.

  • 7/29/2019 Introduction to Oracle Functions (1)

    40/62

    Grouping Data with GROUPBY

    GROUP BY clause is used to group or categorize the data. In other

    words it divide rows in a table into smaller groups. We can thenuse the group functions to return summary information for each

    group.

    If no GROUP BY clause is specified, then the default grouping is

    the entire result set. When the query executes and the data isfetched, it is grouped based on the GROUP BY clause and the

    group function is applied.

  • 7/29/2019 Introduction to Oracle Functions (1)

    41/62

    Syntax:

    SELECT column,group_function(column) FROM table

    [WHERE condition]

    [GROUP BY group_by_expression][ORDER BY column];

    Here, group_by_expression specifies columns whose values

    determine the basis for grouping rows.

  • 7/29/2019 Introduction to Oracle Functions (1)

    42/62

    For example, If we have to find the total salary of each department

    manually, first we group the records on the basis of department

    number and then we apply the sum function on salary of each group

    to obtain the required result. Similarly in SQL we apply the GROUP

    BY clause on deptno and then calculate the total salary for each groupby Sum(sal) function as shown below:

    SQL>SELECT deptno, Sum(sal) FROM emp GROUP BY

    deptno;

    The output is:

    DEPTNO SUM(SAL)10 2916.6667

    20 2175

    30 1566.6667

  • 7/29/2019 Introduction to Oracle Functions (1)

    43/62

  • 7/29/2019 Introduction to Oracle Functions (1)

    44/62

    Here is how this SELECT statement, containing a GROUP BY clause,is evaluated:

    The SELECT clause specifies the columns to be retrieved i.e

    Department number column in the EMP table, the sum of all thesalaries in the group you specified in the GROUP By clause

    The FROM clause specifies the tables that the database must

    access i.e EMP table.

    The WHERE clause specifies the rows to be retrieved. Since

    there is no WHRE clause, by default all rows are retrieved.

  • 7/29/2019 Introduction to Oracle Functions (1)

    45/62

    The GROUP BY clause specifies how the rows should be grouped.

    Department number groups the rows, so the AVG function that is

    being applied to the salary column will calculate the average salary

    for each department.

    List the average salary of each job in the emp table.

    SQL>SELECT JOB,AVG(SAL) FROM EMP GROUP BY JOB;

    List the maximum salary for each dept.

    SQL>SELECT DEPTNO,MAX(SAL) FROM EMP GROUP BYDEPTNO;

    G i b th

  • 7/29/2019 Introduction to Oracle Functions (1)

    46/62

    Grouping by more than one

    column

    Sometimes there is a need to see results for groups within groups.For example if we have to find the total salary being paid to each

    job title, within each department. Then there is a need to having

    grouping on department number and within each department

    number grouping on the basis of job or in other words there is aneed for grouping within a group.

    Thus, the EMP table is grouped first by department number, and

    within that grouping, it is grouped by job title. For example, the two

    clerks in department 20 are grouped together and a single result(total salary) is produced for all clerks people within that group.

  • 7/29/2019 Introduction to Oracle Functions (1)

    47/62

    SQL> SELECT deptno,job,sum(sal) FROM emp GROUP BY

    deptno, job;

    The output is:

  • 7/29/2019 Introduction to Oracle Functions (1)

    48/62

    By above example it is clear that we can return summary results for

  • 7/29/2019 Introduction to Oracle Functions (1)

    49/62

    groups and subgroups by listing more than one GROUP BY column.

    We can determine the default sort order of the results by the order ofthe columns in the GROUP BY clause.

    The SELECT clause specifies the column to be retrieved:

    Department number in the EMP table Job title in the EMP table

    The sum of all the salaries in the group that you specified in the

    GROUP BY clause

    The FROM clause specifies the tables that the database must

    access the EMP table

    The GROUP BY clause specifies how we must group the rows

    First, department number groups the rows. Second, within the

    department number groups, the rows are grouped by job title. So, the

    SUM function is being applied to the salary column for all job titles

    within each department number group.

    Illegal Queries Using Group

  • 7/29/2019 Introduction to Oracle Functions (1)

    50/62

    Illegal Queries Using Group

    Functions

    Whenever you use a mixture of individual items (DEPTNO) and

    group functions (COUNT) in the same select statement, you mustinclude a Group By clause that specifies the individual items (in

    this case, DEPTNO). If the GROUP By clause is missing, then the

    error message not a single-group group function appears and an

    asterisk (*) points to the offending column. You can correct theerror by adding the GROUP BY clause.

    For e ample follo ing is the illegal q er :

  • 7/29/2019 Introduction to Oracle Functions (1)

    51/62

    For example, following is the illegal query:

    SQL> SELECT deptno, COUNT(ename) FROM emp;

    The Output will be:

    Column missing in the Group By clauseSelect deptno,count(ename)

    *

    ERROR at line 1:

    ORA-00937: not a single-group group function In above selectstatement individual items DEPTNO and group function COUNT

    appears in the same SELECT statement without GROUP BY clause

    which results error, it can be corrected by adding the GROUP BY

    clause as shown below:

    SQL> SELECT deptno,COUNT(ename) FROM emp Group By deptno;

  • 7/29/2019 Introduction to Oracle Functions (1)

    52/62

    DEPTNO COUNT(ENAME)

    10 3

    20 5

    30 6

    Note:

    Any column or expression in the SELECT list that is not an

    aggregate function must be in the GROUP By clause.

  • 7/29/2019 Introduction to Oracle Functions (1)

    53/62

    Restricting Group Results

    As we use the WHERE clause to restrict the rows that we select,

    we can use the HAVING clause to restrict groups.

    For example: To find the maximum salary of each department,

    but show only the departments that have a maximum salary of

    more than Rs.2900, we need to do the following. Find the maximum salary for each department by grouping

    by department number.

    Restrict the groups to those departments with a maximum

    salary greater the Rs.2900.

  • 7/29/2019 Introduction to Oracle Functions (1)

    54/62

    Syntax:

    SELECT column, group_functionFROM table

    {WHERE condition]

    [GROUP BY group_by_expression]

    [HAVING group_condition]

    [ORDER BY Column];

  • 7/29/2019 Introduction to Oracle Functions (1)

    55/62

    Here we use the HAVING clause to specify which groups are to bedisplayed. Therefore, we further restrict the groups on the basis of

    aggregate information.

    In the syntax:HAVING clause restricts the groups of rows returned to those

    groups for which the specified condition is TRUE

    The Oracle Server performs the following steps when you use the

    HAVING clause:

  • 7/29/2019 Introduction to Oracle Functions (1)

    56/62

    Rows are grouped

    The group function is applied to the group. The groups that match the criteria in the HAVING clause are

    displayed.

    The HAVING clause can precede the GROUP By clause, but it is

    recommended that you place the GROUP By clause first because it is

    more logical. Groups are formed and group functions are calculated

    before the HAVING clause is applied to the groups in the SELECTlist.

  • 7/29/2019 Introduction to Oracle Functions (1)

    57/62

    For example: To find the maximum salary of each department, butshow only the departments that have a maximum salary of more than

    Rs.2900

    SQL> SELECT deptno,max(sal) FROM emp

    GROUP BY deptno Having max(sal)>2900;

    The output is:

    DEPTNO MAX(SAL)

    10 5000

    20 3000

  • 7/29/2019 Introduction to Oracle Functions (1)

    58/62

    Use of WHERE clause with GROUP BY clause

    List the total salary, maximum and minimum salary and the average

    salary of employees job wise, for department number 20 and

    display only those rows having average salary greater than 1000

    SQL>SELECT job, SUM(sal), avg(sal), max(sal), min(sal) from

    emp

    WHERE deptno=20

    GROUP by job

    HAVING AVG(sal)>1000;

  • 7/29/2019 Introduction to Oracle Functions (1)

    59/62

    The output is:

    JOB SUM(SAL) AVG(SAL) MAX(SAL) MIN(SAL)

    ANALYST 6000 3000 3000 3000

    MANAGER 2975 2975 2975 2975

    SQL> SELECT job SUM(sal) PAYROLL

  • 7/29/2019 Introduction to Oracle Functions (1)

    60/62

    SQL> SELECT job, SUM(sal) PAYROLL

    FROM empWHERE job NOT LIKE 'SALE%'

    GROUP BY job

    HAVING SUM(sal)>5000

    ORDER BY SUM(sal);

    The output is:

    JOB PAYROLL

    ANALYST 6000

    MANAGER 8275

    The above query displays the job title and total monthly salary foreach job title with a total payroll exceeding Rs.5000. The example

    excludes salespeople and sorts the list by the total monthly salary.

  • 7/29/2019 Introduction to Oracle Functions (1)

    61/62

    Display total no of suppliers supplying red part

    Display total qty supplied by by each supplier

    Display total Qty supplied for each part excluding P3

    Only display those where supplied qty is greater than100

    Display info in descending order of Qty

    Select Pno, Sum(QTY) from P,SP WHERE PNOP3GROUP BY PNO HAVING SUM(QTY)>1000 ORDER BY

    PNO;

  • 7/29/2019 Introduction to Oracle Functions (1)

    62/62

    Display total Qty supplied for each part excluding part having red

    color, Only display those where supplied qty is greater than100

    Select Pno, Sum(QTY) from P,SP WHERE PNO NOT

    IN(SELECT PNO FROM P WHERE COLOR=RED) GROUP

    BY PNO HAVING SUM(QTY)>1000 ORDER BY PNO;