Sq l Queries

29
SQL 1. List annual salary of all employees assuming 50% increase in their salary. Include commission also. 2. List employee having 'LL' or 'TH' in their names 3. List all jobs without duplicating 4. List employees sorting on jobs, Sort on salary within job 5. List employee hired during December of any year. 6. Select all information from Salgrade table. 7. List departments in order of department names 8. List employees who are in department 10 and 20 working as managers. 9. Question 8 with sorting on employee names. 10. List all clerks in dept 20 11. Display names, annual Sal and commission of all salespeople whose monthly Sal is greater than their comm. o/p must be sorted with Sal highest first. 12. Generate a statement which prompts the user at runtime to accept 2 dates and list employees hired during that period. 13. List employees who have annual remuneration greater than 25000. 15. Count no of 'E' characters in dname col for each row. 16. List emps who have more than 3 characters in their names. 17. List emps who have character 'L' in pos 3 in their names. 18. Get following output without using concatenation operator. Smith gets Sal of 800 19. Write a query which display data with first char being caps and rest being lower case. 20. Write query which performs case insensitive search on emp table based on ename. 21. List emps who are managers or salesman with sail greater than 1500 22. List emps who are managers or clerks with Sal greater than 1000. 23. List all clerks and employees earning Sal greater than 2000. 24. List employees hired on Monday; 1. List annual salary of all employees assuming 50% increase in their salary. Include commission also. SELECT ENAME, SAL, COMM, ((SAL + NVL (COMM, 0)) + (SAL+ NVL (COMM, 0))* 50 / 100) "INCREMENT" FROM EMP; 2. List employee having 'LL' or 'TH' in their names SELECT * FROM EMP WHERE ENAME LIKE '%LL%' OR ENAME LIKE '%TH%'; 1

description

Sq l Queries

Transcript of Sq l Queries

Page 1: Sq l Queries

SQL

1. List annual salary of all employees assuming 50% increase in their salary. Include commission also.2. List employee having 'LL' or 'TH' in their names3. List all jobs without duplicating4. List employees sorting on jobs, Sort on salary within job5. List employee hired during December of any year.6. Select all information from Salgrade table.7. List departments in order of department names8. List employees who are in department 10 and 20 working as managers.9. Question 8 with sorting on employee names.10. List all clerks in dept 2011. Display names, annual Sal and commission of all salespeople whose monthly Sal is greater than their comm. o/p must be sorted with Sal highest first. 12. Generate a statement which prompts the user at runtime to accept 2 dates and list employees hired during that period.13. List employees who have annual remuneration greater than 25000.15. Count no of 'E' characters in dname col for each row.16. List emps who have more than 3 characters in their names.17. List emps who have character 'L' in pos 3 in their names.18. Get following output without using concatenation operator. Smith gets Sal of 80019. Write a query which display data with first char being caps and rest being lower case.20. Write query which performs case insensitive search on emp table based on ename.21. List emps who are managers or salesman with sail greater than 150022. List emps who are managers or clerks with Sal greater than 1000.23. List all clerks and employees earning Sal greater than 2000.24. List employees hired on Monday;

1. List annual salary of all employees assuming 50% increase in their salary. Include commission also.SELECT ENAME, SAL, COMM, ((SAL + NVL (COMM, 0)) + (SAL+ NVL (COMM, 0))* 50 / 100) "INCREMENT" FROM EMP;

2. List employee having 'LL' or 'TH' in their namesSELECT * FROM EMP WHERE ENAME LIKE '%LL%' OR ENAME LIKE '%TH%';

1

Page 2: Sq l Queries

3. List all jobs without duplicating.SELECT DISTINCT JOB FROM EMP;

4. List employees sorting on jobs, Sort on salary within job.SELECT * FROM EMP ORDER BY JOB, SAL;

5. List employee hired during December of any year.SELECT * FROM EMP WHERE HIREDATE LIKE '%DEC%';

6. Select all information from salgrade table.SELECT * FROM SALGRADE;

7. List departments in order of department namesSELECT DNAME FROM DEPT ORDER BY DNAME;

8. List employees who are in department 10 and 20 working as managers.SELECT * FROM EMP WHERE JOB='MANAGER' AND DEPTNO IN (10, 20);

9. Question 8 with sorting on employee names.SELECT * FROM EMP WHERE JOB='MANAGER' AND DEPTNO IN (10, 20) ORDER BY ENAME;

10. List all clerks in dept 20SELECT * FROM EMP WHERE JOB='CLERK' AND DEPTNO=20;

11. Display names, annual Sal and comm of all salespeople whose monthly Sal is greater than their comm. O/P must be sorted with Sal highest first. SELECT ENAME, SAL*12 "ANNUAL SAL", COMM FROM EMP WHERE (SAL>COMM) ORDER BY SAL DESC;

12. Generate a statement which prompts the user at runtime to accept 2 dates and list employees hired during that period.SELECT * FROM EMP WHERE HIREDATE BETWEEN '&FIRSTDATE' AND '&SECONDDATE';

13. List employees who have annual renumeration greater than 25000.SELECT * FROM EMP WHERE ((SAL*12) +NVL (COMM, 0)) > 25000;

15. Count no of 'E' characters in dname col for each row.SELECT DNAME, LENGTH (DNAME)-LENGTH (REPLACE (DNAME,'E','')) FROM DEPT;

OR

SELECT DNAME, LENGTH (DNAME)-LENGTH (TRANSLATE (DNAME,'%E','E')) FROM DEPT;

16. List employees who have more than 3 characters in their names.SELECT ENAME FROM EMP WHERE LENGTH (ENAME) > 3;

17. List employees who have character 'L' in pos 3 in their names.SELECT ENAME FROM EMP WHERE ENAME LIKE '__L%';

18. Get following output without using concatenation operatorSmith gets Sal of 800

2

Page 3: Sq l Queries

SELECT CONCAT (CONCAT (LOWER (ENAME),' gets Sal of '), SAL) FROM EMP;

19. Write a query which display data with first char being caps and rest being lower case.SELECT INITCAP (LOC) FROM DEPT;

20. Write query which performs case insensitive search on emp table based on ename.SELECT * FROM EMP WHERE UPPER (ENAME) =UPPER ('&NAME');

21. List employees who are managers or salesman with Sal greater than 1500SELECT * FROM EMP WHERE JOB IN ('MANAGER','SALESMAN') AND SAL > 1500;

22. List employees who are managers or clerks with Sal greater than 1000.SELECT * FROM EMP WHERE JOB IN ('MANAGER','CLERK') AND SAL > 1000;

23. List all clerks and employees earning Sal greater than 2000.SELECT * FROM EMP WHERE JOB = 'CLERK' OR SAL > 2000;

24. List employees hired on Monday;SELECT * FROM EMP WHERE TRIM (TO_CHAR (HIREDATE,'FM DY')) = ‘MON’;

1. List employees earning salary greater than 2000 using sign function.2. List employee names and salary increased by 15% and expressed as a whole number.3. List employee having experience greater than 200 months.4. Write a query which accepts date in format mm/dd/yyyy and display respective records.5. Write a query which calculates bonus based on the jobs. If job is manager, bonus is 3 months of Sal, for salesman 2.5 months of Sal, for clerks 2 months of Sal and other 4 months of Sal.6. Write a query the displays messages "Good Salary" if Sal > 3000, "Average Salary" if equal to 3000,"Poor Salary" if Sal is less than 3000.7. List employees with hike in Sal as "25%" for grade 5, "20%" for grade 4,"15%" for grade 3 and "10%" for rest.8. Display each employee name with hire date and salary review date. Assume review date is one year after hire date, order the o/p in ascending order of review date.9. Write a query to calculate the length of time any employee has been with the company Display no of years and months.10. Employee hired on or before the 15th of any month is paid on the last Friday of that month, those hired after the 15th are paid the last Friday of the following month, Print a list of employees, their hire date and first pay ,date Sort on hire date.11. Display minimum Sal among clerks.12. List how many managers are their.13. List total salary in each job type.14. List average salary of all jobs except managers.15. List the average monthly salary bill for each job type within a department.16. List deptno and average Sal in each department where more than 3 employees are their.17. Display jobs with maximum salary greater than 3000.18. Find out the difference between lowest and highest salaries.19. List all departments which have more than 3 employees.

3

Page 4: Sq l Queries

20. List lowest paid employees working for each manager. Exclude any groups where the minimum salary is less than 1000. Sort the o/p by salary.21. List employees along with their department details for those earning Sal > 2000;22. List employees with grade 3, 4.23. Show all employees located in "DALLAS"24. List employees name, job, Sal, grade, dname, loc for every one in the company except clerks, Sort on salary displaying highest first.25. List employees who earn Sal less than their managers.26. List the job that was filled in the first half of 1982, and the same job that was filled during the same period in 1983.27. Find employee who joined the company before their manager.28. Find employees drawing least salary in each job type.29. List employees having salary greater than average salary of sales department.30. Find most recently hired employees in each department. Order by hire date.31. In which year did most people join the company. List the year and no of employees.

1. List employees earning salary greater than 2000 using sign function.SELECT * FROM EMP WHERE SIGN (SAL-2000) = 1;

2. List employee names and salary increased by 15% and expressed as a whole number.SELECT ENAME, FLOOR (SAL+ (SAL*15/100)) FROM EMP;

3. List employee having experience greater than 200 months.SELECT * FROM EMP WHERE MONTHS_BETWEEN (SYSDATE, HIREDATE) > 200;

4. Write a query which accepts date in format mm/dd/yyyy and Display respective records.SELECT * FROM EMP WHERE HIREDATE = TO_DATE ('&DT','DD/MM/YYYY');

5. Write a query which calculates bonus based on the jobs. If job is manager, bonus is 3 months of Sal, for salesman 2.5 months of Sal, for clerks 2 months of Sal and other 4 months of Sal.SELECT ENAME, JOB, SAL, DECODE( JOB, 'MANAGER', SAL*3, 'SALESMAN', SAL*2.5,'CLERK',SAL*2,SAL*4) BONUS FROM EMP;

6. Write a query the displays messages "Good Salary" if Sal > 3000, "Average Salary" if equal to 3000,"Poor Salary" if Sal is less than 3000.SELECT ENAME, JOB, SAL, DECODE( SIGN(SAL-3000),1, 'GOOD SALARY', 0,'AVERAGE SALARY','POOR SALARY') FROM EMP;

7. List employees with hike in Sal as "25%" for grade 5, "20%" for grade 4,"15%" for grade 3 and "10%" for rest.SELECT ENAME,GRADE,SAL OLDSAL, DECODE(grade,5,sal+(Sal*25/100), 4,SAL+(SAL*20/100), 3,SAL+(SAL*15/100),SAL+(SAL*10/100)) New Sal FROM EMP E,SALGRADE S WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL;

8. Display each employee name with hire date and salary review date. Assume review date is one year after hire date; order the o/p in ascending order of review date.SELECT ENAME, HIREDATE, (HIREDATE+365) REVIEWDATE FROM EMP ORDER BY (HIREDATE+365);

9. Write a query to calculate the length of time any employee has been with the company Display no of years and months.

4

Page 5: Sq l Queries

SELECT ENAME, HIREDATE, TRUNC (TRUNC (MONTHS_BETWEEN(SYSDATE, HIREDATE))/12) YEAR, TRUNC (MONTHS_BETWEEN (SYSDATE, HIREDATE)) MONTH FROM EMP

10. Employee hired on or before the 15th of any month is paid on the last Friday of that month. Those hired after the 15th are paid the last Friday of the following month. Print a list of employees, their hire date and first pay date, Sort on hire date.SELECT ENAME, HIREDATE, NEXT_DAY (LAST_DAY (ROUND (HIREDATE,'MONTH'))-7, 6) "FIRST Pay" FROM EMP;

11. Display minimum Sal among clerks.SELECT JOB, MIN (SAL) FROM EMP GROUP BY JOB HAVING JOB='CLERK';

12. List how many managers are their.SELECT JOB, COUNT (*) FROM EMP GROUP BY JOB HAVINGJOB='MANAGER';

13. List total salary in each job type.SELECT JOB, SUM (SAL) FROM EMP GROUP BY JOB;

14. List average salary of all jobs except managers.SELECT JOB, AVG (SAL) FROM EMP GROUP BY JOB HAVING JOB! ='MANAGER';

15. List the average monthly salary bill for each job type within a department.SELECT DEPTNO, JOB, ROUND (AVG (SAL), 2) FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT) GROUP BY JOB, DEPTNO;

16. List deptno and average Sal in each department where more than 3 employees are their.SELECT DEPTNO, ROUND (AVG (SAL), 2) FROM EMPGROUP BY DEPTNO HAVING COUNT (*) > 3;

17. Display jobs with maximum salary greater than 3000.SELECT JOB FROM EMP GROUP BY JOB HAVING MAX (SAL) > 3000;

18. Find out the difference between lowest and highest salaries.SELECT JOB, MAX (SAL)-MIN (SAL) FROM EMP WHERE JOB IN ('CLERK','SALESMAN','MANAGER','PRESIDENT')GROUP BY JOB;

19. List all departments which have more than 3 employees.SELECT DNAME FROM DEPT WHERE DEPTNO IN (SELECT DEPTNO FROM EMP GROUP BY DEPTNO HAVING COUNT (*)>3);

20. List lowest paid employees working for each manager. Exclude any groups where the minimum salary is less than 1000. Sort the o/p by salary.SELECT MGR, ENAME, SAL FROM EMP WHERE (MGR, SAL) IN (SELECT MGR, MIN (SAL) FROM EMP GROUP BY MGR HAVING MIN (SAL) > 1000) ORDER BY SAL;

21. List employees along with their department details for those earning Sal > 2000;SELECT ENAME, DNAME, LOC, D.DEPTNO FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO AND E.SAL > 2000;

22. List employees with grade 3, 4.

5

Page 6: Sq l Queries

SELECT ENAME, S.GRADE, SAL FROM EMP E, SALGRADE S WHERE SAL BETWEEN S.LOSAL AND S.HISAL AND GRADE IN (SELECT GRADE FROM SALGRADE WHERE GRADE IN (3, 4));

23. Show all employees located in "DALLAS"SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC='DALLAS');

24. List employees name, job, Sal, grade, dname, loc for every one in the company except clerks, Sort on salary displaying highest first.SELECT ENAME, JOB, SAL, GRADE, LOC, DNAME FROM EMP E, DEPT D, SALGRADE S WHERE E.DEPTNO = D.DEPTNO ANDSAL BETWEEN S.LOSAL AND S.HISAL AND JOB! = 'CLERK' ORDER BY SAL DESC;

25. List employees who earn Sal less than their managers.SELECT E.ENAME, E.SAL "EMPLOYEE SAL", M.SAL "MANAGER SAL" FROM EMP E, EMP M WHERE E.MGR = M.EMPNO AND E.SAL < M.SAL;

26. List the job that was filled in the first half of 1982, and the same job that was filled during the same period in 1983.SELECT JOB FROM EMP WHERE (HIREDATE BETWEEN '01-JAN-82' AND '30-JUN-82') OR (HIREDATE BETWEEN '01-JAN-83' AND '30-JUN-83') GROUP BY JOB;

27. Find employee who joined the company before their manager.SELECT E.ENAME, E.HIREDATE, M.ENAME MANAGER, M.HIREDATE FROM EMP E, EMP M WHERE E.MGR = M.EMPNOAND E.HIREDATE < M.HIREDATE;

28. Find employees drawing least salary in each job type.SELECT JOB, ENAME FROM EMP WHERE SAL IN (SELECT MIN (SAL) FROM EMP GROUP BY JOB) ;

29. List employees having salary greater than average salary of sales department.SELECT ENAME, SAL FROM EMP WHERE SAL > (SELECT AVG (SAL) FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME='SALES')) ;

30. Find most recently hired employees in each department, Order by hire date.SELECT E.ENAME, HIREDATE, DEPTNO FROM EMP E WHERE E.HIREDATE= (SELECT MAX (HIREDATE) FROM EMP WHERE DEPTNO = E.DEPTNO) ORDER BY HIREDATE;

31. In which year did most people join the company. List the year and no of employees.SELECT TO_CHAR (HIREDATE,'YYYY') "YEAR”, COUNT (*) "TOTAL EMPLOYEE" FROM EMP GROUP BY TO_CHAR (HIREDATE,'YYYY')HAVING COUNT (*) = (SELECT MAX (COUNT (*)) FROM EMP GROUP BY TO_CHAR (HIREDATE,'YYYY'));

6

Page 7: Sq l Queries

1. Display the department with maximum average salary.2. Add no_of_emps column to dept table and insert number of employees in each department. 3. Move smith to sales dept using sub query.4. Create a view with empno, ename, job, Sal, deptno from emp table in dept 30.5. Create a view with ename, job, Sal, deptno, dname, loc, and grade.6. Create a sequence to generate only even numbers from 2 to 20.7. Update salary of smith with salary of Allen.

7

Page 8: Sq l Queries

1. Display the department with maximum average salary.SELECT D.DNAME, ROUND (AVG (SAL), 2) FROM EMP E, DEPT DWHERE E.DEPTNO = D.DEPTNOGROUP BY D.DNAME HAVING AVG (SAL) = (SELECT MAX (AVG (SAL)) FROM EMP GROUP BY DEPTNO);

2. Add no_of_emps column to dept table and insert number of employees in each department. UPDATE DEPT SET NO_OF_EMP = (SELECT COUNT (*) FROM EMP WHERE DEPTNO = DEPT.DEPTNO);

3. Move smith to sales dept using sub query.UPDATE EMP SET DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME = 'SALES') WHERE ENAME='SMITH';

4. Create a view with empno, ename, job, Sal, deptno from emp table in dept 30.CREATE VIEW DEPT30_EMP AS SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE DEPTNO = 30;

5. Create a view with ename, job, Sal, deptno, dname, loc, and grade.CREATE VIEW EMP_DEPT_SALGRADE AS SELECT ENAME, JOB, SAL, E.DEPTNO, DNAME, LOC, GRADE FROM EMP E, DEPT D, SALGRADE SWHERE E.DEPTNO = D.DEPTNO AND E.SAL BETWEEN S.LOSAL AND S.HISAL;

6. Create a sequence to generate only even nos from 2 to 20.CREATE SEQUENCE S1INCREMENT BY 2START WITH 2MAXVALUE 20CACHE 5;

7. Update salary of smith with salary of Allen. UPDATE EMP SET SAL = (SELECT SAL FROM EMP WHERE ENAME = 'ALLEN')WHERE ENAME='SMITH';

8

Page 9: Sq l Queries

1. Display the department with maximum average salary.SELECT D.DNAME, ROUND (AVG (SAL), 2) FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO GROUP BY D.DNAME HAVING AVG (SAL) = (SELECT MAX (AVG (SAL)) FROM EMP GROUP BY DEPTNO);

2. Add no_of_emps column to dept table and insert number of employees in each department. UPDATE DEPT SET NO_OF_EMP = (SELECT COUNT (*) FROM EMP WHERE DEPTNO = DEPT.DEPTNO);

3. Move smith to sales dept using sub query.UPDATE EMP SET DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME = 'SALES') WHERE ENAME='SMITH';

4. Create a view with empno, ename, job, sal, deptno from emp table in dept 30.CREATE VIEW DEPT30_EMP AS SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE DEPTNO = 30;

5. Create a view with ename, job, Sal, deptno, dname, loc, and grade.CREATE VIEW EMP_DEPT_SALGRADE AS SELECT ENAME, JOB, SAL, E.DEPTNO, DNAME, LOC, GRADE FROM EMP E, DEPT D, SALGRADE SWHERE E.DEPTNO = D.DEPTNO AND E.SAL BETWEEN S.LOSAL AND S.HISAL;

6. Create a sequence to generate only even nos from 2 to 20.CREATE SEQUENCE S1INCREMENT BY 2START WITH 2MAXVALUE 20CACHE 5;

7. Update salary of smith with salary of Allen. UPDATE EMP SET SAL = (SELECT SAL FROM EMP WHERE ENAME = 'ALLEN')WHERE ENAME='SMITH';

9

Page 10: Sq l Queries

PL/SQL Program 1:

CL SCRDECLARE

EMP_SAL NUMERIC (9, 2);ANUAL_SAL NUMERIC (11, 2);TAX NUMERIC (9, 2);PER NUMERIC (4, 2);TNAME VARCHAR2 (20):= '&EMP_NAME';

BEGINSELECT SAL INTO EMP_SAL FROM EMP WHERE

ENAME =TNAME;ANUAL_SAL:= EMP_SAL * 12;IF ANUAL_SAL > 50000 THEN

TAX: = ANUAL_SAL * 0.15;PER: = 15;

ELSIF ANUAL_SAL > 30000 THENTAX: = ANUAL_SAL * 0.12;PER: = 12;

ELSIF ANUAL_SAL > 15000 THENTAX: = ANUAL_SAL * 0.10;PER: = 12;

ELSIF ANUAL_SAL > 10000 THENTAX: = ANUAL_SAL * 0.05;PER: = 12;

ELSETAX: = ANUAL_SAL * 0.02;PER: = 2;

END IF;

DBMS_OUTPUT.PUT_LINE ('MONTHLY SALARY OF ' || TNAME || ' IS $ ' || EMP_SAL);DBMS_OUTPUT.PUT_LINE ('ANUAL SALARY OF ' || TNAME || ' IS $ ' || ANUAL_SAL);DBMS_OUTPUT.PUT_LINE (' TAX PER IS ' || PER || '%.');DBMS_OUTPUT.PUT_LINE ('TOTAL TAX FOR ' || TNAME || ' IS $ ' || TAX);DBMS_OUTPUT.PUT_LINE ('NET SALARY OF ' || TNAME || ' IS $ ' || (ANUAL_SAL-TAX));

END;

Program 2:

CL SCRDECLARE

EMP_SAL NUMERIC (9, 2);ANUAL_SAL NUMERIC (11, 2);BONUS NUMERIC (9, 2);

10

Page 11: Sq l Queries

PER NUMERIC (4, 2);TNAME VARCHAR2 (20):= '&EMP_NAME';

BEGINSELECT SAL INTO EMP_SAL FROM EMP WHEREENAME =TNAME;ANUAL_SAL:= EMP_SAL * 12;

CASEWHEN ANUAL_SAL > 50000 THEN

BONUS: = ANUAL_SAL * 0.1;PER: = 10;

WHEN ANUAL_SAL > 30000 THENBONUS: = ANUAL_SAL * 0.15;PER: = 15;

WHEN ANUAL_SAL > 15000 THENBONUS: = ANUAL_SAL * 0.2;PER: = 20;

WHEN ANUAL_SAL > 10000 THENBONUS: = ANUAL_SAL * 0.25;PER: = 25;

ELSEBONUS: = ANUAL_SAL * 0.5;PER: = 50;

END CASE;

DBMS_OUTPUT.PUT_LINE ('MONTHLY SALARY OF '|| TNAME || ' IS $ ' || EMP_SAL);DBMS_OUTPUT.PUT_LINE ('ANUAL SALARY OF ' || TNAME || ' IS $ ' || ANUAL_SAL);DBMS_OUTPUT.PUT_LINE (' BONUS PER IS ' || PER || '%.');DBMS_OUTPUT.PUT_LINE ('TOTAL BONUS FOR ' || TNAME || ' IS $ ' || BONUS);DBMS_OUTPUT.PUT_LINE ('NET SALARY OF ' ||TNAME || ' IS $ ' || (ANUAL_SAL+BONUS));INSERT INTO BONUS(ENAME,SAL) VALUES(TNAME,BONUS);COMMIT;

END;

Program 3:

WRITE A PL/SQL TO CALCULATE INCOMETAX ON THE BASE OF SALARYFROM EMP TABLE

CL SCRDECLARE

ANUAL_SAL NUMERIC (11, 2);TAX NUMERIC (9, 2);CURSOR C1 IS SELECT * FROM EMP;EMP_REC C1%ROWTYPE;

BEGINOPEN C1;LOOP

FETCH C1 INTO EMP_REC;

11

Page 12: Sq l Queries

EXIT WHEN C1%NOTFOUND;ANUAL_SAL:= EMP_REC.SAL * 12;IF ANUAL_SAL > 50000 THEN

TAX: = ANUAL_SAL * 0.15;ELSIF ANUAL_SAL > 30000 THEN

TAX: = ANUAL_SAL * 0.12;ELSIF ANUAL_SAL > 15000 THEN

TAX: = ANUAL_SAL * 0.10;ELSIF ANUAL_SAL > 10000 THEN

TAX: = ANUAL_SAL * 0.05;ELSE

TAX: = ANUAL_SAL * 0.02;END IF;

DBMS_OUTPUT.PUT_LINE ('ANUAL SALARY OF ' || EMP_REC.ENAME || ' IS $ ' || ANUAL_SAL || ' AND TAX IS $ ' || TAX);END LOOP;CLOSE C1;

END;

Program 4:

WRITE A PL/SQL FOR CALCULATING BONUS ON THE BASE OF JOBAND STORE THEM INTO BONUS TABLE.

CL SCRDECLARE

ANUAL_SAL NUMERIC (11, 2);BONUS NUMERIC (9, 2);CURSOR C1 IS SELECT * FROM EMP;

BEGINDELETE BONUS;DBMS_OUTPUT.PUT_LINE ('ENAME JOB SAL ANUAL_SAL BONUS');DBMS_OUTPUT.PUT_LINE ('---------------------------------------------------------------------------------------------------------');FOR EMP_REC IN C1LOOP

ANUAL_SAL:= EMP_REC.SAL * 12;CASE

12

Page 13: Sq l Queries

WHEN EMP_REC.JOB = 'PRESIDENT' THENBONUS: = ANUAL_SAL * 0.5;

WHEN EMP_REC.JOB = 'MANAGER' THENBONUS: = ANUAL_SAL * 0.25;

WHEN EMP_REC.JOB = 'ANALYST' THENBONUS: = ANUAL_SAL * 0.15;

WHEN EMP_REC.JOB = 'SALESMAN' THENBONUS: = ANUAL_SAL * 0.1;

ELSEBONUS: = ANUAL_SAL * 0.5;

END CASE;DBMS_OUTPUT.PUT_LINE (‘’);DBMS_OUTPUT.PUT_LINE (RPAD (EMP_REC.ENAME, 15) ||''||

RPAD (EMP_REC.JOB, 15) ||' '||RPAD (EMP_REC.SAL, 15)||' '|| RPAD (ANUAL_SAL,15)||' '||RPAD (BONUS, 15));INSERT INTO BONUS (ENAME, SAL) VALUES (EMP_REC.ENAME, BONUS);

END LOOP;COMMIT;

END;

Program 5:

WRITE A PL/SQL FOR FINDING TOP 5 EARNERS OF EMP TABLE.

CL SCRDECLARECURSOR C1 IS SELECT ENAME, JOB, SAL FROM EMP ORDER BY SAL DESC;I C1%ROWTYPE;NODATA EXCEPTION;BEGIN

OPEN C1;LOOP

FETCH C1 INTO I;IF C1%NOTFOUND THEN

RAISE NODATA;END IF;EXIT WHEN C1%ROWCOUNT > 5;DBMS_OUTPUT.PUT_LINE (RPAD (C1%ROWCOUNT, 8) ||RPAD (I.ENAME, 20) ||RPAD (I.JOB, 20) ||RPAD (I.SAL, 15));

END LOOP;CLOSE C1;

EXCEPTIONWHEN NODATA THEN

DBMS_OUTPUT.PUT_LINE ('NO RECORD IN EMP ');

13

Page 14: Sq l Queries

WHEN OTHERS THENDBMS_OUTPUT.PUT_LINE ('ERROR OCCURE');

END;

Program 6:

WRITE A PL/SQL TO CALCULATE INCOMETAX ON THE BASE OF SALARY FROM EMP TABLE.

CL SCRDECLAREANUAL_SAL NUMERIC (11, 2);TAX NUMERIC (9, 2);CURSOR C1 IS SELECT * FROM EMP;EMP_REC C1%ROWTYPE;BEGIN

OPEN C1;LOOP

FETCH C1 INTO EMP_REC;EXIT WHEN C1%NOTFOUND;ANUAL_SAL:= EMP_REC.SAL * 12;IF ANUAL_SAL > 50000 THEN

TAX: = ANUAL_SAL * 0.15;ELSIF ANUAL_SAL > 30000 THEN

TAX: = ANUAL_SAL * 0.12;ELSIF ANUAL_SAL > 15000 THEN

TAX: = ANUAL_SAL * 0.10;ELSIF ANUAL_SAL > 10000 THEN

TAX: = ANUAL_SAL * 0.05;ELSE

TAX: = ANUAL_SAL * 0.02;END IF;DBMS_OUTPUT.PUT_LINE ('ANUAL SALARY OF ' || EMP_REC.ENAME || ' IS $ ' || ANUAL_SAL || ' AND TAX IS $ ' || TAX);

END LOOP;CLOSE C1;

END;

14

Page 15: Sq l Queries

Program 7:

WRITE A PL/SQL FOR CALCULATING BONUS ON THE BASE OF JOBAND STORE THEM INTO BONUS TABLE.

CL SCRDECLAREANUAL_SAL NUMERIC (11, 2);BONUS NUMERIC (9, 2);CURSOR C1 IS SELECT * FROM EMP;BEGIN

DELETE BONUS;DBMS_OUTPUT.PUT_LINE ('ENAME JOB SAL ANUAL_SAL BONUS');DBMS_OUTPUT.PUT_LINE ('---------------------------------------------------------------------------------------------------------');FOR EMP_REC IN C1LOOPANUAL_SAL := EMP_REC.SAL * 12; CASE

WHEN EMP_REC.JOB = 'PRESIDENT' THENBONUS: = ANUAL_SAL * 0.5;

WHEN EMP_REC.JOB = 'MANAGER' THENBONUS: = ANUAL_SAL * 0.25;

WHEN EMP_REC.JOB = 'ANALYST' THENBONUS: = ANUAL_SAL * 0.15;

WHEN EMP_REC.JOB = 'SALESMAN' THENBONUS: = ANUAL_SAL * 0.1;

ELSEBONUS: = ANUAL_SAL * 0.5;

END CASE;DBMS_OUTPUT.PUT_LINE ('');DBMS_OUTPUT.PUT_LINE (RPAD (EMP_REC.ENAME, 15) ||''|| RPAD

(EMP_REC.JOB, 15) ||''||RPAD (EMP_REC.SAL, 15)||''|| RPAD(ANUAL_SAL,15)||''||RPAD (BONUS, 40));

INSERT INTO BONUS (ENAME, SAL) VALUES (EMP_REC.ENAME, BONUS);END LOOP;COMMIT;

END;

15

Page 16: Sq l Queries

Program 8:

WRITE A PL/SQL FOR FINDING TOP 5 EARNERS OF EMP TABLE.

CL SCRDECLARECURSOR C1 IS SELECT ENAME, JOB, SAL FROM EMP ORDER BY SAL DESC;I C1%ROWTYPE;BEGIN

OPEN C1;LOOP

FETCH C1 INTO I;EXIT WHEN C1%ROWCOUNT > 5;DBMS_OUTPUT.PUT_LINE(RPAD(C1%ROWCOUNT,8)||RPAD(I.ENAME,20) ||RPAD(I.JOB ,20)||RPAD(I.SAL,15));

END LOOP;CLOSE C1;EXCEPTIONWHEN OTHERS THENDBMS_OUTPUT.PUT_LINE(SQLCODE||' '||SUBSTR(SQLERRM,1,80));

END;

Program 9:

FUNCTION TO CALCULATE BONUS ON THE BASE OF SALARY

CREATE OR REPLACE FUNCTION BONUS_CALC (EMP_SAL NUMBER)

16

Page 17: Sq l Queries

RETURN NUMBERIS

ANUAL_SAL NUMERIC (11, 2);BONUS NUMERIC (9, 2);

BEGINANUAL_SAL:= EMP_SAL * 12;CASE

WHEN ANUAL_SAL > 50000 THENBONUS: = ANUAL_SAL * 0.10;

WHEN ANUAL_SAL > 30000 THENBONUS: = ANUAL_SAL * 0.15;

WHEN ANUAL_SAL > 15000 THENBONUS: = ANUAL_SAL * 0.20;

WHEN ANUAL_SAL > 10000 THENBONUS: = ANUAL_SAL * 0.25;

ELSEBONUS: = ANUAL_SAL * 0.50;

END CASE;RETURN BONUS;

END;

Program10:

Declareemp_sal number(4) := & nu ;bonu number(4);Beginbonu:=bonus_cla (emp_sal);dbms_output.put_line (bonu);End;

17

Page 18: Sq l Queries

functionprogram2 (count.sql)

declarestr varchar2(20):='&name';num number(2);beginnum:=countchar(str);dbms_output.put_line(num);end;

functionprogram3

(count1.sql)---------------

create or replace function countchar(str varchar2) return integer is c number(2); begin c=length(str); return c; end;

function program4

(nextno.sql)

declare next number(5); begin next:=nextno(); dbms_output.put_line(next); end;

function program5 (nextno1.sql)

create or replace function nextno return number is no number(5); next number(5);

18

Page 19: Sq l Queries

begin select max(empno) into next from emp; no:=next+1;return no; end;

function program6 (specific count.sql)

declarestr varchar2(20):='&name';c char:='&char';cou number;begincou:=specificcount(str,c);dbms_output.put_line(cou);end;

functionprogram7 (specificount1.sql)

create or replace function specificcount(str varchar2, c char) return integeriscou number;begincou:=length(str)-(length(replace(str,c,'')));return cou;end;

pl/sql procedure programs

program1:-(emp_delete.sql)

CREATE OR REPLACE PROCEDURE EMP_DEL (EMP_REC IN VARCHAR2, NO_REC OUT NUMBER)ISEMPNAME EMP.ENAME%TYPE;CURSOR C1(EMP_NAME VARCHAR2) ISSELECT ENAME FROM EMP WHERE ENAME = EMP_NAME;BEGIN

19

Page 20: Sq l Queries

NO_REC := 0;OPEN C1(EMP_REC);LOOPFETCH C1 INTO EMPNAME;EXIT WHEN C1%NOTFOUND;END LOOP;NO_REC := C1%ROWCOUNT;CLOSE C1;DELETE EMP WHERE ENAME = EMP_REC;END;/

procedureprogram2

/*CALL PROCEDURE TO DELETE RECORD FROM EMP TABLE AND RETURN NUMBER OF

RECORDS DELETED.*/

CL SCRDECLARE EMPNAME EMP.ENAME%TYPE := '&NAME'; COUNTER NUMBER(2);BEGIN EMP_DEL(EMPNAME,COUNTER); DBMS_OUTPUT.PUT_LINE('TOTAL RECORD DELETED IS : ' || COUNTER);END;/

programprocedure3

create or replace procedure pro_cur(depno in number) is emp_name varchar2(10); cursor c1(depno number) is select ename from emp where deptno=depno; begin open c1(depno); loop fetch c1 into emp_name; exit when c1%notfound; dbms_output.put_line(emp_name); end loop;

20

Page 21: Sq l Queries

close c1; end;

pl/sql trigger programs

trigger program1:

/* TRIGGER TO VALIDATE ACCOUNT TRANSACTION IN ACC_MAST AND ACC_TRANSACTION TABLE AT THE TIME OF INSERTION OF DATA. */CREATE OR REPLACE TRIGGER ACC_TRIGBEFORE INSERT ON ACC_TRANSACTIONFOR EACH ROWDECLAREAMOUNT NUMBER(9,2);MAST_ROW ACC_MAST%ROWTYPE;CURSOR C1(AC_NO NUMBER) ISSELECT * FROM ACC_MAST WHERE ACCTNO = AC_NO;BEGINIF ((:NEW.DEPOSIT IS NOT NULL) AND (:NEW.WITHDRAW IS NOT NULL))OR (:NEW.DEPOSIT = 0) OR (:NEW.WITHDRAW = 0) THENRAISE_APPLICATION_ERROR(-20090,'TRANSACTION ERROR');ELSIF (:NEW.DEPOSIT IS NULL) AND (:NEW.WITHDRAW IS NULL) THENRAISE_APPLICATION_ERROR(-20000,'ATLEAST DEPOSIT OR WITHDRAWAL AMOUNT SHOULD BE THERE.');END IF;OPEN C1(:NEW.ACCTNO);FETCH C1 INTO MAST_ROW ;IF C1%NOTFOUND THENIF (:NEW.DEPOSIT IS NULL) THENRAISE_APPLICATION_ERROR(-20000,'WITHDRAWAL SHOULD NOT BE ALLOWED IN NEWACCOUNT.');END IF;INSERT INTO ACC_MAST VALUES (:NEW.ACCTNO,:NEW.DEPOSIT);ELSEIF (:NEW.DEPOSIT IS NULL) THENAMOUNT := :NEW.WITHDRAW;IF AMOUNT > MAST_ROW.BAL THENRAISE_APPLICATION_ERROR(-20000,'INSUFFICIENT FUND.');END IF;

21

Page 22: Sq l Queries

UPDATE ACC_MAST SET BAL = BAL - AMOUNT WHERE ACCTNO = :NEW.ACCTNO ;ELSEAMOUNT := :NEW.DEPOSIT;UPDATE ACC_MAST SET BAL = BAL + AMOUNT WHERE ACCTNO = :NEW.ACCTNO ;END IF;END IF;CLOSE C1;END;/

trigger2

CREATE OR REPLACE TRIGGER ONEAFTER INSERT ON TRANSFOR EACH ROWBEGINUPDATE ACCOUNTS SET BALANCE=BALANCE+:NEW.DEPOSITWHERE ACC_NO=:NEW.ACC_NO;UPDATE ACCOUNTS SET BALANCE=BALANCE+:NEW.WITHDRAWlWHERE ACC_NO=:NEW.ACC_NO;

END;/

pl/sql cursor,records

cursor1:

DECLAREcursor emp_cur(no number) is select ename from emp where sal>no;emprec emp.ename%type;num number(2);beginopen emp_cur(&no);loop fetch emp_cur into emprec;exit when emp_cur%notfound;dbms_output.put_line(emprec);end loop;num:=emp_cur%rowcount;dbms_output.put_line(num);if emp_cur%isopen then dbms_output.put_line('cur is opened');end if;close emp_cur;

22

Page 23: Sq l Queries

end;/

cursor2:

1.WRITE A PL/SQL CALCULATE BONUS OF ALL EMPLOYEES USING CURSORS AND INSERT INTO BONUS TABLE.SOLUTION:

DECLARE ANN_SAL NUMBER(9,2); BONUS NUMBER(9,2); CURSOR CUR1 IS SELECT EMPNO,ENAME,JOB,SAL,DEPTNO FROM EMP; REC CUR1%ROWTYPE; BEGIN OPEN CUR1; DELETE BONUSTAB; LOOP FETCH CUR1 INTO REC; ANN_SAL:=REC.SAL*12; IF(ANN_SAL>35000) THEN BONUS:=1000; ELSIF(ANN_SAL>20000) THEN BONUS:=500; ELSE BONUS:=100; END IF; EXIT WHEN CUR1%NOTFOUND; INSERT INTO BONUSTAB VALUES(REC.ENAME,REC.SAL,ANN_SAL,BONUS); END LOOP; CLOSE CUR1; END;

cursor3

2.WRITE A PL/SQL PROGRAM FOR LISTING TOP FIVE EARNERS IN THE COMPANYSOLUTION:

DECLARE CURSOR CUR2 IS SELECT * FROM EMP ORDER BY SAL DESC;BEGIN DELETE EMPTAB5;

23

Page 24: Sq l Queries

FOR REC1 IN CUR2 LOOP INSERT INTO EMPTAB5 VALUES(REC1.ENAME,REC1.SAL,REC1.JOB); EXIT WHEN CUR2%ROWCOUNT=5; END LOOP;END;

cursor4

declare cursor emp_cur is select * from emp where deptno=&dno; emprec emp%rowtype; begin open emp_cur; loop fetch emp_cur into emprec; exit when emp_cur%notfound; dbms_output.put_line(emprec.ename); end loop; close emp_cur; end;

Records:

record1:

declaretype rec is record(dno dept.deptno%type,dnam dept.dname%type,loca dept.loc%type);drec rec;beginselect deptno,dname,loc into drec from dept where deptno=&no;dbms_output.put_line(drec.dno||','||drec.dnam||','||drec.loca);end;

record2

DECLARE drec dept%rowtype; begin select * into drec from dept where deptno=&no; dbms_output.put_line(drec.deptno||','||drec.dname||','||drec.loc); end;

packages:programpackage1:

package declaration

24

Page 25: Sq l Queries

create or replace package emp_tab as procedure hire_emp(empno number,ename varchar2,job varchar2,sal number,mgr number,comm number,deptno number);procedure del_emp(empid number);procedure inc_sal(empid number,inc number);end emp_tab;

package definition

create or replace package body emp_tab as procedure hire_emp (empno number,ename varchar2,job varchar2, sal number,mgr number,comm number,deptno number) is begin insert into emp values(empno,ename,job,sal,mgr,sysdate,comm,deptno); end hire_emp; procedure del_emp(emp_id number) is begin delete from emp where empno=empid; end del_emp; procedure inc_sal(empid number,inc number) is begin update emp set sal=sal+inc where empno=empid;z if sql%notfound then dbms_output.put_line('emp not found');end if; end inc_sal; end emp_tab;

package2:create or replace package emp_cur ascursor c1 return emp%rowtype;end emp_cur;

create or replace package body emp_cur ascursor c1 return emp%rowtype isselect * from emp where sal>4000;end emp_cur;

25

Page 26: Sq l Queries

declare emprec emp%rowtype; begin open emp_cur.c1; loop fetch emp_cur.c1 into emprec; exit when emp_cur.c1%notfound; dbms_output.put_line(emprec.ename||','||emprec.sal); end loop; close emp_cur.c1; end;

package3:

declaration

create or replace package kick_p as procedure kick_i(sno number,name varchar2); procedure kick_d(no number); procedure kick_u(no number,sanme varchar2); end kick_p;

definition create or replace package body kick_p as procedure kick_i(sno number,name varchar2) is begin insert into kick values(sno,name); end kick_i; procedure kick_d(no number) is begin delete from kick where sno=no; if sql%notfound then dbms_output.put_line('no data found for del'); end if; end kick_d; procedure kick_u(no number,sanme varchar2) is begin update kick set name=sanme where sno=no; if sql%notfound then dbms_output.put_line('no data found for matching');

26

Page 27: Sq l Queries

end if; end kick_u; end kick_p;

package4:

declaration:create or replace package matrix_p as procedure mat_i(sno number,sname varchar2); procedure mat_d(no number); procedure mat_u(no number,name varchar2); end matrix_p;

definition:

create or replace package body matrix_p as procedure mat_i(sno number,sname varchar2) is begin insert into matrix values(sno,sname); end mat_i; procedure mat_d(no number) is begin delete from matrix where sno=no; if sql%notfound then dbms_output.put_line('no data match'); end if; end mat_d; procedure mat_u(no number,name varchar2) is begin update matrix set sname=name where sno=no; if sql%notfound then dbms_output.put_line('no data found for matching'); end if; end mat_u; end matrix_p;

27

Page 28: Sq l Queries

package5

Declaration:

create or replace package dept_t as procedureinsert_dept(deptno number,dname varchar2,loc varchar2);procedure delete_dept(empid number);end dept_t;

defenition:

create or replace package dept_t asprocedure ins_dep10(deptno number,dname varchar2,loc varchar2);procedure del_dep10(empid number);end dept_t;

create or replace package dept_t as procedureinsert_dept(deptno number,dname varchar2,loc varchar2)isbegininsert into dep10 values(deptno,dname,loc);end insert_dept;procedure delete_dept(deptid number)isbegindelete from dep10 where deptno=deptid;if sql%notfound thendbms_output.put_line('not found to del');end if;end delete_dept;end dept_t;

create or replace procedure pro_cur(depno in number) is emp_name varchar2(10); cursor c1(depno number) is select ename from emp where deptno=depno;

28

Page 29: Sq l Queries

begin open c1(depno); loop fetch c1 into emp_name; exit when c1%notfound; dbms_output.put_line(emp_name); end loop; close c1; end;

29