Sunteți pe pagina 1din 2

1.Where clause in select statement: used for conditions.

---------------------------------------------
SQL>select sal,deptno,job from emp where sal>1000;
2.Group by clause in select statement:
-------------------------------------------------
used to group data based on columns specified in
group by clause.
when we use columns along with group functions
in select statement ,we have to specify all normal
columns in group by clause.
sql>select deptno,job from emp;
sql>select count(*),sum(sal),min(sal),max(sal),avg(sal) from emp;
--group functions
sql>select deptno,job,count(*),sum(sal) from emp; --error
sql>select deptno,job,count(*),sum(sal)
from emp group by deptno,job;
--------------------------------------------------------------------------------
--------------
3.Having clause in select statement:
----------------------------------------------
It is used to specify group functions in conditions.
where clause will not allow group functions.
Invalid statement:
-----------------------
sql>select deptno,job,count(*),sum(sal)
from emp where sal>1000 and count(*)>1
group by deptno,job;
Valid statement:
----------------------
sql>select deptno,job,count(*),sum(sal) from emp
group by deptno,job having count(*)>1;
--------------------------------------------------------------------------------
---------------
4.Order by clause in select statement:
------------------------------------------------
Used to arrange data either in ascending order or
descending order.
By default it is ascending order.
for descending order we use 'desc' keyword.
It should be the last clause.
sql>select deptno from emp order by deptno;
sql>select * from emp order by deptno desc;
sql>select deptno,job,count(*),sum(sal)
from emp where sal>1000 group by deptno,job having count(*)>1
order by job desc;
select statement execution order:
-------------------------------------------
checks for table -> where clause -> group by ->
group functions -> having clause -> order by -> display order.
======================================================
joins:
-------
used to retrieve data from multiple tables without duplicate data.
There are 5 types of joins:
To perform join we need common column and join condition.
Common column should be denoted with tablename.
1. equi join: used to generate matching records from
---------------- multiple tables.
sql>select empno,ename,dname,loc,emp.deptno from emp,dept where emp.deptno = dep
t.deptno;
SQL>select empno,ename,dname,loc,emp.deptno from emp
JOIN dept ON(emp.deptno = dept.deptno);
2. non-equi join: used to generate unmatched records
--------------------- from multiple tables.
sql>select empno,ename,dname,loc,EMP.DEPTNO from emp,dept
where emp.deptno != dept.deptno;
3. left outer join: Used to display extra records of
---------------------- left side table also along with
matching records.
sql>select empno,ename,dname,loc,dept.deptno
from emp left outer join dept on(emp.deptno = dept.deptno);
4. right outer join: Used to display extra records of
-------------------- right side table also along with
matching records.
sql>select empno,ename,dname,loc,dept.deptno
from emp right outer join dept on(emp.deptno = dept.deptno);
5. full outer join: Used to display extra records of
--------------------- both tables also along with
matching records.
sql>select empno,ename,dname,loc from emp
full outer join dept on(emp.deptno = dept.deptno);
======================================================
self join: Join based on single table.
-----------
display emp working under which manager?
sql>select e.empno empno,e.ename empname,
m.empno managerNo, m.ename mangername
from emp e, emp m
where m.empno = e.mgr;
table alias: used to create another duplicate table in memory for
-------------- convinience purpose.
======================================================

S-ar putea să vă placă și