Chapter03-02 - Data Manipulation Language
Chapter03-02 - Data Manipulation Language
SSK 3408
CHAPTER 3
DATA MANIPULATION
LANGUAGE
Learning Objectives
UPDATE inventory
SET price = price*1.05;
UPDATE inventory
SET price = price*1.05
WHERE product_id = 'P103';
DML – Remove Data
Join
NAME IDENTITYNO
SELECT last_name as name, nokp as identityno
Siti Razilah 790120-14-6222
FROM employee; Musa 800828-10-6323
Ahmad
Martin Luke 870401-12-6087
Column Heading Default ..cont.
SELECT last_name as “NaMe”, nokp as “identity no”
FROM employee;
NaMe Identity no
MONTHLY
DEPARTMENT_ID DEPARTMENT_ID
10 10
20 20
30 30
20
10
Note: If you specify multiple columns after the DISTINCT
qualifier, the result is every distinct combination of the
20
columns.
Displaying the Table Structure
Use DESCRIBE command
e.g:
desc faculty;
Name Null Type
------------------------- -------------- -----------------------------------------------------------------
FACULTYID NOT NULL NUMBER
LNAME NOT NULL VARCHAR2(30)
FNAME NOT NULL VARCHAR2(20)
DEPT VARCHAR2(5)
OFFICEID NUMBER
PHONE VARCHAR2(15)
EMAIL VARCHAR2(75)
RANK CHAR(4)
8 rows selected
Quiz
Identify the SELECT statements that execute successfully.
2.
SELECT first_name, last_name, job_id, salary*12 “yearly salary”
FROM employees;
Operator Meaning
AND Return TRUE If both component conditions are true
OR Return TRUE If either component condition is true
NOT Return TRUE If the condition is true
AND Logical Operator
LAST_NAME DEPT_ID SALAR COMMISSIO SALARY*COMMISSIO
Y N N
Siti Razilah 10 1000 (null) (null)
Musa Ahmad 20 1200 0.2 240
FUNCTIONS
MULTIPLE-
SINGLE-ROW
ROW
FUNCTIONS
FUNCTIONS
Return one result Return one result
per row per set of rows
Single Row Functions – Character Data Type
LOWER(column/expression)
UPPER(column/expression)
INITCAP(column/expression)
CONCAT(column/expression, column/expression)
SUBSTR(column/expression, m[,n])
m – character position (if m is negative, the count starts from the
end of the character value.
n – character long (if n is omitted, all characters to the end of the
string are returned)
LENGTH(column/expression)
INSTR(column/expression, ‘string’ [, m] , [n])
An Example
FUNCTION RESULT
LOWER(‘Database application’) database application
UPPER(‘Database application’) DATABASE
APPLICATION
INITCAP(‘Database application’) Database Application
CONCAT(‘Hello’, ’World’) HelloWorld
SUBSTR(‘HelloWorld’,2,5) elloW
INSTR(‘HelloWorld’, ’W’) 6
LPAD(salary,10,’*’) *****24000
RPAD(salary,10,’*’) 24000*****
REPLACE(‘JACK AND JUE’, ’J’, ’BL’) BLACK AND BLUE
Single Row Functions – Number Data
Type
ROUND: Rounds value to a special decimal
TRUNC: Truncates value to specified decimal
MOD: Returns remainder of division
FUNCTION RESULT
ROUND(45.926, 2) 45.93
TRUNC(45.926, 2) 45.92
MOD(1600, 300) 100
Nesting Functions
LAST_NAME DEPT_ID SALAR COMMISSIO SALARY*COMMISSIO
Y N N
Siti Razilah 10 1000 (null) (null)
Musa Ahmad 20 1200 0.2 240
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 “STATUS”
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
Multiple-Row Functions
Group functions operate on sets of rows to give one
result per group.
LAST_NAME DEPT_ID SALAR
Y
Siti Razilah 10 1000
EMPLOYE
Maximum
salary in
ES table
Musa Ahmad 20 1200 MAX(SALAR
Ammar Faizul 20 1800
Y)
Safiyah Shahizan 10 1230
2500
Taylor Duff 20 2500
Type of Multiple Row Functions
AVG(column)
MAX(column)
MIN(column)
SUM(column)
COUNT(*) or COUNT(column) or COUNT(DISTINCT column)
COUNT(*) returns the number of rows in a table.
COUNT(commission) returns the number of rows with non-null values.
COUNT(DISTINCT commission) returns the number of distinct
commission values.
Multiple-Row Functions and Null
Values
Group functions ignore null values in the column:
SELECT
AVG(commission)
FROM employee;
The NVL function forces group functions to
include null values:
SELECT
AVG(NVL(commission,0))
FROM employee;
LAST_NAM DEPT_I SALAR COMMISSI
E D Y ON
Siti Razilah
10 1000 (null)
Musa Ahmad
20 1200 0.2
Martin Luke
30 1500 0.6
Ammar Faizul
20 1800 0.3
Safiyah Shahizan
10 1230 (null)
Taylor Duff
20 2500 0.8
GROUP BY clause
Group functions operate on sets of rows to give one
result per group.
10 1230 1230
20 2500 2500
30 1500 1500
Use Group Functions in ORDER
BY
SELECT dept_id, MAX(salary)
FROM employee
GROUP BY dept_id
ORDER BY MAX(salary); DEPT_ID MAX(SALARY)
20 2500
30 1500
10 1230
Restricting Group Results
LAST_NAME DEPT_ID SALAR
Y
Siti Razilah 10 1000
Maximum salary
DEPT_ID MAX(SALARY)
department BUT
greater than 1
for each
Martin Luke 30 1500
1300
20 2500
Ammar Faizul 20 1800 30 1500
Safiyah Shahizan 10 1230
x
clause.
SELECT e.last_name, e.salary, d.dept_name
FROM employee e
JOIN department d
USING (dept_id)
WHERE d.dept_id in (‘10’,’20’);
EMPLOYEE (MANAGER)
EMPI LAST_NAME DEPT_I SALAR COMMISSI MGR_I
D D Y ON D
1 Siti Razilah 10 1000 (null) 6
2 Musa Ahmad 20 1200 0.2 4
3 Martin Luke 30 1500 0.6 6
4 Ammar Faizul 20 1800 0.3 6
5 Safiyah Shahizan 10 1230 (null) 1
6 Taylor Duff 20 2500 0.8 (null)
Self-Join using the ON clause
an equality operator.
EMPLOYEE JOB_GRADE
EMPI LAST_NAME DEPT_I SALAR COMMISSI MGR_I
GRADE_LEV LOW_SA MAX_SA
D D Y ON D
EL L L
1 Siti Razilah 10 1000 (null) 6
A 0 999
2 Musa Ahmad 20 1200 0.2 4
B 1000 1999
3 Martin Luke 30 1500 0.6 6
C 2000 2999
4 Ammar Faizul 20 1800 0.3 6
D 3000 3999
5 Safiyah Shahizan 10 1230 (null) 1
E 4000 10000
6 Taylor Duff 20 2500 0.8 (null)
LAST_NAME SALAR GRADE_LE
Y VEL
SELECT e.last_name, e.salary, j.grade_level Siti Razilah 1000 B
FROM employee e Musa Ahmad 1200 B
JOIN job_grade j Martin Luke 1500 B
Month 30 90 3%
Quarter 90 120 5%
Overdue 120 9999 10%
SELECT *
FROM AR JOIN LateCategory
ON ((Date() - AR.DateDue) >= LateCategory.MinDays)
AND ((Date() - AR.DateDue) < LateCategory.MaxDays)
Returning Record with No Direct
Match
Use OUTER joins.
LEFT OUTER JOIN clause
RIGHT OUTER JOIN clause
FULL OUTER JOIN clause
LEFT OUTER JOIN
Used when you may not have matching data in the
secondary table, but still want to include the data
you have in the primary table.
Main Query:
Sub Query:
SELECT select_list
FROM table
WHERE expr operator
(Select select_list
FROM TABLE);
Multiple-row subquery
Main Query
returns 1200
SubQuery 1800
2500
x
An Example
SELECT last_name, salary
FROM employee Single-row operator with
multiple-row subquery
where salary = (SELECT salary
FROM employee
WHERE dept_id = 20); EMPI LAST_NAME DEPT_I SALAR COMM
D D Y ON
1 Siti Razilah 10 1000
2 Musa Ahmad 20 1200
3 Martin Luke 30 1500
SELECT last_name, salary 4 Ammar Faizul 20 1800
FROM employee 5
6
Safiyah Shahizan
Taylor Duff
10 1230
20 2500
where salary in (SELECT salary
FROM employee
WHERE dept_id = 20);
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
Is enclosed in parenthesis and must appear on the right in a
WHERE clause condition
Cannot include an ORDER BY clause
The number of rows returned by the subquery must match
the number expected by the main query
Combining Subqueries
Multiple subqueries can be used to check for more than one
condition in a statement.
Example:
SELECT last_name, salary
FROM employees
WHERE dept_id = (SELECT dept_id
FROM employee
WHERE last_name = ‘Taylor Duff’)
AND salary > (SELECT salary
FROM employee
WHERE last_name = ‘Taylor Duff’);
Same or different types can be nested.
NOTE: Nested subqueries are executed from the most deeply nested to the least deeply nested
subquery.
Example
Subquery is embedded in
parentheses. In this case it
returns a list that will be used
in the WHERE clause of the
outer query
Sally Pet Store
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.
A B C
List the name of any employee
who has worked for both the
East and West regions.
T1 T2