002 Murali SQL Notes
002 Murali SQL Notes
Table:
table name naming conventions for object we can follow below conventions:
1. Employ-> entity
2. Employ->attributes - empno,ename,job,hiredate,sal,commission,deptno.
3. next datatypes :
ename:char(20)
Job:varchar2(15)
sal:Number(7,2)
hiredate:date
comm:Number(7,2)
Deptno:number(2)
Creation of Table:
(
<column name1><data type1>,
-------------------------------------
-----------------------------------
(id number(5),
branch varchar2(10),
section char(1) );
SQL>
table created:
SQL>desc emp;
Username-scott
password-tiger
Gkce students
studentid
name
branch
( cname varchar2(10),
city varchar2(10));
company
cname city
Tcs Chennai
cts hyderabad
(Account no number(5),
SBI BANK
Account no 11088676896
Branch name Sullurpet
Interest rate 1.25%
Account type savings
( mobile no number(11),
Airtel
Mobile no 9000001234
Account type Postpaid
Month bill RS.350.27
SQL
STRUCTURE QUERY LANGUAGE
SQL :- it is classified into 5 types they are
1)DDL-DATA DEFINATION LANGUAGE
2)DML-DATA MANIPULATION LANGUAGE
3)DCL-DATA CONTROL LANGUAGE
4)TCL-TRANSACTION CONTROL LANGUAGE
5)DQL/DRL
UPDATE:-
empnoenamesaldeptno
1234 VIJAY 2000 10
update emp set ename=’VIJAYRAM’ where empno=1234;
update emp set ename=’VIJAYRAM’,sal=5000 where empno=1234;
update emp set sal=(5000+sal)where deptno=10;
update emp set sal=(5000+sal),deptno=null where deptno=10;
DELETE:-
Delete from emp ;
emp records will delete
delete from emp where ename-‘VIAJY’;
truncate from emp; //auto commitable permanently deleted.
delete from emp; //not utocommitable(temporarly deleted).
1-Select:-
select *from emp;
*=>all records
select empno,ename from emp;
Projections, selections are possible in select statement .
select *from emp where deptno=10;
select *from emp where deptno=10 and sal>5000;
Quries:
write a query to display managers information ?
select *from emp where job=’MANAGER’;
waq to dtde who is getting salary more than 5000 and working as ANALYST ?
select *from emp where sal>5000 and job=’ANALYST’;
waq to dtde who is working in 10th department and totalsal<3500 ?
select *from emp where (sal+comm)>3500 and deptno=10 andcomm>500;
who is not getting comm ?
select *from emp where comm is null;
waq to dtde whose deptno=10 join before 1-JAN-1984 and working as MANAGER who is
getting some comm and MANAGER=7839 ?
select *from emp where deptno=10 and job=’MANAGER’ and hiredate<’01-JAN-1984’ and
comm is notnull and manager=7839;
waq to dtde whose sal between 3000 and 5000?
select *from emp where sal>3000 and sal<5000;
select *from emp where sal between 3000 and 5000;
waq to dtde whose sal not lies between 7000 and 1000?
select *from emp where sal not between 7000 and 10000;
waq to dtdewhos salary is 3000 or 4000or 5000 or 6000?
select *from emp where sal=3000 or sal=4000 or sal=5000 or sal=6000;
wqa to dtde who is working 10 th department as a manager or deptno20 as a analyst?
select *from emp where (deptno=10 and
job=’MANAGER’ )or(job=’ANALYST’anddeptno=20);
waq to dtde who is working under 7866?
select * from emp where mgr=7866;
waq to dtde who is working under some manager?
select *from emp where mgr is not null;
PATTERN SEARCH
LIKE:-
select *from emp where enamelike’v%’;
start with ‘v’ and end with’m’ .
select *from emp where ename like ’v%’and like ‘%M’;
select *from emp where enamelike’v%m’;
select *from emp where enamelike’v%’;
COMMIT:
update emp set sal=5000 where deptno=10;
commit;
* it saves the record into database permanently.
ROLLBACK:
update emp set sal=5000 where deptno=10;
rollback;
* if any database is performed except DDL command it retrives the previous stored data if
we use rollback.
*rollback should write before commit.
SAVE POINT:
insert 1;
insert 2;
.
.
.
.
.
insert 10;
save point A;
insert 30;
.
SQL FUNCTIONS
single row function Multirow function
Each row gives output and takes input then it is said to be single row functions.
Multirow function:
Multirow functions group of lines takes input and produce output is said to be Multirow functions.
Dual Table;
Dual table is a oracle provided table which will be used for normal calculation purpose.
Desc dual;
desc dual
D
X
1 row selected
Column name Dummy varchar2(1) and value is 'X' which consists of single row& Single column.
o/p->125
1 row selected
25*5
125
'
'
125
14 rows selected
CHARACTER FUNCTIONS
Upper(argument)->Always return character datatype.
Upper function takes one argument and converted into Upper case
Ex:
o/p-VIJAY
select upper(ename),upper(job) from emp;
all ename values & job values are converted into upper case
EX:
o/p-vijay
o/p->Vijay.
o/p-> 65.
LENGTH:
o/p->5.
CONCAT:
concatenate two strings vijay ram
o/p->vijayram
o/p->vijayramk
o/p->vijayramkb.tech
_____________________________________________________________________________________
select empno||'is getting salary'||sal||'and also commission||comm from emp;
___________________________________________________________________________________
o/p->ayaram
o/p->tech
o/p-> ch.
o/p->vijay.
o/p->vijayayara.
TRIM;
o/p->ijayaram.
LPAD:
vijay-5 characters.
o/p-> vijay**********
o/p-> *****vijay*****
REPLACE:
o/p-> vij1y1r1mk
o/p-> v1jay
_____________________________________________________________________________________
o/p-> null
____________________________________________________________________________________-
REVERSE:
o/p-> yajiv
INSTRING:
instr(char1,char2,(m),(n));
m- position
n- occurance
o/p-> 6
o/p-> 8
o/p-> 8
o/p-> 0
o/p-> 8
o/p-> 8
o/p-> 6
o/p-> 5
o/p-> 4
SUBSTRING:
substr(chr1,(m),(n))
m, n are optionals
chr1 M N o/p
vijayaram 1 8 vijayara
5 3 yar
7 - ram
-5 - yaram
ABS(n):-
select -8,124,abs(-8),abs(124) from dual;
-8,124,8,124
CEIL(n):-
select ceil(75)from dual;
if fraction is there it produce upper value.
FLOOR(n):-
select floor(7.5) from dual; o/p= 7
select florr(7.5) from dual; o/p=7
trunc(m,[n])
round(m,[n])
TRUNC(m,[n]):-
trunk(m,[n])
n number of digits after the decimal point.
it removes fractional parts.
select trunk(785.27333,2)from dual;
o/p =785.27
M N TRUNC O/P ROUND O/P
POWER:-
select power(2,3)from dual; o/p=8
select power(2,-3)from dual; o/p=0.125
select power(2,2)from dual; o/p=4
MOD(m,n):-
select mod(3,2)from dual; o/p=1
select mod(8,3)from dual; o/p=2
select mod(1,8)from dual; o/p=1
select mod(2,8)from dual; o/p=2
SQRT( ):-
select sqrt(25)from dual; o/p=5
EXP(n):-
select exp(2)from dual; o/p=7.3890561
LN( ):-
select ln(10)from dual; o/p=2.30258509
LOG(m,n):-
select log(10,100)from dual;
log 100=log
10 10 102 =2log1010 =2
LEAST LEAST
1 -3
LEAST LEAST
a saketh
VSIZE:-
vsize(empno);
Decode(expression,value1,return1,value2,return2,...........return n)
'
'
'
otherwise return
Gender,decode(gender, ,'male','f','female',
CASE:-
'
'
else return
end;
'
'
'
end;
_______________________________________________________________________________
_____________________________________________________________________________________
CONVERSION FUNCTIONS
TO_CHAR -> date in to character
TO_CHAR FUNCTIONS:-
o/p-> JANUARY
o/p-> DECEMBER
FEBRUARY
'
'
o/p-> DEC,FEB,.......
o/p-> dec,feb.........
o/p-> 1985,1981.....
9. select to_char(hiredate,'year') from emp;
jan,feb,mar=1
apr,may,jun=2
jul,aug,sept=3
oct,nov,dec=4
o/p-> WEDNESDAY,FRIDAY
1-JAN-4712BC
__________________________________________________________________________________-
o/p-> 2011
o/p-> 8
o/p-> 23
o/p-> error
_____________________________________________________________________________________
QUERIES:
* If we are not using trim it does not produce output because highest length is WEDNESDAY 10
characters but monday is 6 characters.
_____________________________________________________________________________________
o/p-> 9,999
o/p-> 10,000
o/p-> $00,800,$01,600,$01,250........
___________________________________________________________
o/p-> 9999
__________________________________________________________
_-> trim because to eliminate spaces because in MONTH section we have SEPTEMBER in MAY six spaces
are there so by using trim it eliminate spaces.
__________________________________________________________________________
MULTIROW FUNCTIONS
1) SUM:-
select sum(sal)from emp;
2) AVG:-
select avg(sal)from emp;
3) MIN:-
4) MAX:-
select max(sal)from emp;
5)COUNT:-
select count(*)from emp;
Any group function wouldn’t conside null value if null is there it will eliminate.
Sal
5000
7000
3000
2000
0
select max(sal)from emp; o/p is 7000.
select count(sal)from emp; o/p is 5 because it not count null value.
select count(*) from emp; o/p is 6 because it count null value.
User _tables:-how many tables created , when created ,memory no.of records ,.user ,owner,
related information.
Tab – less information.
user_tab_columns.
desc – sql*plus.
* all_tables,all_tab_columns,user_table,user_tab_columns,all_user.
matter table
Dict Dictionary
in this table data dictionary views information is stored.
Basic structure of select statement
select col1,col2,----------coln
from table 1,table 2,table 3---------table n
where <condition> filter conditions by using these we can filter like 10th dept,we can select
one dept
group<condition>
having class
having<condition>
order by<clause>
*group by class is there no need of having class.
*if having class is there means defneatly above having group by is there.
Order by class:-
1)Ascending order
2)Descending order
select *from emp where deptno=10 order by sal desc;
order by having more than one column.
select *from emp where deptno=10 order by sal desc,emp,asc;
empno ename sal deptno job
3 v 2000 10 developer
1 I 3000 10 analyst
2 j 5000 10 analyst
5 j 5000 10 ______
6 k ____ 10 _______
7 r _____ 10 _______
*)select *from emp where deptno=10 order by sal desc nulls last;
*)select *from emp where deptno=10 order by sal desc nulls first;
-)it prints all null values abpve because null is the high value is to remove null value last then we
nulls last.
select deptno,empno as empno,sal,job from emporder by emp_no desc;
we can use alias names in order by class.
we can’t use alias names in where,group,having .
order by class we can use alias names because it execute last so alias names already assigned
so we can use ‘ALIAS NAMES’in order by class.
GROUP BY CLASS
Select max(sal)from emp;
select max(sal)from emp groupby deptno;
select empno,ename,sal,decode(round(sal/1000),1,’*’,2,’**’,3,’***’,4,’****’,5,’*****’,’enter
number other than zero’)as’******’ from emp;
empno ename sal ******
7369 SMITH 800 *
1000 **
1250 *
2850 ***
2450 **
3000 ***
5000 ******
1500 **
1100 *
950 *
3000
Table paper:
_____________________________________________________________________________________
_______________________________________________________________
enter
password=manager
show user=sys
enter newpassword=tiger
_____________________________________________________________________________________
SQL*PLUS SETTINGS:
SQL*plus:-
clscr:- no semicolon
SQL>@login
ORACLE RDBMS
DATA:- Some information if just like characters.
Information:
Management system:-
RDBMS
(4G)SQL (3G)SQL
editor
(SQL engine)
SQL engine interpeter command gives in editor and manipulates the data.
CUI
ISQL * +
+OAD
SQL Navigator
PL/SQL developer
TABLE:-
DCL:-
only owner can drop the table other user doesnot possible to drop that one.
scott->vijay->Ram->emp
vijay can give permission to other user because here we are mention grant option .
We want to see data dictionary view to see what permissions are given to other users.
5. WAQ to DTDE who is getting some comm and working as manager and sal<5000?
select * from emp where comm is not null and comm <>0 and job='MANAGER' and sal<5000;
7. WAQ to DTDE for the employees who's name consists exactly 4 characters?
USAGE OF ESCAPE:
if ename like '%mur%i' if we want search the '%' symbol in ename then we use escape if we write like .
ename empno
mur%ali 1234
m%ural%i 1245
%rali 3678
murali 7658
ravi 1237
ename empno
mur%ali 1234
m%ural%i 1245
%rali 3678
If we want to print ename who's name starts with '%' then this query will not for that we use escape
ename empno
%rali 3678
ename empno
murali% 2
_________________________________________________________________________
___________________________________________________
AUXULIARY COLUMN:
empno,ename,sal,annsal,experience,deptno
Auxuliary columns which does not exist in real emp table.
sal*12 annsal
DUAL TABLE:
value-> X
D
X
1 row selected.
_________________________________________________________________________________
WAQ to add ***** before the ename 5 stars after the ename of the employee emp table:-
___________________________________________________________________________________
Feedback is used for how many rows are selected while executing select statement.
cl scr;
VIJAY> L
VIJAY> L
VIJAY> R
VIJAY>c/8/*
here
c-> change
VIJAY>select
2 * from
3 emp
4 where
set feedback 3
set echo on
>4
4 * where
>5
5 * deptno=10;
____________________________________________________________________________
SPOOL option:-
_____________________________________________________________________________
______________________________________________________________________________
_________________________________________________________________________________
VIJAY> ed
editor is open
VIJAY> /
VIJAY> ed login.sql
login.sql file - if we write anything then it executes at every time whenever we login in oracle.
ed login.sql -> set line 200
cl scr
Alt + F4
_____________________________________________________________________________
Tab:
select * from DBA_tables ; -> All information about table master table details.
_________________________________________________________________________________
comments
__________________________________________________________________________________