Sie sind auf Seite 1von 9

SQL complete material:

SQL> SELECT * FROM EMP; EMPNO --------7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934 ENAME JOB ---------- --------SMITH CLERK ALLEN SALESMAN WARD SALESMAN JONES MANAGER MARTIN SALESMAN BLAKE MANAGER CLARK MANAGER SCOTT ANALYST KING PRESIDENT TURNER SALESMAN ADAMS CLERK JAMES CLERK FORD ANALYST MILLER CLERK MGR --------7902 7698 7698 7839 7698 7839 7839 7566 7698 7788 7698 7566 7782 HIREDATE --------17-DEC-80 20-FEB-81 22-FEB-81 02-APR-81 28-SEP-81 01-MAY-81 09-JUN-81 09-DEC-82 17-NOV-81 08-SEP-81 12-JAN-83 03-DEC-81 03-DEC-81 23-JAN-82 SAL COMM DEPTNO --------- --------- --------1000 20 1000 300 30 1000 500 30 1000 20 1000 1400 30 1000 30 1000 10 1000 20 1000 10 1000 0 30 1000 20 1000 30 1000 20 1000 0

SQL> SELECT * FROM DEPT; DEPTNO DNAME --------- -------------10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS LOC ------------NEW YORK DALLAS CHICAG BOSTON

List all information about all employees from emp table: SQL> SELECT * FROM EMPNO ENAME ---------------7369 SMITH 7499 ALLEN 7521 WARD EMP; JOB ----CLERK SALESMAN SALESMAN MGR HIREDATE --------- --------7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 SAL --------800 1000 1000 COMM --------DEPATNO --------------20 300 30 500 30

List the employee details not belonging to the department 10,30 and 40:
SQL> SELECT * FROM EMP WHERE DEPTNO NOT IN (10,30,40); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------- ---------- ----------------- ----------------- --------- --------7369 SMITH CLERK 7902 17-DEC-80 800 20 7566 JONES MANAGER 7839 02-APR-81 1000 20 7788 SCOTT ANALYST 7566 09-DEC-82 1000 20

List all information about all employees from dept table: SQL> SELECT * FROM DEPT; DEPTNO DNAME LOC --------- --------------------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS

List the employee name and salary, whose salary is between 1000 & 2000: SQL> SELECT ENAME, SAL FROM EMP WHERE SAL BETWEEN 1000 AND 2000; ENAME SAL ---------- --------ALLEN 1000 WARD 1000 JONES 1000 List employee names, who have joined before 30th June 81 and after December 81: SQL> SELECT ENAME FROM EMP WHERE HIREDATE NOT BETWEEN '30-JUN-81' AND '31-DEC-81'; ENAME ---------SMITH ALLEN WARD List the different jobs (Designations) available in the emp table: SQL> SELECT DISTINCT JOB FROM EMP; JOB --------ANALYST CLERK MANAGER List the employee names, who are not eligible for commission: SQL> SELECT ENAME FROM EMP WHERE COMM IS NULL; ENAME ---------SMITH JONES BLAKE Prepared by: K.SRINIVASA RAO M.C.A.,

SQL> SELECT ENAME FROM EMP; ENAME ---------SMITH ALLEN WARD

List all deptnos, empnums & their mgrs nos in that order from emp table: SQL> SELECT DEPTNO,EMPNO,MGR FROM EMP; DEPTNO EMPNO MGR -------------------------20 7369 7902 30 7499 7698 30 7521 7698 List dept names and locations from the dept table: SQL> SELECT DNAME, LOC FROM DEPT; DNAME LOC -------------------------ACCOUNTING NEW YORK RESEARCH DALLAS SALES CHICAGO

List the employees belonging to the department 20: SQL> SELECT * FROM DEPT WHERE DEPTNO=20; DEPTNO DNAME LOC --------- -------------- ------------20 RESEARCH DALLAS

P: (2) List the name of the employee and designation(job) of the employee, who does not report to anybody (managers is NULL) SQL> SELECT ENAME, JOB FROM EMP WHERE MGR IS NULL; ENAME JOB ---------- --------KING PRESIDENT List the employees not assigned to any department: SQL> SELECT ENAME FROM EMP WHERE DEPTNO IS NULL; no rows selected

List the employees number and name of managers: SQL> SELECT EMPNO, ENAME FROM EMP WHERE JOB='MANAGER'; EMPNO ENAME --------- ---------7566 JONES 7698 BLAKE 7782 CLARK List the names of analysts and salesmen: SELECT ENAME FROM EMP WHERE JOB = 'SALESMAN' OR JOB = 'ANALYST' SQL> / ENAME ---------ALLEN WARD MARTIN List the names of employees who are not managers: SQL> SELECT ENAME FROM EMP WHERE JOB <> 'MANAGER'; ENAME ---------SMITH ALLEN WARD List the name of the employees whose employee numbers are 7369, 7521, 7839, 7788 SQL> SELECT ENAME FROM EMP WHERE EMPNO IN (7369, 7521, 7839, 7788); ENAME ---------SCOTT KING WARD List the employees whose names ends with an S (not s): SQL> SELECT ENAME FROM EMP WHERE ENAME LIKE '%S'; ENAME ---------JONES ADAMS JAMES List the name, salary and PF amount of all the employees (PF is calculated as 10% of salary): SELECT ENAME, SAL, SAL * .1 FROM EMP ENAME SAL SAL*.1 ---------- --------- --------SMITH 800 80 ALLEN 1000 100 WARD 1000 100 ORDER BY: SELECT [DISTINCT]<column list>|<expr> FROM <table>[<table>][WHERE condition] [ORDER BY <columns>][ASC|DESC] List the empno, ename, sal in ascending order of salary: SQL> SELECT EMPNO, ENAME, SAL FROM EMP ORDER BY SAL; EMPNO ENAME SAL --------- -----------------7369 SMITH 800 7900 JAMES 950 7499 ALLEN 1000 List the employee details in ascending order of salary: SQL> SELECT EMPNO, ENAME, SAL FROM EMP ORDER BY 3; EMPNO ENAME SAL --------- -----------------7369 SMITH 800 7900 JAMES 950 7499 ALLEN 1000 List the number of jobs available in the emp table:

List the employees who are eligible for commission:


SQL> SELECT * FROM EMP WHERE COMM IS NOT NULL; 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 --------- --------- --------1000 300 30 1000 500 30 1000 1400 30

List the details of employees, whose salary is greater than 2000 and commission is NULL: SQL> SELECT * FROM EMP WHERE SAL > 2000 AND COMM IS NULL; no rows selected

List the employees whose names start with an S (not s): SELECT ENAME FROM EMP WHERE ENAME LIKE 'S%' ENAME ---------SMITH SCOTT

List the names of employees whose names have exactly 5 characters: SQL>SELECT ENAME FROM EMP WHERE ENAME LIKE '_____' ENAME SAL SAL*.1 ---------- --------- --------SMITH 800 80 ALLEN 1000 100 WARD 1000 100 List the names of employees, who are more than 2 years old in the organization: SQL> SELECT ENAME FROM EMP WHERE (SYSDATE - HIREDATE) > (2 * 365); ENAME ---------SMITH ALLEN WARD List the Emp name, Sal, Job and Deptno, in ascending of dept no, and then on des. Orderering order of salary: SELECT DEPTNO, JOB, ENAME, SAL FROM EMP ORDER BY DEPTNO, SAL DESC; DEPTNO JOB ENAME SAL -------- -------------------------0 CLERK MILLER 1000 10 MANAGER CLARK 1000 10 PRESIDENT KING 1000

Aggregate Functions: COUNT, SUM, MAX, MIN, AVG: COUNT: List the number of employees working with the company: COUNT (*|[Distinct]|ALL|column name) SQL> SELECT COUNT (*) FROM EMP; COUNT(*) --------14

P: (3) SQL> SELECT COUNT (DISTINCT JOB) FROM EMP; COUNT(DISTINCTJOB) -----------------5 SUM: List the total salaries payable to employees: SUM([DISTINCT|ALL]column name) SQL> SELECT SUM(SAL) FROM EMP; SUM(SAL) --------13750 MIN: List the mini. Salary from emp tables: MIN(column name) SQL> SELECT MIN (SAL) FROM EMP; MIN(SAL) --------800 GROUP BY: used to order the final result. It is also used to divide the rows in a table into smaller groups. This is used with SELECT clause. SYN: SELECT[DISTINCT]<column list>|<expr> FROM <table>][WHERE condition] GROUP BY <col | expr> [HAVING <cond>] List the dept. nos and number of employees in each department: SQL> SELECT DEPTNO, COUNT(*) FROM EMP GROUP BY DEPTNO; DEPTNO COUNT(*) --------- ----------10 3 20 5 30 6 CUBE: It appears in the GROUP BY clause in a SELECT statement. SYN: SELECT . GROUP BY CUBE (grouping_reference_column_list);

MAX: List the max slary of employee working as a salesman: MAX(column name) SQL> SELECT MAX(SAL) FROM EMP WHERE JOB = 'SALESMAN'; MAX(SAL) --------1000 AVG: The function returns the average of column values. AVG([DISTINCT|ALL] column name] List the average salary and number of employees working in the department 20: SQL> SELECT AVG (SAL), COUNT(*) FROM EMP WHERE DEPTNO = 20; AVG(SAL) COUNT(*) ----------------2175 5

GROUP BY: List the dept number and the total salary payable in each dept: SQL> SELECT DEPTNO, SUM(SAL) FROM EMP GROUP BY DEPTNO; DEPTNO SUM(SAL) --------- --------10 8750 20 10875 30 9400

GROUP BY: List the jobs and the number of employees in each job. The result should be in descending order of the no.of employees: SQL> SELECT JOB, COUNT(*) FROM EMP GROUP BY JOB ORDER BY 2 DESC JOB COUNT(*) ----------------CLERK 4 SALESMAN 4 MANAGER 3 ANALYST 2 PRESIDENT 1 GROUP BY: list the avg salary from each job excluding managers: SQL> SELECT JOB, AVG(SAL) FROM EMP WHERE JOB !='MANAGER' GROUP BY JOB; JOB AVG(SAL) ----------------ANALYST 3000 CLERK 1037.5 PRESIDENT 5000 GROUPS WITHIN GROUPS: It can be used to provide results for groups within groups List the avg monthly salary for each job type within dept: SQL> SELECT DEPTNO, JOB, AVG(SAL) FROM EMP GROUP BY DEPTNO, JOB; DEPTNO JOB AVG(SAL) --------- ----------------10 CLERK 1300 10 MANAGER 2450 10 PRESIDENT 5000 List the total salary, Min sal, Avg sal, Max salaries of employees job wise, for dept no 20 and display only those rows having average salary greater than 1000; SELECT JOB, SUM(SAL), MIN(SAL), AVG(SAL), MAX(SAL) FROM EMP WHERE DEPTNO=20 GROUP BY JOB HAVING AVG(SAL) > 1000; JOB SUM(SAL) MIN(SAL) AVG(SAL) MAX(SAL) --------- --------------------------------8000 1000 1333.3333 3000

GROUP BY: List the total salary, minimum & maximum salary and the average salary of employees job wise: SQL> SELECT JOB, MIN(SAL), AVG(SAL), MAX(SAL) FROM EMP GROUP BY JOB; JOB MIN(SAL) AVG(SAL) MAX(SAL) --------- --------- ------------ --------ANALYST 3000 3000 3000 CLERK 800 1037.5 1300 GROUP BY: list the total salary, Min, Avg and Max sal of employees job wise, for dept number 20 only: SQL> SELECT JOB, SUM(SAL), MIN(SAL), AVG(SAL), MAX(SAL) FROM EMP WHERE DEPTNO=20 GROUP BY JOB JOB SUM(SAL) MIN(SAL) AVG(SAL) MAX(SAL) ------------------------- ----------------ANALYST 6000 3000 3000 3000 CLERK 1900 800 950 1100 MANAGER 2975 2975 2975 2975 HAVING: Used to specify which groups are to be displayed, i.e., the groups that you return on the basis of aggregate functions. List jobs aof all the employees where max salary is greater than or equal to 2000: SELECT JOB, MAX(SAL) FROM EMP GROUP BY JOB HAVING MAX(SAL) >=2000; JOB MAX(SAL) --------- --------3000 ROLL UP: Appears in the Group By clause in the SELECT statement. Syn: SELECT . GROUP BY ROLLUP (grouping_column_reference_list);

SQL*PLUS: DESC[RIBE]: Display the columns of a table SAVE<FILE-NAME>: save the latest SQL statement to an OS file.

SELECT JOB, TO_CHAR (HIREDATE, 'YYYY') YEAR, DEPTNO, SUM(SAL) AS SUM_SAL FROM EMP GROUP BY ROLLUP (JOB, TO_CHAR(HIREDATE, 'YYYY'), DEPTNO); JOB YEAR DEPTNO SUM_SAL -------------------- --------ANALYST 1981 20 1000 JOB YEAR --------SALESMAN 1981 DEPTNO SUM_SAL --------- --------30 4000

GET and RUN: Retrieve and Execute the script. /: Execute the SQL script present in the buffer.

JOIN: used to combine columns from different tables. Syn: for the select statement where we join 2 tables: SELECT <select-list> FROM <table1>, <table2>, <tableN> WHERE <table1.column1> = table2.column2> and <table2.column3> = <tableN.column> .. additional conditions.

Equi joins: When 2 tables joined together using equality of values in 1 (or) more columns that is called equi joins. List the employee numbers, names, dept nos and the dept name: SELECT EMPNO, ENAME, EMP.DEPTNO, DNAME FROM EMP, DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO EMPNO ENAME DEPTNO DNAME --------- ------------------------------7782 CLARK 10 ACCOUNTING 7839 KING 10 ACCOUNTING EMPNO ENAME --------------7844 TURNER 7900 JAMES DEPTNO --------30 30 DNAME -----SALES SALES

SELECT E.EMPNO, E.ENAME, E.DEPTNO, D.DNAME FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO EMPNO ENAME DEPTNO DNAME ------- ------------------ ----------7782 CLARK 10 ACCOUNTING 7839 KING 10 ACCOUNTING EMPNO ENAME DEPTNO DNAME ------ ----------------------7844 TURNER 30 SALES 7900 JAMES 30 SALES OUTER JOINS: SELECT EMPNO, ENAME, EMP.DEPTNO, DNAME, LOC FROM EMP, DEPT WHERE EMP.DEPTNO (+) = DEPT.DEPTNO EMPNO ENAME -------------7782 CLARK 7839 KING DEPTNO DNAME ---------------------10 ACCOUNTING 10 ACCOUNTING LOC ----------NEW YORK NEW YORK

Cartesian Joins: Without any joining condition the join becomes a Cartesian join. SELECT EMPNO, ENAME, DNAME, LOC FROM EMP, DEPT;

SELF JOIN: Is possible by providing table name aliases for the table. SELECT E.ENAME, E.HIREDATE, M.ENAME MANAGER, M.HIREDATE FROM EMP E, EMP M WHERE E.MGR = M.EMPNO AND E.HIREDATE < M.HIREDATE ENAME HIREDATE MANAGER HIREDATE ---------- -------------------------SMITH 17-DEC-80 FORD 03-DEC-81 ALLEN 20-FEB-81 BLAKE 01-MAY-81 WARD 22-FEB-81 BLAKE 01-MAY-81

EMPNO ENAME DEPTNO DNAME LOC ---------------------- -------------- -------7844 TURNER 30 SALES CHICAGO 7900 JAMES 30 SALES CHICAGO SET OPERATORS: These are used to combine results from different queries. The operators used are: 3 types. 1. UNION: Rows of 1st query plus rows of 2nd query, less duplicate rows. 2. INTERESECT: Common rows from all the queries. 3. MINUS: Rows unique to the 1st query. 2. INTERSECT OPERATOR: Returns the rows that are common between 2 sets of rows. Syn: select stmt1 INTERESECT Select stmt2 [order-by-clause] SELECT JOB FROM EMP WHERE DEPTNO=20 INTERSECT SELECT JOB FROM EMP WHERE DEPTNO=30 JOB ------CLERK MANAGER 3. MINUS: It returns the rows unique to 1st query. Syn: select stmt1 INTERSECT Select stmt2 [order-by-clause] SELECT WHERE MINUS SELECT WHERE MINUS SELECT WHERE JOB ------JOB FROM EMP DEPTNO =20 JOB FROM EMP DEPTNO = 10 JOB FROM EMP DEPTNO = 30

1. UNION: It merges the outputs of 2 or more queries into a single set of rows and columns. Syn: SELECT <stmt1> UNION SELECT <stmt2> [order-by-clause] SELECT JOB FROM EMP WHERE DEPTNO=20 UNION SELECT JOB FROM EMP WHERE DEPTNO = 30 JOB --------ANALYST CLERK MANAGER

MINUS: SELECT JOB FROM EMP WHERE DEPTNO = 20 MINUS SELECT JOB FROM EMP WHERE DEPTNO IN(10,30) JOB ------ANALYST NESTED QUERIES: used in a situation where the condition of the query is dependent on the outcome of an inner query. List the employees belonging to the department of MILLER: SQL> SELECT DEPTNO FROM EMP WHERE ENAME = 'MILLER'; DEPTNO ---------

ANALYST NESTED QUERIES: SELECT ENAME FROM EMP WHERE DEPTNO = 10; ENAME ---------CLARK KING VENKY List the names of the employee drawing the highest salary: SQL> SELECT ENAME FROM EMP WHERE SAL = (SELECT MAX (SAL) FROM EMP); ENAME ---------VENKAT

0 Combining the above 2 queries: SQL> SELECT ENAME FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME = 'MILLER'); ENAME ---------MILLER Aggregate functions in sub queries: To get full details of employees.
SQL> SELECT * FROM EMP WHERE SAL > (SELECT AVG (SAL) FROM EMP WHERE HIREDATE < '01-APR-01'); EMPNO ENAME JOB MGR HIREDATE --------- ---------- --------- --------- --------5 RMAY 1100 114 RAMUE 1200 112 RAMUE 2000 SAL COMM DEPTNO --------- --------- --------10 10 10

List the job with highest avg salary: SELECT JOB, AVG(SAL) FROM EMP GROUP BY JOB HAVING AVG(SAL) = (SELECT MAX(AVG (SAL)) FROM EMP GROUP BY JOB); JOB AVG(SAL) --------- --------1330

List the names of the employees, who earn lowest salary in each dept: SQL> SELECT ENAME, SAL, DEPTNO FROM EMP WHERE SAL IN (SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO); ENAME SAL DEPTNO ---------- --------- --------SMITH 1000 20 ALLEN 1000 30 WARD 1000 30 CORRELATED SUB QUERY: This is nested sub query which is executed once for each candidate row considered by the main query and which on execution uses a value from a column in the outer query. List employees details who earn salary greater than the avg sal for their dept: SELECT EMPNO, ENAME, SAL, DEPTNO FROM EMP E WHERE SAL > (SELECT AVG(SAL) FROM EMP WHERE DEPTNO = E.DEPTNO) EMPNO ENAME SAL DEPTNO ------- ------------------ --------107 VENKAT 3000 20 112 RAMUE 2000 10

Special Operators in Sub queries: (4) types. 1. EXISTS: This operator is used to check for the existence of values. 2. ANY, SOME: It compares the lowest value from the set. 3. ALL OPERATORS: It returns TRUE if every value selected by the sub query satisfies the condition in the predicate of the outer query.

1. EXISTS: List the names of employees from the employee table where the increment amount is greater than 1000 and the number of employees receiving the same increment is greater than 5: SQL> SELECT ENAME, JOB FROM EMP E WHERE NOT EXISTS (SELECT MGR FROM EMP WHERE MGR=E.EMPNO); ENAME JOB ---------- --------SMITH CLERK ALLEN SALESMAN WARD SALESMAN FUNCTIONS: Used to manipulate data items. These are (2) types: 1. Single Row Function: It can further be categorized into Arithmetic Functions, Character Functions. 2. Group Function: These are statistical functions which gives information about a group of value taken whole.

2. ANY: SQL> SELECT ENAME FROM EMP WHERE SAL > ANY (SELECT SAL FROM EMP WHERE DEPTNO = 20); ENAME ---------VENKAT RAMUE RAMUE

3. ALL OPERATORS: List the details of the employee earning more than the highest paid MANAGER: SQL> SELECT EMPNO, ENAME, SAL FROM EMP WHERE SAL > ALL (SELECT SAL FROM EMP WHERE JOB = 'MANAGER') EMPNO ENAME SAL --------- -----------------107 VENKAT 3000 112 RAMUE 2000 114 RAMUE 1200 1. Arithmetic Functions: ABS(n), CEIL(n), FLOCR(n), MOD(m,n), POWER(m,n), SIGN(n), SQRT(n), TRUNC(m,[n]), ROUND (m,[n]), EXP(n)

COLUMN FUNCTIONS: Supported by Oracle can be classified into: 1. Arithmetic Functions: 2. Character Functions: 3. Date Functions. 4. General Functions. 5. Group Functions.

2. Character Functions: CHAR(x), CONCAT(String1, string2), INITCAP(string), LOWER(string), LOWER(string), LPAD (char1, n[, char2]), RPAD(char1, n[, char2]), LTRIM(string, char/s), RTRIM(string, char/s), REPLACE(String, search_str[, replace_str]), TRANSLATE(string, from_str, to_str), Character Functions Returning Numeric Values.

3. Date Functions: SYSDATE, ADD_MONTHS(d,n), ROUND(d[, format]), TRUNC(d[, format]), MONTHS_BETWEEN (d1, d2), LAST_DAY (d), NEXT_DAY(date, day), TO_CHAR(d,f), TO_DATE(char, f) 4. General Functions: GREATEST, LEAST, NVL(col, value), TRANSLATE(char, find, new), DECODE(C,V1,S1,V2,S2, D), UID, USER.

5. Group Functions: COUNT, SUM([DISTINCT|ALL]column name),

1.1.ABS: Returns absolute value of the column

1.1.CEIL (n): It finds the smallest integer greater than (or) equal to n.n can be a column

MAX(column name), MIN(column name), AVG([DISTINCT|ALL]Column name)

(or) value passed. SQL> SELECT ABS(-65) FROM DUAL; ABS(-65) --------65

name also. SQL> SELECT CEIL (SAL) "CEIL (88.9)" FROM EMP WHERE SAL BETWEEN 3000 AND 5000; CEIL (88.9) ----------3000 1.1 POWER (m,n): It returns m raised to the power n. The 2nd argument n must be an integer. SQL> SELECT SAL, POWER (SAL, 2) FROM EMP WHERE DEPTNO=10; SAL POWER(SAL,2) --------- -----------1000 1000000 1000 1000000 1000 1000000 1.1: TRUNC(m,[n]): It truncates the m (or) column to n decimal places. SQL> SELECT SAL, TRUNC(SQRT(SAL),2) FROM EMP WHERE DEPTNO=10; SAL --------1000 1000 1000 TRUNC(SQRT(SAL),2) -----------------------31.62 31.62 31.62

1.1.FLOOR(n): It finds the largest integer less


than (or) equal to the value n.n can be either column or expression. SQL> SELECT FLOOR (SAL), CEIL (88.9) FROM EMP WHERE SAL BETWEEN 3000 AND 5000; FLOOR(SAL) CEIL(88.9) ---------- ---------3000 89

1.1: MOD (m,n): It returns the remainder of m divided by n; or m if n=0. SQL> SELECT MOD (200, 30) FROM DUAL; MOD(200,30) ----------20

1.1: SIGN(n): It returns 1 if n is negative, it returns 1 if n is positive and it returns 0 if n is 0. SQL> SELECT COMM-SAL, SIGN (COMM-SAL) FROM EMP WHERE DEPTNO=30; COMM-SAL ------------700 -500 400 SIGN(COMM-SAL) --------------------1 -1 1

1.1: SQRT(n): This function returns the square root of n. If value n is NULL. (or) negative, when NUL is returned. SQL> SELECT SAL, SQRT (SAL) FROM EMP WHERE DEPTNO=10; SAL --------1000 1000 1000 SQRT(SAL) --------31.622777 31.622777 31.622777

ROUND (m, [n]): It round the column, expression (or) value m to n decimal places. SQL> SELECT SAL, ROUND (SQRT (SAL), 2) FROM EMP WHERE DEPTNO=10; SAL --------1000 1000 1000 ROUND(SQRT(SAL),2) ------------------------31.62 31.62 31.62

C. FUNCTIONS: CHR(x): It returns the character that has the value equivalent to x in the database character set. SQL> SELECT CHR (37) A, CHR (100) B, CHR (101) C FROM DUAL; ABC --%de

INTCAP (String): It capitalizes the 1st character of each word in the string. SQL> SELECT INITCAP (DNAME) FROM DEPT; INITCAP(DNAME) -------------Accounting Research Sales

LOWER (String): SQL> SELECT LOWER (DNAME), LOWER('XYZ') FROM DEPT; LOWER(DNAME) LOW ------------------accounting xyz research xyz sales xyz RPAD(char1, n[, char2]): It pads the column (or) literal value to the right, to a total width of n character positions. SQL> SELECT RPAD(DNAME, 15, '$'), RPAD (DNAME, 15, ' ') FROM DEPT; RPAD(DNAME,15,' ------------------ACCOUNTING$$$$$ RESEARCH$$$$$$$ SALES$$$$$$$$$$ RPAD(DNAME,15,' --------------ACCOUNTING RESEARCH SALES

UPPER: This function converts the string into upper case. SQL> SELECT UPPER (DNAME), UPPER ('ABC') FROM DEPT; UPPER(DNAME) UPP ----------------- --ACCOUNTING ABC RESEARCH ABC SALES ABC LTRIM (string, char/s;): It removes all blank spaces from the left. SQL> SELECT DNAME, LTRIM(DNAME), LTRIM(DNAME, 'R') FROM DEPT; DNAME LTRIM(DNAME) -------------- -------------ACCOUNTING ACCOUNTING RESEARCH RESEARCH SALES SALES LTRIM(DNAME,'R -------------ACCOUNTING RESEARCH SALES

L PAD (Char1, n[, char2]): It pads the column (or) literal value from the left, to a total width of n character positions.

RTRIM(char/s): It removes trailing occurrence(s) of all blank spaces. SQL> SELECT DNAME, RTRIM(DNAME), RTRIM(DNAME, 'S') FROM DEPT; DNAME RTRIM(DNAME) RTRIM(DNAME,'S -------------- ------------------ -------------ACCOUNTING ACCOUNTING ACCOUNTING RESEARCH RESEARCH RESEARCH SALES SALES SALE

REPLACE: (string, search_str[, replace_str]) It returns string with every occurrence of search_str replaced with replace_str. SQL> SELECT REPLACE ('THIS AND THAT', 'TH', 'B') "FIRST" FROM DUAL; FIRST ----------BIS AND BAT

SUBSTR :(string, m [,n]): (M= Starting position, n=No.of characters). It returns a substring. N characters long from the string specified. SQL> SELECT DNAME, SUBSTR(DNAME, 2,4), SUBSTR(DNAME,4) FROM DEPT; DNAME -------------ACCOUNTING RESEARCH SALES SUBS SUBSTR(DNAM ---------- ----------CCOU OUNTING ESEA EARCH ALES ES

TRANSLATE: (string, from_str, to_str). It returns string with all occurrences of each character in from_str replaced by the corresponding character in to_str. SQL> SELECT TRANSLATE ('ABCDEFGHIJ', 'ABCDEF', '123456') FROM DUAL; TRANSLATE( ---------123456GHIJ

ASCII: (string). It returns the decimal representation of the 1st byte of string in the database character set. SQL> SELECT ASCII (' ASCII('') --------32 ') FROM DUAL;

INSTR: (string, char). It returns the position of 1st occurrence of char in string. SQL> SELECT DNAME, INSTR (DNAME, 'E') FROM DEPT; DNAME -------------ACCOUNTING RESEARCH SALES INSTR(DNAME,'E') --------------------0 2 4

LENGTH: (string). It returns the length of a string. SQL> SELECT DNAME, LENGTH (DNAME) FROM DEPT; DNAME -------------ACCOUNTING RESEARCH SALES LENGTH(DNAME) ------------------10 8 5

DATE FUNCTIONS: SYSDATE: It is a psedudo-column, that returns the current date and time of type DATE SQL> SELECT SYSDATE FROM DUAL; SYSDATE --------24-JUL-06

ADD_MONTHS: (d, n). Adds (or) subtracts months to (or) from a date. Returns a date as result. SQL> SELECT HIREDATE, ADD_MONTHS (HIREDATE, 4), ADD_MONTHS (HIREDATE, -4) FROM EMP WHERE DEPTNO=10; HIREDATE ADD_MONTH ADD_MONTH ------------------------------09-JUN-81 09-OCT-81 09-FEB-81 17-NOV-81 17-MAR-82 17-JUL-81 LAST_DAY (d). It returns the date of the last day of the month specified. SQL> SELECT SYSDATE, LAST_DAY (SYSDATE) FROM DUAL; SYSDATE --------24-JUL-06 LAST_DAY( -----------31-JUL-06

ROUND: (d [,format]). It rounds the date d to the unit specified by format. SQL> SELECT ROUND (TO_DATE ('12-APR-71'), 'MM') "NEAREST MONTH" FROM DUAL; NEAREST M -----------01-APR-71

MONTHS_BETWEEN (d1, d2). It returns the number of months between 2 dates, d1 and d2. SELECT MONTHS_BETWEEN ('05-JAN-98', '05MAR-98'), MONTHS_BETWEEN ('05-MAR-98', '05-JAN-98') FROM DUAL;
MONTHS_BETWEEN('05-JAN-98','05-MAR-98') MONTHS_BETWEEN('05-MAR-98','05-JAN-98') --------------------------------------- ------------------2 2

TO_CHAR: (d,f). It converts the date d to character format f. SQL> SELECT SYSDATE, TO_CHAR (SYSDATE, 'DAY') FROM DUAL; SYSDATE --------24-JUL-06 TO_CHAR(S ------------MONDAY

GENERAL FUNCTIONS: GREATEST: (expr1, [, expr2]). It returns the greatest expression of its arguments. SQL> SELECT GREATEST (10, '7', -1) FROM DUAL; GREATEST(10,'7',-1) ------------------10 TRANSLATE: (char, find, new). It returns char with each find changed to new. SQL> SELECT DNAME, TRANSLATE (DNAME, 'E', '1') FROM DEPT; DNAME -------------ACCOUNTING RESEARCH SALES TRANSLATE(DNAM -------------------ACCOUNTING R1S1ARCH SAL1S

LEAST: (expr1 [, expr2]). It returns the least value in the list of expressions. SQL>SELECT LEAST ('abcd', 'ABCD', 'a', 'xyz') "Least" FROM dual; L A

NVL: (col, value). The columns with NULL values are ignored in all of the group functions such as SUM, AVG etc. SQL> SELECT ENAME, SAL, COMM, SAL+COMM GROSS, SAL+NVL (COMM, 0) "NEW GROSS" FROM EMP;
ENAME ---------SMITH ALLEN WARD SAL -----1000 1000 1000 COMM -------300 500 GROSS NEW GROSS ------- ------- --------1000 1300 1300 1500 1500

DECODE: (C,V1,S1, V2, S2. D). This function substitutes on a value-by-value basis. It actually does a if-then-else test. SQL> SELECT ENAME, JOB, DECODE (JOB, 'CLERK', 'EXECUTIVE', 'MANAGER', 'RM', JOB) FROM EMP; ENAME JOB DECODE(JO ---------- --------------------SMITH CLERK EXECUTIVE ALLEN SALESMAN SALESMAN WARD SALESMAN SALESMAN JONES MANAGER RM MAX: This function returns the maximum value of the selected list of item. Syn: MAX (column name) SQL> SELECT MAX(SAL) FROM EMP WHERE JOB = 'SALESMAN'; MAX(SAL) ----------1600 CREATING TABLES: Syn: CREATE TABLE <table-name> ( <column-name> <datatype (size)>, ..) NOT NULL: Prevent a column from accepting NULL values.

COUNT: This function determines the number of rows of non-NULL column values. If * is passed, then the total number of rows is returned. Syn: COUNT (*|[Distinct]|ALL|column name) SQL> SELECT COUNT(*) FROM EMP; COUNT(*) -----------14

SUM: This function returns the sum of values for the select list of columns. Syn: SUM([DISTINCT|ALL]column name) SQL> SELECT SUM(SAL) FROM EMP; SUM(SAL) --------29025 AVG: Returns the avg of column values. Syn: AVG ([DISTINCT|ALL] column name) SQL> SELECT AVG(SAL) FROM EMP WHERE JOB = 'SALESMAN'; AVG(SAL) --------1400 DEFAULT: Assigns a default value for the column(s), at the time of insertion when no value is given for that column. REFERENCES: Assigns a Foreign Key constraint to

MIN: Returns the minimum value of the selectd list of items. Syn: MIN(column name) SQL> SELECT MIN(SAL) FROM EMP WHERE JOB = 'SALESMAN'; MIN(SAL) -----------1250 UNIQUE: Ensures that the values entered into a column are unique. PRIMARY KEY: Same as Unique, But only 1 column per table is allowed. CHECK: Controls the value of a column(s) being inserted.

ON DELETE CASCADE OPTION: using this option whenever a parent row is deleted then all the corresponding child rows are deleted from the detail table.

TAB: To display the list of tables which are created by user, Data dictionary is TAB Table. SELECT * FROM TAB;

maintain Referential Integrity. INSERTING VALUES INTO A TABLE: used to insert rows in the table. SNY: INSERT INTO <table-name> VALUES (<list-of-values>); SQL> INSERT INTO EMP VALUES (7311, 'SRINU', 'CLERK', 7002, '18-APR-95', 3000, NULL, 20);
EMPNO ENAME ------ ---------7369 SMITH 7311 SRINU JOB MGR HIREDATE SAL COMM DEPTNO --------- --------- --------- --------- --------- ------------CLERK 7902 17-DEC-80 800 20 CLERK 7002 18-APR-95 3000 20

CREATING TABLE WITH ROWS FROM ANOTHER TABLE: A table can be created using CREATE TABLE statement with rows, derived from another table. Syn: CREATE TABLE DEPT [(column name , ..)] AS SELECT statement. CREATE TABLE EMP2 AS SELECT EMPNO, ENAME, JOB, MGR, SAL FROM EMP WHERE DEPTNO IN (10, 20) Table created. DELETE: Delete command allows to remove 1 (or) more rows from a table. Syn: DELETE FROM <table-name> [WHERE <condition>]; DELETE FROM EMP;

UPDATING COLUMN(s) OF A TABLE: Used to update the columns in a table. Syn: UPDATE <table-name> SET <col-name> = <value> [, col-name=value, .] [WHERE <condition>] Give everybody a comm. Of Rs. 500: SQL> UPDATE EMP SET COMM = 500;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- --------- --------- --------- --------- --------------------7369 SMITH CLERK 7902 17-DEC-80 800 500 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 500 30

UPDATE: Increase everybodys salary by 10% SQL> SELECT * FROM EMP;


EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------------------- ---------- --------- --------- --------- --------- -------7369 SMITH CLERK 7902 17-DEC-80 880 500 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1760 500 30 7521 WARD SALESMAN 7698 22-FEB-81 1375 500 30

DROPING COLUMNS: For unused columns from table EMP. ALTER TABLE emp DROP UNUSED COLUMNS;

VIEW: These are database objects whose contents are derived from another table. Syn: CREATE [OR REPLACE] VIEW <view name> [(column1, column2, .)] AS <select statement> SQL> CREATE VIEW DEPT20 AS SELECT * FROM EMP WHERE DEPTNO = 20; View created.

Das könnte Ihnen auch gefallen