Professional Documents
Culture Documents
create:
create table Student(sno number(10),name varchar(30));
desc student;
sno number(10)
name varchar(30)
To view the table in structure form :
desc <tablename>;
Alter :
It is used to change existing table structure.
=> add, modify and drop
-->add : It is used to add no of column into the existing table.
alter table student add(sec number(10))
=>desc student;
sno number(10)
name varchar(30)
sec number(10)
--->modify:
It is used to change column data type or column syntax.
ex. alter table student modify sno date;
desc student;
sno Date
name varchar(30)
sec number(10)
=>Drop:
It is used to remove column from the table .
Ex. alter table student drop column sno;
desc student;
name varchar(30)
sec number(10)
NOTE : If you want to drop a single or multiple column with
using parameters :
ex. alter table student drop (sno, name);
-->It is used to remove database of objects from database;
-->drop table student;
If you want to remove table permanently :
ex. drop table student purge;
==>Truncate:
It is used to delete total data from a table and also this data
permanently.
Ex. truncate table student;
Testing :
want to view all data;
select * from student;
Rename:
It is used to rename the table and rename the column name
also.
Ex. rename student to stu;
alter table student rename column sno to srNo ;
DML :
insert :
ex. insert into student values(1,'abc', 12)
1 row is inserted
select * from student;
sno name sec
1 abc 12
ex2. insert into student (sno, name) values (2,'bc');
alter table student add address;
select * from student;
sno name sec address
1 abc 12 NULL
Q. what is the difference between truncate and delete
command?
truncate is used to delete total data, delete is used to delete
the data also but stired in buffer.
Q 1) write a query to display ename,sal,annualsal from emp
table;
=> select ename,sal,sal*12 from emp;
Q2) Write a query to display a employee accept job as clerk
from emp table.
select * from emp where job = clerk;
Q3) Write a query to display the employee who are belong to
20,30,50 deptno from emp table.
=> select * from emp where deptno = 20 or deptno =30 or
deptno = 50;
Special operators:
1) in
select * from emp where deptno in(20,50,30);
select * from emp where ename in ('ab','bc');
2)Not in:
it doesn't work with null values.
3)between:
select * from emp where sal between 5000 and 2000;
Q4) Write a query to display ename, salary, commission from
emp table
=> select ename ,sal, commission from emp where ename =
'nikita';
Q5) write a query to display the emp who are not getting
commission from emp table.
=> select * from emp where comm is null;
Q 6) Write a query to display the employee where ename start
with capital letter M from emp table;
=> select * from emp where ename like 'M%';
o/p = Manoj
Q 7) Write a query to display the employee where secon letter
'L';
=>select * from emp where ename like '_L%'
Q8 ) Write a query to display those employee who are joining
in the year 97 from emp table
=> select * from emp where hiredate like '%97';
Q9 ) Write a query to display those employee who are joining
in the month of Dec from emp table
=> select * from emp where hiredate like '%Dec%';
Q10) write a query to display ename,salary, from emp table
where ename = 'smith'
=>select ename,sal from emp where ename = 'smith'
NVL function :
NVL is a predefined function which is used to replace or
suitable userdefined value.
Syntax : NVL(exp1,exp2)
->Here exp1,exp2 must belong to same data type
->If exp1 is null then it return exp2 otherwise it return exp1.
e.g. NVL(null,50) // output: 50
NVL(30,50) //output:30
solution: select ename,sal,comm,sal+NVL(comm,0) from emp
where ename='smith'
Ename sal comm sal+comm
smith 4000 ---- 4000
sal + NVL(comm,0)
==>If exp1, exp2 data type are not same then it doesn't work
so we have to go for type casting.
e.g. select ename,sal,comm,sal+NVL(to_char(comm),0) from
emp where ename='smith'
Group function or Aggregate function :
Max(): select max(sal) from emp;
e.g. select max(hiredate) from emp;
Min():
Avg():
select avg(sal) from emp;
Sum():
select sum(sal) from emp;
count(*):
It counts the number of rows in a table
e.g. select count(*) from emp;
count(column_name):
select count(empno) from emp;
select count(comm) from emp;
***Group by:
this class is used to arrangedsimilar data items into
set of logical group.
=>this class is used in select statements only.
e.g. select deptno,count(*) from emp group by deptno.
o/p :
deptno. count(*)
10 3
20 5
q1) write a query to display no of employee ,jobwise from
emp table using group by.
select job,count(*) from emp group by job;
o/p count(*)
clerk 4
salesman 3
Note: we can also use group by class without using group
function:
select deptno from emp group by deptno.
o/p:
deptno.
30
20
10
***Having:
after group by class we are not allowed to use where
class in place this one sql provide us
e.g. select deptno,sum(sal) from emp group by deptno having
sum(sal)>9000
o/p :
deptno sum(sal)
30 87000
20 1000780