Publicidad

Dbms record

Technical Support Engineer
25 de May de 2014
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Publicidad
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Dbms record
Próximo SlideShare
LabLab
Cargando en ... 3
1 de 144
Publicidad

Más contenido relacionado

Publicidad
Publicidad

Dbms record

  1. INDEX Basic Queries 1.Create student table with rno, name, class, mid1, mid2 and final marks in GE. Apply necessary constraints 2. Create course table with course id, course name(GE), instructor. Apply necessary constraints. 3. Insert rows in both the tables 4. Add a column total, average and course id to student table 5.Make course id as foreign key in student table 6.Increase the width of name column to 15. 7. Change the marks of a particular student. 8. Add five marks grace in m1 subject to all students who scored less than 10. 9. Update total and average columns. 10. Delete the students of solar processing course and rollback. 11. List all students data 12. Display all the students belonging to NCE. 13. Display all the students of NCE who scored more than 150 marks. 14. Display all students information in ascending order 15. List all the students who do not belong to NCE. 16. List all students having average between 60 and 80 17. List all students belonging to Mr.Smith. 18. List the rollno and class of the students 19. List the students belonging to course id 631 and 633. 20. Count the number of students in each course. 21. List the course id having minimum of 2 students.
  2. 22. Find the names of the students that begin with D. 23. Find the names of the students that ends with K. 24. Find the names of the students who have S in their name. 25.Display all students name in uppercase. 26. Determine the names and length of each name. 27. Display all students having 4 characters. 28. Display student details in descending order. 29. List the rno, name, course name, course id. 30. To display all records of course table even if there is no matching course id in student. 31. Display the maximum and minimum total and average. 32. Find the highest marks obtained in PCMT. 33. To display the second highest and second lowest marks. 34. Display all students who have not opted for wed designing. Queries on Emp and Dept tables 1.Simple Queries 2.Updating the Queries 3.Order By Clause 4.Data Functions 5.Group By Clause 6.Subqueries 7.Group Functions 8.Joins 9.Set Operators
  3. 10.Views 11.Co-related subquery and multicolumn subquery 12.Miscellaneous 12.DDL Queries INTERACTIVE SQL:PART -1 1.SQL Statement for creating the tables: a)Table Name: client_master b)Table Name: product_master c) Table Name: salesman_master d) Table Name: sales_order e) Table Name: sales_order_details 2.SQL Statement for inserting data into their respective tables: a)Data for client_master table b)Data for product_master table c)Data for salesman_master table d)Data for sales_order table e)Data for sales_order_details table 3.SQL Statement for retrieving records from a table 4.SQL Statement for updating records in a table 5.SQL Statement for deleting records in a table 6.SQL Statement for altering the table structure 7.SQL Statement for renaming the table
  4. INTERACTIVE SQL:PART -2 1.Generate SQL Statements to perform the computations on table data. 2.SQL Statement for Date Manipulation INTERACTIVE SQL:PART -3 1.SQL Statement for using Having and Group By Clauses 2.Exercise on Joins and Correlation 3.SQL Statements for exercise on Sub-queries PL/SQL QUERIES Simple PL/SQL Queries a)Pl/Sql to add two numbers b)PL/Sql to find factorial of a number c)PL/Sql for demo on for loop d)PL/Sql for case structure e)PL/Sql for simple loop. f).PL/Sql to increase the vaue by 10. g)PL/SQL for performing arithmetic operations h)PL/SQL to find square, cube, double of a number i)PL/SQL program to swap two numbers j)PL/SQL program to find multiplication table k)PL/SQL to determine whether a year is leap year or not l)PL/SQL to insert item numbers from 1 to 5 using a loop. m)PL/SQL to delete an item whose itemnum=4 n)PL/SQL program to display name, salary , commission and emp no of an employee based on the id. o)PL/SQL program for inverting a number. p)PL/SQL program to calculate the area of a circle for value of radius ranging from 3 to 7.
  5. q)Write a program to print empno, ename, job and salary of an employee given empno. r)Write a program to set the values for DA, HRA, GROSS fields of all the records of the given table. s)Write a PL/SQL block that will accept a account number from the user , check if the users balance is less than the minimum balance then deduct Rs.100 from the balance as a fine. t)Write a PL/SQL block that will accept a account number and incremented amount from the user, and increments the balance with the incremented amount. u)Write a PL/SQL block that withdraws an amount of Rs.1000 then deposit’s an amount of Rs.10,000, updates the current balance ,then checks to see if the current balance of all the accounts does not exceed 50,000 if so then undo the deposit made. PL/SQL Queries on Cursors a)Write a PL/SQL block using the cursor to display details of all employee’s from emp2 table whose sum of sal and comm is > 2000. b)Write a PL/SQL block that display’s employee name and the salary of the first 5 employee’s having the highest salary. c)Write a pl/sql program to find the sum of the salaries in a particular department. d)PL/SQL to print all employees begin with S. e)PL/SQL to print names of all managers PL/SQL Queries on Procedures a)PL/SQL to update salary of an employee whose empno and increment mentioned. PL/SQL Queries on Exceptional Handling a)Write a PL/SQL block which accepts the empno from the user and display the details of the employee. When the user enters an empno that is not in the emp table then the PL/SQL block must display an appropriate message to the user. b)PL/SQL block that retrieves course table into cursor. Input course id to search. If course exists, print the information. If the course id does not exist, print user defined message by throwing user defined exception. c)Write a program to handle ZERO_DIVIDE exception
  6. PL/SQL Queries on Functions a)Write a PL/SQL function which checks for an employee number in the table and returns 1 if present otherwise returns 0. PL/SQL Queries on Tiggers a)PL/SQL that creates a trigger that inserts or update values of ename, job as lower case strings.
  7. BASIC QUERIES 1.Create student table with rno, name, class, mid1, mid2 and final marks in GE. Apply necessary constraints. SQL> create table student( 2 rno number(3), 3 name varchar2(10), 4 class varchar2(5), 5 mid1 number(2) , 6 mid2 number(2) , 7 finalmarks number(3) 8 ); Table created. 2. Create course table with course id, course name(GE), instructor. Apply necessary constraints. SQL> create table course( 2 courseid varchar(10) primary key, 3 coursename varchar2(10), 4 instructor varchar2(10) 5 ); 3. Insert rows in both the tables Student table: SQL> insert into student( 2 rno,name,class,mid1,mid2,finalmarks) 3 values(101,'smith','ncs',35,41,78); 1 row created.
  8. SQL>insert into student( 2 rno,name,class,mid1,mid2,finalmarks) 3 values(102,'john','nce',10,45,81) ; 1 row created. SQL> insert into student( 2 rno,name,class,mid1,mid2,finalmarks) 3 values(103,'ashok','nct',47,46,91); 1 row created. SQL>insert into student( 2 rno,name,class,mid1,mid2,finalmarks) 3 values(104,'Karthik','ncc',37,36,81); 1 row created. SQL>1 insert into student( 2 rno,name,class,mid1,mid2,finalmarks) 3 values(105,'durga','nce',48,49,92) 4 ; 1 row created. SQL> insert into student( 2 rno,name,class,mid1,mid2,finalmarks) 3 values(105,'durga','nce',48,49,92); 1 row created. SQL> select * from student
  9. 2 ; RNO NAME CLASS MID1 MID2 FINALMARKS ---------- ---------- ----- ---------- ---------- ---------- 101 smith ncs 32 42 77 102 john nce 20 46 79 103 ashok nct 45 44 81 104 Karthik ncc 33 36 83 105 durga nce 41 43 90 Course table: SQL> insert into course( 2 courseid,coursename,instructor) 3 values(631,'pcmt','Mrsmith'); 1 row created. SQL> insert into course( 2 courseid,coursename,instructor) 3 values(632,'webdesign','john'); 1 row created. SQL> insert into course( 2 courseid,coursename,instructor) 3 values(633,'solar','joe'); 1 row created. SQL> insert into course( 2 courseid,coursename,instructor) 3 values(634,'acounting','latha');
  10. 1 row created. SQL> insert into course( 2 courseid,coursename,instructor) 3 values(635,'statics','rani'); 1 row created. SQL> insert into course( 2 courseid,coursename,instructor) 3 values(636,'horti','vamshi'); 1 row created SQL> select * from course; Output: COURSEID COURSENAME INSTRUCTOR ---------- ---------- ---------- 631 pcmt Mrsmith 632 webdesign john 633 solar joe 634 acounting latha 635 statics rani 636 horti vamshi 4. Add a column total, average and course id to student table SQL> alter table student 2 add(total number(5), 3 average number(3),
  11. 4 courseid number(10) 5 ); Table altered. Output: SQL> select * from student; RNO NAME CLASS MID1 MID2 FINALMARKS TOTAL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- ------------- ---------- ------------------ ------------ 101 smith ncs 32 42 77 151 50 102 john nce 20 46 79 145 48 103 ashok nct 45 44 81 170 56 104 Karthik ncc 33 36 83 152 50 105 durga nce 41 43 90 174 58 5. SQL> alter table student 2 (add constraint fk foriegn key(courseid) 3 refernces courses(courseid)); 1 row updated 6.Increase the width of name column to 15. SQL> alter table student 2 modify 3 (name varchar2(15)); Table altered.
  12. Output: Name Null? Type ----------------------------------------- -------- ---------------------------- RNO NOT NULL NUMBER(3) NAME VARCHAR2(15) CLASS VARCHAR2(5) MID1 NUMBER(2) MID2 NUMBER(2) FINALMARKS NUMBER(3) TOTAL NUMBER(5) AVERAGE NUMBER(3) COURSEID NUMBER(10) 7. Change the marks of a particular student. SQL> update student 2 set finalmarks=95 3 where name='john'; 1 row updated. Output: SQL> select * from student; RNO NAME CLASS MID1 MID2 FINALMARKS TOTAL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- ----------------- ---------- ---------------- ------------ 101 smith ncs 32 42 77 152 50 634 102 john nce 20 46 79 145 48 631
  13. 103 ashok nct 45 44 81 170 56 636 104 Karthik ncc 33 36 83 152 50 632 105 durga nce 41 43 90 174 58 633 8. Add five marks grace in m1 subject to all students who scored less than 10. SQL> select mid1+5 2 from student 3 where mid1<10; Output : MID1+5 -------- 14 9. Update total and average columns. SQL> update student 2 set total=mid1+mid2+finalmarks, 3 average=total/3; 5 rows updated. Output: SQL> select * from student; RNO NAME CLASS MID1 MID2 FINALMARKS TOTAL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- ----------------- ---------- -------------- -------------- 101 smith ncs 32 42 77 151 50 102 john nce 20 46 79 14548 103 ashok nct 45 44 81 17056 104 Karthik ncc 33 36 83 152 50
  14. 105 durga nce 41 43 90 174 58 10. Delete the students of solar processing course and rollback. SQL> delete from course 2 where coursename='solar'; 1 row deleted. Output: SQL> select * from course; COURSEID COURSENAME INSTRUCTOR ---------- ---------- ---------- 631 pcmt Mrsmith 632 webdesign john 634 accounting latha 635 statics rani 636 horti vamshi SQL>rool back; OUTPUT: COURSEID COURSENAME INSTRUCTOR ---------- ---------- ---------- 631 pcmt Mrsmith 632 webdesign john 633 solar joe 634 acounting latha
  15. 635 statics rani 636 horti vamshi 11. List all students data SQL> select * from student; RNO NAME CLASS MID1 MID2 FINALMARKS TOTAL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- -------------------------------------------------------- 101 smith ncs 32 42 77 151 50 634 102 john nce 20 46 79 145 48 631 103 ashok nct 45 44 81 170 56 636 104 Karthik ncc 33 36 83 152 50 632 105 durga nce 41 43 90 174 58 633 12. Display all the students belonging to NCE. SQL> select * 2 from student 3 where class='nce'; Output: RNO NAME CLASS MID1 MID2 FINALMARKS ---------- --------------- ----- ---------- ---------- --------- ---------- 102 john nce 10 45 81 105 durga nce 48 49 92 13. Display all the students of NCE who scored more than 150 marks. SQL> select * 2 from student 3 where class='nce' and total>150;
  16. Output: RNO NAME CLASS MID1 MID2 FINALMARKS TOTAL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- ------------------- --------------- ---------- 102 john nce 10 45 81 163 54 632 105 durga nce 48 49 92 189 63 634 14. Display all students information in ascending order 1 select * 2 from student 3 order by 2; Output: RNO NAME CLASS MID1 MID2 FINALMARKS ---------- --------------- ----- ---------- ---------- ---------- ---------- 103 ashok nct 47 46 91 102 john nce 10 45 81 105 durga nce 48 49 92 101 smith ncs 35 41 78 104 Karthik ncc 37 36 81 15. List all the students who do not belong to NCE. SQL> select * 2 from student 3 where not(class='nce');
  17. Output: RNO NAME CLASS MID1 MID2 FINALMARKS TOTAL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- -------------------------------------------------------- 101 smith ncs 35 41 78 15451 634 103 ashok nct 47 46 91 184 61 636 104 Karthik ncc 37 36 81 154 51 632 16. List all students having average between 60 and 80 SQL> select * 2 from student 3 where average>60 and average<80; Output: RNO NAME CLASS MID1 MID2 FINALMARKS TOTAL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- -------------------------------------------------------- 103 ashok nct 47 46 91 184 61 636 105 durga nce 48 49 92 189 63 633 17. List all students belonging to Mr.Smith. SQL> select name 2 from student 3 where courseid=(select courseid from courses where instructor='Mrsmith'); Output: NAME --------------- John
  18. 18. List the rollno and class of the students SQL> select rno,class 2 from student; Output: RNO CLASS ---------- ----- 101 ncs 102 nce 103 nct 104 ncc 105 nce 19. List the students belonging to course id 631 and 633. SQL>select courseid 2 from course 3 where courseid=631 or courseid=633; no rows selected. Output: NAME --------------- john durga 20. Count the number of students in each course. SQL> select courseid, count(*) 2 from student
  19. 3 group by courseid; Output: COURSEID COUNT(*) ---------- ---------- 631 2 632 1 633 1 634 1 21. List the course id having minimum of 2 students. SQL> select courseid,count(*) 2 from student 3 group by courseid having count(*)>=2; Output: COURSEID COUNT(*) ---------- ---------- 631 2 22. Find the names of the students that begin with D. SQL> select name 2 from student 3 where name like 'd%'; Output: NAME ---------- Durga
  20. 23. Find the names of the students that ends with K. SQL> select name 2 from student 3 where name like '%k'; Output: NAME --------------- Karthik 24. Find the names of the students who have S in their name. SQL> select name 2 from student 3 where name like '%s%'; Output: NAME --------------- ashok 25.Display all students name in uppercase. SQL> select upper(name) 2 from student; Output: UPPER(NAME) --------------- SMITH JOHN
  21. ASHOK KARTHIK DURGA 26. Determine the names and length of each name. SQL> select name, length(name) 2 from student; Output: NAME LENGTH(NAME) --------------- ------------ smith 5 john 4 ashok 5 Karthik 7 durga 5 27. Display all students having 4 characters. SQL> select name 2 from student 3 where name like'____'; Output: NAME --------------- john Karthik 28. Display student details in descending order.
  22. SQL> select * 2 from student 3 order by 2 desc; Output: RNO NAME CLASS MID1 MID2 FINALMARKS TOATL AVERAGE COURSEID ---------- --------------- ----- ---------- ---------- ---------- ---------- 104 Karthik ncc 37 36 81 154 51 632 101 smith ncs 35 41 78 154 51 634 105 durga nce 48 49 92 189 63 633 102 john nce 14 45 81 163 54 631 103 ashok nct 47 46 91 184 61 636 29. List the rno, name, course name, course id. SQL> select rno,name,student.courseid,coursename 2 from student,course 3 where student.courseid=course.courseid; RNO NAME COURSEID COURSENAME ---------- --------------- ---------- ---------- 101 smith 634 acounting 102 john 631 PCMT 103 ashok 636 horti 104 Karthik 632 webdesign 105 durga 633 solar 30. To display all records of course table even if there is no matching course id in student. SQL> select *
  23. 2 from course c,student s 3 where c.courseid=s.courseid; Output: COURSEID COURSENAME INSTRUCTOR RNO NAME CLASS MID1 MID2 FINALMARKS ---------- ---------- ---------- ---------- ---------- ----- TOATL AVERAGE COURSEID ---------- ---------- --- ---- ---- ------ ---------- 634 acounting latha 101 smith ncs 35 41 78 154 51 634 631 pcmt Mrsmith 102 john nce 14 45 81 163 54 631 636 horti vamshi 103 ashok nct 47 46 91 184 61 636 632 webdesign john 104 Karthik ncc 37 36 81 154 51 632 633 solar joe 105 durga nce 48 49 92 189 63 633 31. Display the maximum and minimum total and average. SQL> select max(total)"max-toatl", 2 min(total)"min-total", 3 max(average)"max-avg", 4 min(average)"miv-avg" 5 from student; Output:
  24. max-toatl min-total max-avg miv-avg ---------- ---------- ---------- ---------- 189 154 63 51 32. Find the highest marks obtained in PCMT. SQL> select max(toatl) 2 from student 3 where courseid=(select courseid from courses where coursename='pcmt'); Output: MAX(TOATL) ---------- 163 33. To display the second highest and second lowest marks. Sql>select max(total) 2 from student 3 where total not in(select max(total)from student) Output: MAX(TOTAL) --------- 184 34. Display all students who have not opted for wed designing. SQL> select rno,name,class,coursename 2 from course c, student s 3 where c.courseid=s.courseid and coursename <> 'web designing';
  25. RNO NAME CLASS COURSENAME ---------- --------------- ----- ---------- 101 smith ncs acounting 102 john nce pcmt 103 ashok nct horti 105 durga nce solar Queries on Emp and Dept tables: 1.Simple queries 1)List the name, job and salary from emp. SQL> select ename,job,sal 2 from emp; Output:- ENAME JOB SAL ---------- --------- ---------- SMITH CLERK 800 ALLEN SALESMAN 1600 WARD SALESMAN 1250 JONES MANAGER 2975 MARTIN SALESMAN 1250 BLAKE MANAGER 2850 CLARK MANAGER 2450 SCOTT ANALYST 3000 KING PRESIDENT 5000
  26. TURNER SALESMAN 1500 ADAMS CLERK 1100 JAMES CLERK 950 FORD ANALYST 3000 MILLER CLERK 1300 14 rows selected. 2.Select names of all managers. SQL> select ename 2 from emp 3 where job='MANAGER'; Output: ENAME ---------- JONES BLAKE CLARK 3.Display the details of the employee joined in Feb 1981. SQL> select* 2 from emp 3 where to_char(hiredate,'mon-yyyy')='feb-1981'; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
  27. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 4.List the total salary of each employee. SQL> select sum(sal+comm) 2 from emp; Output: SUM(SAL+COMM) ------------- 7800 5.List all employees who receive no commission. SQL> select* 2 from emp 3 where comm null; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20
  28. 7934 MILLER CLERK 7782 23-JAN-82 1300 10 10 rows selected. 6.List all rows by converting null values in commission to 0 SQL> select empno,ename,nvl(comm,0) 2 from emp 3 where comm is null; Output: EMPNO ENAME NVL(COMM,0) ---------- ---------- ----------- 7369 SMITH 0 7566 JONES 0 7698 BLAKE 0 7782 CLARK 0 7788 SCOTT 0 7839 KING 0 7876 ADAMS 0 7900 JAMES 0 7902 FORD 0 7934 MILLER 0 10 rows selected. 7.To list names & salary greater than 2000. SQL> select ename,sal 2 from emp 3 where sal>2000;
  29. Output: ENAME SAL ---------- ---------- JONES 2975 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 FORD 3000 14 rows selected. 8.List names and salary less than 2000. SQL> select ename,sal 2 from emp 3 where sal<2000; Output: ENAME SAL ---------- ---------- SMITH 800 ALLEN 1600 WARD 1250 MARTIN 1250 TURNER 1500 ADAMS 1100
  30. JAMES 950 MILLER 1300 8 rows selected. 9.To list distinct jobs from employee. SQL> select distinct(job) 2 from emp; Output: JOB --------- ANALYST CLERK MANAGER PRESIDENT SALESMAN 10.List all employees who are neither clerks or managers. SQL> select* 2 from emp 3 where job not in('clerk','manager'); Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- --------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
  31. 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 14 rows selected. 11)List all emps who earn between 1000 and 3000. SQL>select* 2 from emp 3 where sal between 1000 and 3000; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
  32. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 11 rows selected. 12)List all employees whose name start with ‘A’. SQL> select ename 2 from emp 3 where ename like 'A%'; Output: ENAME ---------- ALLEN ADAMS 13)List all employees whose name end with ‘S’. SQL> select ename 2 from emp 3 where ename like '%S'; Output: ENAME ---------- JONES
  33. ADAMS JAMES 14)List all employees whose names contain ‘S’. SQL> select ename 2 from emp 3 where ename like '%S%'; Output: ENAME ---------- SMITH JONES SCOTT ADAMS JAMES 15)Select records whose names have only 4 characters. SQL> select ename 2 from emp 3 where ename like '____'; Output: ENAME ---------- WARD KING FORD
  34. 16)List the names and job who are clerk. SQL> select ename,job 2 from emp 3 where job='CLERK'; Output: ENAME JOB ---------- --------- SMITH CLERK ADAMS CLERK JAMES CLERK MILLER CLERK 17)List empno, name, length of each name. Display the name in uppercase, lowercase, titlecase and use rpad and lpad. SQL> select empno,upper(ename),lower(ename),initcap(ename),length(ename),lpad(ename,10,'*'),rpad(en ame,10,'*') 2 from emp; Output: EMPNO UPPER(ENAM LOWER(ENAM INITCAP(EN LENGTH(ENAME) LPAD(ENAME RPAD(ENAME ---------- ---------- ---------- ---------- ------------- ---------- ---------- 7369 SMITH smith Smith 5 *****SMITH SMITH***** 7499 ALLEN allen Allen 5 *****ALLEN ALLEN***** 7521 WARD ward Ward 4 ******WARD WARD******
  35. 7566 JONES jones Jones 5 *****JONES JONES***** 7654 MARTIN martin Martin 6 ****MARTIN MARTIN**** 7698 BLAKE blake Blake 5 *****BLAKE BLAKE***** 7782 CLARK clark Clark 5 *****CLARK CLARK***** 7788 SCOTT scott Scott 5 *****SCOTT SCOTT***** 7839 KING king King 4 ******KING KING****** 7844 TURNER turner Turner 6 ****TURNER TURNER**** 7876 ADAMS adams Adams 5 *****ADAMS ADAMS***** EMPNO UPPER(ENAM LOWER(ENAM INITCAP(EN LENGTH(ENAME) LPAD(ENAME RPAD(ENAME ---------- ---------- ---------- ---------- ------------- ---------- ---------- 7900 JAMES james James 5 *****JAMES JAMES***** 7902 FORD ford Ford 4 ******FORD FORD****** 7934 MILLER miller Miller 6 ****MILLER MILLER**** 14 rows selected. 18)List the empno, ename, salary per day, salary per month, salary per year. SQL>select empno,ename,sal/30 "per day",sal "per month",sal*12 "per year" 2 from emp; Output: EMPNO ENAME per day per month per year ---------- ---------- ---------- ---------- ---------- 7369 SMITH 26.6666667 800 9600 7499 ALLEN 53.3333333 1600 19200
  36. 7521 WARD 41.6666667 1250 15000 7566 JONES 99.1666667 2975 35700 7654 MARTIN 41.6666667 1250 15000 7698 BLAKE 95 2850 34200 7782 CLARK 81.6666667 2450 29400 7788 SCOTT 100 3000 36000 7839 KING 166.666667 5000 60000 7844 TURNER 50 1500 18000 7876 ADAMS 36.6666667 1100 13200 7900 JAMES 31.6666667 950 11400 7902 FORD 100 3000 36000 7934 MILLER 43.3333333 1300 15600 14 rows selected. 19)Ceil the salary of employees. SQL> select ceil(sal) 2 from emp; Output: CEIL(SAL) ---------- 800 1600 1250 2975
  37. 1250 2850 2450 3000 5000 1500 1100 CEIL(SAL) ---------- 950 3000 1300 2)Update: 1)To set salary of smith to 1000. SQL>update emp 2 set sal=1000 3* where ename='SMITH' 4 ; Output: 1 row updated. To view the update.
  38. SQL> select * 2 from emp 3 where ename='SMITH'; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 SMITH CLERK 7902 17-DEC-80 1000 20 2)To increase the salary by 10%. SQL> update emp 2 set sal=sal+sal*0.1; Output: 14 rows updated. To view the update. SQL> select* 2 from emp; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 SMITH CLERK 7902 17-DEC-80 1100
  39. 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1760 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1375 500 30 7566 JONES MANAGER 7839 02-APR-81 3272.5 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1375 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 3135 30 7782 CLARK MANAGER 7839 09-JUN-81 2695 10 7788 SCOTT ANALYST 7566 19-APR-87 3300 20 7839 KING PRESIDENT 17-NOV-81 5500 10 7844 TURNER SALESMAN 7698 08-SEP-81 1650 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1210 20 7900 JAMES CLERK 7698 03-DEC-81 1045 30 7902 FORD ANALYST 7566 03-DEC-81 3300
  40. 20 7934 MILLER CLERK 7782 23-JAN-82 1430 10 14 rows selected. 3)Update salary and commission for manager with 2000 and 800. SQL>update emp 2 set sal=2000,comm=800 3 where job='MANAGER'; Output: 3 rows updated. To view the update. SQL> select* 2 from emp; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 SMITH CLERK 7902 17-DEC-80 1100 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1760 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1375 500 30
  41. 7566 JONES MANAGER 7839 02-APR-81 2000 800 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1375 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2000 800 30 7782 CLARK MANAGER 7839 09-JUN-81 2000 800 10 7788 SCOTT ANALYST 7566 19-APR-87 3300 20 7839 KING PRESIDENT 17-NOV-81 5500 10 7844 TURNER SALESMAN 7698 08-SEP-81 1650 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1210 20 7900 JAMES CLERK 7698 03-DEC-81 1045 30 7902 FORD ANALYST 7566 03-DEC-81 3300 20 7934 MILLER CLERK 7782 23-JAN-82 1430 10 14 rows selected. 4)Delete the records of clerk.
  42. SQL> delete from 2 emp where job='CLERK'; Output: 4 rows deleted. To view whether rows are delted or not. SQL> select * 2 from emp; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7788 SCOTT ANALYST 7566 19-APR-87 3000 20
  43. 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 10 rows selected. 3.Order by caluse 1)List the employees in ascending order account to experience. SQL> select ename,job,months_between(sysdate,hiredate)/12 as "no of months" 2 from emp order by 3; Output: ENAME JOB no of months ---------- --------- ------------ ADAMS CLERK 26.2891545 SCOTT ANALYST 26.3832405 MILLER CLERK 31.6224878 JAMES CLERK 31.7595846 FORD ANALYST 31.7595846 KING PRESIDENT 31.8052835 MARTIN SALESMAN 31.9423803 TURNER SALESMAN 31.9961437 CLARK MANAGER 32.2434555 BLAKE MANAGER 32.3482943
  44. JONES MANAGER 32.4289394 WARD SALESMAN 32.5418426 ALLEN SALESMAN 32.547219 SMITH CLERK 32.7219502 14 rows selected. 2)List the emp details in descending order a/c to sal. SQL> select * 2 from emp 3 order by sal desc; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
  45. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7934 MILLER CLERK 7782 23-JAN-82 1300 10 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7369 SMITH CLERK 7902 17-DEC-80 800 20 14 rows selected. 3)Emp details in ascending order working in dept no 10and 30. SQL> select * 2 from emp 3 where deptno in(10,30) order by ename desc; Output:
  46. EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7934 MILLER CLERK 7782 23-JAN-82 1300 10 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7839 KING PRESIDENT 17-NOV-81 5000 10 7900 JAMES CLERK 7698 03-DEC-81 950 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 9 rows selected.
  47. 4)List the names and empno of manager who earn more than 2600 and display in alphabetical order. SQL> select empno,ename 2 from emp 3 where job='MANAGER' and sal>2600 order by ename; Output: EMPNO ENAME ---------- ---------- 7698 BLAKE 7566 JONES 4.Data Functions 1)List all employees who joined in a particular year. SQL> select * 2 from emp 3 where to_char(hiredate,'yy')=81; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
  48. 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 10 rows selected. 2)Add 2 months to date of join of employee with id=7839. SQL> update emp set hiredate=add_months(hiredate,2) 2 where empno=7839; Output: 1 row updated. To view the update. SQL> select *
  49. 2 from emp; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-JAN-82 5000 10
  50. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 300 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 14 rows selected. SQL> select empno,hiredate 2 from emp 3 where empno=7839; EMPNO HIREDATE ---------- --------- 7839 17-JAN-82 3)To find the employee hired recently. SQL> select* 2 from emp 3 where hiredate=(select max(hiredate) from emp); Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM
  51. ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 4)To list ename and hiredate whose hiredate matches with minimum hiredate. SQL> select ename,hiredate 2 from emp 3 where hiredate=(select min(hiredate)from emp); Output: ENAME HIREDATE ---------- --------- SMITH 17-DEC-80 5)To list the employees experience in days in company. SQL> select ename,(hiredate-sysdate)/24"experience" 2 from emp; Output: ENAME experience ---------- ---------- SMITH -498.19434 ALLEN -495.48601 WARD -495.40268 JONES -493.77768 MARTIN -486.31934
  52. BLAKE -492.56934 CLARK -490.94434 SCOTT -401.77768 KING -484.23601 TURNER -487.15268 ADAMS -400.36101 JAMES -483.56934 FORD -483.56934 MILLER -481.44434 14 rows selected. 6)To display emps who joined before 30-june-81 and 31-dec-81 from emp. SQL> select* 2 from emp 3 where hiredate not between '30-JUNE-81' and '31-DEC-81'; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500
  53. 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 9 rows selected. 5.Group by Clause. 1)To llist the deptno, no. of emps in each department. SQL> select deptno,count(*)"total_employees" 2 from emp 3 group by deptno; Output: DEPTNO total_employees ---------- ---------------
  54. 10 3 20 5 30 6 2)List the total, maximum, minimum and average salary of emps job wise. SQL> select sum(sal),max(sal),min(sal),avg(sal),job 2 from emp 3 group by job; Output: SUM(SAL) MAX(SAL) MIN(SAL) AVG(SAL) JOB ---------- ---------- ---------- ---------- --------- 6000 3000 3000 3000 ANALYST 4150 1300 800 1037.5 CLERK 8275 2975 2450 2758.33333 MANAGER 5000 5000 5000 5000 PRESIDENT 5600 1600 1250 1400 SALESMAN 3)To list the job and no. of emp’s in each job. SQL> select job,count(*) 2 from emp 3 group by job; Output: JOB COUNT(*) --------- ----------
  55. ANALYST 2 CLERK 4 MANAGER 3 PRESIDENT 1 SALESMAN 4 4)To list the department and no. of clerks in each department. SQL> select deptno,count(*) 2 from emp 3 where job='CLERK' 4 group by deptno; Output: DEPTNO COUNT(*) ---------- ---------- 10 1 20 2 30 1 5)To list the job which are done by minimum of 2 persons. SQL> select job 2 from emp 3 group by job having count(*)>=2; Output: JOB
  56. --------- ANALYST CLERK MANAGER SALESMAN 6)List the no. of employees under each manager. SQL> select mgr,count(*) 2 from emp 3 group by mgr; Output: MGR COUNT(*) ---------- ---------- 7566 2 7698 5 7782 1 7788 1 7839 3 7902 1 6 rows selected. 7)List the no. of employees along with their total salary in deptno 10 & 20. SQL> select deptno,count(*),sum(sal) 2 from emp 3 group by deptno having deptno in(10,20); Output:
  57. DEPTNO COUNT(*) SUM(SAL) ---------- ---------- ---------- 10 3 8750 20 5 10875 8)Find the highest and lowest salary for each job. SQL> select job,max(sal),min(sal) 2 from emp 3 group by job; Output: JOB MAX(SAL) MIN(SAL) --------- ---------- ---------- ANALYST 3000 3000 CLERK 1300 800 MANAGER 2975 2450 PRESIDENT 5000 5000 SALESMAN 1600 1250 9)Find the employees having similar jobs. SQL> select job,count(*) 2 from emp 3 group by job having count(*)>1; Output: JOB COUNT(*) --------- ---------- ANALYST 2
  58. CLERK 4 MANAGER 3 SALESMAN 4 10)To perform group on dept & sort on sum of salaries. SQL> select sum(sal),deptno 2 from emp 3 group by deptno order by sum(sal); Output: SUM(SAL) DEPTNO ---------- ---------- 8750 10 9400 30 10875 20 11)Find all departments who have more than three employees SQL> select deptno,count(*) 2 from emp 3 group by deptno having count(*)>3; Output: DEPTNO COUNT(*) ---------- ---------- 20 5 30 6 6.Subqueries: 1.List ename, deptno who works in ‘sales’.
  59. SQL>select ename,deptno 2 from emp 3 where deptno=(select deptno from dept where dname='SALES'); Output: ENAME DEPTNO ---------- ---------- ALLEN 30 WARD 30 MARTIN 30 BLAKE 30 TURNER 30 JAMES 30 6 rows selected. 2)Display the information about employees working in new York. SQL> select* 2 from emp 3 where deptno=(select deptno from dept where loc='NEW YORK'); Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
  60. 7839 KING PRESIDENT 17-NOV-81 5000 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 3)List all the employees in dallas having salary greater than scott. SQL>select empno,ename,emp.deptno,loc,sal 2 from emp,dept 3 where loc='DALLAS' and sal>(select sal from emp where ename='SCOTT'); Output: EMPNO ENAME DEPTNO LOC SAL ---------- ---------- ---------- ------------- ---------- 7839 KING 10 DALLAS 5000 4)To list ename, job and empno who have same job as clerk and salary greater than manager. SQL> select empno,job,ename,sal 2 from emp 3 where job='CLERK'; Output: EMPNO JOB ENAME SAL ---------- --------- ---------- ---------- 7369 CLERK SMITH 800 7876 CLERK ADAMS 1100 7900 CLERK JAMES 950 7934 CLERK MILLER 1300
  61. 5)To list the deptno in which maximum employees work. SQL> select deptno,count(*) 2 from emp 3 group by deptno having count(*)=(select max(count(*)) from emp group by deptno); Output: DEPTNO COUNT(*) ---------- ---------- 30 6 6) To print names who draw maximum salary. SQL> select ename,sal 2 from emp 3 where sal=(select max(sal) from emp); Output: ENAME SAL ---------- ---------- KING 5000 7)To find the second maximum salary. SQL> select max(sal)smax 2 from emp 3 where sal<(select max(sal) from emp); Output: SMAX ---------- 3000
  62. 8)To list the jobs, which are done by maximum no. of persons. SQL> select job 2 from emp 3 group by job having count(*)=(select max(count(*)) from emp group by job); Output: JOB --------- CLERK SALESMAN 9)Find employees who earn more than the lowest salary in deptno 30. SQL> select empno,ename 2 from emp 3 where sal>any(select min(sal) from emp where deptno=30); Output: EMPNO ENAME ---------- ---------- 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT
  63. 7839 KING 7844 TURNER 7876 ADAMS 7902 FORD 7934 MILLER 12 rows selected. 10)Find the employees who earn lowest salary in each department. SQL>select* 2 from emp 3 where sal in(select min(sal) from emp group by deptno); Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7934 MILLER CLERK 7782 23-JAN-82 1300 10 11)Find employees who earn less than every employee in deptno 30. SQL> select* 2 from emp
  64. 3 where sal<all(select min(sal) from emp where deptno=30); Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- ---------- --------- ---------- --------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 12)Find the job with highest avg salaries. SQL> select job,sal 2 from emp 3 where sal in(select max(avg(sal)) from emp group by job); Output: JOB SAL --------- ---------- PRESIDENT 5000 13)List all employees in deptno 10 having the same job as any one in deptno 30. SQL> select* 2 from emp 3 where deptno=10 and job in(select job from emp where deptno=30); Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7934 MILLER CLERK 7782 23-JAN-82 1300 10 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 14)Display the list of employees who have same job & salary as turner.
  65. SQL> select ename,job,sal 2 from emp 3 where(job,sal) in (select job,sal from emp where ename='TURNER'); Output: ENAME JOB SAL ---------- --------- ---------- TURNER SALESMAN 1500 15 )Display the information about employees working in the department in the which miller work. SQL> select * 2 from emp 3 where deptno=(select deptno from emp where ename='MILLER'); Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTN ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7839 KING PRESIDENT 17-NOV-81 5000 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 16)Display the emp details whose salary is greater than any of their managers. SQL> select* 2 from emp 3 where sal>any(select sal from emp where job='ANALYST');
  66. Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7.Group Functions: 1)Count the managers of the company. SQL>select count(*) 2 from emp 3 where job='MANAGER'; Output: COUNT(*) ---------- 3 2)Find the highest, lowest and difference of salary. SQL> select max(sal)"max",min(sal)"min",max(sal)-min(sal)"diff" 2 from emp; Output: max min diff ---------- ---------- ---------- 5000 800 4200
  67. 3)Find the average salary of salesman. SQL> select avg(sal) 2 from emp 3 where job='SALESMAN'; Output: AVG(SAL) ---------- 1400 4)Find the average annual salary of all employees. SQL> select avg(sal*12) 2 from emp; Output: AVG(SAL*12) ----------- 24878.5714 5)Find the no. of employees in deptno 30 who receive commission. SQL> select count(*)"no_of_emp" 2 from emp 3 where deptno=30 and comm is not null; Output: no_of_emp ---------- 4
  68. 8.Joins: 1) List empno, name, designation, salary, deptname and location. SQL> select empno,ename,job,sal,dname,loc 2 from emp,dept 3 where emp.deptno=dept.deptno; Output: EMPNO ENAME JOB SAL DNAME LOC ---------- ---------- --------- ---------- -------------- ------------- 7369 SMITH CLERK 800 RESEARCH DALLAS 7499 ALLEN SALESMAN 1600 SALES CHICAGO 7521 WARD SALESMAN 1250 SALES CHICAGO 7566 JONES MANAGER 2975 RESEARCH DALLAS 7654 MARTIN SALESMAN 1250 SALES CHICAGO 7698 BLAKE MANAGER 2850 SALES CHICAGO 7782 CLARK MANAGER 2450 ACCOUNTING NEW YORK 7788 SCOTT ANALYST 3000 RESEARCH DALLAS 7839 KING PRESIDENT 5000 ACCOUNTING NEW YORK 7844 TURNER SALESMAN 1500 SALES CHICAGO 7876 ADAMS CLERK 1100 RESEARCH DALLAS 7900 JAMES CLERK 950 SALES CHICAGO 7902 FORD ANALYST 3000 RESEARCH DALLAS 7934 MILLER CLERK 1300 ACCOUNTING NEW YORK 14 rows selected.
  69. 2)List the employee names and their managers. SQL> select e1.ename"WORKER",e2.ename"MANAGER" 2 from emp e1,emp e2 3 where e1.empno=e2.mgr; Output: WORKER MANAGER ---------- ---------- FORD SMITH BLAKE ALLEN BLAKE WARD KING JONES BLAKE MARTIN KING BLAKE KING CLARK JONES SCOTT BLAKE TURNER SCOTT ADAMS BLAKE JAMES JONES FORD CLARK MILLER 13 rows selected. 3)List the empno, ename, salary, deptname where salary < 5000. SQL> select empno,ename,sal,dname 2 from emp,dept
  70. 3 where emp.deptno=dept.deptno and sal<5000; Output: EMPNO ENAME SAL DNAME ---------- ---------- ---------- -------------- 7369 SMITH 800 RESEARCH 7499 ALLEN 1600 SALES 7521 WARD 1250 SALES 7566 JONES 2975 RESEARCH 7654 MARTIN 1250 SALES 7698 BLAKE 2850 SALES 7782 CLARK 2450 ACCOUNTING 7788 SCOTT 3000 RESEARCH 7844 TURNER 1500 SALES 7876 ADAMS 1100 RESEARCH 7900 JAMES 950 SALES 7902 FORD 3000 RESEARCH 7934 MILLER 1300 ACCOUNTING 13 rows selected. 9.Set Operators: 1)List jobs of deptno 10 7 20. SQL> select job 2 from emp 3 where deptno=10 4 union
  71. 5 select job from emp where deptno=20; Output: JOB --------- ANALYST CLERK MANAGER PRESIDENT 2)Common jobs in deptno 10 & 20. SQL> select job 2 from emp 3 where deptno=10 4 intersect 5 select job from emp where deptno=20; Output: JOB --------- CLERK MANAGER 3)List unique jobs to deptno 10 but not in 20. SQL>select job from emp where deptno=10 2 minus 3 select job from emp where deptno=20;
  72. Output: JOB --------- PRESIDENT 4)All jobs in deptno 10 & 20. SQL> select job from emp where deptno=10 2 union all 3 select job 4 from emp 5 where deptno=20; Output: JOB --------- MANAGER PRESIDENT CLERK CLERK MANAGER ANALYST CLERK ANALYST 8 rows selected. 10.Views: 1)Create a view of salaries and ename of emp table.
  73. SQL> create view vv_sal_name as 2 select ename,sal 3 from emp; Output: View created. 2)Update the salary of view. SQL> update vv_sal_name set sal=800 2 where ename='SMITH'; Output: 1 row updated. 3)Drop the view. SQL> drop view vv_sal_name; Output: View dropped. 11.Co-related subquery and multicolumn subquery: 1)List the name, salary, deptno of employees earning salary more than the average salary of their department. SQL>select ename,sal,deptno 2 from emp e 3 where sal>(select avg(sal) 4 from emp m 5 where e.deptno=m.deptno); Output: ENAME SAL DEPTNO
  74. ---------- ---------- ---------- ALLEN 1600 30 JONES 2975 20 BLAKE 2850 30 SCOTT 3000 20 KING 5000 10 FORD 3000 20 6 rows selected. 2)To print the employees who draw firstthree maximum salaries. SQL> select rownum,ename,sal 2 from(select ename,sal from emp order by sal desc) 3 where rownum<=3; Output: ROWNUM ENAME SAL - --------- ---------- ---------- 1 KING 5000 2 SCOTT 3000 3 FORD 3000 3)To print all the employees who have atleast one person reporting to them. SQL>select ename,job 2 from emp e 3 where exists(select mgr from emp where mgr=e.empno); Output: ENAME JOB
  75. ---------- --------- JONES MANAGER BLAKE MANAGER CLARK MANAGER SCOTT ANALYST KING PRESIDENT FORD ANALYST 6 rows selected. 12.Miscellaneous: 1)Create a table to hold records whose salary < 2000. SQL> create table emp3 as 2 select* 3 from emp 4 where sal<2000; Output: Table created. 2)Create table to hold ename, empno, job and salary columns. SQL> create table emp4 as 2 select empno,ename,job,sal 3 from emp; Output: Table created. 3)Insert into the above table where ename starts with ‘s’. SQL> insert into emp4
  76. 2 select empno,ename,job,sal 3 from emp 4 where ename like's%'; Output: 0 rows created. 13.DDL Queries: 1)Create employee table. SQL>create table employee( 2 empno number(4), 3 ename varchar2(20), 4 job varchar2(15), 5 sal number(5), 6 comm number(3), 7 hiredate date, 8 deptno number(2)); Output: Table created. 2)Create depts table. SQL>create table depts( 2 deptno number(2), 3 dname varchar2(20), 4 loc varchar2(10)); Output: Table created.
  77. 3)Modify the job column to hold 10 characters. SQL> alter table employee 2 modify job varchar2(10); Output: Table altered. 4)Add primary key constraint to column. SQL> alter table employee 2 add constraint pk_key primary key(empno); Output: Table altered. 5)Add foreign key constraint to dept setting primary key to depts.. SQL> alter table depts 2 add constraint pk_key1 primary key(deptno); Output: Table altered. SQL>alter table employee 2 add(constraint fk_key foreign key(deptno) references depts); Output: Table altered. 6)Drop constraint: SQL> alter table employee 2 drop constraint fk_key; Output: Table altered.
  78. 1.SQL Statement for creating the table. a)Table Name:client_master SQL> create table client_master( 2 clientno varchar2(6) primary key, 3 name varchar2(20) not null, 4 city varchar2(15), 5 pincode number(8), 6 state varchar2(15), 7 baldue number(10,2)); Output: Table created. b)Table Name:product_master SQL> create table product_master( 2 productno varchar2(6) primary key, 3 description varchar2(15) not null, 4 profitpercent number(4,2) not null, 5 unitmeasure varchar2(10) not null, 6 qtyonhand number(8) not null, 7 reorderlvl number(8) not null, 8 sellprice number(8,2) not null, 9 costprice number(8,2) not null, 10 constraint ck_product check(productno like 'p%'), 11 constraint ck_sell check(sellprice <> 0), 12 constraint ck_cost check(costprice <> 0));
  79. Output: Table created. c) Table Name:salesman_master SQL>create table salesman_master( 2 salesmanno varchar2(6) primary key, 3 salesmanname varchar2(20) not null, 4 address1 varchar2(30) not null, 5 address2 varchar2(30), 6 city varchar2(20), 7 pincode number(8), 8 state varchar2(20), 9 salamt number(8,2) not null, 10 tgttoget number(6,2) not null, 11 ytdsales number(6,2) not null, 12 remarks varchar2(60), 13 constraint ck_salesman check(salesmanno like 's%'), 14 constraint ck_sal check(salamt <> 0), 15 constraint ck_target checktgttoget <> 0)); Output: Table created. d)Table Name:sales_order SQL> create table sales_order( 2 orderno varchar2(6) primary key,
  80. 3 clientno varchar2(6) references client_master, 4 orderdate date, 5 delyaddr varchar2(25), 6 salesmanno varchar2(6) references salesman_master, 7 delytype char(1) default 'f', 8 billedyn char(1), 9 delydate date, 10 orderstatus varchar2(10), 11 constraint ck_order check(orderno like 'o%'), 12 constraint ck_dely_type check(delytype in ('p','f')), 13 constraint ck_ord_status 14 check(orderstatus in ('in process', 'fulfilled','backorder','cancelled'))); Output: Table created. e)Table Name: sales_order_details SQL> create table sales_order_details( 2 orderno varchar2(6) references sales_order, 3 productno varchar2(6) references product_master, 4 qtyordered number(8), 5 qtydisp number(8), 6 productrate number(10,2), 7 primary key(orderno,productno)); Output: Table created.
  81. 2. SQL Statement for inserting data into their respective tables. a)Data for client_master table: SQL> insert into client_master(clientno,name,city,pincodeno,state,baldue) 2 values('c00001','ivanbayross','mumbai',400054,'maharashtra',15000); 1 row created. SQL> insert into client_master(clientno,name,city,pincodeno,state,baldue) 2 values('c00002','mamtamuzumdar','madras',780001,'tamil nadu',0); 1 row created. SQL> insert into client_master(clientno,name,city,pincodeno,state,baldue) 2 values('c00003','chhayabankar','mumbai',400057,'maharashtra',5000); 1 row created. SQL> insert into client_master(clientno,name,city,pincodeno,state,baldue) 2 values('c00004','ashwinijoshi','bangalore',560001,'karnataka',0); 1 row created. SQL> insert into client_master(clientno,name,city,pincodeno,state,baldue) 2 values('c00005','hanselcolaco','mumbai',400060,'maharashtra',2000); Output: 1 row created. SQL> insert into client_master(clientno,name,city,pincodeno,state,baldue) 2 values('c00006','deepaksharma','mangalore',560050,'karnataka',0); Output: 1 row created.
  82. To view the client_master table. SQL> select * 2 from client_master; Output: CLIENT NAME CITY PINCODE STATE BALDUE ------ -------------------- --------------- ---------- --------------- c00001 ivan bayross mumbai 400054 maharashtra 15000 c00002 mamta muzumdar madras 780001 tamil nadu 0 c00003 chhaya bankar mumbai 400057 maharashtra 5000 c00004 ashwini joshi bangalore 560001 karnataka 0 c00005 hansel colaco mumbai 400060 maharashtra 2000 c00006 deepak sharma mangalore 560050 karnataka 0 b) Data for product_master table: SQL> insert intoproduct_master 2 values('p00001','t-shirts',5,'piece',200,50,350,250); Output: 1 row created. SQL> insert into product_master 2 values('p03453','shirts',6,'piece',150,50,500,350); Output: 1 row created. SQL> insert into product_master 2 values('p06734','cotton jeans',5,'piece',100,20,600,450);
  83. Output: 1 row created. SQL> insert into product_master 2 values('p07865','jeans',5,'piece',100,20,750,500); Output: 1 row created. SQL> insert into product_master 2 values('p07868','trousers',2,'piece',150,50,850,550); Output: 1 row created. SQL> insert into product_master 2 values('p07885','pull overs',2.5,'piece',80,30,700,450); Output: 1 row created. SQL> insert into product_master 2 values('p07965','denim shirts',4,'piece',100,40,350,250); Output: 1 row created. SQL> insert into product_master 2 values('p07975','lycra tops',5,'piece',70,30,300,175); Output: 1 row created. SQL> insert into product_master 2 values('p08865','skirts',5,'piece',75,30,450,300);
  84. Output: 1 row created. To view the product_master table. SQL> select * 2 from product_master; Output: PRODUC DESCRIPTION PROFITPERCENT UNITMEASUR QTYONHAND REORDERLVL SELLPRICE ------ --------------- ------------- ---------- ---------- ---------- ---------- COSTPRICE ---------- p00001 t-shirts 5 piece 200 50 350 250 p03453 shirts 6 piece 150 50 500 350 p06734 cotton jeans 5 piece 100 20 600 450 P07865 jeans 5 piece 100 20 750 500 p07868 trousers 2 piece 150 50 850 550 p07885 pull overs 2.5 piece 80 30 700 450 p07965 denim shirts 4 piece 100 40 350 250
  85. p08865 skirts 5 piece 75 30 450 300 p07975 lycra tops 5 piece 70 30 300 175 9 rows selected. c) Data for sales_master table: SQL> insert into salesman_master 2 values( 's00001','aman','a/14','worli','mumbai',400002,'maharashtra',3000,100,50,'good'); Output: 1 row created. SQL> insert into salesman_master 2 values('s00002','omkar','65','nariman','mumbai',400001,'maharashtra',3000,200,100,'good'); Output: 1 row created. SQL> insert into salesman_master 2 values('s00003','raj','p-7','bandra','mumbai',400032,'maharashtra',3000,200,100,'good'); Output: 1 row created. SQL> insert into salesman_master 2 values( 's00004','ashish','a/5','juhu','bombay',400044,'maharashtra',3500,200,150,'good'); Output: 1 row created. To view the salesman_master table. SQL> select *
  86. 2 from salesman_master; Output: SALESM SALESMANNAME ADDRESS1 ADDRESS2 CITY PINCODE ------ -------------------- ------------------------------ STATE SALAMT TGTTOGET YTDSALES REMARKS -------------------- ---------- ---------- ---------- s00001 aman a/14 worli mumbai 400002 maharashtra 3000 100 50 good s00002 omkar 65 nariman mumbai 400001 maharashtra 3000 200 100 good s00003 raj p-7 bandra mumbai 400032 maharashtra 3000 200 100 good s00004 ashish a/5 juhu bombay 400044 maharashtra 3500 200 150 good 4 rows selected. d) Data for sales_order table: SQL> insert into sales_order(orderno,orderdate,clientno,delytype,billedyn,salesmanno,delydate,orderstatus) 2 values('o19001','12-june-02','c00001','f','n','s00001','20-july-02','in process'); Output: 1 row created. SQL> insert into sales_order(orderno,orderdate,clientno,delytype,billedyn,salesmanno,delydate,orderstatus) 2 values('o19002','25-june-02','c00002','p','n','s00002','27-july-01','cancelled'); Output:
  87. 1 row created. SQL> insert into sales_order(orderno,orderdate,clientno,delytype,billedyn,salesmanno,deltdate,orderstatus) 2 values('o19003','18-feb-02','c00003','f','y','s00003','20-feb-02','fulfilled'); Output: 1 row created. SQL> insert into sales_order(orderno,orderdate,clientno,delytype,billedyn,salesmanno,deltdate,orderstatus) 2 values('o19004','03-apr-02','c00001','f','y','s00001','07-apr-02','fulfilled'); Output: 1 row created. SQL> insert into sales_order(orderno,orderdate,clientno,delytype,billedyn,salesmanno,deltdate,orderstatus) 2 values('o46866','20-may-02','c00004','p','n','s00002','22-may-02','cancelled'); Output: 1 row created. SQL> insert into sales_order(orderno,orderdate,clientno,delytype,billedyn,salesmanno,deltdate,orderstatus) 2 values('o19008','24-may-02','c00005','f','n','s00004','26-july-96','in process'); Output: To view the sales_order table. SQL> select * 2 from sales_order; Output: ORDERN CLIENT ORDERDATE DELYADDR SALESM D B DELTDATE
  88. ------ ------ --------- ------------------------- ------ - - --------- ORDERSTATUS ---------- o19001 c00001 12-JUN-02 s00001 f n 20-JUL-02 in process o19002 c00002 25-JUN-02 s00002 p n 27-JUL-01 cancelled o19003 c00003 18-FEB-02 s00003 f y 20-FEB-02 fulfilled o19004 c00001 03-APR-02 s00001 f y 07-APR-02 fulfilled o46866 c00004 20-MAY-02 s00002 p n 22-MAY-02 cancelled o19008 c00005 24-MaY-02 s00004 f n 26-JULY-96 in process 6 rows selected. e) Data for sales_order_details table: SQL> insert into sales_order_details(orderno,productno,qtyordered,qtydisp,productrate) 2 values('o19001','p00001',4,4,525); Output: 1 row created. SQL> insert into sales_order_details(orderno,productno,qtyordered,qtydisp,productrate) 2 values('o19001','p07965',2,1,8400); Output:
  89. 1 row created. SQL> insert into sales_order_details(orderno,productno,qtyordered,qtydisp,productrate) 2 values('o19001','p07885',2,1,5250); Output: 1 row created. SQL> insert into sales_order_details(orderno,productno,qtyordered,qtydisp,productrate) 2 values('o46865','p07868',3,3,150); Output: 1 row created. SQL> insert into sales_order_details(orderno,productno,qtyorder,qtydisp,productrate) 2 values('o19003','p03453',2,2,1050); Output: 1 row created. SQL> insert into sales_order_details(orderno,productno,qtyordered,qtydisp,productrate) 2 values('o19003','p06734',1,1,12000); Output: 1 row created. SQL> insert into sales_order_details(orderno,productno,qtyordered,qtydisp,productrate) 2 values('o46866','p07965',1,0,8400); Output: 1 row created. SQL>insert into sales_order_details(orderno,productno,qtyordered,qtydisp,productrate) 2 values('o46866','p07975',1,0,1050); Output:
  90. 1 row created. ORDERNO PRODUCTNO qtyordered qtydisp productrate ----------------- ------------------- ------------------ ---------------- ----------------- o19001 p00001 4 4 525 o19001 p07965 2 1 8400 o19001 p07885 2 1 5250 o46865 p07868 3 3 150 o19003 p03453 2 2 1050 o19003 p06734 1 1 12000 o46866 p07965 1 0 8400 o46866 p07975 1 0 1050 3.SQL Statement for retrieving records from a table. a)Find out the names of all the clients. SQL> select name 2 from client_master; Output: NAME -------------------- ivan bayross mamta muzumdar chhaya bankar ashwini joshi hansel colaco
  91. deepak sharma 6 rows selected. b)Retrieve the entire contents of the client_master table. SQL> select * 2 from client_master; Output: CLIENT NAME CITY PINCODE STATE BALDUE ------------------------------ --------------- --------------- c00001 ivan bayross mumbai 400054 maharashtra 15000 c00002 mamta muzumdar madras 780001 tamil nadu 0 c00003 chhaya bankar mumbai 400057 maharashtra 5000 c00004 ashwini joshi bangalore 560001 karnataka 0 c00005 hansel colaco mumbai 400060 maharastra 2000 c00006 deepak sharma mangalore 560050 karnataka 0 6 rows selected. c)Retrieve the list of names, city and the state of all the clients. SQL> select name,city,state 2 from client_master; Output: NAME CITY STATE -------------------- --------------- --------------- ivan bayross mumbai maharashtra mamta muzumdar madras tamil nadu
  92. chhaya bankar mumbai maharashtra ashwini joshi bangalore karnataka hansel colaco mumbai maharastra deepak sharma mangalore karnataka 6 rows selected. d)List the various products available from the product_master table. SQL> select description 2 from product_master; Output: DESCRIPTION --------------- t-shirts shirts cotton jeans jeans trousers pull overs denim shirts skirts lycra tops 9 rows selected. e)List all the clients who are located in Mumbai. SQL> select * 2 from client_master
  93. 3 where city='mumbai'; Output: CLIENT NAME CITY PINCODE STATE BALDUE ------ ----------- --------- --------------- ---------- --------------- c00001 ivan bayross mumbai 400054 maharashtra 15000 c00003 chhaya bankar mumbai 400057 maharashtra 5000 c00005 hansel colaco mumbai 400060 maharashtra 2000 f)Find the names of salesman who have a salary equal to Rs.3000. SQL>select salesman_name 2 from salesman_master 3 where salamt=3000; Output: SALESMANNAME -------------------- aman omkar raj 4.SQL Statement for update records in a table. a)Change the city of clientno ‘c00005’ to ‘bangalore’. SQL> update client_master 2 set city='bangalore' 3 where clientno='c00005'; Output: 1 row updated.
  94. To check whether table is updated or not. SQL> select * 2 from client_master; Output: CLIENT NAME CITY PINCODE STATE BALDUE ------ -------------------- --------------- ---------- --------------- c00001 ivan bayross mumbai 400054 maharashtra 15000 c00002 mamta muzumdar madras 780001 tamil nadu 0 c00003 chhaya bankar mumbai 400057 maharashtra 5000 c00004 ashwini joshi bangalore 560001 karnataka 0 c00005 hansel colaco bangalore 400060 maharashtra 2000 c00006 deepak sharma mangalore 560050 karnataka 0 6 rows selected. b)Change the baldue of clientno’c00001’ to Rs.1000. SQL> update client_master 2 set baldue=1000 3 where clientno='c00001'; Output: 1 row updated. To check whether table is updated or not. SQL> select * 2 from client_master; Output: CLIENT NAME CITY PINCODE STATE BALDUE
  95. ------ -------------- ------ --------------- ---------- --------------- c00001 ivan bayross mumbai 400054 maharashtra 1000 c00002 mamta muzumdar madras 780001 tamil nadu 0 c00003 chhaya bankar mumbai 400057 maharashtra 5000 c00004 ashwini joshi bangalore 560001 karnataka 0 c00005 hansel colaco bangalore 400060 maharashtra 2000 c00006 deepak sharma mangalore 560050 karnataka 0 6 rows selected. c)Change the cost price of ‘Trousers’ to Rs.950.00. SQL>update product_master 2 set costprice=950.00 3 where description='trousers'; Output: 1 row updated. To check whether table is updated or not. SQL> select * 2 from product_master; Output: PRODUC DESCRIPTION PROFITPERCENT UNITMEASURE QTYONHAND REORDERLVL SELLPRICE ------ --------------- ------------- ---------- ---------- ---------- ---------- COSTPRICE ---------- p00001 t-shirts 5 piece 200 50 350
  96. 250 p03453 shirts 6 piece 150 50 500 350 p06734 cotton jeans 5 piece 100 20 600 450 p07865 jeans 5 piece 100 20 750 500 p07868 trousers 2 piece 150 50 850 950 p07885 pull overs 2.5 piece 80 30 700 450 p07965 denim shirts 4 piece 100 40 350 250 p08865 skirts 5 piece 75 30 450 300 p07975 lycra tops 5 piece 70 30 300 175 9 rows selected. d)Change the city of the salesman to pune. SQL> update client_master 2 set city='pune'; Output: 6 rows updated.
  97. To check whether table is updated or not. SQL> select * from client_master; Output: CLIENT NAME CITY PINCODE STATE BALDUE ------ ----------------- --- --------------- ---------- --------------- c00001 ivan bayross pune 400054 maharashtra 1000 c00002 mamta muzumdar pune 780001 tamil nadu 0 c00003 chhaya bankar pune 400057 maharashtra 5000 c00004 ashwini joshi pune 560001 karnataka 0 c00005 hansel colaco pune 400060 maharashtra 2000 c00006 deepak sharma pune 560050 karnataka 0 6 rows selected. 5.SQL Statement for deleting records in a table. a)Delete all salesmen from the salesman_master whose salaries are equal to Rs.3500. SQL> delete from salesman_master 2 where salamt=3500; Output: 1 row deleted. To check whether the row is deleted or not . SQL> select * 2 from salesman_master; Output:
  98. SALESMANNO SALESMANNAME ADDRESS1 ADDRESS2 CITY PINCODE ------ -------------------- ------------------------------ STATE SALAMT TGTTOGET YTDSALES REMARKS -------------------- ---------- ---------- ---------- s00001 aman a/14 worli mumbai 400002 maharashtra 3000 100 50 good s00002 omkar 65 nariman mumbai 400001 maharashtra 3000 200 100 good s00003 raj p-7 bandra mumbai 400032 maharashtra 3000 200 100 good b)Delete all products from product_master where the quantity on hand is equal to 100. SQL> delete from product_master 2 where qtyonhand=100; Output: 3 rows deleted. To check whether the row is deleted or not . SQL> select * from product_master; Output: PRODUC DESCRIPTION PROFITPERCENT UNITMEASUR QTYONHAND REORDERLVL SELLPRICE ------ --------------- ------------- ---------- ---------- ---------- ---------- COSTPRICE ---------- p00001 t-shirts 5 piece 200 50 350
  99. 250 p03453 shirts 6 piece 150 50 500 350 p07868 trousers 2 piece 150 50 850 950 p07885 pull overs 2.5 piece 80 30 700 450 p08865 skirts 5 piece 75 30 450 300 p07975 lycra tops 5 piece 70 30 300 175 6 rows selected. c)Delete from client_master where the column state holds the value ‘tamil nadu’. SQL> delete from client_master 2 where state='tamil nadu'; Output: 1 row deleted. To check whether the row is deleted or not . SQL> select * 2 from client_master; Output: CLIENT NAME CITY PINCODE STATE BALDUE ------ -------------------- --------------- ---------- ---------------
  100. c00001 ivan bayross pune 400054 maharashtra 1000 c00003 chhaya bankar pune 400057 maharashtra 5000 c00004 ashwini joshi pune 560001 karnataka 0 c00005 hansel colaco pune 400060 maharastra 2000 c00006 deepak sharma pune 560050 karnataka 0 6.SQL Statement for altering the table structure. a)Add a column called ‘telephone’ of data type ‘number’ and size=’10’ to the client_master table. SQL> alter table client_master 2 add(telephone number(10)); Output: Table altered. To check whether the table is altered or not. SQL> select * 2 from client_master; Output: CLIENT NAME CITY PINCODE STATE BALDUE TELEPHONE ------ -------------------- --------------- ---------- --------------- c00001 ivan bayross pune 400054 maharashtra 1000 c00003 chhaya bankar pune 400057 maharashtra 5000 c00004 ashwini joshi pune 560001 karnataka 0 c00005 hansel colaco pune 400060 maharastra 2000 c00006 deepak sharma pune 560050 karnataka 0
  101. b)Change the size of sellprice column in product_master to 10,2. SQL> alter table product_master 2 modify(sellprice number(10,2)); Output: Table altered. To check whether the table is altered or not. SQL> select * 2 from product_master; Output: PRODUC DESCRIPTION PROFITPERCENT UNITMEASUR QTYONHAND REORDERLVL SELLPRICE ------ --------------- ------------- ---------- ---------- ---------- ---------- COSTPRICE ---------- p00001 t-shirts 5 piece 200 50 350 250 p03453 shirts 6 piece 150 50 500 350 p07868 trousers 2 piece 150 50 850 950 p07885 pull overs 2.5 piece 80 30 700 450 p08865 skirts 5 piece 75 30 450 300
  102. p07975 lycra tops 5 piece 70 30 300 175 6 rows selected. 7.SQL Statement for renaming the table. a)Change the name of the salesman_master to sman_mast. SQL> rename salesman_master to sman_mast; Output: Table renamed. To view whether the table is renamed or not. SQL>select * 2 from sman_mast; Output: SALESM SALESMANNAME ADDRESS1 ADDRESS2 CITY PINCODE ------ -------------------- ------------------------------ STATE SALAMT TGTTOGET YTDSALES REMARKS -------------------- ---------- ---------- ---------- s00001 aman a/14 worli mumbai 400002 maharashtra 3000 100 50 good s00002 omkar 65 nariman mumbai 400001 maharashtra 3000 200 100 good s00003 raj p-7 bandra mumbai 400032 maharashtra 3000 200 100 good s00004 ashish a/5 juhu bombay 400044 maharashtra 3500 200 150 good
  103. 4 rows selected. 9.INTERACTIVE SQL 1.Generate SQL Statements to perform the following computations on table data: a) Listing of the names of all clients having ‘a’ as the second letter in their names. SQL> select name 2 from client_master 3 where name like'_a%'; Output: NAME -------------------- mamta muzumdar hansel colaco b)Listing of clients who stay in a city whose first letter is’m’. SQL>select clientno,name 2 from client_master 3 where city like'm%' 4 ; Output: CLIENT NAME ------ -------------------- c00001 ivan bayros c00002 mamta muzumdar c00003 chhaya bankar c00005 hansel colaco
  104. c00006 deepak Sharma c)List all clients who stay in ’bangalore’ or ‘mangalore’. SQL> select clientno,name 2 from client_master 3 where city in('bangalore','mangalore'); Output: CLIENT NAME ------ -------------------- c00004 ashwini joshi c00006 deepak sharma d)List all clients whose baldue is greater than value 10000. SQL> select clientno,name 2 from client_master 3 where baldue>10000; Output: CLIENT NAME ------ -------------------- c00001 ivan bayros e)Print the information from sales_order table for orders placed in the month of june. SQL> select * 2 from sales_order 3 where to_char(orderdate,'mon')='jun'; Output:
  105. ORDERN CLIENT ORDERDATE DELYADDR SALESM D B DELTDATE ORDERSTATU ------ ------ --------- ------------------------- ------ - - --------- o19001 c00001 12-JUN-02 s00001 f n 20-JUL-02 in process f)Displayingthe order information of clintno ‘c00001’ and ‘c00002’. SQL> select * 2 from sales_order 3 where clientno in('c00001','c00002'); Output: ORDERN CLIENT ORDERDATE DELYADDR SALESM D B DELTDATE ORDERSTATU ------ ------ --------- ------------------------- ------ - - --------- o19001 c00001 12-JUN-02 s00001 f n 20-JUL-02 in process g)List products whose selling price is greater than 500 and less than or equal to 750. SQL> select productno,description 2 from product_master 3 where sellprice>500 and sellprice<750; Output: PRODUC DESCRIPTION ------ --------------- p06734 cotton jeans p07885 pull overs
  106. h)Listing of products whose selling price is more than 500 with the new selling price calculated as original selling price plus 15%. SQL> select productno,description,sellprice,sellprice*15 new_price 2 from product_master 3 where sellprice>500; Output: PRODUC DESCRIPTION SELLPRICE NEW_PRICE ------ --------------- ---------- ---------- p06734 cotton jeans 600 9000 p07865 jeans 750 11250 p07868 trousers 850 12750 p07885 pull overs 700 10500 i)Listing of names,city and state of clients who are not in the state of ‘maharashtra’. SQL> select name,city,state 2 from client_master 3 where state not in('maharashtra'); Output: NAME CITY STATE -------------------- --------------- --------------- mamta muzumdar madras tamil nadu ashwini joshi bangalore karnataka deepak sharma mangalore karnataka j)Count the total number of orders. SQL> select count(orderno)"no. of order"
  107. 2 from sales_order; Output: no. of order ---------- 4 k)Calculating the average price of all the products. SQL> select avg(sellprice) 2 from product_master; Output: AVG(SELLPRICE) --------------- 538.888889 l)Determining the maximum and minimum price for the product prices. SQL> select max(sellprice)max_price,min(sellprice) 2 from product_master; Output: MAX_PRICE MIN(SELLPRICE) ---------- --------------- 850 300 m)Count the number of products having price greater than or equal to 500. SQL> select count(productno) 2 from product_master 3 where sellprice<=1500; Output:
  108. COUNT(PRODUCTNO) ---------------- 9 n)Find all the products whose qtyonhand is less than rereorder level. SQL> select productno,description 2 from product_master 3 where qtyonhand<reorderlvl; Output: no rows selected 2.SQL Statements for Date Manipulation: a)Display the order number and day on which clients placed their order. SQL> select orderno,to_char(orderdate,'day') 2 from sales_order; Output: ORDERN TO_CHAR(O ------ --------- o19001 wednesday o19002 tuesday o19003 monday o46866 Monday b)Display the month (in alphabets) and date when the order must be delivered. SQLselect to_char(deltdate,'month'),deltdate 2 from sales_order 3 order by to_char(deltdate,'month');
  109. Output: TO_CHAR(D DELTDATE --------- --------- february 20-FEB-02 july 20-JUL-02 july 27-JUL-01 may 22-MAY-02 c)List the orderdate in the format ‘dd-month-yy’. SQL> select to_char(orderdate,'dd-mm-yy') 2 from sales_order; Output: TO_CHAR( -------- 12-06-02 25-06-02 18-02-02 20-05-02 d)Find the date, 15 days after today’s date. SQL> select sysdate+15 2 from dual; Output: SYSDATE+1 ---------
  110. 08-OCT-13 10.INTERACTIVE SQL 1.SQL Statements for using having and group by clauses: a)Printing the description and total quantity sold for each product. SQL> select description,sum(qtydisp) 2 from product_master,sales_order_details 3 where product_master.productno=sales_order_details.productno 4 group by description; Output: DESCRIPTION SUM(QTYDISP) --------------- ------------ cotton jeans 1 denim shirts 1 lycra tops 0 pull overs 1 shirts 2 t-shirts 4 6 rows selected. b)Finding the value of each product sold. SQL> select sales_order_details.productno,product_master.description, 2 sum(sales_order_details.qtydisp*sales_order_details.productrate)"sales per product" 3 from sales_order_details,product_master
  111. 4 where product_master.productno=sales_order_details.productno 5 group by sales_order_details.productno,product_master.description; Output: PRODUC DESCRIPTION sales per product ------ --------------- ----------------- p00001 t-shirts 2100 p03453 shirts 2100 p06734 cotton jeans 12000 p07885 pull overs 5250 p07965 denim shirts 8400 p07975 lycra tops 0 6 rows selected. c)Calculating the average quantity sold for each client that has a maximum order value of 15000.00. SQLselect cm.clientno,cm.name,avg(sod.qtydisp)"avg.sales" 2 from sales_order_details sod,sales_order so,client_master cm 3 where cm.clientno=so.clientno and so.orderno=sod.orderno 4 group by cm.clientno,name having max(sod.qtyordered*sod.productrate)>15000; Output: CLIENT NAME avg.sales ------ -------------------- ---------- c00001 ivan bayros 2
  112. e)Listing the products and orders from customers who have ordered less than 5 units of’pull overs’. SQL> select sales_order_details.productno,sales_order_details.orderno 2 from sales_order_details,sales_order,product_master 3 where sales_order.orderno=sales_order_details.orderno 4 and product_master.productno=sales_order_details.productno 5 and sales_order_details.qtyordered<5 and product_master.description='pull overs'; Output: PRODUC ORDERN ------ ------ p07885 o19001 g)Finding the products and their quantities for the orders placed by clientno ‘c00001’ and ‘c00002’. SQL> select so.clientno,sod.productno,pm.description,sum(qtyordered)"units ordered" 2 from sales_order so,sales_order_details sod,product_master pm,client_master cm 3 where so.orderno=sod.orderno and sod.productno=pm.productno 4 and so.clientno=cm.clientno 5 group by so.clientno,sod.productno,pm.description 6 having so.clientno='c00001' or so.clientno='c00002'; Output: CLIENT PRODUC DESCRIPTION units ordered ------ ------ --------------- ------------- c00001 p00001 t-shirts 4 c00001 p07885 pull overs 2 c00001 p07965 denim shirts 2
  113. c00002 p00001 t-shirts 10 3.SQL Statements for exercises on Sub-queries: a)Finding the non-moving products i.e. products not being sold. SQL> select productno,description 2 from product_master 3 where productno not in(select productno from sales_order_details); Output: PRODUC DESCRIPTION ------ --------------- p07865 jeans p07868 trousers p08865 skirts b)Finding the names and complete address for the customer who has placed order number ‘o19001’. SQLselect name,city,state,pincode 2 from client_master 3 where clientno in (select clientno from sales_order where orderno='o19001'); Output: NAME CITY STATE PINCODE -------------------- --------------- --------------- ---------- ivan bayros mumbai maharashtra 400054
  114. c)Find the clients who have placed orders before the month of may’02. SQL> select clientno,name 2 from client_master 3 where clientno in(select clientno from sales_order where to_char(orderdate,'mm,yy')<'may,02'); Output: CLIENT NAME ------ -------------------- c00001 ivan bayros c00002 mamta muzumdar c00003 chhaya bankar c00004 ashwini joshi d)Find out if the product ‘lycra tops’ has been ordered by any client and print the clientno, name to whom it was sold. SQL> select clientno,name 2 from client_master 3 where clientno in(select clientno 4 from sales_order where orderno in(select orderno from sales_order_details where productno in(se lect productno from product_master where description='lycratops'))); Output: no rows selected e)Find the names of clients who have placed orders worth Rs.10000 or more. SQL> select name from client_master 2 where clientno in(select clientno from sales_order
  115. 3 where orderno in(select orderno from sales_order_details 4 where (qtyordered*productrate)>=10000)); Output: NAME -------------------- ivan bayros chhaya banker Simple pl/sql queries a)Pl/Sql to add two numbers set serveroutput on; declare num1 number(2):=&first; num2 number(2):=&second; num3 number(2); begin num3:=num1+num2; dbms_output.put_line(num1||'+'||num2||'='||num3); end; OUTPUT: Enter value for first: 10 old 2: num1 number(2):=&first; new 2: num1 number(2):=10; Enter value for second: 20 old 3: num2 number(2):=&second; new 3: num2 number(2):=20; 10+20=30 PL/SQL procedure successfully completed.
  116. b)PL/Sql to find factorial of a number set serveroutput on; declare num1 number(2):=&number; fact number(10):=1; i number(2):=1; begin while i<=num1 loop fact:=fact*i; i:=i+1; end loop; dbms_output.put_line('factorial of'||num1||'is'||fact); end; OUTPUT: Enter value for number: 10 old 2: num1 number(2):=&number; new 2: num1 number(2):=10; factorial of10is3628800 PL/SQL procedure successfully completed. c)PL/Sql for demo on for loop set serveroutput on; declare s number(2):=&number; e number(2):=&number; i number(2); begin for i in s..e loop dbms_output.put_line('i= '||i); end loop; end; OUTPUT: Enter value for number: 2 old 2: s number(2):=&number; new 2: s number(2):=2; Enter value for number: 3 old 3: e number(2):=&number; new 3: e number(2):=3; i= 2 i= 3
  117. PL/SQL procedure successfully completed. d)PL/Sql for case structure set serveroutput on; declare oper char(1):='&operation'; num1 number(2):=&first; num2 number(2):=&second; num3 number(2); begin case when oper='+' then num3:=num1+num2; when oper='-' then num3:=num1-num2; when oper='*' then num3:=num1*num2; when oper='/' then num3:=num1/num2; when oper='%' then num3:=mod(num1,num2); else dbms_output.put_line('invalid operation'); end case; dbms_output.put_line(num1||oper||num2||'='||num3); end; OUTPUT: Enter value for operation: + old 2: oper char(1):='&operation'; new 2: oper char(1):='+'; Enter value for first: 2 old 3: num1 number(2):=&first; new 3: num1 number(2):=2; Enter value for second: 3 old 4: num2 number(2):=&second; new 4: num2 number(2):=3; 2+3=5 e)PL/Sql for simple loop. set serveroutput on; declare i number:=0; begin
  118. loop dbms_output.put_line('i='|| i); i:=i+2; exit when i>10; end loop; end; OUTPUT: i=0 i=2 i=4 i=6 i=8 i=10 PL/SQL procedure successfully completed. f)PL/Sql to increase the vaue by 10. declare n number(2); begin n:=&number; n:=n+10; dbms_output.put_line('value of n ='||n); end; OUTPUT: Enter value for number: 10 old 4: n:=&number; new 4: n:=10; value of n =20 PL/SQL procedure successfully completed. g)PL/SQL for performing arithmetic operations declare v_num1 number(2):=&first_num; v_num2 number(2):=&second_num; v_res number(3); v_choice char(1):='&choice';
  119. begin case v_choice when '+' then v_res:=v_num1+v_num2; when '-' then v_res:=v_num1-v_num2; when '*' then v_res:=v_num1*v_num2; when '/' then v_res:=v_num1/v_num2; when '%' then v_res:=mod(v_num1,v_num2); end case; dbms_output.put_line(v_num1||' '||v_choice||' '||v_num2||' '||' = '||v_res); end; OUTPUT: Enter value for first_num: 8 old 2: v_num1 number(2):=&first_num; new 2: v_num1 number(2):=8; Enter value for second_num: 9 old 3: v_num2 number(2):=&second_num; new 3: v_num2 number(2):=9; Enter value for choice: + old 5: v_choice char(1):='&choice'; new 5: v_choice char(1):='+'; 8 + 9 = 17 PL/SQL procedure successfully completed. h) PL/SQL to find square, cube, double of a number
  120. declare v_num number(3); v_sq v_num%type; v_cube v_sq%type; v_double v_cube%type; declare v_num:=&p_num; v_sq:=v_num*v_num; v_cube:=v_sq*v_num; v_double:=v_num*2; dbms_output.put_line('number is '||v_num); dbms_output.put_line('square is '||v_sq); dbms_output.put_line('cube is'||v_cube); dbms_output.put_line('double is'||v_double); end; OUTPUT: Enter value for p_num: 5 old 7: v_num:=&p_num; new 7: v_num:=5; number is 5 square is 25 cube is125 double is10
  121. PL/SQL procedure successfully completed. i) PL/SQL program to swap two numbers declare a number(2); b number(2); begin a:=&num1; b:=&num2; dbms_output.put_line('before swap a='||a||'b='||b); a:=a+b; b:=a-b; a:=a-b; dbms_output.put_line('after swap a='||a||'b='||b); end; OUTPUT: Enter value for num1: 15 old 5: a:=&num1; new 5: a:=15; Enter value for num2: 9 old 6: b:=&num2; new 6: b:=9; before swap a=15b=9 after swap a=9b=15
  122. PL/SQL procedure successfully completed. j)PL/SQL program to find multiplication table declare v_multiplicand number(2):=&multiplicand; v_res number(3); v_count number(2); begin for v_count in 1..10 loop v_res:=v_multiplicand*v_count; dbms_output.put_line(v_multiplicand||'*'||v_count||'='||v_res); end loop; end; OUTPUT: Enter value for multiplicand: 2 old 2: v_multiplicand number(3):=&multiplicand; new 2: v_multiplicand number(3):=2; 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16
  123. 2*9=18 2*10=20 PL/SQL procedure successfully completed. k)PL/SQL to determine whether a year is leap year or not declare v_year number(4):=&year; begin if(mod(v_year,4)=0 and mod(v_year,100)<>0 or mod(v_year,400)=0) then dbms_output.put_line(v_year || ' is leap year'); else dbms_output.put_line(v_year || ' is not leap year'); end if; end; OUTPUT: Enter value for year: 1992 old 2: v_year number(4):=&year; new 2: v_year number(4):=1992; 1992 is leap year PL/SQL procedure successfully completed. l)PL/SQL to insert item numbers from 1 to 5 using a loop. create table item(itemnum number(2)); declare
  124. v_num item.itemnum%type; begin for ctr in 1..5 loop v_num:=ctr; insert into item values(v_num); end loop; end; OUTPUT: PL/SQL procedure successfully completed. SQL> select * from item; ITEMNUM ---------- 1 2 3 4 5 m)PL/SQL to delete an item whose itemnum=4 declare v_num item.itemnum%type; begin select itemnum into v_num from item where itemnum=&p_num;
  125. delete from item where itemnum=v_num; end; OUTPUT: Enter value for p_num: 1 old 4: select itemnum into v_num from item where itemnum=&p_num; new 4: select itemnum into v_num from item where itemnum=1; PL/SQL procedure successfully completed. SQL> select * from item; ITEMNUM ---------- 2 3 4 5 n)PL/SQL program to display name, salary , commission and emp no of an employee based on the id. declare eno emp.empno%type; empname emp.ename%type; esal emp.sal%type; ecom emp.comm%type;
  126. begin select empno,ename,sal,comm into eno,empname,esal,ecom from emp where empno=&no; dbms_output.put_line('EMPNO'|| eno); dbms_output.put_line('ENAME'|| empname); dbms_output.put_line('SALARY'|| esal); dbms_output.put_line('COMM'|| ecom); end; OUTPUT: Enter value for no: 7902 old 7: select empno,ename,sal,comm into eno,empname,esal,ecom from emp where empno=&no; new 7: select empno,ename,sal,comm into eno,empname,esal,ecom from emp where empno=7902; EMPNO7902 ENAMEFORD SALARY3000 COMM PL/SQL procedure successfully completed. o)PL/SQL program for inverting a number. declare given_num varchar(5):='&num'; str_len number(2); inv_num varchar(5);
  127. begin str_len:=length(given_num); for cntr in reverse 1..str_len loop inv_num:=inv_num||substr(given_num,cntr,1); end loop; dbms_output.put_line('the given number is '||given_num); dbms_output.put_line('the inverted number is '||inv_num); end; OUTPUT: Enter value for num: 12 old 2: given_num varchar(5):='&num'; new 2: given_num varchar(5):='12'; the given number is 12 the inverted number is 21 PL/SQL procedure successfully completed. p)PL/SQL program to calculate the area of a circle for value of radius ranging from 3 to 7. SQL>create table areas(radius number(5),area number(14,2)); SQL>declare pi constant number(4,2):=3.14; radius number(5); area number(14,2); begin
  128. radius:=&radius; while radius<=7 loop area:=pi*power(radius,2); insert into areas values(radius,area); radius:=radius+1; end loop; end; OUTPUT: Enter value for radius: 5 old 6: radius:=&radius; new 6: radius:=5; PL/SQL procedure successfully completed. SQL> select * from areas; RADIUS AREA ---------- ---------- 5 78.5 6 113.04 7 153.86 q)Write a program to print empno, ename, job and salary of an employee given empno.
  129. SQL> declare 2 type emprec is record(eno emp.empno%type, n emp.ename%type, j emp.job%type,s emp.sal%type); 3 r emprec; 4 eno emp.empno%type:=&eno; 5 begin 6 select empno,ename,job,sal into r from emp where empno=eno; 7 dbms_output.put_line(r.eno||' '||r.n||' '||r.j||' '||r.s); 8 end; 9 / Output: Enter value for eno: 7369 old 4: eno emp.empno%type:=&eno; new 4: eno emp.empno%type:=7369; 7369 SMITH CLERK 800 PL/SQL procedure successfully completed. r)Write a program to set the values for DA, HRA, GROSS fields of all the records of the given table. Condition DA HRA GROSS Basic < 2000 10% of Basic 5% of Basic Basic+DA+HRA Basic >=2000 and <=4000 15% of Basic 10% of Basic Basic+DA+HRA Basic>4000 20% of Basic 12% of Basic Basic+DA+HRA SQL> create table employee( 2 empno number(3) primary key,
  130. 3 ename varchar2(10), 4 basic number(6,2), 5 da number(6,2), 6 hra number(6,2), 7 gross number(6,2)); Table created. SQL> insert into employee(empno,ename,basic) values(121,'Ravi',5000); 1 row created. SQL> insert into employee(empno,ename,basic) values(122,'Kiran',1500); 1 row created. SQL> insert into employee(empno,ename,basic) values(123,'Amar',6000); 1 row created. Sql>begin 2 update employee set da=basic*0.1,hra=basic*0.05 where basic<2000; 3 update employee set da=basic*0.15,hra=basic*0.1 where basic>=2000 and basic<=4000; 4 update employee set da=basic*0.2,hra=basic*0.12 where basic>4000; 5 update employee set gross=basic+hra+da; 6 commit; 7 end; SQL> / PL/SQL procedure successfully completed.
  131. OUTPUT: SQL> select * from employee; EMPNO ENAME BASIC DA HRA GROSS ---------- ---------- ---------- ---------- ---------- ---------- 121 Ravi 5000 1000 600 6600 122 Kiran 1500 150 75 1725 123 Amar 6000 1200 720 7920 s)Write a PL/SQL block that will accept a account number from the user , check if the users balance is less than the minimum balance then deduct Rs.100 from the balance as a fine. declare macno number; mcurbal number; minbal constant number:=520.00; fine constant number(4):=100; begin macno:=&account_number; select bal into mcurbal from Account where acno=macno; ifmcurbal<minbal then update account set bal=bal-fine where acno=macno; end if; end; OUTPUT: Enter value for account_number: 2005 old 7: macno:=&account_number; new 7: macno:=2005; PL/SQL procedure successfully completed. t)Write a PL/SQL block that will accept a account number and incremented amount from the user, and increments the balance with the incremented amount.
  132. declare macno number; mcurbal number; minc number(5); begin macno:=&account_number; minc:=&inc_number; select bal into mcurbal from Account where acno=macno; if (minc>0) then update account set bal=bal+minc where acno=macno; end if; end; / OUTPUT: Enter value for account_number: 2005 old 6: macno:=&account_number; new 6: macno:=2005; Enter value for inc_number: 1000 old 7: minc:=&inc_number; new 7: minc:=1000; PL/SQL procedure successfully completed. u)Write a PL/SQL block that withdraws an amount of Rs.1000 then deposit’s an amount of Rs.10,000, updates the current balance ,then checks to see if the current balance of all the accounts does not exceed 50,000 if so then undo the deposit made. declare mbal number(7,2); macno number(4); begin macno:=&account_no; update Account set bal=bal-1000 where acno=macno; update Account set bal=bal+10000 whereacno=macno; savepoint s1; select sum(bal) into mbal from Account; if mbal> 50000 then rollback to s1; end if;
  133. commit; end; / OUTPUT: Enter value for account_no: 2005 old 5: macno:=&account_no; new 5: macno:=2005; PL/SQL procedure successfully completed. SQL> SELECT * FROM ACCOUNT; ACNO BAL ---------- ---------- 2005 10410 CURSORS a)Write a PL/SQL block using the cursor to display details of all employee’s from emp2 table whose sum of sal and comm is > 2000. declare mempno emp2.empno%type; mename emp2.ename%type; msal emp2.sal%type; cursor c1 is select empno,ename,sal+nvl(comm,0) from emp2 where sal+nvl(comm,0)>2000; begin open c1; dbms_output.put_line('employeeno'||'empname'||'netsalary'); loop fetch c1 into mempno,mename,msal; dbms_output.put_line(mempno||' '||mename||' '||msal); exit when c1%notfound; end loop; close c1; end;
  134. OUTPUT: employeenoempnamenetsalary 7566 JONES 2975 7654 MARTIN 2650 7698 BLAKE 2850 7782 CLARK 2450 7788 SCOTT 3000 7839 KING 5000 7902 FORD 3000 7902 FORD 3000 PL/SQL procedure successfully completed. b)Write a PL/SQL block that display’s employee name and the salary of the first 5 employee’s having the highest salary. declare mempno emp2.empno%type; mename emp2.ename%type; msal emp2.sal%type; cursor c1 is select empno,ename,sal from emp2 order by sal desc; begin open c1; dbms_output.put_line('empname'||' netsalary'); loop fetch c1 into mempno,mename,msal; dbms_output.put_line(mename||' '||msal); exit when (c1%rowcount -1)=5 or c1%notfound; end loop; close c1; end; / OUTPUT: empname netsalary KING 5000 SCOTT 3000 FORD 3000
  135. JONES 2975 BLAKE 2850 CLARK 2450 PL/SQL procedure successfully completed. c)Write a pl/sql program to find the sum of the salaries in a particular department. Sql>declare 2 s number:=0; 3 pay emp.sal%type; 4 dno emp.deptno%type; 5 cursor c2 is select sal from emp where deptno=&dno; 6 begin 7 open c2; 8 loop 9 fetch c2 into pay; 10 exit when c2%notfound; 11 s:=s+pay; 12 end loop; 13 close c2; 14 dbms_output.put_line('sum of the salaries in deptno 10 is '||s); 15 end; 16 / Output: Enter value for dno: 10 old 5: cursor c2 is select sal from emp where deptno=&dno;
  136. new 5: cursor c2 is select sal from emp where deptno=10; sum of the salaries in deptno 10 is 8750 PL/SQL procedure successfully completed. d)PL/SQL to print all employees begin with S. declare n emp.ename%type; cursor c1 is select ename from emp where ename like 'S%'; begin open c1; fetch c1 into n; while c1%found loop dbms_output.put_line(n); fetch c1 into n; end loop; close c1; end; OUTPUT: SMITH SCOTT PL/SQL procedure successfully completed.
  137. e)PL/SQL to print names of all managers declare n emp.ename%type; cursor c1 is select ename from emp where job='MANAGER' order by ename; begin open c1; loop fetch c1 into n; exit when c1%notfound; dbms_output.put_line(c1%rowcount||':'||n); end loop; close c1; end; OUTPUT: 1:BLAKE 2:CLARK 3:JONES PL/SQL procedure successfully completed. PROCEDURES a)PL/SQL to update salary of an employee whose empno and increment mentioned. create or replace procedure empupdate(enoemp.empno%type,incr number) as
  138. begin update emp set sal=sal+incr where empno=eno; commit; dbms_output.put_line('updated successfully'); end; //Usage at sql prompt Exec empupdate(7369,1000); OUTPUT: Procedure created. SQL> Exec empupdate(7369,1000); updated successfully PL/SQL procedure successfully completed. SQL> SELECT * FROM EMP 2 WHERE EMPNO=7369; EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 SMITH CLERK 7902 17-DEC-80 1800 20
  139. EXCEPTIONAL HANDLING a)Write a PL/SQL block which accepts the empno from the user and display the details of the employee. When the user enters an empno that is not in the emp table then the PL/SQL block must display an appropriate message to the user. declare mempno emp2.empno%type; begin select empno into mempno from emp2 where empno=&empno; dbms_output.put_line(mempno || 'found!'); Exception When no_data_found then dbms_output.put_line('Employee number not found!'); end; / OUTPUT: Enter value for empno: 7902 old 4: select empno into mempno from emp where empno=&empno; new 4: select empno into mempno from emp where empno=7902; 7902found! PL/SQL procedure successfully completed. b)PL/SQL block that retrieves course table into cursor. Input course id to search. If course exists, print the information. If the course id does not exist, print user defined message by throwing user defined exception. declare c_name course.course_name%type; cursor c1 is select course_name from course where courseid='&id'; course_not_found exception; begin open c1;
  140. fetch c1 into c_name; if c1%found then dbms_output.put_line('course name is'||c_name); else raise course_not_found; end if; exception when course_not_found then dbms_output.put_line('course id not found'); end; OUTPUT: Enter value for id: 1600 old 3: cursor c1 is select course_name from course where courseid='&id'; new 3: cursor c1 is select course_name from course where courseid='1600'; course name ispcmt PL/SQL procedure successfully completed. c)Write a program to handle ZERO_DIVIDE exception SQL> declare 2 x number:=&x; 3 y number:=&y; 4 begin 5 dbms_output.put_line('Division='||x/y); 6 exception when zero_divide then 7 dbms_output.put_line('check the denominator');
  141. 8 end; 9 / Output: Enter value for x: 5 old 2: x number:=&x; new 2: x number:=5; Enter value for y: 0 old 3: y number:=&y; new 3: y number:=0; check the denominator PL/SQL procedure successfully completed. FUNCTIONS a)Write a PL/SQL function which checks for an employee number in the table and returns 1 if present otherwise returns 0. create or replace function checkemp(eno number) return number is mempno emp2.empno%type; begin select empno into mempno from emp2 where empno=eno; return 1; exception when no_data_found then return 0; end; / SQL>exec dbms_output.put_line(checkemp(7499));
  142. OUTPUT: Function created. SQL> exec dbms_output.put_line(checkemp(7499)); 1 PL/SQL procedure successfully completed. SQL> SELECT * FROM EMP 2 ; EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 TRIGGERS a)PL/SQL that creates a trigger that inserts or update values of ename, job as lower case strings. create or replace trigger lowername before insert or update on emp for each row begin :new.ename:=lower(:new.ename); :new.job:=lower(:new.job); end; update emp set job='MANAGER' where ename='SMITH'; OUTPUT: Trigger created.
  143. SQL> update emp set job='MANAGER' where ename='SMITH'; 1 row updated. SQL> SELECT * FROM EMP; EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7369 smith manager 7902 17-DEC-80 1800 20 CREATE AN ACCOUNT TABLE Query:  Creating a table account and inserting values in it. create table account ( acno number(5), bal number(5) );  Enter values into table ‘account’. insert into account values(2005,510);  Displaying items in table ‘account’.
  144. select * from account; ACNO BAL ---------------- -------------------- 2005 510
Publicidad