The SELECT query is the most often used query in any database. We will be referring tables from default schema/user SCOTT in Oracle. SELECT [ DISTINCT| ALL] 1. Select all the details of the employee from emp table. select * From emp; Select records using multiple columns; Select eno, ename, sal From emp; Select records using multiple alias Select eno as empno, ename as empname, sal as salary from emp; 2. Using functions to enhance alias Select count(sal) as totalsa From emp; 3. Derived or Computed fields: Columns values were manipulated as it gets retrived. a. Find the monthly salary of employee,( The salary stored is on Annum basis). Select sal/12 From emp; b. Using Alias to decorate derived or computed fields. Select sal/12 as montly_salary From emp; c. Calculate the sum of monthly salary and the commissions of the employee. Select ename, (sal/12)+nvl(comm,0) as monthsalwithcomm from emp; 4. Using Aggregate functions: Count,Min,Max,Sum,Avg. a. Find the number of rows in emp Select count(*) From emp; b.Find the how many different Job profile are there in Employee table Select Count(distinct job) From emp; c. Find out how many people were given commision ( Count function does n't include null as it counts) Select Count(comm) From emp; 5. Select records from two tables; Select e.ename, d.deptno, e.sal From emp e, dept d; 6. Select records from two tables depending condition suppose where deptno of from both emp and dept mathes Select e.ename, d.deptno, e.sal From emp e, dept d Where e.deptno=d.deptno; 7: Select records using between and; Select * From emp Where sal between 2000 and 3000 8. Select records using in; Select * From emp Where ename In ('SCOTT', 'WARD', 'ALLEN') and sal In(1000, 2000, 3000,3200, 3300); 9: Select records using not in; Select * From emp Where ename Not In ('SCOTT','WARD', 'ALLEN') 10: Select records using null; Select * From emp Where comm Is null; 11:Select records using not null; Select * From emp Where comm Is Not null; 12:Select records using like; Select * From emp Where ename Like'S%' or ename Like 'sC__' or ename Like 'A_L_N' or ename Like '___L%'; 13:Select records using not like; Select * From emp Where ename Not Like 'A%'; 14:Select records using multiple conditions. Select * From emp Where comm is not null and ename not like(A%) and sal in (1000, 4000); 15:Select records using function; Select ename, sum(sal+comm), avg(sal) as avgssal as total From emp Group by deptno; 16:Select records using order by; Select * From emp Order by sal Desc, empno,empname; 17:Select records using group by and having clause; Select deptno, sum(sal) Group by deptno Having deptno>20 18:Select with-in select; Select * From emp Where max(sal)< (Select max(sal) From emp); Select * From emp Where deptno=(select deptno From dept); Select * From emp Where deptno Not In (Select deptno From dept); 19:create a table by selecting record from another table; Create Table emp2 as select * From emp; 20:Select records using exist; Select deptno From dept d where exists (Select * from emp e Where d.deptno=e.deptno); 21:Select sysdate; Select sysdate From dual 22:Select constraint name, constraint_type; Select constraint_name, constraint_type From user_constraints where table_name='emp'; 23:Select nextval, currval from sequence; Select emp_sequence.nextval From dual Select emp_sequence.currval from dual 24. Set Operators Set oepratros basically combine the result of two quesies into one. There queries are known as compound queries. These are 4 set operators:
(1) UNION: Returns all rows from either queries; no duplicate rows. (2) UNION ALL: Returns all rows from either query, including duplicates. (3) INTERSETCT: Returns distinct rows that are returned by both queries. (4) MINUS: Returns distinct rows that are returned by the first query but not returned by the second. Example: Query1: Select ename from emp where dept_id=40; This query returns result: Alex, Peter, John, Richa Query2: Select ename from emp where sal>=4000; This query returns result: Aled, Peter, John, Chrish Union: (Select ename from emp where dept_id=40) Union ( Select ename from emp where sal>=4000) This will return result: Alex, Peter,John, Richa, Aled, Chrish UNION ALL: (Select ename from emp where dept_id=40) Union All ( Select ename from emp where sal>=4000) This will return: Alex, Peter, John, Richa, Aled, Peter, John, Chrish INTERSETCT: (Select ename from emp where dept_id=40) INTERSETCT ( Select ename from emp where sal>=4000) This will return: Peter, John MINUS: (Select ename from emp where dept_id=40) MINUS ( Select ename from emp where sal>=4000) This will return: Alex, Richa
|
|