Upcoming SlideShare
×

# Oracle examples

1,377 views

Published on

ORACLE (SQL and PL/SQL examples )

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,377
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
121
0
Likes
0
Embeds 0
No embeds

No notes for slide
• … …
• … …
• ### Oracle examples

2. 2. 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 SELECT last_name, salary, NVL(commission_pct, 0), (salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SAL FROM employees; . .8-2 Copyright © 2004, Oracle. All rights reserved.
3. 3. Using the NVL2 Function SELECT last_name, salary, commission_pct, NVL2(commission_pct,SAL+COMM, SAL) income FROM employees WHERE department_id IN (50, 80); . .8-3 Copyright © 2004, Oracle. All rights reserved.
4. 4. 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. SELECT last_name, job_id, salary FROM employees WHERE job_id = ST_CLERK (SELECT job_id FROM employees WHERE employee_id = 141) AND salary > 2600 (SELECT salary FROM employees WHERE employee_id = 143); The result in next slide8-4 Copyright © 2004, Oracle. All rights reserved.
6. 6. Executing Single-Row Subqueries ex2:- Find the job with the lowest average salary. SELECT job_id, AVG(salary) FROM employees GROUP BY job_id HAVING AVG(salary)=(SELECT MIN(AVG(salary)) FROM employees GROUP BY job_id);8-6 Copyright © 2004, Oracle. All rights reserved.
7. 7. Will This Statement Return Rows? SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = Haas); 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 rows8-7 Copyright © 2004, Oracle. All rights reserved.
8. 8. Executing multiple-Row Subqueries ex3:- display the employees whose there salary equal to the min salary of each department. SELECT last_name, salary, department_id FROM employees WHERE salary IN (SELECT MIN(salary) FROM employees GROUP BY department_id); The result in next slide8-8 Copyright © 2004, Oracle. All rights reserved.
10. 10. Executing multiple-Row Subqueries ex4:- displays employees who are not IT programmers and whose salary is less than that of any IT programmer. SELECT employee_id, last_name, job_id, salary FROM employees 9000, 6000, 4800,4200 WHERE salary < ANY (SELECT distinct salary FROM employees WHERE job_id = IT_PROG) AND job_id <> IT_PROG; =ANY is equivalent to IN. >ANY means more than the minimum. <ANY means less than the maximum The result in next slide8-10 Copyright © 2004, Oracle. All rights reserved.