Having with aggregate function : Aggregate Functions Basics « Aggregate Functions « Oracle PL / SQL





Having with aggregate function

 
SQL>
SQL>
SQL> CREATE TABLE employees
  2  ( employee_id          number(10)      not null,
  3    last_name            varchar2(50)      not null,
  4    email                varchar2(30),
  5    hire_date            date,
  6    job_id               varchar2(30),
  7    department_id        number(10),
  8    salary               number(6),
  9    manager_id           number(6)
 10  );

Table created.

SQL>
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)
  2                values ( 1001, 'Lawson', '[email protected]', '01-JAN-2002','MGR', 30000,1 ,1004);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                values ( 1002, 'Wells', '[email protected]', '01-JAN-2002', 'DBA', 20000,2, 1005 );

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                 values( 1003, 'Bliss', '[email protected]', '01-JAN-2002', 'PROG', 24000,3 ,1004);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1004,  'Kyte', '[email protected]', SYSDATE-3650, 'MGR',25000 ,4, 1005);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1005, 'Dillon', 'sdillon@a .com', SYSDATE, 'PROG', 20000, 1, 1006);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)
  2                 values( 1006, 'Beck', '[email protected]', SYSDATE, 'PROG', 20000, 2, null);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1007, 'Java', '[email protected]', SYSDATE, 'PROG', 20000, 3, 1006);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1008, 'Oracle', '[email protected]', SYSDATE, 'DBA', 20000, 4, 1006);

1 row created.

SQL>
SQL> select * from employees;

EMPLOYEE_ID LAST_NAME                                          EMAIL                          HIRE_DATE JOB_ID                         DEPARTMENT_ID     SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
       1001 Lawson                                             [email protected]                   01-JAN-02 MGR                                        1      30000       1004
       1002 Wells                                              [email protected]                    01-JAN-02 DBA                                        2      20000       1005
       1003 Bliss                                              [email protected]                    01-JAN-02 PROG                                       3      24000       1004
       1004 Kyte                                               [email protected]                 13-JUN-98 MGR                                        4      25000       1005
       1005 Dillon                                             sdillon@a .com                 10-JUN-08 PROG                                       1      20000       1006
       1006 Beck                                               [email protected]                   10-JUN-08 PROG                                       2      20000
       1007 Java                                               [email protected]                   10-JUN-08 PROG                                       3      20000       1006

EMPLOYEE_ID LAST_NAME                                          EMAIL                          HIRE_DATE JOB_ID                         DEPARTMENT_ID     SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
       1008 Oracle                                             [email protected]                  10-JUN-08 DBA                                        4      20000       1006

8 rows selected.

SQL>
SQL>
SQL> select job_id, avg(salary) avg_salary
  2        from employees
  3       group by job_id
  4      having avg(salary) >= 10000
  5       order by avg(salary) desc
  6      /

JOB_ID                         AVG_SALARY
------------------------------ ----------
MGR                                 27500
PROG                                21000
DBA                                 20000

3 rows selected.

SQL>
SQL> drop table employees;

Table dropped.

SQL>

 










Related examples in the same category

1.Use the aggregate functions with any valid expression
2.Order by the result of aggregate function
3.In Oracle, nulls in calculations involving aggregate functions are ignored
4.Limit the rows passed to the aggregate function using a WHERE clause
5.Combine the max and avg function together
6.Retrieves the city and average salary of the employees grouped by city
7.Aggregate function and alias column name