Вы находитесь на странице: 1из 4

SQL

select * from emp;


select ename, empno from emp ;
select * from emp where empno=7369;
select * from emp where deptno =20;
select ename,sal from emp where sal>2000;
select ename from emp where job='CLERK' OR job='MANAGER'
or
select ename from emp where job='MANAGER' AND job='CLERK';
select ename from emp where job<> 'MANAGER';-- (!=)
select ename from emp where empno =7369 or empno=7934 or empno=7788;
select ename from emp where empno IN(7369,7934,7788);
select ename from emp where empno NOT IN(7369,7934,7788);
select ename, sal from emp where sal between 1000 and 2000;
select ename from emp where hiredate between '30-June-81' and '31-Dec-81';
select ename from emp where ename >'ALLEN' and ename<'JONES';--try between
select * from emp where ename like 'A%';-- pattern matching
select * from emp where ename like '%E'; like _D%-- 2nd char is D
select * from emp where ename like '____';
select ename, sal , sal* 0.1 as "PF" from emp;
select empno || ename as " employee details " from emp;
select ename||' is a '|| job as " designation " from emp ;
select ename || it�s manager is : || mgr as Manager from emp;
select ename || q�[ it�s manager is : ]� || mgr as Manager from emp;
q�[ �it�s manager is : ]�
--select ename || � it�s manager is : � || mgr as Manager from emp;
--Alternative Quote (q) Operator --Specify your own quotaion mark delimiter

select DISTINCT deptno,job from emp;


select distinct job,deptno from emp;
select * from emp where comm =0;
select ename from emp where comm IS null ;
select * from emp where comm NOT IN (500,1000,0);
select sal,comm, sal+comm as "Total Sal" from emp;
select sal,comm,sal+nvl(comm,0) from emp ;
--order by clause
select * from emp order by sal;
select ename, deptno,sal from emp order by deptno ,sal desc;
--select ename, sal,deptno from emp order by sal,deptno desc;
select ename,mgr,sal from emp order by 3;
select ename , sal, sal*0.1 "PF", sal*05 "HRA", sal*0.3 "DA" from emp order by 3;
--Operator precedence
select ename,job,sal,deptno from emp order by job, deptno;
select ename,job,sal from emp where
job='CLERK' OR JOB='MANAGER' AND sal> 2500
order by job, deptno;
select ename,job,sal from emp where
( job='CLERK' OR JOB='MANAGER' ) and sal> 2500
order by job, deptno;

� Summarizing Data
� Using Aggregate Functions
� Using the GROUP BY clause
� Using the HAVING clause

select deptno,SUM(sal), AVG(sal), MAX(sal),MIN(sal) from emp group by deptno;


select SUM(sal) from emp group by job;
select job,comm from emp order by job;
select deptno,job, sum(sal) from emp group by deptno;
select job,sum(comm) from emp group by job;
;-- null columns not included in agg fun
select deptno, sum(sal) ,avg(sal) from emp
group by deptno
having deptno=20;

select deptno,avg(sal) " avg sal " from emp


group by deptno
having deptno in(10,20)

select job,min(sal) from emp


group by job
having job=�CLERK� and min(sal) <=2000;

select deptno,sum(sal) from emp


group by deptno having avg(sal)>2000

select max(avg(sal)) from emp group by deptno;

Syntax:
select column_name, group_fun(column) from table
[where condition]
[group by group_by_expression]
[having group_condition]
[order by column];
Rules :

� Columns listed in select statement apart from group by function have to be


listed in group by clause
� Columns listed in group by need not be listed in select statement
� Only group functions can be used in having clause
� Group functions listed in having clause need not be listed in select
statement

Spatiality of count functions. This is a group function.


There three forms of it, they are
� count(*)
� count(a_column_name)
� count(distinct column_name)

select count(*) from emp;


select count(comm) from emp;
select count(distinct comm) from emp;

select
count(*) "A",
count(job) "B",
count(comm) "C",
count(distinct job)"D"
from emp;

--single row functions /Scalar Functions

--char function (Character functions accept character input and return either
character or number.)

select length('mymaster') from dual ;


select Lower('MYMaster') from dual ;

select Lpad('Mymaster',10,'*') from dual; --**Mymaster

select Rpad('Mymaster',10,'*') from dual; --Mymaster**

select Rtrim('Mymaster','ster') from dual ; --Myma

select Ltrim('Mymaster' ,'myma') from dual ;--Mymaster

select Ltrim('Mymaster' ,'Myma') from dual ;--ster

select substr('mymaster',3,4) from dual ;

select chr(65) from dual ;

select ascii('A') from dual ;

select replace ('This and That', 'TH','b') from dual;-need exact match

select Translate('This and That', 'TH','b') from dual;

SELECT instr('Tech on the net', 'e') from dual; would return 2; the first
occurrence of 'e'

select ename from emp where soundex(ename) =soundex('MULLER'); -MILLER

---Number functions

select sqrt(16) from dual ;


SELECT ename, sal, MOD( sal, 100) FROM emp ;

select power(3,2) from dual;


select greatest(10,40,20) from dual ;
select least(10,30,50) from dual ;

select round (65.824,1) from dual;


select round (65.874,2) from dual;
select round (65.824,1) from dual;
select round (65.874,2) from dual;

select round (65.824) from dual;

select trunc(20.345,1) from dual;


select trunc(20.345)20 from dual;

Assignments:
� List all the details of employees
� List employee names, hire date, salaries, department numbers of all
employees.
� List name, sal , commission and department number of the employees who are
working in departments 20 , 30 and 40
� List the name , comm And job of the employees whose designation are either
clerk or salesman
� List the name and monthly income of the employees belonging to dept no 30.
� List the name & Designation of the employees who have the highest
designation.
� List the name, sal & designation of the employees who are having managers.
(people who are reporting to someone)
� List the name & Designation of the employees who have joined before Jan 82.
� List the names, designation & income for 10 years annual income of the
employees who are working in departments 10 & 30.
� List the name & experience (in years) of the employees who are working as
CLERKS.
� List the names & Mgr of the employees whose names should start with �A� & end
with �S�.
� List the names & salary of the employees whose names should start with �M�or
end with R.
� List the names & job of the employees whose names should contain �A� as the
second or third character & Ending with either �N�or�S�
� List the names of employees who have joined in the year 1982
� List the names, exp in yrs and comm. Of president. IF commission exists
display it otherwise display 0 for comm.
� List the details of dept where the location of the dept is in Boston
� List the details of the employees with the elaborated Column Headers
� List the names and salary of the employees who are earning more than 40 k per
annum
� List the names, job and hire date of the employees and sort them depending
on their exp in ascending order
� List the names, monthly income and annual income of the employees and sort
them in descending order depending on their annual income.
� List the different types of department numbers from the emp table
� List the different types of designations and sort them in ascending order
� List the name and salary of the employees who are earning between 1000 and
3000 and sort them depending on their sal and income
� List the empno, names and deptno and no of employees who have got exp of
more than 18 yrs.

Вам также может понравиться