Top Banner
8-1 Copyright © 2004, Oracle. All rights reserved.
24

Oracle examples

Nov 11, 2014

Download

Education

MaRwA AL-AmRi

ORACLE (SQL and PL/SQL examples )
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
Page 1: Oracle examples

8-1 Copyright © 2004, Oracle. All rights reserved.

Page 2: Oracle examples

8-2 Copyright © 2004, Oracle. All rights reserved.

SELECT last_name, salary, NVL(commission_pct, 0), (salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SALFROM employees;

Using the NVL Function

ex1:- To calculate the annual compensation of all employees, you need to multiply the monthly salary by 12 and then add the commission percentage to the result

.

.

Page 3: Oracle examples

8-3 Copyright © 2004, Oracle. All rights reserved.

SELECT last_name, salary, commission_pct, NVL2(commission_pct,'SAL+COMM', 'SAL') incomeFROM employees WHERE department_id IN (50, 80);

Using the NVL2 Function

.

.

Page 4: Oracle examples

8-4 Copyright © 2004, Oracle. All rights reserved.

SELECT last_name, job_id, salaryFROM employeesWHERE job_id = (SELECT job_id FROM employees WHERE employee_id = 141)AND salary > (SELECT salary FROM employees WHERE employee_id = 143);

Executing Single-Row Subqueries

ex1:- displays employees whose job ID is the same as that of employee 141 and whose salary is greater than that of employee 143.

ST_CLERK

2600

The result in next slide

Page 5: Oracle examples

8-5 Copyright © 2004, Oracle. All rights reserved.

Page 6: Oracle examples

8-6 Copyright © 2004, Oracle. All rights reserved.

Executing Single-Row Subqueries

ex2:- Find the job with the lowest average salary.

SELECT job_id, AVG(salary) FROM employeesGROUP BY job_id HAVING AVG(salary)=(SELECT MIN(AVG(salary)) FROM employees GROUP BY job_id);

Page 7: Oracle examples

8-7 Copyright © 2004, Oracle. All rights reserved.

SELECT last_name, job_idFROM employeesWHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas');

Will This Statement Return Rows?

there is no employee named Haas. So the subquery returns no rows

The outer query takes the results of the subquery (null) and uses these results in its WHERE clause.

The outer query finds no employee with a job ID equal to null, and so returns no rows

Page 8: Oracle examples

8-8 Copyright © 2004, Oracle. All rights reserved.

Executing multiple-Row Subqueries

ex3:- display the employees whose there salary equal to the min salary ofeach department.

SELECT last_name, salary, department_idFROM employeesWHERE salary IN (SELECT MIN(salary) FROM employees GROUP BY department_id);

The result in next slide

Page 9: Oracle examples

8-9 Copyright © 2004, Oracle. All rights reserved.

Page 10: Oracle examples

8-10 Copyright © 2004, Oracle. All rights reserved.

SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary < ANY (SELECT distinct salary FROM employees WHERE job_id = 'IT_PROG')AND job_id <> 'IT_PROG';

9000, 6000, 4800,4200

Executing multiple-Row Subqueries

ex4:- displays employees who are not IT programmers and whose salary is less than that of any IT programmer.

=ANY is equivalent to IN.

>ANY means more than the minimum.

<ANY means less than the maximum

The result in next slide

Page 11: Oracle examples

8-11 Copyright © 2004, Oracle. All rights reserved.

.

.

Page 12: Oracle examples

8-12 Copyright © 2004, Oracle. All rights reserved.

SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary < ALL (SELECT distinct salary FROM employees WHERE job_id = 'IT_PROG')AND job_id <> 'IT_PROG';

9000, 6000, 4800,4200

Executing multiple-Row Subqueries

ex4:- displays employees who are not IT programmers and whose salary is less than that of all IT programmer.

>ALL means more than the maximum

<ALL means less than the minimum

The result in next slide

Page 13: Oracle examples

8-13 Copyright © 2004, Oracle. All rights reserved.

.

.

.

.

Page 14: Oracle examples

8-14 Copyright © 2004, Oracle. All rights reserved.

Create a query that displays the first eight characters of the employees’ last names and indicates the amounts of their salaries with stars. Each star signifies a thousand dollars. Sort the data in descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.

Page 15: Oracle examples

8-15 Copyright © 2004, Oracle. All rights reserved.

Page 16: Oracle examples

8-16 Copyright © 2004, Oracle. All rights reserved.

Page 17: Oracle examples

8-17 Copyright © 2004, Oracle. All rights reserved.

Page 18: Oracle examples

8-18 Copyright © 2004, Oracle. All rights reserved.

The function check_sal is written to check if the salary of a particular employee is greater or less than the average salary of all employees working in his department. The function returns TRUE if the salary of the employee is greater than the average salary of employees in his department else returns FALSE. The function returns NULL if a NO_DATA_FOUND exception is thrown.

Page 19: Oracle examples

8-19 Copyright © 2004, Oracle. All rights reserved.

Page 20: Oracle examples

8-20 Copyright © 2004, Oracle. All rights reserved.

Observe that the function checks for the employee with the employee ID 205. The function is hard coded to check for this employee ID only. If you want to check for any other employees, you will have to modify the function itself. You can solve this problem by declaring the function such that it accepts an argument. You can then pass the employee ID as parameter.

Page 21: Oracle examples

8-21 Copyright © 2004, Oracle. All rights reserved.

Page 22: Oracle examples

8-22 Copyright © 2004, Oracle. All rights reserved.

Page 23: Oracle examples

8-23 Copyright © 2004, Oracle. All rights reserved.

Page 24: Oracle examples

8-24 Copyright © 2004, Oracle. All rights reserved.