Professional Documents
Culture Documents
SAK 3408
Chapter 3
Query Language
What is SQL?
SELECT CUSTOMER_ID
FROM CUSTOMER;
SQL Grammar
SQL statements can be entered on a
single line or split across multiple lines
(preferred)
SELECT CUSTOMER_ID, CUSTOMER_NAME, CREDIT_LINE
FROM CUSTOMER WHERE CREDIT_LINE > 1000000
AND CUSTOMER_ID IN [1000,2000];
Returns data for all requests where the state is either GA, WA
or NJ. No records would have been returned if the where
clause had been:
Constraint_name Constraint_attributes
Constraint_type
Table with Foreign Key
CREATE TABLE faculty(
facultyID number,
lname varchar2(30) NOT NULL,
fname varchar2(20) NOT NULL,
dept varchar2(5),
officeID number,
phone varchar2(15),
email varchar2(75) UNIQUE,
rank char(4) CHECK (rank IN ('INST',
'ASOC','ASST','FULL','SENR')),
CONSTRAINT faculty_pk PRIMARY KEY (facultyID),
CONSTRAINT faculty_fk FOREIGN KEY (officeID)
REFERENCES room(roomID));
Table with Compound Primary Key
create table participation(
eventID number,
memberID number,
constraint participation_pk primary key
(eventID, memberID),
constraint participation_event_fk foreign key
(eventID) references event(eventID),
constraint participation_member_fk foreign key
(memberID) references member(memberID));
DDL - Altering a Table
Use the Alter and Add/Modify keywords
and specify:
table name
new or existing field name(s)
field type
CONs
Requires additional space
Increased overhead
Data Manipulation Language
(DML)
Used to create, modify and retrieve data
Insert
Select
Update
Delete
DML - Adding Data to a Table
Use the Insert keyword and specify:
table name
field names - optional
values for each field
insert into customer
values(‘Teplow’,’MA’,23445.67);
OR
insert into customer (last_name,
state_cd, sales) values (‘Teplow’
,’MA’,23445.67);
DML - Updating Data in a Table
Use the Update and Set keywords and
specify:
table name
field name(s)
where clause (optional)
update inventory
set price = price*1.05;
update inventory
set price = price*1.05
where product_id = 'P103';
DML-Deleting Records
Use the Delete From keywords and
specify:
table name
where clause (optional)
Returns
LAST_NAME STATE_CD SALES
----------------- -------- ----------------
Teplow MA 23445.67
Abbey CA 6969.96
Porter CA 6989.99
Martin CA 2345.45
Laursen CA 34.34
Bambi CA 1234.55
McGraw NJ 123.45
Select Statement - Simple
SELECT last_name, state_cd
FROM customer;
Returns
LAST_NAME STATE_CD
----------------- -------
Teplow MA
Abbey CA
Porter CA
Martin CA
Laursen CA
Bambi CA
McGraw NJ
WHERE - Optional
Use to:
specify how to join two tables
restrict the data returned
SQL Operators
Logical Operators
and
or
Comparison Operators
= equality
!= or <> inequality
Like string search
In list of values
Between range of values
Select Statement - Equals
SELECT *
FROM customer
WHERE state_cd = ‘CA’;
Returns
LAST_NAME ST SALES
----------------- -- ---------
Abbey CA 6969.96
Porter CA 6989.99
Martin CA 2345.45
Laursen CA 34.34
Bambi CA 1234.55
Select Statement – Not Equals
SELECT *
FROM customer
WHERE state_cd <> ‘CA’;
Returns
LAST_NAME ST SALES
-------------- -- ----------
Teplow MA 23445.67
McGraw NJ 123.44
Select Statement - Like
SELECT last_name, state_cd, sales
FROM customer
WHERE upper(state_cd) LIKE ‘%A’;
Returns
LAST_NAME ST SALES
--------------- -- ----------------
Teplow MA 23445.67
Abbey CA 6969.96
Porter CA 6989.99
Martin CA 2345.45
Laursen CA 34.34
Bambi CA 1234.55
Select Statement - In
SELECT last_name, state_cd
FROM customer
WHERE state_cd IN (’MA’,’NJ’);
Returns
LAST_NAME ST
------------- --
Teplow MA
McGraw NJ
Select Statement – Between
SELECT *
FROM customer
WHERE sales BETWEEN 6500 AND 25000;
Returns
LAST_NAME ST SALES
-------------- -- ----------
Teplow MA 23445.67
Abbey CA 6969.96
Porter CA 6989.99
Select Statement – Multiple
Conditions Using OR
SELECT *
FROM customer
WHERE state_cd <> ‘CA’ OR
sales BETWEEN 6500 AND 25000;
Returns
LAST_NAME ST SALES
-------------- -- ----------
Teplow MA 23445.67
Abbey CA 6969.96
Porter CA 6989.99
McGraw NJ 123.44
Select Statement – Multiple
Conditions Using AND
SELECT *
FROM customer
WHERE state_cd <> ‘CA’ AND
sales BETWEEN 6500 AND 25000;
Returns
LAST_NAME ST SALES
-------------- -- ----------
Teplow MA 23445.67
Select Statement – Calculated
Field
SELECT last_name, sales, sales*05 as Tax
FROM customer;
Returns
LAST_NAME SALES TAX
----------- ---------- --------
Teplow 23445.67 1172.28
Abbey 6969.96 348.50
Porter 6989.99 349.50
Martin 2345.45 117.27
Laursen 34.34 1.71
Bambi 1234.55 61.73
ORDER BY - Optional
Used to specify sorting order
Can sort
by multiple fields
in ascending or descending order
Select Statement - Sorting
select state_name, last_name, sales
from customer, state
where customer.state_cd = state.state_cd
and state_cd in (‘CA’,’MA’,’NJ’)
order by state_name;
Returns
STATE_NAME LAST_NAME SALES
------------------- ------------------------- --------
California Abbey 6969.96
California Porter 6989.99
Massachusetts Teplow 23445.67
New Jersey McGraw 123.45
Overview
Select Statements continued
Table Joins
Distinct
Group By
Having
Decode function
Sequence numbers
Subqueries
Insert
Update
Delete
Select - Table Join
SELECT state_name, last_name, sales
FROM customer, state
WHERE customer.state_cd = state.state_cd;
Returns
STATE_NAME LAST_NAME SALES
-------------- ----------------- --------
California Abbey 6969.96
California Porter 6989.99
Massachusetts Teplow 23445.67
New Jersey McGraw 123.45
Table Joins
Process of combining data from two or
more tables, normally using primary
and/or foreign keys.
Basic types of joins:
Equijoin (Equal or Inner Join)
Left join (Outer Join)
Right join
Equi Joins
Most common type of join
Look for records which have matching
values of the join fields
Join Processing
Table joins are done by matching the first
record from the primary table’s first record
with each record in the secondary table, then
matching the second record in the primary
table with each record in the secondary
table.
Continue until each record from the primary
table has been combined with each record
from the secondary table. This is called a
Cartesian product.
Join Processing - cont
Oracle then goes through the Cartesian
product, discarding combined records
that have non-matching join fields.
The remaining records are returned.
Cartesian Product Example
Select ename, emp.deptno dept.deptno, dname
FROM emp, dept
Research 20 20 Smith
Sales 30 30 Allen
Sales 30 30 Ward
Finance 50
Right Joins
Not as common as left joins. Used
when you may have data in the
secondary table with no matching data
in the primary table.
Right Join Example
Assign an employee to a brand new dept,
deptno=50, but there’s no record in dept yet
Select ename, dept.deptno deptno, dname
FROM emp, dept
WHERE + emp.deptno = dept.deptno;
Select distinct(student_id)
From course_section
Where term = '1001';
DISTINCT – example
SELECT Category SELECT DISTINCT Category
FROM Animal; FROM Animal;
Category
Category
Fish
Bird
Dog
Cat
Fish
Dog
Cat
Fish
Cat
Dog
Fish
Dog
Bird
Dog
Fish
Cat
Dog
Advanced Select- Group By
Clause
Allow you to group data together for
summary calculations
avg (column)
count (*)
max (column)
min (column)
sum (column)
Advanced Select- Group By
Clause
Group by clause must be included in a
select statement that uses a group
function (count, sum, min, max, and avg)
All fields in the select clause which are
not part of a group function must be
included in the Group By clause.
Advanced Select- Group By
Clause
avg (column)
1 select deptno, avg(sal)
2 from scott.emp
3* group by deptno;
DEPTNO AVG(SAL)
-------------- ----------------
10 2916.6667
20 2175
30 1566.6667
Advanced Select - Having Clause
Allows you to specify conditions for a record set
instead of a single record
Must have a group by clause
1 select deptno, avg(sal)
2 from scott.emp
3 group by deptno
4* having avg(sal) > 2000
DEPTNO AVG(SAL)
------------- ----------------
10 2916.6667
20 2175
Where vs Having
Where - a constraint on individual
records.
Having - a constraint on a group
of records.
They can be used together in the
same SQL statement
Select ename
From emp
Where upper(loc) = 'NEW YORK'
Group by deptno
Having avg(sal) > 2000
Subquery Characteristics
Can be used in the SELECT, CREATE,
INSERT, UPDATE and DELETE
statements
Can be used as part of a condition in
the WHERE clause
Can be used in multiple AND or OR
predicates of the same SQL statement
Subquery Characteristics - Cont
Is enclosed in parenthesis and must
appear on the right in a WHERE clause
condition
May or may not retrieve data from a
table used in the main, or outer, SQL
statement in which it’s embedded.
Cannot include an ORDER BY clause
Subquery Characteristics - Cont
The number of rows returned by the
subquery must match the number
expected by the main query
The number of columns returned must
also match the number expected
Combining Subqueries
Multiple subqueries can be used to
check for more than one condition in a
statement.
Same or different types can be nested.
Subquery is embedded in
parentheses. In this case it
returns a list that will be used
in the WHERE clause of the
outer query
Subquery for Calculation
Which cats sold for more than the average sale price of cats?
Assume we know the average price is $170.
Usually we need to compute it first.
SELECT SaleAnimal.AnimalID, Animal.Category,
SaleAnimal.SalePrice
FROM Animal
INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID
WHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice>170));
T1 T2
Select AnimalID,
CASE
WHEN Date()-DateBorn < 90 Then “Baby”
WHEN Date()-DateBorn >= 90
AND Date()-DateBorn < 270 Then “Young”
WHEN Date()-DateBorn >= 270
AND Date()-DateBorn < 365 Then “Grown”
ELSE “Experienced”
END
FROM Animal;
Used to change data to a different context.
Example: Define age categories for the animals.
Less than 3 months
Between 3 months and 9 months
Between 9 months and 1 year
Over 1 year
Inequality Join
AccountsReceivable
Categorize by Days Late
30, 90, 120+
Three queries?
New table for business rules
AR(TransactionID, CustomerID, Amount, DateDue)
Month 30 90 3%
Quarter 90 120 5%
Overdue 120 9999 10%
SELECT *
FROM AR INNER JOIN LateCategory
ON ((Date() - AR.DateDue) >= LateCategory.MinDays)
AND ((Date() - AR.DateDue) < LateCategory.MaxDays)