Vous êtes sur la page 1sur 7

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 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

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 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

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

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: SQL> SELECT COUNT (DISTINCT JOB) FROM EMP; COUNT(DISTINCTJOB) -----------------5 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

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 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);

GROUP BY: List the dept number and the total salary payable in each dept:

GROUP BY:

SQL> SELECT DEPTNO, SUM(SAL) FROM EMP GROUP BY DEPTNO; DEPTNO SUM(SAL) --------- --------10 8750 20 10875 30 9400

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); 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

SQL*PLUS: DESC[RIBE]: Display the columns of a table SAVE<FILE-NAME>: save the latest SQL statement to an OS file. 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 JOB FROM EMP WHERE DEPTNO =20 MINUS SELECT JOB FROM EMP WHERE DEPTNO = 10 MINUS SELECT JOB FROM EMP WHERE DEPTNO = 30 JOB ------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

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 --------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 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

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

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), MAX(column name), MIN(column name), AVG([DISTINCT|ALL]Column name)

1.1.ABS: Returns absolute value of the column


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

1.1.CEIL (n): It finds the smallest integer greater than (or) equal to n.n can be a column 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.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: SIGN(n): It returns 1 if n is negative, it returns 1 if n is positive and it returns 0 if n is 0.

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

Vous aimerez peut-être aussi