You are on page 1of 19

Sql : (Structured query language )

Data: collection of facts and figure.


Database : A database is a special software that is purely
design for maintaing data.
Data redundancy and data in -consistancy:
Data integrity: This is about maintaing proper data and
maintaing paper data every organisation imposes a set of rules
on the data and we call these rules as buisness rule.Database
provide an option of imposing buisness rule with the help of
constraints and triggers.
SQL :
Sql divided into five parts:
1)DDL (Data definition Language) - create,Alter, drop, Truncate
2)DML (Data Manipulation Language ): DML deals with the
data directly and it contains three command -
insert,update,delete
3)DQL(Data Query language) : - select
4)DCL(Data control language): It deals with the securiity of the
data by setting the permission for user to access or restrict the
data. - grant,revoke,deny
5)TCL (Transaction control Language): -
commit,rollback,savepoint
Creating a Table :
=> table name must be unique under database
=> the name of the table should in range 1-128 character.
=>The maximum no of column can hae in the 1024.
To create a table CUSTOMER:
create table CUSTOMER (cust_id integer,Cname
varchar(30),balance decimal, city varchar(30))
How to insert value inside in CUSTOMER:
INSERT INTO CUSTOMER VALUES (100,'ABC',25.25,'kanpur');
How to view a data:
select * from CUSTOMER;
select cname from CUSTOMER where balance<5000;
update : update CUSTOMER set balance = 6000 where cust_id
=100;
while updating or deleting a record where condition is
mandatory for unique identification of the record.
delete: It is used to delete records from a table.
Syn: delete from CUSTOMER where cust_id =100;
Droping a table: It is a process of destroying the existance of
the object in the database.
drop table CUSTOMER;
Data Integrity : In computing ,data integrity refers to
maintaining and assuring the accuracy and consistent of data
ever its entire life and is an important feature of database
(RDBMS- relational data base management system)
Integrity constraints are basically divided into three types:
1.Entity integrity : it not allow multiple rows to have some
identity within a table.
e.g. Primary keys.
2.Domain Integrity: Retreive data to predefined data
types.e.g. Dates,check;
3.Refrential Integrity: Required existance of a related row in
another table.
e.g. foreign key
CONSTRINTS:
1.Not Null
2.Unique
3.primary key
4.check
5.foreign key
Note: constraints are imposed on column of a table.
1.Not Null: - if these constrints is imposed on a column
,column will not allow null values in it.
e.g. Insert into CUSTOMER values (100,'abc',5000,NULL);

SQL NULL constraints:


By default , a column can hold NULL.
Ex. create table CUSTOMER (ID integer NOT NULL, LName
varchar(30) NOT NULL)
insert into CUSTOMER values ( ,"asd");
Unique Constraints: in this constraints imposed on column,
that column will not allow duplicate value into it.
ex. create table CUSTOMER(cust_id unique, cname
varchar(30), bal decimal, city varchar(30))
Note : If we want to insert a duplicate value into a column
that contains unique constraints on id, the stmts got
terminated by the error message.
unique , primary,check, foreign key constraints are
independent object under the dbase which are created and
linked with the column of the table.
create table customer (cust_id int constraints cust_id unique)
check constraints: this constraints ensure that all values in a
column satisfy the certain condition .Once defined, dbase will
only insert a new row or update an existing row.
create table customer(id integer CHECK(id>0),
Lname varchar(30), Fname varchar(30)
)
Primary key constraints :
A primary key is used to uniquely identify each row in a
table .It is a condition of unique and Not Null which doesn't
allow NULL as well as duplicate values into a column or
columns. we can impose only one primary key constraints on
a table which can be either on a single or multiple columns .
Ex. create table CUSTOMER (cust_id constraints cust_id
primary key, cname varchar(30))

Foreign Key Constraints: This constraints is used for providing


referential integrity for the data that is present in a column or
columns that is it requires the existance of a related row in
another table.
Department Employee
DeptNo. Empno.
Dname Empname
Location job
salary
Deptno.
In the above case, the deptNo. of Department table is being
reformed under the employee table.So in this context
Department table is represented as Master or parent table
and the employee table is represented as Detailed table or
child table.
note: The common column that is present in both the two
tables need not have the same name but their dtype must be
same.
The common column that is present under the master table is
known as reference key column .It not contain duplicate
values

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

*** order by:


this class is used to arrange data in short in order with this
class ,we are using two keywords a) asc b) desc
e.g. select * from emp by sal
select * from emp order by sal desc.
Join:
Join are used to retrieve data from multiples tables. If we are
joining n tables then we are using (n-1) joining condition.
1)equi join/ inner join 2) Non equi join 3) outer join 4)
self join
These join are also called 8i join.
1)equi join/ inner join : Based on equlity condition we are
retriving data from multiple tables, here joining condition
column must belong to same data type.
->whenever tables contains column names then only we are
using equi join.
e.g. select ename,sal ,deptno, dname,loc from emp,dept
where emp.deptno = dept.deptno.
=>we can also use alias name
e.g. select ename,sal ,dept.deptno, dname,loc from emp,dept
where emp.deptno = dept.deptno.
Q1) write a query to display where the employee who are
working in the location chicago from emp,dept table.
select ename,loc from emp,dept where
emp.deptno=dept.deptno AND loc = 'CHICAGO'
Q2) write a query to display d.name, sum of sal from
emp,dept table
select d.deptno,d.name, sum(sal) from emp,dept where
e.deptno = d.deptno group by d.deptno,Dname

// select ename,sal deptno. ,deptname,loc from emp ,dept


where emp.deptno = dept.deptno. wrong
solution : select ename,sal ,dept.deptno,dname ,loc from emp
, dept where emp.deptno = dept.dept.no
Q) write a query to display where the employees who are
working in the location ' kanpur'
Outer join:
select ename,sal, d.deptno, dname, loc from emp where
where emp.deptno = d.dept.no
self join :
joining a table to itself is called self join.Here joining condition
columns must belong to same data type.If we want to
compare two different columns values in a single values then
we must use self join.
emp
eno eno1
10 10
20 30
30 40
emp
eno
10
20
30
1) WAQ to display the employee who are working in same city
as nitin .
select b.name,b.city from test a ,test b where a.city=b.city and
a.name = "nitin"
2)WAQ TO display the employee who are getting same salary
as ' manoj'
3) WAQ to display the employee name, manager names from
emp table.
select e1.name ,e2.name from emp e1, emp e2 where e1.mgr
= e2.mgr.
4) WAQ to display the employee who are getting same salary
in different dept from emp table.
select e1.ename,e1.sal ,e1.deptno ,e2.ename , e2.deptno,
e2.sal where e1.deptno = e2.deptno.

You might also like