You are on page 1of 27

SQL

Structured Query
Language
SQL is the language
used to communicate
with the Oracle Database.

SQL was developed by IBM and later


acquired by Oracle

SQL is a non-Procedural language, you


specify what info you want, not how to get it

Why only SQL?


SQL is not a language,but, a set of
commands
SQL is easy to understand as it
contains simple words like
SELECT,INSERT.
SQL is used by a variety of users.

SQL Command Set


Data Definition Language Commands
Data Manipulation Language
Commands

Data Control Language Commands

Step I : Creation of a table


To create a table first
identify the columns
which make up the
table with all
definitions
Tablename should
be unique
Tablename should
start with a alphabet
Tablename cannot
exceed 30 chars

Syntax of CREATE Statement


Create table <tablename>
(< Column 1> <Datatype>(Size),
(< Column 2> <Datatype>(Size),
(< Column 3> <Datatype>(Size),
(< Column n>
<Datatype>(Size));

Step II :Insertion of data


We use the Insert
Statement for this job
The order of the columns in
this command must be the
same while creating the
table
You should not skip any
column values, type NULL
in case you want to omit a
column value
Char, Varchar and Date
values should be enclosed
in Single Quotes

Syntax of INSERTStatement
Insert into <tablename> values
(Col1,Col2,Col3..Col n);

Example for Creation of


table named EMP
SQL > Create table emp
( empno number(3),
ename varchar2(20),
esal nuber(7,2),
doj date,
desgn varchar2(20),
deptno number(3));
Now, our table is created,we insert data
next

Insertion of data into EMP


table
EMP table consists of
Empno,Ename,Esal,Doj,Desgn,Deptno
SQL> Insert into EMP values
(100,Anil,5000,12-Jan-99,Salesman,10);
SQL> Insert into EMP values
(101,Ajay,3000,13-Jan-99,Clerk,20);
SQL >Insert into EMP values
(100,Raj,9000,15-Jan-99,Manager,10);

Create a table with the following


specifications and add about 10 Records
S t r u c t u r e o f S T U D E N T T a b le
STUDEN T
R o lln o
N u m b e r(3 )

Snam e
V a rc h a r2 (2 0 )

B ra n c h
V a rc h a r2 (2 0 )

S e m is t e r
(V a rc h a r2 (1 0 )

P honeno
N u m b e r(7 )

We are now all set to Query records from the database.

Step III:Retrieval of data


We use the Select
Statement for this job
You can select all rows and
columns from the table or
select as you wish
You can restrict the data
retrieved using a WHERE
Clause
Operators used in SELECT

Relational Operators
Logical Operators
SQL Operators

Syntax of SELECT Statement


Select * from <Tablename>
{ Where <Condn>}
Select col1,col2...from
<Tablename> { Where <Condn>}

Operators used in SQL


Relational : >, < , = , !=, >= , < =
Logical : AND, OR, NOT
SQL : LIKE, IN, BETWEEN.. AND

Functions

Single Row

Group functions
Character
Sum()
Number
Date

Max()
Min()
Avg()

Conversion

Count()

Group Functions
These functions operate on a set of records or on the
entire table. They are therefore called as Group or
Aggregate Functions
Sum() SQL> Select sum(sal) from emp;
Avg() SQL> Select avg(sal) from emp;
Max() SQL> Select max(sal) from emp;
Min() SQL> Select min(sal) from emp;
count() SQL> Select count(*) from emp;

Group Functions
Sum() SQL> Select sum(sal) from emp where deptno =
10;
Avg() SQL> Select avg(sal) from emp where
desgn=Clerk and deptno = 10;
Max() SQL> Select max(sal) from emp where deptno in
(10,20,30);
Min() SQL> Select min(sal) from emp where doj like
%98;
count() SQL> Select count(*) from emp where sal
between 3000 and 5000;

GROUP BY CLAUSE

Joins

A Select Statement can be called a Join if,


first, the FROM clause names at least two
table specifications and second,if the WHERE
clause has at least one condition that
compares columns from the different tables.
Types of Joins:
Equi-Join
Non-Equi Join
Outer-Join
Self Join

Examples of Joins
Example of Equi-Joins :
Display the empno,name, deptno and dname .
SQL> Select empno,name,emp.deptno,dname from
emp,dept where emp.deptno=dept.deptno

The table name is specified before deptno in the


Select Statement as deptno is ambigious,since it is
present in both the tables.

Example of Non-Equi Join :


Display all employees falling under Grade 3
SQL> Select emp.empno,ename,esal from emp,sal
where grade=3 and esal between hisal and losal;
EMP
SAL
Empno
100
101
102
103
104

Ename Esal Deptno


Anil 4000 10
Ajay 3200 20
John 2800 10
Allen 5400 20
Sanjay 5800 30

Grade
1
2
3

Losal
2000
3000
5000

Hisal
2999
4999
5999

Example of Self Join :


Display all employees who are drawing a salary more
than that
of their managers.
SQL>Select w.ename,w.esal,m.ename,m.esal from emp
w,emp m where w.sal>m.sal and w.mgr = m.empno;
EMP
Empno
100
101
102
103
104

Ename Esal Deptno


Anil 4000 10
Ajay 3200 20
John 2800 10
Allen 5400 20
Sanjay 5800 30

Mgr
102
103
101
101
102

Example of Outer-Join :
Display all departments which are not present in the EMP table.

SQL>Select empno,ename,dept.deptno from emp,dept


where emp.deptno(+) = dept.deptno;
EMP
DEPT
Empno Ename Esal Deptno
Deptno Dname
Loc
100
Anil 4000 10
10
Sales
Chennai
101
Ajay 3200 20
20
Accounts
Mu
102
John 2800 10
30
Finance
Delhi
103
Allen 5400 20
40
R&D
Calcutta
104
Sanjay 5800 30
Oracle supports Outer-Join indicator (+) on the LHS of the = Operator only

Integrity Constraints
Integrity Constraints are the rules with which the
contents of a database must comply at all times,
and they describe which updates to the database
are permitted
Types : Table Constraints, Column Constraints
Not Null
Primary Key
Unique
Check
Foreign Key

Table creation using column


constraints
SQL > CREATE TABLE CONS
(EMPNO NUMBER(3) CONSTRAINT PK_KEY PRIMARY KEY,
ENAME VARCHAR2(10) CONSTRAINT UNQ_KEY UNIQUE,
DOJ DATE CONSTRAINT CHK_DAT CHECK(DOJ > '1-JAN98'))

Table creation using table


constraints
SQL > create table cons1
(empno number(3) constraint pk1_key primary key,
ename varchar2(10) constraint unq1_key unique,
esal number(7,2),
comm number(4),
constraint chk_sal check((esal+comm) > 5000))

SUB-QUERIES
These are used to specify a subset of rows
whose defining characteristics
(WHERE Clause) are dependent on the values
returned from another Query
Syntax:
Select col1,col2,col3.. From table
Where column= ( Select column from table
where <condn>);

Examples for Sub-queries


1. Display all employees working in the same Dept as Allen.
SQL> Select * from emp where deptno =
(Select deptno from emp where ename = Allen);
2. Display the details of the highest paid employee.
SQL> Select * from emp where esal =
(Select max(sal) from emp;
3. Display the employees who earn more than the highest paid
employee in deptno 30
SQL > Select * from emp where esal >
(Select max(sal) from emp where deptno = 30);

Examples for Sub-queries


4. Display all employees drawing more than than the average sal
SQL> Select * from emp where esal >
(Select avg(sal) from emp);
5.. Which Employees in New York draw a salary more than Scott
SQL> Select empno,ename,job,sal,dept.deptno from emp,dept
where dept.deptno = emp.deptno and loc = New York and
esal > (Select esal from emp where ename = Scott;
6.. Display all emp who have same job as Clark or have a salary
greater than his salary
SQL > Select * from emp where job = ( Select job from emp where
ename = Clark) OR esal > (Select esal from emp where ename =
Clark);

Thank You

You might also like