You are on page 1of 17

Database Management System Database: Organized collection of required information is called database.

Example: Dictionary, Telephone Index, and News paper RDBMS (Relational Database Management System): It is information system, information contained in the form of tables. A table is holding information in terms of the rows and columns. The intersection of row and column must give single value. The set of columns are called row. Features of RDBMS: The ability to create multiple relations and enter data into them. An interactive query language. Retrieval of information stored in more than one table. Features of SQL: Non Procedural Language Unified Language Common Language for All Relational Database It is not a case sensitive Programming Language This language is used to access within the oracle database SQL SQL SQL SQL stands for structured query language is a tool for organizing, managing and retrieving data stored in the database. is a language used to interact with the database works with a one specific database called Relational database

Data Types in SQL: Data Description Type Char(n) Fixed length character data of length n Varchar2 Variable length character of data (n) Number( size) Variable length numeric data. P refers to Precision value range from Number( 1 to 38. S refers to Scale value range from -84 to 127 p, s) Fixed Length date and time. Date ranging from January 1, 4712 BC Date to December 31, 9999 A SQL Language Classified into Five Sub Languages they are 1. DDL, 2. DML, 3. DCL, 4. TCL, 5. DRL 1. DDL (Data Definition Language): These commands are used to working with database object definitions. It consists of commands to Create, Drop and Alter the database objects, such as Tables, Views, and Index etc. In DDL there are following commands. a. Create, b. Alter (i. Add, ii. Drop, iii. Modify), c. Drop, d. Truncate, e. Rename a. Create: This command is used to create the database table (database object). Syntax: Create table <table-name> (col-name1 data-type (size), col-name2 datatype (size),....,col-nameN data-type(size)); Create table std (htno varchar2 (20), sname char (20), dob number, branch char (3), mark1 number (3), mark2 number (3)); b. Alter: It is used to modify the database table. It has 3 attributes 1.Add, 2.Modify, 3. Drop

Is used to add new column to the existing database table. To modify the data type, size of the column. To drop the column from the database table. To add constraint to the column. To drop constraint from the column. Oracle 9i/10g/11g supports changing name of the column. i. add: It is used to add a column to the table. Syntax: alter table <table-name> add <column-name data-type (size); -- alter table std add rank number (3); ii modify: It is used to modify column-size or data-type of the existing column. Syntax: alter table <table-name> modify <column-name> data-type (size); --alter table std modify htno varchar2 (10); --modifying size of the column. --alter table std modify dob date; --modifying the data type. iii. drop: It is used to drop the column from the database table. Syntax: alter table <table-name> drop column <column-name>; --alter table std drop column rank; c. Drop: It is used to drop the table from the database. Syntax: drop table <table-name>; --drop table std; d. Truncate: It is used to delete the data from the dataset table. Once we use this command on a particular table, we cannot rollback the data from that particular table. Syntax: truncate table <table-name> -- truncate table std; e. Rename: It is used to rename the database table. Syntax: rename <old-table-name> to <new-table-name>; --rename std to student; 2. DML (Data Manipulation Language): These commands are used to work with data in the database objects. By using these commands we can insert, delete and update data in the database objects. In DML there are following commands. a. Insert, b. Update, c. Delete a. Insert: It is used to insert the data (records) into the table. Syntax1: insert into <table-name> values (col1, col2,....); --insert into stud values (&sno, '&sname', '&branch', &year, &marks); Syntax2: insert into <table-name>(col1, col2, ..) values (col1-val, col2-val,....); --insert into stud (sno, sname, branch, mark1, mark2, mark3) values (&sno, '&sname', '&branch', &mark1, &mark2, &mark3); b. Update: It is used to edit the already existing data (records) in the database table. Syntax: update <table-name> set column-name=value; --update std set branch='CSE' where sno=054301; Where Clause: It is used to define the condition in select statement. c .Delete: It is used delete the existing data (records) from the table. Syntax: delete from <table-name> where column-name=value; --delete from std where sno=054304; 3. DCL (Data Control Language): It is used to control the data and give the access on the database. There are following commands a. Grant, b. Revoke a. Grant: It is used to give the permissions to the user on particular table. Syntax: grant <privilege> on <table-name> to <user-name>; --grant select on emp to scott;

b. Revoke: It is used to get back (delete) the permissions on the database table from the user. Syntax: revoke <privilege> on <table-name> from <user-name>; -- revoke select on emp from scott; 4. TCL (Transaction Control Language): It is used to control the transactions performed on the database. There are following commands. a. Commit, b. Rollback, c. Savepoint a. Commit: It is used to make the changes to the database permanently. Syntax: commit; b. Rollback: It is used to discard all the work/transactions done by the user. Syntax: Rollback; c. Savepoint: It identifies a point in the transaction to which one cal later rollback with rollback statement. It is helpful when a transaction contain a large number of SQL statements and user want to commit only once. When all transactions are done, if require one can rollback to a particular transaction, it works in LIFO. Syntax: savepoint <savepointname>; --savepoint p1; --rollback p1; 5. DRL (Data Retrieval Language): It is used to retrieve the data from the database objects. a. Select: It is used to retrieve the data from the database table. Syntax: select <col1, col2,...> from <table-name>;

OPERATORS 1. Arithmetic Operators: +, -, *, / 2. Relational Operators: >, >=, <, <=, =, != 3. Logical Operators: AND, OR, NOT 4. Special Operators: Is Null: It is used to check null values. --SQL> SELECT * FROM EMP WHERE COMM. IS NULL; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 10 rows selected. In: It is used to check the values in the set. --SQL> SELECT * FROM EMP WHERE DEPTNO IN(10, 30); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 9 rows selected. Between: It is used to check value within the range. --SQL> SELECT * FROM EMP WHERE SAL BETWEEN 1500 AND 3000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7 rows selected. Like: It is used to match a pattern from the column. % represents any no. of characters and _ represents single character. -- SQL> SELECT * FROM EMP WHERE ENAME LIKE 'A%'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 7876 ADAMS CLERK 7788 23-MAY-87 1100 2 rows selected. -- SQL> SELECT * FROM EMP WHERE ENAME LIKE '_____'; EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 8 rows selected. Order By Clause: It is used to display the records from table either in descending order.

30 20 DEPTNO 20 30 ascending or

FUNCTIONS IN SQL 1. Character functions: a. length (): It returns the length of the specified string. --Write a query to display ename and length of ename from emp table. SQL> SELECT ENAME, LENGTH (ENAME) FROM EMP; ENAME LENGTH (ENAME) ---------- ------------SMITH 5 ALLEN 5 14 rows selected. b. lower (): It converts the given string to lowercase. --Write a query to display ename from emp table. SQL> SELECT ENAME, LOWER (ENAME) FROM EMP; ENAME LOWER (ENAM ---------- ---------SMITH smith ALLEN allen 14 rows selected. c. upper (): It converts the given string to uppercase. SQL> SELECT ENAME, UPPER (JOB) FROM EMP; ENAME UPPER (JOB ---------- --------SMITH CLERK ALLEN SALESMAN 14 rows selected. d. initcap (): It converts the first letter capital for given word. SQL> SELECT ENAME, INITCAP (JOB) FROM EMP; ENAME INITCAP (JOB) ---------- --------SMITH Clerk ALLEN Salesman

14 rows selected. e. ltrim (): To trim off the leading spaces of a given string or column. Syntax: ltrim (column name/string); f. rtrim (): To trim off the trailing spaces of a given string or column. Syntax: rtrim (column name/string); g. trim (): To trim off both sides of a give string or column. Syntax: trim (column name/string); h. lpad (): It returns the string left padded with given characters. Syntax: lpad (string, length, padstring); i. rpad (): It returns the string right padded with given characters. Syntax: rpad (string, length, padstring); j. concatenate (): It returns string by string1 concate with string2. Syntax: concat (string1/column1,string2/column2); k. substr (): It returns the part of the string. Syntax: substr (string/column, starting position, total no. of characters); l. replace(): It returns string with every occurrence of search string replaced with replacement string. Syntax: replace (string, search string, replacement string); -- SQL> SELECT REPLACE ('RAJ','J','M') FROM DUAL; REPLACE ('RAJ','J','M') ----------------------Ram m. translate(): It is used to substitute character by character basis in a string. Syntax: translate (string/column, from text, to text); -- SQL> SELECT TRANSLATE ('NIVEDITA','AEIOU','ABCDE') FROM DUAL; TRANSLATE ('NIVEDITA','AEIOU','ABCDE') ----------------------------------------NCVBDCTA 2. Date functions: a. add_months (): It adds specified months to the given date. Syntax: add_months (date, n); --SQL> SELECT EMPNO, ENAME, HIREDATE, ADD_MONTHS (HIREDATE, 6) FROM EMP; EMPNO ENAME HIREDATE ADD_MONTH ---------- ---------- --------- --------7369 SMITH 17-DEC-80 17-JUN-81 7499 ALLEN 20-FEB-81 20-AUG-81 14 rows selected. b. months_between (): It is used to find no. of months between two specified dates. Syntax: months_between(date1, date2); --SQL> SELECT HIREDATE, SYSDATE, MONTHS_BETWEEN (SYSDATE, HIREDATE) FROM EMP; HIREDATE SYSDATE MONTHS_BETWEEN(SYSDATE,HIREDATE) --------- --------- -----------------------------------------------------17-DEC-80 17-JAN-10 349 20-FEB-81 17-JAN-10 346.919484 14 rows selected.

c. last_day (): It returns the date of last day of month that contains date. Syntax: last_day (date); --SQL> SELECT SYSDATE, LAST_DAY (SYSDATE) FROM DUAL; SYSDATE LAST_DAY (SYSDATE) --------- -----------------------17-JAN-10 31-JAN-10 d. next_day (): It returns the date of first weekend named by the string that is greater than the date. Syntax: next_day (date, string); --SQL> SELECT SYSDATE, NEXT_DAY (SYSDATE, 'THU') FROM DUAL; SYSDATE NEXT_DAY (SYSDATE, 'THU') --------- ------------------------------17-JAN-10 21-JAN-10

3. Conversion Functions: a. to_char (): It converts given date to varchar2 in the format specified in the date format. Syntax: to_char (date, format) --SQL> SELECT SYSDATE, TO_CHAR (SYSDATE, 'DD/MONTH/YY') FROM DUAL; SYSDATE TO_CHAR (SYSDATE, 'DD/MONTH/YY') --------- -----------------------------------------17-JAN-10 17/JANUARY/10 b. to_date (): It converts given string in character/varchar2 to date data type. Syntax: to_date(string) --SQL> SELECT TO_DATE ('19-MAY-1986') FROM DUAL; TO_DATE ('19-MAY-1986') --------------------------19-MAY-86 c. to_number (): It converts the given character into number. Syntax: to_number (number/string); --SQL> SELECT TO_NUMBER ('125') + TO_NUMBER ('125') FROM DUAL; TO_NUMBER ('125') + TO_NUMBER ('125') --------------------------------250 4. Aggregate Functions: These functions produce single value for an entire row. Group By Clause: This Clause is used to group the rows on a particular column. --Write a query to display the total salary department wise from emp tabale. SQL> SELECT SUM (SAL), DEPTNO FROM EMP GROUP BY DEPTNO SUM (SAL) DEPTNO ---------- ---------8750 10 10875 20 9400 30 Having Clause: It is use to compare group functions. It behaves like where clause.

a. distinct (): It is used with select clause to suppress duplicate values if any in the column. Syntax: distinct (column-name); --SQL> SELECT DISTINCT (JOB) FROM EMP; JOB --------ANALYST CLERK MANAGER PRESIDENT SALESMAN b. sum() : It gives the sum of all values in a particular column. Syntax: sum (column-name); SQL> SELECT SUM (SAL) FROM EMP; SUM (SAL) ---------29025

c. average(): It gives the average of all values in a particular column. Syntax: avg (column-name); --SQL> SELECT AVG (SAL) FROM EMP; AVG (SAL) ---------2073.21429 d. max(): It returns the maximum value among given list of values or column. Syntax: max (column-name/list of values); --SQL> SELECT MAX (SAL) FROM EMP; MAX (SAL) ---------5000 e. min(): It returns the minimum value among given list of values or column. Syntax: min (column-name); --SQL> SELECT MIN (SAL) FROM EMP; MIN (SAL) ---------800 f. count(): It returns the no. of rows. --SQL> SELECT COUNT (*) FROM EMP; COUNT (*) ---------14 --Write query to update employee comm 100 for who is not getting comm? SQL> UPDATE EMP SET COMM=100 WHERE COMM IS NULL; 10 ROWS UPDATED. --Write query to display all the employee details whose job is mgr and who is in deptno 20 and 30? SQL> select * from emp where job='MANAGER' and deptno in(20,30); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 --Write query to display all the employee details whose job is salesman or manager? SQL> SELECT * FROM EMP WHERE JOB='SALESMAN' OR JOB='MANAGER'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7 rows selected. --Write query to display employee details whose deptno is 20 & 30? SQL> SELECT * FROM EMP WHERE DEPTNO IN(20,30); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 11 rows selected.

--Write query to display employee details whose salary between 1000 and 3000? SQL> SELECT * FROM EMP WHERE SAL BETWEEN 1000 AND 3000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 11 rows selected. --Write query to display employe details whose name length is 5 characters? SQL> SELECT * FROM EMP WHERE ENAME LIKE '_____'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 8 rows selected. --Write query to display employee details ,whose name length is 5 characters and starting with A? SQL> SELECT * FROM EMP WHERE ENAME LIKE 'A____'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 --Write query to display employee salary by descending order? SQL> SELECT * FROM EMP ORDER BY SAL DESC; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20

14 rows selected. --Write query to display unique deptno? SQL> SELECT DEPTNO FROM EMP GROUP BY DEPTNO; DEPTNO ---------10 20 30 --Write query to display deptno & average salary department wise & in ascending order? SQL> SELECT DEPTNO, AVG(SAL) FROM EMP GROUP BY DEPTNO; DEPTNO AVG(SAL) ---------- ---------10 2916.66667 20 2175 30 1566.66667 --Write query to display all employee details that who are having more than 25 years experience? SQL> SELECT * FROM EMP WHERE (SYSDATE-HIREDATE)/365 >25; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 12 rows selected. --Write query to display ename, substring of 3 characters of ename? SQL> SELECT ENAME, SUBSTR(ENAME,2,3) FROM EMP; ENAME SUB ---------- --SMITH MIT ALLEN LLE 14 rows selected. --Write query to display empno, ename, sal where ALLEN is working? SQL> SELECT EMPNO, ENAME, SAL FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME='ALLEN'); EMPNO ENAME SAL ---------- ---------- ---------7499 ALLEN 1600 7521 WARD 1250 6 rows selected.
1. Display Employee Name And Annual Salary For All Employees --select empno, ename,12*sal+nvl(comm,0) annualsal from emp; 2. Display The Names Of Employees Who Are Working As Clerk, Salesman Or Analyst And Drawing A Salary More Than 3000 --select ename from emp where (job='clerk' or job='salesman' or job='analyst') and sal>3000; 3. Display The Names Of Employees Working In Department Number 10 Or 20 Or 40 Or Employees Working As Clerks, Salesman Or Analyst --select ename from emp where deptno in (10, 20, 40) or job in ('clerk','salesman','analyst');

4. Display the names of employees whose name starts with alphabet S. --select ename from emp where ename like 'S%'; 5. Display the names of employees whose names have sencond alphabet A in their names --select ename from emp where ename like '_S%'; 6. Display the names of employees whose name is exactly five characters in length. --select ename from emp where length (ename)=5; or --select ename from emp where ename like '_____'; 7. Display the names of employees who are not working as SALESMAN or CLERK or ANALYST --select job from emp where job not in ('CLERK', 'ANALYST', 'SALESMAN');

8. Display the list of employees who have joined the company before 30 th june 90 or after 31 st dec 90 --select * from emp where hiredate between '30-jun-1990' and '31-dec-1990';
9. Display names of employees who do not earn any commission --select empno, ename from emp where comm is null and comm = 0;

SUB QUERIES Sub Query: Query within the query. It is also known as nested query. A query that calls another query is known as Sub Query. Functionality of Sub query: Inner query executed first and form a value based on that value outer query will be executed. Syntax: <parent query> where<left value><relational operator> <inner query>; Sub Queries Are classified into three ways 1. Single Row Sub query : Sub queries that executes only one row from the inner select statement. Single Row comparison operators are used in single row sub queries. --List the employee details whose salary is an employee of 7934. SQL> SELECT * FROM EMP WHERE SAL>(SELECT SAL FROM EMP WHERE EMPNO=7934); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 8 rows selected. 2. Multiple Row Sub query : Queries that executes more than one row from the inner select statement. Multiple Row comparison operators are used in single row sub queries. Example:In operator:

--List the employee details who have maximum salary group wise department no SQL> SELECT * FROM EMP WHERE SAL IN (SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 Any Operator: --List the employee details whose salary is greater than any of the clerk SQL> SELECT * FROM EMP WHERE SAL <ANY (SELECT SAL FROM EMP WHERE JOB='CLERK'); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 All Operator: -- List the employee details whose salary is greater than all the avg salaries the employee of deptno. SQL> SELECT * FROM EMP WHERE SAL>ALL (SELECT AVG(SAL)FROM EMP GROUP BY DEPTNO); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7566 JONES MANAGER 7839 02-APR-81 2975 20 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 Multiple Column sub query: Queries that executes more than one column from the inner select statement. -- Write a query to display employee detail where Jones is working? SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE ENAME='JONES'); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 -- Write a query to display employee details who has grade 3? SELECT * FROM EMP WHERE SAL>=(SELECT LOSAL FROM SALGRADE WHERE GRADE=3) AND SAL<=(SELECT HISAL FROM SALGRADE WHERE GRADE=3); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 --Write a query to display empno, ename, sal where ALLEN is working? SQL> SELECT EMPNO,ENAME,SAL FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE ENAME='ALLEN'); EMPNO ENAME SAL ---------- ---------- ---------7499 ALLEN 1600 7521 WARD 1250 7698 BLAKE 2850

--Write a query to display all the employee details who is getting more than 3000 salary? SQL> SELECT * FROM EMP WHERE SAL >3000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 10 --Write query to display empno, ename, sal, comm, who is not getting comm? SQL> SELECT EMPNO, ENAME, SAL, COMM FROM EMP WHERE COMM IS NULL; EMPNO ENAME SAL COMM ---------- ---------- ---------- ---------7369 SMITH 800 7566 JONES 2975 7698 BLAKE 2850 10 rows selected.
1. Display the employee Number and name for employee working as clerk and earning highest salary among the clerks? --select ename, empno from emp where sal=(select max (sal) from emp where job='CLERK') and job='CLERK' ; 2. Display the various jobs along with total number of employees in each job. The output should contain only those jobs with more than three employees? --select job, count (*) from emp group by job having count (*)>3; 3. Display the names of employees who earn Highest salary in their respective departments? --select ename, sal, deptno from emp where sal in (select max (sal) from emp group by deptno);

SET Operators: SET operators are performed on results from two independent queries. The output from both queries must return same data type of attributes. Syntax: Select querry1 set operator select querry2; --where set operator is one of the 4 set operators they are 1. Union, 2. Union All, 3. Intersect, 4. Minus --Create table MCA (HtNo, Number, SName varchar2 (20)); --Insert into MCA values (&HtNo, '&SNname'); --SQL> SELECT * FROM MCA; HTNO SNAME ---------- -----------101 SRAVANI 102 SITARAM 103 GEETA 104 VANI 105 RAMESH --Create table MBA (HtNo Number, SName varchar2 (20)); --Insert into MBA values (&HtNo, '&SNname'); --SQL> SELECT * FROM MBA; HTNO SNAME

---------- ----------------106 HARI 103 GEETHA 105 RAMESH 107 LAXMI 108 NAGESH 1. Union: The union operator takes output from two queries and returns all rows from both the results. The duplicate rows are display once. SQL> SELECT * FROM MCA UNION SELECT * FROM MBA; HTNO SNAME ---------- -------------------101 SRAVANI 102 SITARAM 103 GEETA 103 GEETHA 104 VANI 105 RAMESH 106 HARI 107 LAXMI 108 NAGESH 9 rows selected. 2. Union All: Tthe union all operator is similar to union operator but union all displays duplicate rows also. SQL> SELECT * FROM MCA UNION ALL SELECT * FROM MBA; HTNO SNAME ---------- -------------------101 SRAVANI 102 SITARAM 103 GEETA 104 VANI 105 RAMESH 106 HARI 103 GEETHA 105 RAMESH 107 LAXMI 108 NAGESH 10 rows selected. 3. Intersect: The intersect operator works on output from two separate queries and returns rows that are commonly both the outputs. SQL> SELECT * FROM MCA INTERSECT SELECT * FROM MBA; HTNO SNAME ---------- -------------------105 RAMESH 4 . Minus: It returns rows unique to first query i.e it returns the rows which are not present in second query. SQL> SELECT * FROM MCA MINUS SELECT * FROM MBA; HTNO SNAME ---------- -------------------101 SRAVANI 102 SITARAM 103 GEETA

104 VANI

JOINS Joins are used to retrieve the data from more than one table at a time. In order to extract the data from more than one table we need to take the help of joins. Functionality of Joins: The rows of one object (table) combines with each and every row of other of same object and perform the result. Joins are classified into 3 types, they are 1. Simple join 2. Self join 3. Outer join Simple Join is further classified into 2 types.

1. Equiv. Join: Tables are joined based on the equality of the join. In equiv join table must have common columns. If n tables are join then n-1 conditions are required. --Write a query to display employee details along with their department details? SQL> SELECT ENAME, EMPNO, EMP.DEPTNO, DNAME, LOC FROM EMP, DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO ENAME EMPNO DEPTNO DNAME LOC ---------- ---------- ---------- -------------- ------------SMITH 7369 20 RESEARCH DALLAS ALLEN 7499 30 SALES CHICAGO WARD 7521 30 SALES CHICAGO 14 rows selected. Non Equiv Join: Tables are joined based on the non equality of the condition then it is called non equiv join. Non equiv join no need to have same name attribute (must have same data type). --Write a query to display employee details along with their grades? SQL> SELECT E.EMPNO, E.ENAME, E.SAL, S.GRADE FROM EMP E, SALGRADE S WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL; EMPNO ENAME SAL GRADE ---------- ---------- ---------- ---------7369 SMITH 800 1 7876 ADAMS 1100 1 7900 JAMES 950 1 14 rows selected. Self Join: When a table is referring to itself or when a foreign key is referring to the primary key of its own table then we go for self join. --Write a query to display employee details along with their mgrno and mgrname? SQL> SELECT A.EMPNO, A.ENAME, A.SAL, B.EMPNO, B.ENAME FROM EMP A, EMP B WHERE A.MGR=B.EMPNO; EMPNO ENAME SAL EMPNO ENAME ---------- ---------- ---------- ---------- ---------7369 SMITH 800 7902 FORD 7499 ALLEN 1600 7698 BLAKE 7521 WARD 1250 7698 BLAKE 13 rows selected. Outer Join: It is used to retrieve all the rows from one table but matching rows from the other table. ---it extends the result of equiv join. ---it is used to output all the records from the table. Note: 1. + operator should be assigned to the table where the information is missing. 2. It is illegal to use the + operator on both sides of columns of the join condition. --Write a query to display employee details and dept details even no employees are exists? SELECT E.EMPNO, E.ENAME, E.SAL, D.DEPTNO, D.DNAME, D.LOC FROM EMP E, DEPT D WHERE E.DEPTNO (+) = D.DEPTNO ORDER BY DEPTNO; EMPNO ENAME SAL DEPTNO DNAME LOC ---------- ---------- ---------- ---------- -------------- ------------7782 CLARK 2450 10 ACCOUNTING NEW YORK

7839 7369 7876 7499 7698

KING SMITH ADAMS ALLEN BLAKE

5000 800 1100 1600 2850

10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 20 RESEARCH DALLAS 30 SALES CHICAGO 30 SALES CHICAGO 40 OPERATIONS BOSTON

15 rows selected.

You might also like