Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Structured

Query Language

Overview of SQL
It

is a 4 GL
Oracle9i in specific
DML
DDL

Basic SQL Commands


Stateme
nt
SELECT
INSERT
UPDATE
DELETE
CREATE
ALTER
DROP
RENAME
COMMIT
ROLLBA
CK
SAVEPOI
NT
GRANT
REVOKE

Description

Data retrieval statement.


Data Manipulation Language (DML).
Add rows, change data, and delete few
rows.
Create new tables/views, remove tables/
views, and change the schema.
Modified values of database are
permanently written into disk, rollback
the changes made.
Access control can be assigned or
changed.

Basic data types in Oracle


Data type

Description

CHAR (size)

Fixed length character. Max =


2000.
VARCHAR2(siz Variable length character string.
e
Max = 4000.
DATE

Date. Vaild range is from Jan 1,


4712 B.C. to Dec 31, 4712 A.D.

NUMBER(size) Numbers. Max. size = 40 digits.


NUMBER(size, Numbers. Range = 1.0E-130 to
d)
9.9E125.

Example tables
Employee
SSN
1111
2222
3333
4444
5555

Sala MgrSS
Name
BDate
ry
N
2200
Deepak
5-Jan-62
4444
0
10-Dec- 3000
Gopal
4444
60
0
1800
Pooja
22-Jan-65
2222
0
3200
Prasad Department
11-Jan-57
Null
0
DNo
DName 15-JanLoc
8000 4444
1 Reena
Admin
Chennai
85Bangalore
2
Research
3

Accounts

Bangalore

DNo
1
3
2
3
3

DDL
CREATE TABLE Department(
DNo number(3) not null,
DName varchar2(10) not null,
Loc varchar2(15),
primary key (DNo));
CREATE TABLE Employee(
SSN number(4) not null,
Name varchar2(20) not null,
BDate date,
Salary number(10,2),
MgrSSN number(4),
DNo number(2) not null,
primary key (SSN),
foreign key (MgrSSN) references Employee(SSN),
foreign key (DNo) references Department(DNo));

Data Retrieval Statement


(SELECT)

Syntax

SELECT *|{[DISTINCT] column | expression}


FROM table(s);
The basic SELECT statement must include the
following:
- A SELECT clause.
- A FROM clause.

Example-1

SELECT * FROM

Employee;

The * indicates that it should retrieve all the


columns from Employee table. The output of
this query is shown below:
Output-1
SSN NAME
BDATE
SALARY
MGRSSN
DNO
---- -------------------- --------- --------- --------- --------4444 Prasad
11-JAN-57
32000
3
5555 Reena
15-JAN-85
8000
4444
3
1111 Deepak
05-JAN-62
22000
4444
1
2222 Nandagopal
10-DEC-60
30000
4444
3
3333 Pooja
22-JAN-65
18000
2222
2

Example-2

SELECT * FROM Employee


ORDER BY SSN;
Output-2
SSN NAME
BDATE
SALARY MGRSSN
DNO
----- -------------------- --------- --------- --------- --------------------------------1111 Deepak
05-JAN-62
22000
4444
1
2222 Nandagopal
10-DEC-60
30000
4444
3
3333 Pooja
22-JAN-65
18000
2222
2
4444 Prasad
11-JAN-57
32000
3
5555 Reena
15-JAN-85
8000
4444
3

Using

arithmetic operators

SELECT Name, Salary, Salary * 12


FROM Employee;
Using

aliases
An alias when used for a column:
Renames a column heading
It is useful in arithmetic calculations.
AS keyword can optionally be used between
column name and alias name.
Example-3
SELECT Name, Salary, Salary * 12 AS YRLY_SALARY
FROM Employee;
OR
SELECT Name, Salary, Salary * 12 "YRLY_SALARY"
FROM Employee;

Example-4

DESCRIBE Employee;

OR

DESC Employee;
Output-4

Name
------------------------------SSN
NAME
BDATE
SALARY
MGRSSN
DNO

Null?
-------NOT NULL
NOT NULL

Type
---NUMBER(4)
VARCHAR2(20)
DATE
NUMBER(10,2)
NUMBER(4)
NOT NULL NUMBER(2)

Select Statement with Where


Example-5

SELECTName, Salary
FROM Employee
WHERE Salary > 25000;
Example-6
SELECTDName, Loc
FROM Department
WHERE Loc = 'Bangalore';
Example-7
SELECTName, BDate
FROM Employee
WHERE BDate = '11-Jan-57';

Example-8

SELECT Name, BDate


FROMEmployee
WHERE Salary BETWEEN 25000 AND 30000;
Example-9
SELECT SSN, Name
FROMEmployee
WHERE DNo IN (1, 2);
Example-10
SELECT Name
FROMEmployee
WHERE Name LIKE 'P%';
Example-11
SELECT Name, DNo
FROMEmployee
WHERE BDate LIKE '__-JAN-__';

Example-12

SELECT Name
FROMEmployee
WHERE MgrSSN IS NULL;
Example-13
SELECT Name, Salary, DNo
FROMEmployee
WHERE Salary > 30000 AND DNo = 3;
Example-14
SELECT Name, Salary
FROMEmployee
WHERE Name LIKE 'P%' OR Salary <= 20000;
Example-15
SELECT
Name, Salary, DNo
FROM Employee
ORDER BY DNo DESC, Name;

Working with Dates


Century 19
Year
99
Month 07
Day 23
Hour
4
Minute 10
Second 53

SELECT SYSDATE
FROM DUAL;

Example-16 (MONTHS_BETWEEN)
SELECT MONTHS_BETWEEN(SYSDATE, '09-JAN-1983')
"Experience"
FROM DUAL;
Output-16
Experience
--------------247.73471
Example-17 (GREATEST & LEAST)
The function GREATEST finds the earliest date and LEAST
finds the oldest date in the list.
SELECT GREATEST('10-JAN-93', '10-JAN-98'),
LEAST('10-JAN-93', '10-JAN-98')
FROM DUAL;
Output-17
GREATEST( LEAST('10
--------- --------10-JAN-98 10-JAN-93

Use of TO_DATE

TO_DATE function is to convert any character literal string


into a valid date format.
Example-22
SELECT
TO_DATE('1-Sep-2003', 'DD/MM/YYYY')
FROM DUAL;
Output-22
TO_DATE('
--------01-SEP-03

Example-23
SELECT
TO_DATE('08/30/2003', 'DD/MM/YYYY')
FROM DUAL;
Output-23
ERROR at line 1:
ORA-01843: not a valid month

Character Functions
Program
Output
SELECT LOWER('Bangalore') FROM
bangalore
DUAL;
SELECT UPPER('Bangalore') FROM
BANGALORE
DUAL;

SELECT INITCAP('bangalore institute Bangalore Institute Of


of technology') FROM DUAL;
Technology

SELECT CONCAT(Hello',Dear') FROM Hello Dear


DUAL;

SELECT SUBSTR('Database', 5, 4) base


FROM DUAL;
SELECT LENGTH('Database') FROM
8
DUAL;

SELECT INSTR('Database', 'b')


5
FROM DUAL;

SELECT INSTR('Database', 'x') FROM 0


DUAL;

19

SELECT LPAD(Salary, 8, '*') FROM


Employee
WHERE SSN = 1111;
SELECT RPAD(Salary, 8, '*')
FROM Employee
WHERE SSN = 1111;
SELECT LTRIM(' Database', ' ')
FROM DUAL;
SELECT RTRIM('Database...', '.')
FROM DUAL;
SELECT REPLACE('Database',
'base', 'type')
FROM DUAL;

12/18/15

***22000

22000***

Database

Database

Datatype

Aggregate Functions
COUNT
AVG
MAX
MIN
STDDEV
SUM
VARIANCE

Example-24

SELECT COUNT(*) AS "No. of Employees"


FROM Employee;
Example-25

SELECT SUM(Salary) AS Total


FROM Employee;
Example-26

SELECT Name, MAX(Salary), MIN(Salary)


FROM Employee;

GROUP BY Clause
The rules to be followed while using
GROUP BY clause are given below:
You can't have non-group function or
column in SELECT clause.
Group functions ignore nulls.
By default the result of GROUP BY
clause sort the data in ascending
order.
Example:
SELECT DNo, SUM(Salary), COUNT(*), AVG(Salary)
FROM Employee
GROUP BY DNo;

Example-27

SELECT DNo, SUM(Salary), COUNT(*),


AVG(Salary)
FROMEmployee
GROUP BY DNo, MgrSSN;
HAVING clause
SELECT DNo, AVG(Salary)
FROM
Employee
GROUP BY
DNo
HAVING
DNo = 3;

The order of evaluation when all the clauses


are specified is given below:
1. First all rows matching the WHERE
conditions are retrieved.
2. These rows are grouped using the
column(s) in GROUP BY clause.
3. Finally, groups matching the HAVING
clause condition are retained.
SELECT DNo, AVG(Salary)
FROM
Employee
WHERE BDate LIKE '__-JAN-__'
GROUP BY DNo
HAVING
MAX(Salary) > 10000;

MULTITABLE QUERIES
Simple
Table

Equi-Joins : guidelines

names in the FROM clause is separated


with commas.
Use appropriate joining condition. This means
that the foreign key of table1 will be made
equal to the primary key of table2.
When the attributes or columns have the
same names, tag them with table names
using dot notation.
Without proper joining condition or attributes
the SQL will display the Cartesian product of
the tables in the FROM clause.

Example-28:

Display the employee


names and the department names for
which they work.
SELECT Name, DName
FROM Employee, Department
WHERE Employee.DNo = Department.DNo ;

Example-29

: Display only employees


working for Accounts department.
SELECT Name, Salary, DName
FROM Employee, Department
WHERE (Employee.DNo = Department.DNo)
AND (DName = 'Accounts');

Self-Join and Table


Aliases
Example-30

: Find the employee


who earns more than
Nandagopal.
SELECT e1.Name, e1.Salary
FROM
Employee e1, Employee e2
WHERE (e1.Salary > e2.Salary) AND
(e2.Name = 'Nandagopal');

Right-Outer Join
Example-31

:
SELECT Name, DName
FROMEmployee E, Department D
WHERE E.Name = D.DName(+);
Output-31
NAME
DNAME
-------------------- ---------Deepak
Nandagopal
Pooja
Prasad
Reena

Left-Outer Join
Example-32

SELECT Name, DName


FROM Employee E, Department D
WHERE E.Name(+) = D.DName;
Output-32

NAME
DNAME
-------------------- ---------Accounts
Admin
Research

NESTED QUERIES or SUB QUERIES


SELECT <column(s)>
FROM table
WHERE <condn operator>
(SELECT <column>
FROM table);
Outer

query uses the result of the inner query.


If the inner query returns only one row it is called
as single row sub queries.
Alternatively if the inner query returns a set of
rows (more than one row) it is classified as
multiple-row sub queries.
The comparison condition may be a single row
operator like >, =, >=, <, <=, <>) or multiple
row operators like IN, ANY, ALL.

Single-Row Nested Queries

Display the names of the employees working for


Accounts department.
SELECT Name
FROM
Employee
WHERE DNo =
(SELECT DNO
FROM
Department
WHERE DName = 'Accounts');

Display names of employees whose salary is greater


than the employee SSN=1111.
SELECT Name
FROM
Employee
WHERE
Salary >
(SELECT Salary
FROM
Employee
WHERE Name = 1111);

Example-33
Display

all the employees drawing more than or


equal to the average salary of department
number 3.
SELECT Name, Salary
FROM
Employee
WHERE Salary >=
(SELECT AVG(Salary)
FROM Employee
GROUP BY DNO
HAVING DNo = 3);

Multiple-Row Nested
Queries

IN: Equal

to any member in the list.


ANY: Compare value to each value returned by
the subquery.
ALL: Compare value to all the values returned by
the subquery.

Display the name of the highest paid employee.


SELECT Name, Salary
FROM
Employee
WHERE Salary =
(SELECT
MAX(Salary)
FROM Employee);
SELECT Name, Salary
FROM
Employee
WHERE Salary IN
(SELECT
MAX(Salary)
FROM Employee);

Both '=' and 'IN' works, because the inner


query produces a single
tuple.

Find

the Name and Salary of people who


draw in the range Rs. 20,000 to Rs.
40,000.
Select Name, Salary from Employee
where Salary =
(Select Salary from Employee
where Salary between 20000 and 40000);

Error: ORA-01427: single-row subquery returns more than


one row

Correct

Query:

Select Name, Salary from Employee


where Salary IN
(Select Salary from Employee
where Salary between 20000 and 40000);

You might also like