Sie sind auf Seite 1von 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 Syntax of CREATE Statement
identify the
columns which Create table <tablename>
make up the (< Column 1> <Datatype>(Size),
(< Column 2> <Datatype>(Size),
table with all
(< Column 3> <Datatype>(Size),
definitions (< Column n>
 Tablename <Datatype>(Size));
should be
unique
 Tablename
should start
Step II :Insertion of
data
 We use the Insert
Statement for this
job
 The order of the
columns in this
command must be Syntax of INSERTStatement
the same while Insert into <tablename> values
creating the table (Col1,Col2,Col3…..Col n);
 You should not skip
any column values,
type NULL in case
you want to omit a
column value
 Char, Varchar and
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

STUDENT

R o lln o Snam e B ra n c h S e m is t e r P honeno


N u m b e r(3 ) V a rc h a r2 (2 0 ) V a rc h a r2 (2 0 ) (V a rc h a r2 (1 0 ) N u m b e r(7 )

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


Step III:Retrieval of
 We use the Select
data
Statement for this job
 You can select all rows
Syntax of SELECT Statement
and columns from the
table or select as you Select * from <Tablename>
wish { Where <Condn>}
 You can restrict the Select col1,col2...from
data retrieved using a <Tablename> { Where <Condn>}
WHERE Clause
 Operators used in
SELECT
 Relational Operators
 Logical Operators
Operators used in SQL

Relational : >, < , = , !=, >= , < =

Logical : AND, OR, NOT

SQL : LIKE, IN, BETWEEN.. AND


Functions

Single Row Group functions


Character Sum()

Number Max()
Min()
Date
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
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 Ename Esal Deptno Grade Losal
Hisal
100 Anil 4000 10 1 2000
2999
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 Ename Esal Deptno Mgr
100 Anil 4000 10 102
101 Ajay 3200 20 103
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 L
100 Anil 4000 10 10 Sales Chennai
101 Ajay 3200 20 20 Accounts Mu
102 John 2800 10 30 Finance De
103 Allen 5400 20 40 R&D Calcutta
104 Sanjay 5800 30
Oracle supports Outer-Join indicator (+) on the LHS of the = Operator o
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-


JAN-98'))
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

Das könnte Ihnen auch gefallen