SQL
table students
rollno name age class fee sex dob percentage city
11101 Bhim 18 XI-A 3000 M 2002-04-08 86 Satna
11102 Rani 18 XI-A 1800 F 2002-05-07 75 Rewa
11103 Bharat 18 XI-A 3000 M 2002-04-18 86 Katni
11104 Laxmi 17 XI-A 1800 F 2003-10-09 73 Rewa
Insert Command
SQL> insert into <table name> values ( value1, value2, value3, value4,value5);
Example
SQL> insert into students values ( 11101, 'Bhim',16, 'XI-A',3000,’M’,’2002-04-08',86, 'satna');
when we don’t have all columns value then
rollno name age class fee sex dob percentage city
12110 Ramesh 18 XII-A 3000 M
We have 2 following methods
Method 1. ( we write null explicitly)
SQL> insert into students values ( 12110, 'Ramesh',18,’XII-A’,3000,’M’, null,null,null);
Method 2. (we give column names with table name and gives the values for same columns. In this method column
sequence can be changed)
SQL> insert into students(rollno, name,age, class,fee,sex) values ( 12110, 'Ramesh',18,’XII-A’,3000,’M’);
SQL> insert into students(rollno, name, class, sex, age, fee) values ( 12110, 'Ramesh',’XII-A’, ‘M’, 18, 3000);
both insert command will insert the same values.
Try to insert following values using both methods
rollno name age class fee sex dob percentage city
12111 Suresh 19 XII-A M 2002-04-08
12210 Mohan 20 XII-B 2700 M Satna
10109 Kirti 17 X-A 1800 F
10208 Sonal 18 X-B F
To see values from table :- we use select command.
SQL> select * from <table name>;
SQL> select * from students;
it will show all data from table.
To see some selected columns from table we use following method.
SQL> select <col1, col2, col3......> from <table name>;
//in this statement col1, col2 ….. are the names of selected columns of table.
SQL> Select rollno, name, age, class from students;
// only two columns will be displayed.
We can give any sequence of column names of table.
SQL> select name, rollno, class, age from students;
To find some specific values from table :- we use where clause
SQL> select * from students
where age >=17 ;
SQL> select name, age from students
Where age>=17 ;
To create a condition we can use and operator (when we use ‘and’ , result will come when all conditions that are written
must be true )
SQL> select name, age from students
Where age>=16 and age<=19 ;
SQL> select name, age from students
Where name=’Vijay’ and age<=19 ;
SQL> select name, age from students
Where name=’Vijay’ and age >=19 ;
For some specific condition we can use or operator (when we use ‘or’ , result will come when any condition is true )
SQL> select name, class, fee from students
Where fee=3000 or fee=2700 ;
SQL> select name, class, fee from students
Where name=’Sachin’ or name=’Sourav’ ;
SQL> select name, class, fee from students
Where name=’Sachin’ or name=’Sanjay’ ;
selectename, sal from students
wheresal>= 1000 and sal<= 3000 ;
selectename, sal from students
wheresal between 1000 and 3000 ;
selectename, sal from students
wheresal=5000 or sal=3000 ;
selectename, sal from students
wheresal in(5000,3000) ;
selectename, sal from students
whereename='seth' ;
selectename, sal from students
whereename like 's%' ;
selectename, sal from students
whereename like '%a' ;
selectename, sal from students
whereename like '_ _s%' ;
selectename, sal from students
whereename like '_ _ _ _ _' ;
selectename, sal from students
whereename not like 's%' ;
selectename, sal from students
whereename not like '%a' ;
selectename, sal from students
whereename not like '_ _s%' ;
selectename, sal from students
whereename not like '_ _ _ _ _' ;
selectename, sal from students
whereename like '%s%' ;
select ename, studentsno, deptno from students
order by ename ;
selectename, studentsno, deptno from students
order by ename desc;
selectename, studentsno from students
order by deptno desc ;
selectename, studentsno, deptno from students
order by deptno desc , ename;
selectename, studentsno, deptno from students
order by deptno , ename desc;
selectename, studentsno, deptno from students
order by deptnodesc, enamedesc;
select sum(sal) from students;
select min(sal) from students;
select max(sal) from students;
select count(sal) from students;
select count(comm) from students;
select count(*) from students;
working with NULL ( if we want to use NULL as filter condition then we use is in place of = )
selecte name from students
where comm is null;
selecte name "Name of Studentsloyee", sal+comm "Total Salary" from students;
select deptno, sum(sal) from students
group by deptno ;
delete from <table name>;
delete from <table name>;
delete from students;
delete from <table name>
where .............. ;
delete from students
where age>18;
truncate table <table name>;
drop table <table name>;
drop table std;
update <table name>
set<col name> = <new value>
where .................. ;
update students
set sal=sal+500
update students
set sal=sal+500
where depetno=10;
update students
setsal=sal+500, comm=comm+50
wheredepetno=10;
update students
set comm=50
wheredepetno=10 and comm is null ;
update students
set comm=50
wheredepetno=10 and ename='Akshay' ;
Create table exam2
(
Rno integer,
Pt1 integer,
Pt2 integer,
Hy integer,
Foreign key (Rno) references students(rollno)
);