SQL

March 28, 2018 | Author: Naveen Reddy | Category: Table (Database), Computer Data, Data Management, Areas Of Computer Science, Scientific Modeling


Comments



Description

1) To copy the contents of the table EMP to RAJ1… CREATE table raj1 as select * from emp; 2) Create a tablewhich contain structure is same as EMP… CREATE table raj2 as SELECT * from emp where 2=200; 3) Copy emp table to the table which declare our own attribute names.. CREATE table raj1(eno,emp_nm,salary,dep) as SELECT empno,ename,sal,deptno from emp; ENO 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected. 4) To improve performance we use NOLOGGING.. CREATE table raj2(eno,emp_nm,salary,dep) NOLOGGING as SELECT empno,ename,sal,deptno from emp; EMP_NM SALARY 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 DEP 20 30 30 20 30 30 10 20 10 30 20 30 20 10 -1- Select Syntax: SELECT<col>,<col>,<col> ….FROM <tabname><tabname><tabname>…..; • • • It is a data query language or data retrieval language. * Represent only columns. We also give like this… SELECT all * from emp; 5) giving Elias names to coloms.. SELECT empno enumber,ename Name from emp; Note:- Elias name must not in oracle verbs.. Distinct Syntax: SELECT DISTINCT <col>,<col>,<col> ….FROM <tabname><tabname><tabname>…..; • • • To retrieve distinct rows from table. It is anci standard keyword. We will also use UNIQUE . 6).Display all the unique departments of the employees. SELECT UNIQUE deptno from emp; 7).Display all unique commissions of employees. SELECT DISTINCT comm from emp; 8) Display all the unique DEP and JOB combinations SELECT DISTINCT deptno,job from emp; • • In above query first rows are selected on DEPTNO then it will go to the job. SELECT DISTINCT deptno,job from emp DEPTNO 10 CLERK 10 MANAGER 10 PRESIDENT 20 ANALYST JOB -2- 20 CLERK 20 MANAGER 30 CLERK 30 MANAGER 30 SALESMAN 9 rows selected. • select distinct job,deptno from emp; JOB ANALYST CLERK CLERK CLERK MANAGER MANAGER MANAGER PRESIDENT SALESMAN 9 rows selected. DEPTNO 20 10 20 30 10 20 30 10 30 -3- note:. 11).job. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7698 BLAKE 7782 CLARK 7844 TURNER 7654 MARTIN 7839 KING 7900 JAMES 7902 FORD 7934 MILLER 7788 SCOTT 7876 ADAMS 14 rows selected.empno.ename. JOB CLERK SALESMAN SALESMAN MANAGER MANAGER MANAGER SALESMAN SALESMAN PRESIDENT CLERK ANALYST CLERK ANALYST CLERK MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7839 01-MAY-81 7839 09-JUN-81 7698 08-SEP-81 7698 28-SEP-81 17-NOV-81 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 7566 19-APR-87 7788 23-MAY-87 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 2975 20 2850 30 2450 10 1500 0 30 1250 1400 30 5000 10 950 30 3000 20 1300 10 3000 20 1100 20 DEPTNO JOB 10 PRESIDENT 10 MANAGER 10 CLERK 20 MANAGER 20 CLERK EMPNO ENAME 7839 KING 7782 CLARK 7934 MILLER 7566 JONES 7369 SMITH SAL 5000 2450 1300 2975 800 -4- . (Or) SELECT deptno. emp no. Display the deportment number job. SELECT deptno. Display the descending order of their experience. 9).. FROM <table><table>…. <col> [ASC/DESC]….ename.sal from emp order by 1 asc.….Sorting: Syntax: SELECT [distinct] <col><col><col>..job desc. 10).empno.job.2 desc. Display details of all employees the DECENDING order of there employees salary. ORDER BY <col> [ASC/DESC]. name & salary on the ASCENDING order of deptno and DECENDING order of job. SELECT * from emp order by sal desc.sal from emp order by deptno asc.default sort order is ASCENDING order. SELECT * from emp ORDER BY hiredate. In above result all null values are came first that is because in oracle during data sorting NULL VALUES will be consider as BIGGEST value. Display all the commissions in DESCENDING order. 12).20 CLERK 20 ANALYST 20 ANALYST 30 SALESMAN 30 SALESMAN 30 SALESMAN 30 SALESMAN 30 MANAGER 30 CLERK 14 rows selected. SINGLE BYTE OVERHEADE:In this mechanism oracle first sort all the not null values first. SELECT comm From emp order by comm desc. to biggest not null values memory size one byte is added and this memory size is assigned to the null value. • Hear oracle using SINGLE BYTE OVERHEARD mechanism during data sorting ware aver NULL exist. To diplay null values last… -5- . COMM 7876 ADAMS 7788 SCOTT 7902 FORD 7499 ALLEN 7654 MARTIN 7521 WARD 7844 TURNER 7698 BLAKE 7900 JAMES 1100 3000 3000 1600 1250 1250 1500 2850 950 1400 500 300 0 14 rows selected. considering null as biggest value. Display all the unique deportment number in ascending order. COMM 0 300 500 1400 13). SELECT DISTINCT deptno from emp order by 1. DEPTNO 10 20 30 Where: -6- .SELECT comm From emp order by comm desc NULLS FIRST. <=. IS NOT NULL.]. / . <col> [ASC/DESC]…. FROM <table><table>…. >.. NOT LIKE. ^=  not equal to. is to define the restrictions on data selection i.Syntax: SELECT [distinct] <col><col><col>. NOT IN. NOT.e retrieving specific rows of data based on some condition. INTERSECT. >=. OPERATORS SQL:Arithmetic operations: + . !=.…. MINUS. LIKE. Set operators: UNION. EXAMPLES: -7- . IS NULL. * . Logical operator: AND. =. NOT BETWEEN.. Negation operations: <> . BETWEEN. IN. . Relational operator:< . UNION ALL. OR. WHERE <condition> [ORDER BY <col> [ASC/DESC]. 3571904 21. salary.1654096 27.sal.6065055 21.121574 27.3333333 19200 WARD 1250 41.(it returns days) Date +date is allowed.15).6666667 15000 JONES 2975 99.display all the employees working from deportment 30. EMPNO ENAME JOB 7566 JONES MANAGER 7698 BLAKE MANAGER 7782 CLARK MANAGER MGR HIREDATE 7839 02-APR-81 7839 01-MAY-81 7839 09-JUN-81 SAL COMM 2975 2850 2450 DEPTNO 20 30 10 sql > i (it use to add one more line to exist) 17).3333333 15600 14 rows selected. daily salary.1666667 35700 MARTIN 1250 41.sal*12 asal.6666667 13200 JAMES 950 31. annual salary and experience of all the employees.6503411 27.6666667 9600 ALLEN 1600 53. (it returns days) Date-number is allowed(it returns date). EXP 28.7133548 27.981848 Note: Date-date is allowed. exp from emp. -8- .7928069 27.6666667 15000 BLAKE 2850 95 34200 CLARK 2450 81. Display name. SELECT * from emp where deptno=30.121574 26.(sysdate-hiredate)/365 ENAME SAL DSAL ASAL SMITH 800 26.6666667 29400 SCOTT 3000 100 36000 KING 5000 166.sal/30 dsal.0832178 27.6666667 11400 FORD 3000 100 36000 MILLER 1300 43.9051356 27.7434918 27.8996562 27.3023959 27. 16)display the details of all the managers SELECT * from emp where job=’MANAGER’. SELECT ename.666667 60000 TURNER 1500 50 18000 ADAMS 1100 36. JOB SALESMAN SALESMAN SALESMAN MANAGER SALESMAN CLERK MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7839 01-MAY-81 7698 08-SEP-81 7698 03-DEC-81 SAL COMM DEPTNO 1600 300 30 1250 500 30 1250 1400 30 2850 30 1500 0 30 950 30 18) List all the emp’s working for the manager 7698.EMPNO ENAME 7499 ALLEN 7521 WARD 7654 MARTIN 7698 BLAKE 7844 TURNER 7900 JAMES 6 rows selected. SELECT * from emp where hiredate > ’31-dec-1980’. EMPNO ENAME 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 13 rows selected.list all the emp’s who join after 1980. SELECT * from emp where mgr=7698. EMPNO ENAME 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER 7900 JAMES JOB SALESMAN SALESMAN SALESMAN SALESMAN CLERK MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 7698 03-DEC-81 SAL COMM DEPTNO 1600 300 30 1250 500 30 1250 1400 30 1500 0 30 950 30 19). JOB SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 1600 300 30 1250 500 30 2975 20 1250 1400 30 2850 30 2450 10 3000 20 5000 10 1500 0 30 1100 20 950 30 3000 20 1300 10 -9- . 20) list all the emp’s have an experience more then 27 years. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO .’JAMES’. JOB CLERK SALESMAN SALESMAN SALESMAN SALESMAN CLERK CLERK CLERK MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 1250 1400 30 1500 0 30 1100 20 950 30 1300 10 22).10 - .’DAMS KING’) order by ename desc.’DAMS KING’ on descending order of salary. OR SELECT * from emp where job IN (’CLERK’. JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER PRESIDENT SALESMAN CLERK ANALYST MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 17-NOV-81 7698 08-SEP-81 7698 03-DEC-81 7566 03-DEC-81 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 2975 20 1250 1400 30 2850 30 2450 10 5000 10 1500 0 30 950 30 3000 20 21) display all the Clarks and salesman. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7839 KING 7844 TURNER 7900 JAMES 7902 FORD 11 rows selected. ’SALESMAN’). SELECT * from emp where ename in (‘FORD’. ‘SMITHA’. SELECT * from emp where job=’CLERK’ OR job=’SALESMAN’.Display a detail’s of ‘FORD’. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER 7876 ADAMS 7900 JAMES 7934 MILLER 8 rows selected. SELECT * from emp where (sysdate-hiredate)/365 >27. ‘SMITHA’.’JAMES’. 7900 JAMES 7902 FORD CLERK ANALYST 7698 03-DEC-81 7566 03-DEC-81 950 3000 30 20 23). SELECT * from emp where sal between 1250 and 3000. JOB SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST SALESMAN ANALYST CLERK MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 7698 08-SEP-81 7566 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 1600 300 30 1250 500 30 2975 20 1250 1400 30 2850 30 2450 10 3000 20 1500 0 30 3000 20 1300 10 25).List all the employees joined in beginning of the third quarter of 1981 still the end of the second quarter of 1982.11 - . SELECT * from emp where hiredate between '01-jul-1981' and '30-jun-1982'. SELECT * from emp where hiredate between ’01-jan-1980’ and ’31-dec-1980’.Display the employees receiving sal from 1250 up to 3000. EMPNO ENAME JOB 7369 SMITH CLERK MGR HIREDATE 7902 17-DEC-80 SAL COMM 800 DEPTNO 20 24). EMPNO ENAME 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7844 TURNER 7902 FORD 7934 MILLER 10 rows selected.Display all the employees join in the year 1980. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO .Display all the employees how’s annual salary is ranging from 18000 to 42000. SELECT * from emp where sal*12 between 18000 and 24000. EMPNO ENAME 7654 MARTIN 7839 KING 7844 TURNER 7900 JAMES 7902 FORD 7934 MILLER JOB SALESMAN PRESIDENT SALESMAN CLERK ANALYST CLERK MGR HIREDATE 7698 28-SEP-81 17-NOV-81 7698 08-SEP-81 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 1250 1400 30 5000 10 1500 0 30 950 30 3000 20 1300 10 26). 12 - . IS NULL to identify the null values. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 13 rows selected.Display all the emp’s working for manager. SELECT * from emp where mgr is NOT NULL. select ename from emp where ename like ‘A%’. 29).select the names starting with chars ‘S’. JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST SALESMAN CLERK CLERK ANALYST CLERK MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 2975 20 1250 1400 30 2850 30 2450 10 3000 20 1500 0 30 1100 20 950 30 3000 20 1300 10 28) Display all the five characters name. % to represent a group of characters. ENAME ALLEN ADAMS 30). Like: The delimiter is used is used like character are: • • • • ‘_’ represent a single charactor. SELECT ename from emp where ename LIKE‘-----‘.7499 ALLEN 7844 TURNER SALESMAN SALESMAN 7698 20-FEB-81 7698 08-SEP-81 1600 1500 300 0 30 30 27). . IS NOT NULL to identify not null values.list all the names joining a January. select * from emp where hiredate like '%80'. EMPNO ENAME JOB 7934 MILLER CLERK MGR HIREDATE 7782 23-JAN-82 SAL COMM 1300 DEPTNO 10 31).List all the emp’s of 1980. EMPNO ENAME JOB 7566 JONES MANAGER 7698 BLAKE MANAGER 7782 CLARK MANAGER MGR HIREDATE 7839 02-APR-81 7839 01-MAY-81 7839 09-JUN-81 SAL COMM 2975 2850 2450 DEPTNO 20 30 34).display all the managers working for some other managers. select * from emp where hiredate like '%_E_%'. Select * from emp where deptno=20 and sal*12 between 22000 and 40000.20 and receiving annual salary ranging from 2200 and 40000.13 - . ENAME SAL DEPTNO JOB ASAL . EMPNO ENAME JOB 7369 SMITH CLERK MGR HIREDATE 7902 17-DEC-80 SAL COMM 800 DEPTNO 33). EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER 7900 JAMES 7902 FORD JOB CLERK SALESMAN SALESMAN SALESMAN SALESMAN CLERK ANALYST MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 7698 03-DEC-81 7566 03-DEC-81 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 1250 1400 30 1500 0 30 950 30 3000 20 Note: _% = %_ = % Key: Sql> C/J/E means Change where J replace with E.select * from emp where hiredate like '%JAN%'.display all the emp’s joined in month which secound character is ‘E’.list all employees of department no. 32). Select * from emp where job=’MANAGER’ and mgr is NOT NULL. working with not registering president and analyst. EMPNO ENAME 7369 SMITH 7900 JAMES 7876 ADAMS 7934 MILLER 7782 CLARK 7698 BLAKE 7566 JONES 7788 SCOTT 7902 FORD 7839 KING JOB CLERK CLERK CLERK CLERK MANAGER MANAGER MANAGER ANALYST ANALYST PRESIDENT MGR HIREDATE 7902 17-DEC-80 7698 03-DEC-81 7788 23-MAY-87 7782 23-JAN-82 7839 09-JUN-81 7839 01-MAY-81 7839 02-APR-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 SAL COMM 800 950 1100 1300 2450 2850 2975 3000 3000 5000 DEPTNO 20 30 20 10 10 30 20 20 20 10 Note: hear we not use BETWEEN for selecting experience bcz it was not including 20 and 30.'CLARK'). Between is for including 20 and 30.display all the emp’s having experience more then 20 years and less then 30.14 - . select * from emp where (sysdate-hiredate)/365 >20 and (sysdate-hiredate)/365 <30 and comm is null order by sal. select * from emp where hiredate between '01-jan-1981' and '31-dec-1981' and hiredate not like '0%' and hiredate not like '%_A%' and job not in('PRESIDENT'.display details of all employees joined in 1981 but not belongs to the starting month character ‘A’. 36).SMITH JONES SCOTT ADAMS FORD 800 2975 3000 1100 3000 20 CLERK 20 MANAGER 20 ANALYST 20 CLERK 20 ANALYST 9600 35700 36000 13200 36000 35). EMPNO ENAME JOB 7499 ALLEN SALESMAN 7521 WARD SALESMAN 7654 MARTIN SALESMAN MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 SAL COMM DEPTNO 1600 300 30 1250 500 30 1250 1400 30 .not receiving any commission in the ascending order of salary. Mostly function for calculation or manipulation purpose(not DML).15 - . EVERY FUNCTION IS RETURN A VALUE. .a function witch process 1 row at a time and return a value .Functions: • • • • Function is a predefined function perform a predefined program. (Depending upon the process capabilities ) function are two types: 1)single function:. For few function inputs are mandate or optional or not required. .2) 345.5678.-2) from dual.124.ABC(number) :.5678. ABS(-34343) 34343 b).13 but if we give 345. select round(345.2) 345.567 .12 We also round the integers.2) from dual. 2) character function or string manipulation function. ROUND(345.126.\ 3)date function. these functions based an the data types divided into 4 types : 1) number function or airthematic or scientific.3) from dual.-2) 0 TRUNC():Select trunc(1234. ROUND(345.16 - . ARITHEMATIC FUNCTION: a).124.126.126 is rounded up to 2 decimals it was 345.it returns unsigned value.12.3) 1234. ROUND(50.2) from dual.ROUND():select round(345.124 it will round as 345.-2) 100 Select round(49. Select round(51.2)group function :.a function witch process a set of rows at a time and return a single values . ROUND(49. TRUNC(1234. Select abs(-34343) from dual.13 In above 345.-2) from dual. 4)data convert function or TYPE COST function. Select ceil(1234.17 - .-3) 1000 CEIL():• It rounding only decimal points.00001) 1234 SQRT():Select sqrt(25) from dual. • It always round to upper value.99999) from dual.10) from dual.99999) 1235 FLOOR():Select floor(1234. CEIL(1234.00001) from dual.-3) from dual.-3) from dual.0000001) from dual.-3) 1000 Select trunc(1999. TRUNC(1999. SQRT(25) 5 MOD():Select mod(45. MOD(45. CEIL(1234. TRUNC(1234. FLOOR(1234.Select trunc(1234.10) 5 .0000001) 1235 Select CEIL(1234. POWER(5.2) 25 SIGN():• It returns –1 if it is negative otherwise it returns 0. sal. • This function required two parameters.12.-2) asal.0) dsal.LEAST() :Greatest().35) 35 NVL(<col>.<val>):• Null value conversion:.POWER():Select power(5.least() functions are also called as functions these two function accept any no.08 . • This function works on any parameters belongs to same data type.EXP(). annual salary rounded to nearest 100. round(sal*12. Select greatest(1.sign(0).display the employee number. round(sal/30. Example: select nvl(comm. Select sign(100).sign(-100) from dual. and we also have function LOG().this function used to convert a null value into specified not null value.0) from emp.COS(). SIGN(100) 1 SIGN(0) 0 SIGN(-100) -1 GREATEST().of arguments these two function works with any data type but all parameter of these function belongs to same data type.LN().18 - .SIN().12.ename.sal.2) from dual.35) from dual. name.2) exp from emp. GREATEST(1. trunc((sysdate-HIREDATE)/365. daily salary rounded the rupee and experience with 2 decimal places of all the emp’s Select empno.TAN() 36). EMPNO ENAME 7369 SMITH SAL 800 ASAL 9600 DSAL 27 EXP 28. 6 21.9 27. UPPER('R RAJENDER LOWER(): Select lower(‘RAJENDER’) from dual.display all salaries divided by 100.35 21.19 - .12 27.9 27.7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 19200 15000 35700 15000 34200 29400 36000 60000 18000 13200 11400 36000 15600 53 42 99 42 95 82 100 167 50 37 32 100 43 27.3 27. Select * from emp where mod(sal.12 26.74 27.71 27.100)=0. EMPNO ENAME 7369 SMITH 7499 ALLEN 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7902 FORD 7934 MILLER JOB CLERK SALESMAN ANALYST PRESIDENT SALESMAN CLERK ANALYST CLERK MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7566 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 800 20 1600 300 30 3000 20 5000 10 1500 0 30 1100 20 3000 20 1300 10 CHARACTER FUNTION: UPPER():Select upper('rajender') from dual.16 27. LOWER('R rajender INITCAP(): • The first letter of the word convert into upper case.65 27.79 27. .98 37). CHR(): • It convert ASCII as character.String 2). SU er • It counts chars from reverse order and select 2 chars from starting. INITCAP('RAJ Rajender Mca ASCII(): Select ascii(‘A’) from dual.Select initcap('rajender mca') from dual.6) from dual. C A SUBSTR(): • It require three parameters 1). SUBS meer Select substr('ameerpet'.2. 3). ASCII('A') 65 • So it converts only first char as ASCII. Select substr(‘ameerkpet’.. Select substr('ameerpet'. SUBST erpet • It counts chars from reverse order. Select chr(65) from dual.which position u want. ASCII('A') 65 Select ascii('ABCDEF') from dual.4) from dual. . SUBS kpet Select substr(‘ameerpet’.how many characters u want.2) from dual.-5) from dual.-5..20 - . 6. LTRIM(' . LENGTH('ORACLE') 6 LPAD(): • It pads from left side.1st .2.searching char 3).6.3) 7 Display al the emp’s names having a character ‘L’ at least 2 times after the 2nd possion. • It require three parameters • string or number. Select lpad(‘5000’.3.21 - .INSTR(): • It require four parameters 1). Select ltrim(‘AMEERPER’. 1) Max length of string u want.3rd ). RPAD(' 5000** LTRIM(): • It removes from extreme left. Select rpad('5000'.'e'. 2) Character to be filled.'E'.'*') from dual. LPAD(' **5000 RPAD(): • It pads from right.2)>0.String 2). select * from emp where instr(ename. INSTR('AMEERPET'.'L'.3) from dual. Select instr('ameerpet'.2nd.2.from which position 4). • Default character which is trimmed is space character.’A’) from dul.’*’) from dual. EMPNO ENAME JOB 7934 MILLER CLERK MGR HIREDATE 7782 23-JAN-82 SAL COMM 1300 DEPTNO 10 LENGTH(): Select length(‘ORACLE’) from dual.number of occurred position of that latter(ie. 'THISIS'||'ORCLECLA this is orcle class TRANSLATE(): • It decode the given string. • It require three parameters . Select concat('this is '.MEERPER Select ltrim('AAAAAAMEERPER'. TRANSLAT 1d55RP5T REPLACE(): • This function find the string and replace with encoding string. Select rtrim('AAAAAAMEERPER'.'A') from dual.22 - . RTRIM('AAAAA AAAAAAMEERPE Select rtrim('AAAAAAMEERPERRRRR'. • We also use pipe simbole(||). CONCAT('THISIS' this is myhome… Select 'this is '||'orcle class' from dual. Select translate ('AMEERPET'.'R') from dual. LTRIM(' MEERPER Select ltrim('AAAAAAMEERPER'.'my home…') from dual. • In this function first we give the string and then we specify the coding. LTRI RPER RTRIM(): • It trimmed from extreme right.'AME') from dual. RTRIM('AAAA AAAAAAMEERP CONCAT(): • It joins two strings.'123456789abcd')from dual.'ER') from dual.'ABCDEFGHIJKLM'. when ‘SALESMAN’ then ’S’.the main string.case job When ’CLERK’ then ’C’. Select replace('AMEERPET'. JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 14 rows selected C S S M S M M UNKNOUN JOB UNKNOUN JOB S C C UNKNOUN JOB C DECODE(JOB. ‘SALESMAN’. Select job.'P'.encoding string or characters. when ‘MANAGER’ then ’M’.’S’. ‘MANAGER’. REPLACE('AME AMEER12345ET DECODE(): • It is like a NESTED IF-ELSE statement in languages.'12345') from dual. Select job. 2). JOB CLERK SALESMAN C S JOB_CODE .’CLERK’. ‘UNKNOUN JOB’) from emp. • We also use CASE statement.’M’.23 - . else ‘UNKNOUN JOB’ end job_code from emp. • It decode different string with different coding.string which is to be replaced with encoding.’C’.decode(job. 3).1). 2. EMPNO ENAME 7566 JONES 7698 BLAKE 7900 JAMES 7902 FORD JOB MANAGER MANAGER CLERK ANALYST MGR HIREDATE 7839 02-APR-81 7839 01-MAY-81 7698 03-DEC-81 7566 03-DEC-81 SAL COMM 2975 2850 950 3000 DEPTNO 20 30 30 20 40).display all six character named employees. 38).’O’.24 - .’I’. Select ename from emp where substr(ename. Select ename from emp where length(ename)=6.2) between 1 and 5. ENAME WARD JONES MARTIN KING TURNER . ENAME MARTIN TURNER MILLER 39). S M S M M UNKNOUN JOB UNKNOUN JOB S C C UNKNOUN JOB C Note: hear job-code is alias name.display enames having charact AEIOU IN SECOND position.’E’. Select * from emp where substr(hiredate.SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 14 rows selected.display the all emp’s who’s joined in first 5 days of any month.’U’).1) in(‘A’.1. Allenis worcking for the dept deptno for the manager 7698 as SALESMAN with a salary Rs. 3000 since 87 Mr. 950 since 81 Mr.Kingis worcking for the dept deptno for the manager as PRESIDENT with a salary Rs. EMP_DETAILS Mr. concat(' for the manager '. 800 since 80 Mr.substr(hiredate. concat('is worcking for the dept '. concat(mgr.8))))))))))) ) emp_details from em. select concat('Mr. 1250 since 81 Mr.smith is working with the dept 10 for the manager(mgrno) with a salary Rs.Wardis worcking for the dept deptno for the manager 7698 as SALESMAN with a salary Rs. 2975 since 81 Mr. 2850 since 81 Mr. 1500 since 81 Mr. Mr. concat(' as '.Milleris worcking for the dept deptno for the manager 7782 as CLERK with a salary .25 - .Jonesis working for the dept deptno for the manager 7839 as MANAGER with a salary Rs.Scottis worcking for the dept deptno for the manager 7566 as ANALYST with a salary Rs.Jamesis worcking for the dept deptno for the manager 7698 as CLERK with a salary Rs. concat(upper(job). 2450 since 81 Mr.Martinis working for the dept deptno for the manager 7698 as SALESMAN with a salary Rs.JAMES FORD MILLER 8 rows selected 41)Display details of all emp’s of following formate.Turneris worcking for the dept deptno for the manager 7698 as SALESMAN with a salary Rs. 1100 since 87 Mr. concat(initcap(ename).Smithis working for the dept deptno for the manager 7902 as CLERK with a salary Rs. 1250 since 81 Mr. concat(' since '. concat(' with a salary Rs.Fordis worcking for the dept deptno for the manager 7566 as ANALYST with a salary Rs. 5000 since 81 Mr.Clarkis worcking for the dept deptno for the manager 7839 as MANAGER with a salary Rs. concat(sal.Adamsis worcking for the dept deptno for the manager 7788 as CLERK with a salary Rs. 3000 since 81 Mr.'. concat('deptno'.Blakeis working for the dept deptno for the manager 7839 as MANAGER with a salary Rs.1250 since 81. '. 1600 since 81 Mr. VSIZE(123456) 4 VSIZE(TO_CHAR(123456)) 6 Select to_char(123456. Select vsize(123456). TO_CHAR(SY 01/10/2009 .00 Select to_char(sysdate) from dual.’format’]) Select to-char(1234) from dual. TO_CHAR(S 10-JAN-09 Select to_char(sysdate. Vsize():. Data conversion function 1) IMPLECIT CONVERCTION: when oracle convert the data in one format to other then it is called as implicit converction 2) EXPLICIT CONVERCTION:these are three types.26 - . TO_CHAR(1234 123456. • This function accept any data type required sing paramitor.’mm/dd/yyyy’) from dual..’FORMATE]) To_char(date[.vsize(to_char(123456)) from dual.99’) from dual. • To covert date into character. • To_char() • To_ number() • To_date() To_char():• To convert number into character.Rs. Formate: To_char(number[.virtual memory function • This function is to maser the memory in bytes. 42)encrypt all the employees names with special chares.’9999999. 1300 since 82 14 rows selected. Select months_between(sysdate.00’. TO_NUMBER('123') 123 Select to_number(‘123456.To number():Select to_number(‘123’) from dual.'09-mar-2008')) from dual.00'.date) • It gives the difference between the months.99) from dual. Select add_months(sysdate.number) • Add no.’09-jan-2000’) from dual.999999.999999. To_date:Select to_date(‘10123495’.'09-MAR-2008')) 98 Add_months(date.99) from dual.99) 123456 Select to_number(‘123456’.27 - .of months to given date. TO_DATE(' 12-OCT-95 Date function:Months _between(date. ORA-00939: too many arguments for function (it gives error).999999. TO_NUMBER('123456. ABS(MONTHS_BETWEEN('09-JAN-2000'.045612 Select abs(months_between('09-jan-2000'. • Year must be -4713 and +9999 butnot ‘0’(zero).’mm/dd/yyyy’) from dual. MONTHS_BETWEEN(SYSDATE.'09-JAN-2000') 108.12) from dual. ADD_MONTH 10-JAN-10 Last_day(date) . daily sal of all the salesman (as per days of month) 42) Select empno.sal. ORA-00932: inconsistent datatypes: expected NUMBER got DATE • Above last-day(sysdate) is returning a date which we are dividing with number so this we convert into number or char.sysdate.sal.sysdate.1.) after sys_date of the next week • This function returns the date which specified from sysdate.’tue’) from dual. NEXT_DAY( 13-JAN-09 Greatest(): Select greatest(’09-jan-2000’. (or) . Select greatest(sysdate.sal/to_number(substr(last_day(sysdate). GREATEST( 09-FEB-00 Select greatest(’09-feb-3000’. GREATEST('0 10-JAN-09 Display the empno . Select empno.'09-feb-3000'.sal.’30-mar-1200’. GREATEST('0 30-mar-1200 The above query(v(’09-jan-2000’) is not date it is characters. Select last_day('09-feb-2000') from dual.’23-apr-1980’) from dual. LAST_DAY( 29-FEB-00 Next_day(date. Select next_day(sysdate. name .28 - .2)) ds_sal from emp where job='SALESMAN'.• It returns the last day of month.sal/last_day(sysdate) ds-sal from emp where job=’SALESMAN’) .ename.ename.’09-jan-1200’) from dual. The first one parameter data type is the measure of other data type.mon….'09-jan-1200') from dual.’day’) • It returns first day(sun. 3225806 40.6451613 96.7741935 41.3225806 48.'dd') no_of_days_re from dual.1.3870968 43) Daily salary of previous month.sal.2)) ds_sal from emp where job='SALESMAN'.ename.sal Sal/(to_char(last_day(add_months(sysdate.'dd')total_days. Select empno. EMPNO ENAME 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER SAL 1600 1250 1250 1500 DS_SAL 51.'dd') .'dd') no_of_days_ex.9354839 14 rows selected 44) display total no.3225806 95.6129032 40.Select empno.29 - .3870968 35.sal/to_char(substr(last_day(sysdate).of remaining day in the month after current date. TO 31 10 NO NO_OF_DAYS_RE 21 45)find the total no.of days of any given year. 46) find the total no.ename. Least():- .0322581 96. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER SAL 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 DSAL 25. To_char(last_day(sysdate).6129032 40.of days of current year.’dd’))) d_sal from emp.8064516 51.to_char(sysdate.483871 30.290323 48.-1). Select to_char(last_day(sysdate).7741935 161.3225806 91. To_char(sysdate.9677419 40.9354839 79. .for day of the year .it week day in character from (Monday.'d dd ddd dy day') from dual.'MONMONTHMMR jan january 01 i I YEAR FORMATES: select to_char(sysdate.…) select to_char(sysdate. TO_CHAR(SYSDATE.for date .for day of week.DATE FORMATES: d dd ddd dy day .) .'DDDDDDDYDAY' 7 10 010 sat saturday MONTH FORMATES: select to_char(sysdate. TO_CHAR(SYSDATE. .'year') from dual.Tuesday.tue. TO_CHAR(SYSDA 9 09 009 2009 select to_char(sysdate.week day in character from (mon.'mon month mm rm Rm') from dual.'y yy yyy yyyy') from dual. TO_CHAR(SYSDATE.'YEAR') two thousand nine .30 - . b.'am pm') from dual.week of the month.d.select to_char(sysdate. TO_CHAR(SYSDATE ad ad a.'ddth') from dual.quarter of the year.'YEAR') from dual. TO_CHAR(SYSDATE.'HHH 11 11 11 27 04 41224 Select to_char(sysdate. TO_CHAR(SYSDATE. ORA-01722: invalid number Note: we not able to convert int char to character.'hh hh12 hh24 mi ss sssss') from dual.'YEAR') TWO THOUSAND NINE TIME FORMATES: select to_char(sysdate. (or) Select to_char(sysdate.'am pm') from dual. -. W WW Q J Iw -.31 - .d. Select to_char(to_date('09-jan-2100'). TO_CHAR(SYSDAT tenth Week format.c.’ for this we must give the dots(. TO_C 10th Select to_char(sysdate. a. TO_CHAR(SYSD ten TH: Select to_char(sysdate.'ad bc a. -- .c.'ddspth') from dual. -. TO_CH am am Select to_char(sysdate. Note: ‘a. TO_CH am am Select to_char(‘09-jan-2100’) from dual.'ddthsp') from dual. SP: spelling Select to_char(sysdate.) at end.d.week of the year.') from dual.d. b.it gives the number of days between 1-jan-4413 BC from current date. -.'ddsp') from dual. 1988).'DAY')||'THE'||TO_CHAR(SYSDATE.'j') from dual.' ddth ')||' of '|| to_char(sysdate. TO_CHAR(TO_DATE('25-DEC 52 9 09 009 2009 20099 47)display current date in folling formate. select to_char(sysdate.'q') from dual. Saturday 10th January 2009 ad.'iw i iy iyy iyyy iyyyy') from dual. TO_CHAR(SYSDATE. TO_CHAR(TO_DATE('25-DEC-2009').'DAY')||TO_CHAR(SYS saturday 10th january 2009 ad 48)display current date in folling formate: saturday the 10th of january 2009 12:32::42 select to_char(sysdate.' ddth ')|| to_char(sysdate.'w ww') from dual.'q') in (1.' month yyyy hh24:mi::ss') from dual.'J')/365 6726.'day')|| to_char(sysdate. saturday the 10th of january 2009 12:32::42 49)display all the emp’s 1 st and 4th quarter of 1981 and 1983. T 4 Select to_char(to_date('25-dec-2009').4) and to_char(hiredate.32 - .Select to_char(to_date('25-dec-2009').55068 Select to_char(to_date('25-dec-2009'). S select * from emp where to_char(hiredate.' month ')|| to_char(sysdate.' yyyy bc ') from dual.'yyyy')in(1981. EMPNO ENAME JOB 7499 ALLEN SALESMAN 7521 WARD SALESMAN 7839 KING PRESIDENT MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 17-NOV-81 SAL COMM DEPTNO 1600 300 30 1250 500 30 5000 10 . TO_C 4 52 Select to_char(to_date('25-dec-2009'). TO_CHAR(SYSDATE.'day')||'the'|| to_char(sysdate. In week strings ‘Wednesday’ have the highest length so other week string are trimmed.’dy’)=’SUN’.'fmday')='sunday'. EMPNO ENAME JOB 7521 WARD SALESMAN 7788 SCOTT ANALYST MGR HIREDATE 7698 22-FEB-81 7566 19-APR-87 SAL COMM DEPTNO 1250 500 30 3000 20 FOREIGN KEY(reference constraint) Reference constraints is to define a relationship between the columns of different tables and even between the columns of same table(self reference).33 - .list all the employees joined on Sunday. • Reference constraints is defines the column as a foreign key column. Note:option it refers to unique dey column.7900 JAMES 7902 FORD CLERK ANALYST 7698 03-DEC-81 7566 03-DEC-81 950 3000 30 20 50). • Reference constraints define the table as a dependent table or in child table or detailed table. Select * from emp Where to_char(hiredate. • Reference constraint is to implement normalization. no rows selected Note: hear ‘dy’ not take as a ‘SUNDAY’…it takes as ‘SUNDAY---‘. Select * from emp Where to_char(hiredate. So we do like this… Select * from emp Where rtrim(to_char(hiredate.Full Mode. EMPNO ENAME JOB 7521 WARD SALESMAN 7788 SCOTT ANALYST MGR HIREDATE 7698 22-FEB-81 7566 19-APR-87 SAL COMM DEPTNO 1250 500 30 3000 20 Also we use FM:. The table to which the foreign key referring is called master table.'fmday'))='sunday'. • The clause those used along with reference constraints are i) Foreign key ii) On delete cascade iii) On delete set null . • A foreign key always refers to the primary key of some other table or in some table. job varchar2(10) not null.34 - . deptno number(2) references dept on delete cascade ). insert into emp2 values(2. . On delete cascade: it is a permission to oracle to delete the data from master table those it is having dependent information. insert into dept1 values(30. Oracle delete both master and child data for specified master row.4000. sal number(7) not null.'raJU'. dname varchar2(10) not null. on delete set null: it is also a permission or a pre-Image to oracle to delete master record without deleting it’s corresponding child record .'CLEARK'.but it update all the foreign key value of the delete master recodes to nulls create table dept1 ( deptno number(2) primary key. DEPTNO 10 acc 20 acc 30 fin DNAME hyd hyd ban LOC create table emp2 ( empno number(2) primary key. loc varchar2(10) not null ).'fin'.i) ii) iii) foreign key : it is optional in the case of column level constraints but it mandate for table level constraint.'ban'). ename varchar2(10) not null. EMPNO ENAME 1 ravi 2 raJU SAL JOB 3000 CLEARK 4000 CLEARK DEPTNO 10 20 Page no 41 to 51 pending …………….20). TABLES Emp: EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected.35 - . JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 2975 20 1250 1400 30 2850 30 2450 10 3000 20 5000 10 1500 0 30 1100 20 950 30 3000 20 1300 10 . Note: the above query os based on equvi join.dept where emp. Select dept. • The above query is not based on no join bcz the quary is not based on single table.36 - .deptno=emp. . Select * from emp where ename=’JAMES’. • Hear we not comparing colums of bouth the tables.loc from emp e.deptno. Display the location where james is working.dept where ename=’JAMES’.loc from emp.dept d where e.deptno.ename=’JEMS’ and d. Note: the above query based on ‘equvi join. LOC CHICAGO Elias: Select d.ename=’JAMES’ and dept.Dept : DEPTNO DNAME 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS LOC NEW YORK DALLAS CHICAGO BOSTON Salgrad: GRADE 1 2 3 4 5 LOSAL 700 1201 1401 2001 3001 HISAL 1200 1400 2000 3000 9999 JOINS Display the details of the jems.deptno=e. Select loc from emp. job='SALESMAN'and d.047789 839. EMPNO ENAME 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER JOB SALESMAN SALESMAN SALESMAN SALESMAN MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 SAL COMM DEPTNO 1600 300 30 1250 500 30 1250 1400 30 1500 0 30 53)display the all the employees along with location and experience of all employees.deptno.* from emp e. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER SAL COMM DEPTNO LOC 800 20 DALLAS 1600 300 30 CHICAGO 1250 500 30 CHICAGO 2975 20 DALLAS 1250 1400 30 CHICAGO 2850 30 CHICAGO NEW MANAGER 7839 09-JUN-81 2450 10 YORK ANALYST 7566 19-APR-87 3000 20 DALLAS NEW PRESIDENT 17-NOV-81 5000 10 YORK MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 EXP 854.loc.714456 845.51). EMPNO ENAME 7499 ALLEN 7521 WARD 7654 MARTIN 7698 BLAKE 7844 TURNER 7900 JAMES JOB SALESMAN SALESMAN SALESMAN MANAGER SALESMAN CLERK MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7839 01-MAY-81 7698 08-SEP-81 7698 03-DEC-81 SAL COMM DEPTNO 1600 300 30 1250 500 30 1250 1400 30 2850 30 1500 0 30 950 30 6 rows selected.deptno.464456 830.dname='SALES' and d. 52) display the all the salesman’s of sales deportment.* from emp e.deptno.diplay all the emps of sales deportment.deptno=e.547789 843.464456 826.(sysdate-e. Select e.dept d where d.dept d where d.37 - .dname='SALES'and e.deptno=e.381123 .hiredate)/12 exp from emp e.deptno=e.797789 661.*. Select e. Select e.d.297789 848.dept d where d.881123 848. job in ('MANAGER'. select e. EMPNO ENAME JOB 7782 CLARK MANAGER MGR HIREDATE 7839 09-JUN-81 SAL COMM 2450 DEPTNO 10 55) display the details of all the employees along with their grades. salgrade s where e.sal between s.s.hisal.d. EMPNO ENAME 7369 SMITH 7876 ADAMS 7900 JAMES 7521 WARD 7654 MARTIN 7934 MILLER 7499 ALLEN JOB CLERK CLERK CLERK SALESMAN SALESMAN CLERK SALESMAN MGR HIREDATE 7902 17-DEC-80 7788 23-MAY-87 7698 03-DEC-81 7698 22-FEB-81 7698 28-SEP-81 7782 23-JAN-82 7698 20-FEB-81 SAL COMM DEPTNO GRADE 800 20 1 1100 20 1 950 30 1 1250 500 30 2 1250 1400 30 2 1300 10 2 1600 300 30 3 .sal.* from emp e.loc.dname in('ACCOUNTING'.*.797789 14 rows selected. select e.dept d where e.047789 820. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7782 CLARK MANAGER 7839 09-JUN-81 2450 7698 BLAKE MANAGER 7839 01-MAY-81 2850 7566 JONES MANAGER 7839 02-APR-81 2975 LOC EXP NEW 10 839.048862 20 DALLAS 845.deptno.dept d where e.214456 658.7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD SALESMAN CLERK CLERK ANALYST 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 1500 1100 950 3000 0 7934 MILLER CLERK 7782 23-JAN-82 1300 30 CHICAGO 20 DALLAS 30 CHICAGO 20 DALLAS NEW 10 YORK 832.hiredate)/12 exp from emp e.e. 53)display the managers of all the location in the descending order of their salaries.job='MANAGER'and d.38 - .comm is null or e.losal and s.(sysdate-e.deptno=d.'ANALYST') and d.grade from emp e.465529 54) Display all managers and analyst of both accounting and research deportment having a experience when it from 27 to 29 years.'RESERCH') and (e.comm=0) and months_between(sysdate.deptno order by e.047789 825.*.798862 YORK 30 CHICAGO 843. not receiving any commission but working for some manager.631123 825. Select e.hiredate)/12 between 27 and 29 and e.deptno=e. s.grade=2 and to_char(e.'*****') grade from emp e.losal and s.39 - .sal between s.losal and s.sal. EMPNO ENAME 7369 SMITH 7876 ADAMS 7900 JAMES 7521 WARD 7654 MARTIN 7934 MILLER 7499 ALLEN 7844 TURNER 7566 JONES 7698 BLAKE 7782 CLARK 7788 SCOTT SAL 800 * 1100 * 950 * 1250 ** 1250 ** 1300 ** 1600 *** 1500 *** 2975 **** 2850 **** 2450 **** 3000 **** GRADE .job='MANAGER'and s.daily salary grade in terms of *’s location of all the employees.losal and s.empno.'**'.2.grade.4) and e.hisal.ename.1.*.sal between s.deptno=d. EMPNO ENAME JOB 7521 WARD SALESMAN 7654 MARTIN SALESMAN MGR HIREDATE 7698 22-FEB-81 7698 28-SEP-81 SAL COMM DEPTNO 1250 500 30 1250 1400 30 58) display employes number and name salary. select e.'***'. select e.7844 TURNER 7566 JONES 7698 BLAKE 7782 CLARK 7788 SCOTT 7902 FORD 7839 KING SALESMAN MANAGER MANAGER MANAGER ANALYST ANALYST PRESIDENT 7698 08-SEP-81 7839 02-APR-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 1500 2975 2850 2450 3000 3000 5000 0 30 20 30 10 20 20 10 3 4 4 4 4 4 5 14 rows selected.5.e.hisal.e.* from emp e. 56)display all the grade 3 and 4 managers. EMPNO ENAME JOB MGR HIREDATE 7566 JONES MANAGER 7839 02-APR-81 7698 BLAKE MANAGER 7839 01-MAY-81 7782 CLARK MANAGER 7839 09-JUN-81 SAL COMM DEPTNO GRADE 2975 20 4 2850 30 4 2450 10 4 57)display all grade 2 employees of 1981.sal between s.dept d.3.salgrade s where e.salgrade s where e.deptno and e.grade in(3.hiredate.'****'. select e.hisal.'yyyy')=1981 and e.grade from emp e.'*'.salgrade s where s.4.decode(s. sal between s. EMPNO ENAME 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER 7499 ALLEN 7521 WARD 7654 MARTIN SAL 1600 1250 1250 1500 1600 1250 1250 GRADE 1 1 1 1 2 2 2 DEPTNO 30 30 30 30 30 30 30 .s.4) and d.5)and d.hisal).dept d where s.hisal.grade deptname of all emps those how belongs to first three grads and belongs to sales departments receiving some commission or those how belongs to grades 4 and 5 working with location with NEW YORK with annual salary more then 30000 without commission working for some manager whose number is 3rd digit should not be divisible by 2 or 4.salgrade s Where ( s.40 - .grade in (5.salgrade s.comm<>0) ) --condition 2-( (s.deptno and e.mgr.deptno from emp e.sal.name.eno.name='SALES'AND (e. Select e.grade.grade in (4.loc='NEW YORK') e.1).comm is null or e.2)<>0 or mod (substr(e.3.comm=0) e.deptno and e.name.comm is not null or e. EMPNO ENAME JOB 7782 CLARK MANAGER 7839 KING PRESIDENT MGR HIREDATE 7839 09-JUN-81 17-NOV-81 SAL COMM 2450 5000 DEPTNO 10 10 60)display empno.deptno=d.4)<>0 ) ) and (e.dname in('ACCOUNTING'.7902 FORD 7839 KING 3000 **** 59)display all the grade of 4 and 5 employees of accounting and readerch department select e.sal between s.2.losal and s.* from emp e.losal and s.mgr is not null and ( mod (substr(e.'RESERCH') and e.deptno=d.sal.grade in (1.e.mgr.e.e.3.sal*12>3000 and (e.1).dept d.3) and d. ename=’JEMES’ and a.ename='BLAKE'.sal. Select a. Select a.emp b where a. EMPNO ENAME JOB 7566 JONES MANAGER 7782 CLARK MANAGER 7698 BLAKE MANAGER MGR HIREDATE 7839 02-APR-81 7839 09-JUN-81 7839 01-MAY-81 SAL COMM 2975 2450 2850 DEPTNO 20 10 30 .emp b where b.sal>b.41 - . JOB SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK ANALYST CLERK MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7566 03-DEC-81 7782 23-JAN-82 SAL COMM DEPTNO 1600 300 30 1250 500 30 2975 20 1250 1400 30 2850 30 2450 10 3000 20 5000 10 1500 0 30 1100 20 3000 20 1300 10 62) display the details of all the employees hose job is same as BLACK. This we will use when we have to retrieve row’s from column where checking condition on same column.7844 TURNER 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER 7782 CLARK 13 rows selected. EMPNO ENAME 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7902 FORD 7934 MILLER 12 rows selected. 1500 1600 1250 1250 1500 2450 2 3 3 3 3 4 30 30 30 30 30 10 SELF J0IN:• • It is for join objects of same table.* from emp a.* from emp a. • Select data table and the table checking condition must be different. 61) display details of the employees hose salary is more then JEMS salary.job and b.job=b. • Join condition must be on comparing column. deptno).* from emp a.mgr=b.deptno=d.deptname of all the employees. Select emp e inner join dept d on (e.sal. JOB MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER MGR HIREDATE 7839 01-MAY-81 7839 01-MAY-81 7839 01-MAY-81 7839 02-APR-81 7839 01-MAY-81 7839 01-MAY-81 7839 02-APR-81 7839 09-JUN-81 SAL COMM 2850 2850 2850 2975 2850 2850 2975 2450 DEPTNO 30 30 30 20 30 30 20 10 NEW MODELS FOR JIONS: Display the employees number. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7844 TURNER 8 rows selected JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER SALESMAN MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7698 08-SEP-81 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 2975 20 1250 1400 30 2850 30 2450 10 1500 0 30 65) Display the details of all the emp’s who are senior of their own MANAGER’S .hiredate <a. Select b.empno and b. select b.42 - .emp b where a.64) Display the details of all the emp’s how are senior to to president.job=’PRESIDENT’ and b.job=’MANAGER’. . EMPNO ENAME 7698 BLAKE 7698 BLAKE 7698 BLAKE 7566 JONES 7698 BLAKE 7698 BLAKE 7566 JONES 7782 CLARK 8 rows selected.hiredate. EMPNO ENAME CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK JOB MGR HIREDATE SAL 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 0 1400 300 500 COMM DEPTNO 20 30 30 20 30 30 10 20 10 30 20 30 20 10 DEPTNO 30 SALES 30 SALES 20 RESEARCH 30 SALES 30 SALES 10 ACCOUNTING 20 RESEARCH 10 ACCOUNTING 30 SALES 20 RESEARCH 30 SALES 20 RESEARCH 10 ACCOUNTING DNAME 20 RESEARCH DALLAS CHICAGO CHICAGO DALLAS CHICAGO CHICAGO NEW YORK DALLAS NEW YORK CHICAGO DALLAS CHICAGO DALLAS NEW YORK LOC 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 Note: • Hear default Is inner join.emp b where a.name.* from emp a. Select e. EMPNO ENAME 7369 SMITH 7876 ADAMS 7900 JAMES 7521 WARD 7654 MARTIN 7934 MILLER 7499 ALLEN 7844 TURNER 7566 JONES 7698 BLAKE 7782 CLARK 7788 SCOTT 7902 FORD 7839 KING SAL 800 1100 950 1250 1250 1300 1600 1500 2975 2850 2450 3000 3000 5000 GRADE 1 1 1 2 2 2 3 3 4 4 4 4 4 5 • Display the salesmans of the sales dept in the desc order of there salaries.losal and s.grade.Natural join automatically define join condition on the common column of the table.43 - .empno. Select e.grade.e. Display the enum.sal.e.grade from emp e inner join salgrade s on (e.hisal).losal and s. hisal). • Select deptno from emp e join dept. EMPNO ENAME 7369 SMITH SAL 800 GRADE 1 DALLAS LOC .e.sal.sal.ename.dname='SALES' order by e.e. Using clause is useful to put you’r subjection to oracle to indicate to which column the join to be defind.sal between s. • The table name qualifier is not allowed for joining column.deptno=d.ename.sal desc.sal.sal between s.s.*. • No separate join condition is allowed.dname from emp e inner join dept d on (e.ename.d.loc of the all the emp’s.deptno) where e.d.empno.grade of all the emp’s’ select e.d using (deptno).ename. EMPNO ENAME 7499 ALLEN 7844 TURNER 7521 WARD 7654 MARTIN JOB MGR HIREDATE SALESMAN 7698 20-FEB-81 SALESMAN 7698 08-SEP-81 SALESMAN 7698 22-FEB-81 SALESMAN 7698 28-SEP-81 SAL COMM DEPTNO DNAME 1600 300 30 SALES 1500 0 30 SALES 1250 500 30 SALES 1250 1400 30 SALES Display the empno.loc from (emp e inner join dept d on(e.deptno)) inner join salgrade s on(e.deptno=d.s.job='SALESMAN'and d. then Q2 is called as sub query Q1 is main query.d.losal and hisal)) where s.job='MANAGER' AND d.and that codiction gers the input from condition gets from the same table based on another query (Q2).44 - .*.loc in('NEW YORK'. .7876 ADAMS 7900 JAMES 7521 WARD 7654 MARTIN 7934 MILLER 7499 ALLEN 7844 TURNER 7566 JONES 7698 BLAKE 7782 CLARK 7788 SCOTT 7902 FORD 7839 KING 1100 950 1250 1250 1300 1600 1500 2975 2850 2450 3000 3000 5000 1 DALLAS 1 CHICAGO 2 CHICAGO 2 CHICAGO 2 NEW YORK 3 CHICAGO 3 CHICAGO 4 DALLAS 4 CHICAGO 4 NEW YORK 4 DALLAS 4 DALLAS 5 NEW YORK Display all the managers of grade 4 working for NEWYARK. As if query (Q1) retrieve the data from a table based on some condition .'CHICAGO').deptno) inner join salgrade s on(e.grade. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO GRADE 20 20 30 30 10 10 7566 JONES MANAGER 7839 02-APR-81 2975 7566 JONES MANAGER 7839 02-APR-81 2975 7698 BLAKE MANAGER 7839 01-MAY-81 2850 7698 BLAKE MANAGER 7839 01-MAY-81 2850 7782 CLARK MANAGER 7839 09-JUN-81 2450 7782 CLARK MANAGER 7839 09-JUN-81 2450 LOC NEW 4 YORK 4 CHICAGO NEW 4 YORK 4 CHICAGO NEW 4 YORK 4 CHICAGO SUB QUERY’S:• It is alternative way of defining self join. Maximum of 255 sub querys are one another can be defined always inner most sub-query executed first the propagation is always from inner most to the outer.deptno=d.loc from (emp e inner join dept d on(d. Always sub query afe enclosed within parenthesis always subquerys are executed first.CHICAGO. Select e.sal between s.grade =4 aND e.s. • Select * from emp where sal>(select sal from emp where ename=’JAMES’) . dept d where d.losal and s.job In ( select distinct e.dname in ('ACCOUNTING'.deptno=d. EMPNO ENAME 7782 CLARK JOB MGR HIREDAT SAL COMM DEPTNO DEPTNO E 10 10 DNAME LOC MANAGE 783909-JUN-81 2450 R ACCOUNTIN NEW G YORK 67) display the details of employees whose jobs are same as the jobs of RESEARCH deportment and restrict the details of the employees of research deportment from output.deptno.job from emp e.deptno ) and deptno<>(select deptno from dept where dname='RESEARCH').sal between s.dname='RESEARCH' and e.dept d where d. Select * from emp e.deptno=d. 66) Display details of all the emp’s of employees of ACCOUNTING dept table whose job is name as JONES job.45 - .Select * from emp where job=(select job from emp where ename=’BLAKE’).grade=2 and e.job=(select job from emp where ename ='JONES') and e.'SALES') and e. EMPNO ENAME 7934 MILLER 7900 JAMES 7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER JOB CLERK CLERK SALESMAN SALESMAN SALESMAN SALESMAN MGR HIREDATE 7782 23-JAN-82 7698 03-DEC-81 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 SAL COMM DEPTNO 1300 10 950 30 1600 300 30 1250 500 30 1250 1400 30 1500 0 30 . Select e.salgrade s where s. EMPNO ENAME 7934 MILLER 7900 JAMES 7782 CLARK 7698 BLAKE JOB CLERK CLERK MANAGER MANAGER MGR HIREDATE 7782 23-JAN-82 7698 03-DEC-81 7839 09-JUN-81 7839 01-MAY-81 SAL COMM 1300 950 2450 2850 DEPTNO 10 30 10 30 67) display employees details of employees of both ACCOUNTING and sales deportment where the employees of grade 4.* from emp e.hisal ) and e.deptno=d.job from emp e. Select * from emp Where job in ( select distinct e.dept d Where d.dname='ACCOUNTING' and e.deptno. MANGARS MANAGERS MANAGER is working. EMPNO ENAME JOB 7782 CLARK MANAGER 7839 KING PRESIDENT 7934 MILLER CLERK MGR HIREDATE 7839 09-JUN-81 17-NOV-81 7782 23-JAN-82 SAL COMM 2450 5000 1300 DEPTNO 10 10 10 69) diplay the details of all the employees whose salary is less then PRESIDENT salary and it should be more then salary of the employees 7900 employees and belongs to any department except FORD’s department and join after SMITH but before ADAMS.68) display details of all emp’s are those who belongs to deportment where ADAMS.46 - . Select * from emp Where deptno = ( select deptno from emp where empno=( select mgr from emp where empno= ( select mgr from emp where empno= (select mgr from emp where ename='ADAMS') ) ) ). >ALL  >5000 <ALL  <1300 >ANY <ANY  >1300  <5000 . ORA-01427: single-row subquery returns more than one row To solve above we use FILLERS. Select * from emp where ( ( select sal from emp where deptno=10)>(select sal from emp where deptno=20) ). To solve above query we will see this…… • Display the details of the all the employees deportment 10 whose salaries is more then any employees of department 20. 2450.min() 3.=ANY =ALL  =1300 OR =2400 OR =5000 IN(1300. JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 SAL 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 COMM 300 500 1400 DEPTNO 20 30 30 20 30 30 10 20 10 30 20 30 20 10 0 72) display details of all employees of FORUQ select * from emp where not exists(select * from emp where ename=’FORUQ’). Select * from emp where deptno in (10.5000)  =1300 OR =2400 OR =5000 select sal from emp where deptno=10 and sal> any(select sal from emp where deptno=20). GROUP FUNCTION: A function which process a set of rows at a time and returns a single value.30) and sal> all(select sal from emp where deptno=20). EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected. 70) select details of deportment 10 and 30 whose salaries are more then the salary of all the employees of deportment 20.max() 2. EMPNO ENAME JOB 7839 KING PRESIDENT MGR HIREDATE 17-NOV-81 SAL COMM 5000 DEPTNO 10 71) display the details of all the employees if there is any employees with a name ‘JEMES’ exist in database. Select * from emp where exists (select * from emp where ename='JAMES').sum() .47 - . 1. Select max(e.salgrade s s.avg() 5. Select * from emp Where sal=( select max(e.sal between s.one more time we checking for research deportment way bcz if not give that select statement select all the rows which have greatest sal of emp but we want only REASERCH dept.dept d where d.deptno) and deptno=(select deptno from dept where dname='RESEARCH'). EMPNO ENAME JOB 7788 SCOTT ANALYST 7902 FORD ANALYST MGR HIREDATE 7566 19-APR-87 7566 03-DEC-81 SAL COMM 3000 3000 DEPTNO 20 20 Note: hear in sub query gives max sal of RESEARCH deportment.48 - . All group functions are ignored null values.count() note:all the above functions required a single paramitor.dname='RESEARCH' and e.max(<col name>) 73) Display the junior most employee and display junior most employee hire date.min() • Is to find the minimum of column value. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO .sal) from emp e. 2. 76) Display the details of the senior most employees Select * from emp where hiredate=(select min(hiredate) from emp).grade=4 and e.4.losal and s.min and count function work on any data type but sum and avg work on only number data type. 1. MAX(E. Select max (hiredate) from emp. • Max.SAL) 3000 75) Display all the details of highest paid employees of ‘RESEARCH’ deportment. MAX(HIRED 23-MAY-87 74) Display the highest salary of grade ‘4’.deptno=d.hisal where s.hisal.sal) from emp e. it is not possible bcz we not convert number to char.6033152 21.'j'). Select (months_between(sysdate. 78) Find the total experiences of all the employees’ year how managers to others are.1651432 27.7369 SMITH CLERK 7902 17-DEC-80 800 20 77) display the lowest paid employee working under ‘BLAKE’.hiredate)/12) exp from emp where job=’MANAGER’. Select sum (months_between(sysdate. Select to_char(to_date(round(sum(months_between(sysdate.so using this we done above.CONVERT NUMBERS INTO WORDS. TOTAL_EXP one hundred fifty-nine In above query we are trying to convert the number into words.7081539 27.) Round is round the number bcz decimal number we cannot change into date. Display the total experience of all the mangers. .7431001 27.1194442 79).so hear we first convert number into date(hear also we not convert number into date it only available in junior level(‘j’).hiredate)/12) total_exp from emp Where empno in (select distinct mgr from emp). TOTAL_EXP 27.'jsp') total_exp from emp where empno in( select distinct mgr from emp).for this. Select * from emp Where sal = ( Select min (sal) from emp where mgr= (select empno from emp where ename=’BLAKE’) ) and Mgr= (select empno from emp where ename =’BLAKE’).49 - . EMPNO ENAME JOB 7900 JAMES CLERK MGR HIREDATE 7698 03-DEC-81 SAL COMM 950 DEPTNO 30 3 sum (<col name>) • Is to find the total all column values.hiredate)/12)).7887991 27. 4) Avg(<col name>) • is it find the avg of all column values 80)To display the avg of all the analyst. Select avg(sal) from emp where job=’ANALIST’; 81) Display the details of the all the employees hose salary is more then the avg sal of all the emp’s. Select * from emp where sal>(select avg(sal) from emp); EMPNO ENAME 7566 JONES 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7902 FORD 6 rows selected. JOB MANAGER MANAGER MANAGER ANALYST PRESIDENT ANALYST MGR HIREDATE 7839 02-APR-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7566 03-DEC-81 SAL COMM 2975 2850 2450 3000 5000 3000 DEPTNO 20 30 10 20 10 20 82) display the details of all the employees of accounting department,hose salary is more then the avg salary of accounting department employees. Select e.* from emp e,dept d where d.dname=’ACCOUNTING’and e.sal>(select avg(e.sal) from emp e); EMPNO ENAME 7566 JONES 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7902 FORD 6 rows selected. JOB MANAGER MANAGER MANAGER ANALYST PRESIDENT ANALYST MGR HIREDATE 7839 02-APR-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7566 03-DEC-81 SAL COMM 2975 2850 2450 3000 5000 3000 DEPTNO 20 30 10 20 10 20 5) COUNT (<col name>) • Is to count the number of rows in the given set or given column. 83) Count the number of tables in the emp table. Select count(empno) from emp; 84)display the number of employees join in 1981. Select count(empno) from emp where to_char(hiredate,’yyyy’)=1981 COUNT(EMPNO) 10 - 50 - 85) count the number of employees belongs to grade 2. Select count(*) from emp e,salgrade s where s.grade=2 and e.sal between s.losal and s.hisal; COUNT(*) 3 GORUP BY CLAUSE: Select [distinct] <col>,<col>,… From <tab>,<tab>,….. [where <condation>] GROUP BY <col>,<col>,… [order by <col>,[asc/desc],<col>…] This clause tell oracle to group rows based on distinct values that exist for specified columns i.e creats a data set, containing several sets of record sets,and not on individual records. Restrictions: 1) the columns those are specified is the group clause are only allowed in the select statement . 2) columns other then the columns specified in the group by can also be used in the select but along with group functions (for summery result). ROLLUP: it is used to give total number of columns. CUBE : it is used to give total number of rows. 86) display the number of employees belongs to each year. Select to_char(hiredate,’yyyy’) year, count(*) no_of_emps from emp GROUP BY to_char(hiredate,’yyyy’); • - 51 - 87) display max,min salary of each location grade wise.. select d.loc,s.grade,max(e.sal),min(e.sal) from emp e,dept d, salgrade s where d.deptno=e.deptno and e.sal between s.losal and s.hisal group by d.loc,s.grade; LOC DALLAS DALLAS CHICAGO CHICAGO CHICAGO CHICAGO NEW YORK NEW YORK NEW YORK 9 rows selected. GRADE 1 4 1 2 3 4 2 4 5 MAX(E.SAL) 1100 3000 950 1250 1600 2850 1300 2450 5000 MIN(E.SAL) 800 2975 950 1250 1500 2850 1300 2450 5000 88) display number of Clark’s working for each deportment. Select deptno,count(*) from emp Where job='CLERK' group by deptno; DEPTNO 10 20 30 COUNT(*) 1 2 1 89) select to_char(hiredate,'yyyy') year, to_char(hiredate,'mm') month,count(*) no_of_emps from emp group by rollup(to_char(hiredate,'yyyy'), to_char(hiredate,'mm')); YEAR 1980 1980 1981 1981 1981 1981 1981 1981 1981 1981 1982 1982 1987 1987 YEAR 12 02 04 05 06 09 11 12 01 04 05 MO NO_OF_EMPS MO NO_OF_EMPS 1 1 2 1 1 1 2 1 2 10 1 1 1 1 - 52 - DEPTNO 20 30 NO_OF_EMPS 5 6 91)display grade to which atleast 3 employees belong to … select s.grade having count(*) >=3.sal between s.deptno.1987 2 14 16 rows selected.losal and s.count(*) no_of_emps From emp Group by deptno Having count(*)>=5.…-----------1 [where <condiction>]-----------2 group by <condiction>---------3 HAVING <condiction>---------4 [order by <col> [asc/desc]]---6 • • having clause is used only along with group by clause.slagrade s Where e. Select e.s.hisal group by s.salgrade s where e.53 - .grade.deptno.s.losal and s. 90) display the deportment where at least 5 emp’s are working.count(*) no_of_emps from emp e. HAVING CLAUSE: Select [distinct] <col>.grade . Having clause is to check the condition on individual groups defined by group by clsuse.<tab>. GRADE 1 2 4 NO_OF_EMPS 3 3 5 92) display the count of the employees belongs to grade In each deportment where it is more then 2.grade Having count(*)>2 . Select deptno.sal between s. <col>.hisal group by e.……----5 From <tab>.count(*) From emp e. DEPTNO 30 COUNT(*) 6 94)display the details of managers fro whom max employees rae working .66667 2175 2916.dept d where d.dept d Where d.54 - . LOC CHICAGO DALLAS NEW YORK AVG(E.deptno=e. Select d.deptno=e.dept d where d.loc from emp e.deptno group by d. EMPNO ENAME JOB 7698 BLAKE MANAGER MGR HIREDATE 7839 01-MAY-81 SAL COMM 2850 DEPTNO 30 95)display location where lowest avg salary is paid.sal) from emp e. Select deptno.loc).count(*) from emp Group by deptno Having count(deptno)=(select max(count(deptno)) from emp group by deptno).SAL) 1566.loc .loc Having avg(e.deptno=e.deptno group by d.66667 97)details of highest paid employee select * from emp where sal=(select max(sal) from emp). select * from emp where empno=( select mgr from emp group by mgr having count(*)=(select max(count(*)) from emp group by mgr)). EMPNO ENAME JOB 7839 KING PRESIDENT MGR HIREDATE 17-NOV-81 SAL COMM 5000 DEPTNO 10 98) if the number of emp’s dept 30 are more the 5 displays all those emp’s other than don’t display. Select * from emp .DEPTNO 20 GRADE 4 COUNT(*) 3 93)display deportment where maximum number of employees are working. LOC CHICAGO 96) avg salary based on grades select d.avg(e.sal)=(select min(avg(e.loc.deptno Group by d.sal)) from emp e. sal) 100)display lower paid employess of each location select e. EMPNO ENAME 7499 ALLEN 7521 WARD 7654 MARTIN 7698 BLAKE 7844 TURNER 7900 JAMES 6 rows selected.loc.dept d where d.*.sal) from emp e.deptno=d.deptno. JOB SALESMAN SALESMAN SALESMAN MANAGER SALESMAN CLERK MGR HIREDATE 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7839 01-MAY-81 7698 08-SEP-81 7698 03-DEC-81 SAL COMM DEPTNO 1600 300 30 1250 500 30 1250 1400 30 2850 30 1500 0 30 950 30 99)Display details of highest paid employees of each deportment select * from emp where (deptno.deptno=e. EMPNO ENAME 7839 KING 7788 SCOTT 7902 FORD 7698 BLAKE JOB PRESIDENT ANALYST ANALYST MANAGER MGR HIREDATE 17-NOV-81 7566 19-APR-87 7566 03-DEC-81 7839 01-MAY-81 SAL COMM 5000 3000 3000 2850 DEPTNO 10 20 20 30 • It is a condition correct when deptno..55 - .dept d where (d.min(e.deptno.e.sal) from emp e.loc) and e.sal) in (select to_char(hiredate. no rows selected Select * from emp Where deptno=30 and not exists (select deptno from emp where deptno=30 group by deptno having count(*)>6).sal) in(select d.d.sal) in ( select d.deptno=d.dept d where e.'yyyy')). EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO .'yyyy').deptno).deptno group by d.max(e.'yyyy').loc.loc from emp e.max(sal) from emp group by to_char(hiredate. select * from emp where (to_char (hiredate.sal both set match with in(deptno.deptno group by d. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO LOC 7369 SMITH CLERK 7902 17-DEC-80 800 20 DALLAS 7900 JAMES CLERK 7698 03-DEC-81 950 30 CHICAGO 7934 MILLER CLERK 7782 23-JAN-82 1300 10 NEW YORK 101) display the highest paid employees of each year.Where deptno=30 and exists (select deptno from emp where deptno=30 group by deptno having count(*)>6). loc and e.deptno) and e.deptno=x.hiredate)/12 >= . select e.7369 SMITH 7839 KING 7934 MILLER 7788 SCOTT CLERK PRESIDENT CLERK ANALYST 7902 17-DEC-80 17-NOV-81 7782 23-JAN-82 7566 19-APR-87 800 5000 1300 3000 20 10 10 20 CO RELATED SUBQUERY: 102)display details of employees hose salary is more then avg sal of there won deportment.deptno).e.salgrade s where months_between(sysdate.loc=x.56 - .dept y where y. Select * from emp x Where sal>(select avg(sal) from emp y where y.loc from emp e.deptno.x.deptno=x.deptno) EMPNO ENAME 7499 ALLEN 7566 JONES 7698 BLAKE 7788 SCOTT 7839 KING 7902 FORD 6 rows selected. JOB SALESMAN MANAGER MANAGER ANALYST PRESIDENT ANALYST MGR HIREDATE 7698 20-FEB-81 7839 02-APR-81 7839 01-MAY-81 7566 19-APR-87 17-NOV-81 7566 03-DEC-81 SAL COMM DEPTNO 1600 300 30 2975 20 2850 30 3000 20 5000 10 3000 20 103)Display the details of all the employees hose salary is less the avg salary of there job Select * from emp x where sal=(select max(sal) from emp y where y.sal>=(select max(e. EMPNO ENAME 7698 BLAKE 7788 SCOTT 7839 KING 7902 FORD JOB MANAGER ANALYST PRESIDENT ANALYST MGR HIREDATE 7839 01-MAY-81 7566 19-APR-87 17-NOV-81 7566 03-DEC-81 SAL COMM DEPTNO LOC 2850 30 CHICAGO 3000 20 DALLAS 5000 10 NEW YORK 3000 20 DALLAS 105) display the details of employees whose experience is more then the employees of their won grade.*.dept x where e.deptno=y.deptno=x. EMPNO ENAME 7698 BLAKE 7788 SCOTT 7839 KING 7902 FORD JOB MANAGER ANALYST PRESIDENT ANALYST MGR HIREDATE 7839 01-MAY-81 7566 19-APR-87 17-NOV-81 7566 03-DEC-81 SAL COMM 2850 3000 5000 3000 DEPTNO 30 20 10 20 104)Display employees whose salary is more then of their won location select e.sal) from emp e.* from emp e. hiredate)/12) from emp e.* from emp e.hisal.grade=s. Select e. emp m where e.empno and e.mgr = m. Select sal.hiredate<m.hiredate.57 - .densc_rank() over(order by sal desc) from emp.losal and g.dense_rank() (order by sal desc) rank from emp) where rank=&rank.sal between s. Select * from (select sal.grade and e. Select * from emp e Where (select count(distinct sal) from emp s where s. JOB CLERK SALESMAN SALESMAN MANAGER MANAGER MANAGER MGR HIREDATE 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7839 01-MAY-81 7839 09-JUN-81 SAL COMM DEPTNO 800 20 1600 300 30 1250 500 30 2975 20 2850 30 2450 10 107)display 2nd highest salary.losal and s.hisal) and e.sal between s. EMPNO ENAME 7369 SMITH 7521 WARD 7499 ALLEN 7566 JONES 7839 KING JOB CLERK SALESMAN SALESMAN MANAGER PRESIDENT MGR HIREDATE 7902 17-DEC-80 7698 22-FEB-81 7698 20-FEB-81 7839 02-APR-81 17-NOV-81 SAL COMM DEPTNO 800 20 1250 500 30 1600 300 30 2975 20 5000 10 106) display the details all the employees where all senior to there own managers.salgrade g where g. EMPNO ENAME 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7698 BLAKE 7782 CLARK 6 rows selected.(select max(months_between(sysdate.sal from(select distinct sal from emp order by 1 desc) where rank=&rank) . Select * from (select rownum rank.sal)=2. Select * from emp where (select max(sal) from emp e where sal<(select max(sal) from emp)).sal >e. i.e the bit map index also be created in the joining column of the query. The index with a concatenated key concatenate key are composite key.it has max of 32 column ii)Row id. Heap table : Heap table is a table in which the table is stored in the order the way you enter. • Oracle 9i introduced a new type of is IOT .………………………Multiple updates…………………. Cordiality level: -duplication level types are i) High (B-tree preferred) ii) Normal Bit-map preferred) iii) Low (Bit-map preferred) If the table is transaction table or heap table B-tree is preferred.in which the data is stored based on function. Oracle 8i introduced a new type index called as bit map index. INDEX • • • • • • • • • Every index maintain two columns i) Key column: . Max 32 columns are allowed.58 - . concatenate key. The first column of the concatenated key is called priority column. 32 are allowed only for B-tree index. In bit-map max 30 colums are allowed for the comcatination key. In oracle 9i bit-map indexes are begin enhanced wit multi table concept. When a key column of index is built on or multiple column combination then it is called composite key. INDEX: Syntax: CREATE [unique] INDEX <index name>ON <table name> (<col>. 3)create an index on lower case emp names on employees . • These table are require explicit indexes • Every index occupy memory creates data intendency more no. 2)create an index on job column create index on emp(job). • Creation of unique indexes on primary key and unique key not allowed. 5) Create index on the hiredate column on descending column create index n_index on emp((hiredate) order by desc). . ERROR at line 1: ORA-00907: missing right parenthesis So we do like this: create index hiredate_ind on emp(hiredate desc). 1)Create an index on COMM column.hiredate)/12).In which data is stored in the sorted order of primary key value for IOT primary key is must. ERROR at line 1: ORA-01743: only pure functions can be indexed Note: sysdate is changed daily so we can’t create index in it. create index e_noind on emp(comm). • By default primary key column have unique indexes created by orcle.59 - .B in which data modification is common task. 4) Create index on experience of employees Create index l_ind on emp(months_between(sysdate.of indexes creates performance issu that grade performance. This B-tree indexes two types: i)non unique index: ii)unique index. 6)create composite index on deportment number and job columns in the ascending deportment number is descending. • Constants is a part of definition of table index is external object. • IOT’s are exclusively design for data ware has data Base and heep table mend for transaction D. <col>……). ERROR at line 1: ORA-02158: invalid CREATE INDEX option Note: index can’t use both desc and reverse.job desc). Cluster is created before insert the data into the cluster table. create cluster dnoclu(dno number(2)). CLUSTER Syntax: Create cluster <cluster name> (<colname> data type (width)). which is used for deferent tables. 10)create a bitmap index on the department number column of emp table. . create table tab1(dept number(2) primary key. Every cluster must require cluster index. A cluster is a definition of common column.dname varchar2(10))cluster dnoclu(dept). • • • • Create index <inde name> on cluster <cluster name>. Create index g_ind on salgrade (grade)reverse • It starts searching is done in B-tree from leaf node. Cluster is required for those tables are commonly used together using join condition. create bitmap index bm_deptno on emp(deptno).create index dept_ind on emp(deptno asc. 8)Create a reverse key index on the department column number of dept table on descending order create index raj on dept(deptno desc) reveres . VIEW .60 - . 9) create unique index on deptno on emp. 7). Create unique index dept_u_index on dept(deptno). Create reverse key index on grade column of sal grade table. empno.hiredate)/12 from emp. Create view cl_view as select * from emp where job=’CLACK’. ORA-00998: must name this expression with a column alias Note:All the columns of the views those are derived from arethematic expressions. column name aliases. name.sal.and experience of all the emp’s create view exp as select ename. create view exp as select ename.. .2) Type ii) Create a view to display the employee number.empno. psedo columns and function-based columns must required. that’s way it is a virtual table.2) NUMBER(2) NUMBER(7. salary and deptno of all the emp’s on the descending order of deptno employees. • It is vary versatile in nature because parametric mehanisum. iii).empno. i)Create a view to list all the clarks.(it store in compilation version). Create a view to list the employee no.61 - . • Register into disk.<cpl>.. Desc cl_view Name EMPNO ENAME JOB MGR HIREDATE COMM DEPTNO SALARY NOT NULL Null? NUMBER(4) VARCHAR2(10) VARCHAR2(9) NUMBER(4) DATE NUMBER(7.months_between(sysdate.• It is a logical representation.months_between(sysdate.hiredate)/12 exp from emp. Note: order by clause is invalid in view definition up to oracle 8i in 9i it is allowed. • Security. Syntax: Create view <view name> [<col>.deptno from emp order by deptno desc.] as select statement. View created. • It not use any memory to store the data. Create view raj As Select ename.ename. psudo coloumn and function.* updating is not allowed on the columns of the view those are derived from airhematic expression.loc. For data insertion: Above 2 rules And it is not possible to insert the data into view of the view is missing the mandatory columns of the database table.grade.hisal and e. Through view is violating the above rules.ename. Create view emp_raj As Select e.losal and s. • . v)create a view to list employees of deptno 20 create a view r_dept as select * from emp e where deptno=20 with check option.empno.salary.s.e.name. from emp e.sal between s. Alter view <view name> compile.d. We not built index on view. For data updating: Updating is not allowed more then one table . dept d Where e.iv)create a view to list empno.deptno=d. View trigger: Oracle 8i interduced view trigger concept which allowes the view to accept the DML operations to view.deptno.grade and location of all the employees.62 - . read only: it is only for see. salgrade s. Rules for DML on view: • • For data deletion: Data deletion is not possible if the view is derived from more then one data base table. Replace view <view name> as select statement… The above commend is use when the view is created on a table if the table Is deleted and again I created table with same name and definition by this command we retrieve the view. We create a view without a base table is called forced table is called forced table: Create force view <view name> As select statement… To know hole deffination: Select * from user_views where view_name=’view_name’. 3)create synonym for above synonym. . Syntax: Create [public] synonym <syn name> for <object name >. 1)create a synonym for emp table. A synonym is to providing security to data base object as well as providing convenience during data query. • Types of synonym: i)private synonym: private synonym is a synonym created by any valid user of database as well as by the DBS. Create public synonym emp for scott.emp. SYNONYM • • • Synonym is a data base object Synonym is alternate name is to the data object. ii)public synonym it is created only by the DBS. 2)create a synonym for view emp_details. Create synonym emp_d_syn for emp_details. Create synonym edj for emp_d_syn.63 - . Create synonym emp_sy for emp. 4)create public synonym for emp table. To drop: Drope <view> <view name>. .64 - . .65 - .
Copyright © 2024 DOKUMEN.SITE Inc.