Professional Documents
Culture Documents
DDL
CREATE
ALTER
TRUNCATE
DROP
RENAME
DML
INSERT
UPDATE
DELETE
TCL
COMMIT
ROLLBACK
DCL
GRANT
REVOKE
Purpose
Syntax
Example
UCT_CODE=1
TOPIC
Arithmetic Operators
+
*
/
Concatenation Operator
||
Comparison Operators
=
<>
>
<
>=
<=
IN
NOT IN
BETWEEN
NOT BETWEEN
LIKE (%,_)
NOT LIKE
IS NULL
IS NOT NULL
Logical Operators
AND
OR
Set Operators
UNION
UNION ALL
MINUS
INTERSECT
EXAMPLE
SELECT
SELECT
SELECT
SELECT
sal
sal
sal
sal
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
*
*
*
*
*
*
PURPOSE
You can use an arithmetic operator in an
expression to negate, add, subtract, multiply,
and divide numeric values. The result of the
operation is also a numeric value.
TOPIC
SQL Functions
Number Functions
CEIL
FLOOR
ROUND
MOD
ABS
POWER
TRUNC
Character Functions
LTRIM
RTRIM
TRIM
LOWER
UPPER
INIPCAP
LPAD
RPAD
SUBSTR
REPLACE
INSTR
LENGTH
LENGTHB
Date Functions
CURRENT_DATE
CURRENT_TIMESTAMP
SYSDATE
SYSTIMESTAMP
EXTRACT
ADD_MONTHS
TO_CHAR
LAST_DAY
MONTHS_BETWEEN
NEXT_DAY
YEAR,MONTH,DAY,HH,MM,SS,YY,MM,DD,MON,DY
Conversion Functions
CAST
TO_DATE
MAX
MIN
SUM
AVG
COUNT
Other Functions
CASE
NVL
DECODE
EXAMPLE
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT SAL, CASE WHEN SAL>1000 AND SAL < 2000 THEN 'A'
WHEN SAL >2000 AND SAL <3000 THEN 'B' END AS RANK
FROM EMP
SELECT COMM, NVL(COMM,0) FROM EMP
PURPOSE
SQL functions are similar to SQL operators in that both manipulate data items
and both return a result. SQL functions differ from SQL operators in the
format in which they appear with their arguments.
Number functions accept numeric input and return numeric values.
Returns smallest integer greater than or equal to n.
Returns largest integer equal to or less than n.
Returns n rounded to m places to the right of the decimal point; if m is
omitted, to 0 places. m can be negative to round off digits left of the decimal
point. m must be an integer.
MOD returns the remainder of n2 divided by n1. Returns n2 if n1 is 0.
ABS returns the absolute value of n.
POWER returns n2 raised to the n1 power.
The TRUNC (number) function returns n1 truncated to n2 decimal places. If
n2 is omitted, then n1 is truncated to 0 places.
Single row character functions accept character input and can return both
character and number values.
Returns the string argument char, with its left-most characters removed up to
the first character which is not in the string argument set, which defaults to
(a single space).
Returns the string argument char, with its right-most characters removed
following the last character which is not in the string argument set. This
defaults to ' ' (a single space).
Removes leading and/or trailing blanks (or other characters) from a string.
Returns a string argument char, with all its letters in lowercase.
Returns the string argument char with all its letters converted to uppercase.
Returns char, with the first letter of each word in uppercase, all other letters
in lowercase.
LPAD returns expr1, left-padded to length n characters with the sequence of
characters in expr2. This function is useful for formatting the output of a
query.
RPAD returns expr1, right-padded to length n characters with expr2,
replicated as many times as necessary. This function is useful for formatting
the output of a query.
The SUBSTR functions return a portion of char, beginning at character
position, substring_length characters long.
REPLACE returns char with every occurrence of search_string replaced with
replacement_string.
LAST_DAY returns the date of the last day of the month that contains date.
The return type is always DATE
MONTHS_BETWEEN returns number of months between dates date1 and
date2. If date1 is later than date2, then the result is positive. If date1 is
earlier than date2, then the result is negative.
NEXT_DAY returns the date of the first weekday named by char that is later
than the date date.
CASE expressions let you use IF ... THEN ... ELSE logic in SQL statements
NVL lets you replace null (returned as a blank) with a string in the results of a query.
DECODE compares expr to each search value one by one. If expr is equal to a
search, then Oracle Database returns the corresponding result.
sults of a query.
TOPIC
WHERE
EXAMPLE
SELECT* FROM EMP WHERE COMM IS NULL
GROUP BY
HAVING
FROM
DISTINCT
ORDER BY
PURPOSE
A WHERE clause is an optional part of a SelectExpression, DELETE statement, or UPDATE statement.
A GROUP BY clause, part of a SelectExpression, groups a result into subsets that have matching value
more columns. In each group, no two rows have the same value for the grouping column or columns.
considered equivalent for grouping purposes.
A HAVING clause restricts the results of a GROUP BY in a SelectExpression. The HAVING clause is appli
group of the grouped table, much as a WHERE clause is applied to a select list. If there is no GROUP B
the HAVING clause is applied to the entire result as a single group.
The FROM clause is a mandatory clause in a SelectExpression. It specifies the tables (TableExpression
the other clauses of the query can access columns for use in expressions.
The DISTINCT clause acts as a filter to remove duplicate records from a result set. It ensures that any
are returned are unique for the column or columns specified in the SELECT statement.
The ORDER BY clause is an optional element of a SELECT statement. An ORDER BY clause allows you
the order in which rows appear in the ResultSet.
TOPIC
JOINS
EQUI JOIN
NON EQUI JOIN
INNER JOIN
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
CROSS JOIN
SELF JOIN
EXAMPLE
PURPOSE
A
join is a query that combines rows from two or more tables.If any
two of these tables have a column name in common, then you must
qualify all references to these columns throughout the query with
table names to avoid ambiguity. The columns in the join conditions
need
not also
appear
in the
select
list. containing an equality
An
equijoin
is a
join with
a join
condition
operator.
An equijoin
combines
have
equivalent
values for
An
NONEQUI
JOIN returns
rows rows
from that
the left
side
of the predicate
for
the specified
columns.
which
there are
no corresponding rows on the right side of the
predicate.
It returns rows
failAto
match
IN)Bthe
subquery
on
Inner
Join compares
each that
row of
with
each(NOT
row of
to find
all pairs
the
right
side.
of rows which satisfy the join-predicate. When the join-predicate is
satisfied, column values for each matched pair of rows of A and B are
combined
into
a
result
row.
The
result
of a
left
outer
join
(orclosely
simplyresembles
left join) for
tables
A and
A
right
outer
join
(or
right
join)
a left
outer
join,B
always with
contains
all recordsof
ofthe
thetables
"left" table
(A), even
the from
join- the
except
the treatment
reversed.
Everyifrow
condition
does
any
matching
record
in at
the
"right"
table
(B).
"right"
table
(B)
willfind
appear
in combines
the joined
table
least
once.
If both
no
Conceptually,
anot
full
outer
join
the
effect
of
applying
matching
rowouter
from the
"left"
table
(A) exists,
appear
in
left
and right
joins.
Where
records
in theNULL
FULLwill
OUTER
JOINed
columns
A for those
records
match
in B.for every
tables
dofrom
not match,
the result
setthat
willhave
haveno
NULL
values
column of the table that lacks a matching row. For those records that
do match,
a returns
single row
be produced
in of
the
result
settables in the
CROSS
JOIN
thewill
Cartesian
product
rows
from
join. In other words, it will produce rows which combine each row from
the first table with each row from the second table
A self-join is joining a table to itself
TOPIC
Column Level
PRIMARY KEY
FOREIGN KEY
NOT NULL
UNIQUE
CHECK
DEFAULT
Table Level
PRIMARY KEY
UNIQUE
ALTERING CONSTRAINTS
DEFAULT
PRIMARY KEY
FOREIGN KEY
UNIQUE
CHECK
DROP CONSTRAINTS
EXAMPLE
CREATE TABLE PRODUCTS
CREATE
TABLE TRANSACTIONS1
(
(PRODUCT_CODE NUMBER(10) PRIMARY KEY,
PRODUCT_CODE
PRODUCT_NAME NUMBER(10),
VARCHAR(50),
PRODUCT_NAME
VARCHAR(50),
PRICE NUMBER (5)
QTY
NUMBER
(3),
)
PRICE NUMBER (5) NOT NULL,
TOTAL_PRICE NUMBER(5),
CREATE
TABLETRANS_PROD_CODE
PRODUCTS
CONSTRAINT
FOREIGN KEY (PRODUCT_CODE) REFERENCES PRODUCTS (PRODUC
()
PRODUCT_CODE NUMBER(10) ,
CREATE
TABLE PRODUCTS
PRODUCT_NAME
VARCHAR(50) NOT NULL,
(PRICE NUMBER (5) NOT NULL
PRODUCT_CODE
NUMBER(10) PRIMARY KEY,
)CREATE TABLE CONSTR_CHECK
PRODUCT_NAME VARCHAR(50) NOT NULL,
(PRICE NUMBER (5) NOT NULL,
AC
NUMBER
(10) (10)
PRIMARY
KEY,
PHONE
NUMBER
UNIQUE
CREATE
TABLE IP_Profile_t
TRANS_AMOUNT
NUMBER
(10)
NOT NULL,
)(
PHONE NUMBER(10) UNIQUE ,
cid
int NOT NULL,
WITHDRAW_AMOUNT
NUMBER (5),
LastName
varchar(255)
NULL,
CONSTRAINT abc CHECKNOT
(WITHDRAW_AMOUNT
between 100 and 25000)
FirstName
varchar(255),
)
Address varchar(255),
City varchar(255) DEFAULT 'NA'
)
PURPOSE
A primary key constraint combines a NOT NULL constraint and a unique constraint in a single declarat
A foreign key constraint requires values in one table to match values in another table.
A unique constraint prohibits multiple rows from having the same value in the same column or combin
A check constraint requires a value in the database to comply with a specified condition.
The DEFAULT constraint is used to insert a default value into a column.The
default value will be added to all new records, if no other value is specified.
aint in a single declaration. That is, it prohibits multiple rows from having the same value in the same column o
condition.
n the same column or combination of columns and prohibits values from being null.
TOPIC
EXAMPLE
Nested Subquery
SELECT DEPTNO
DEPT(SELECT
d.deptno,FROM
d.dname,
WHERE DEPTNO
NOTeAS
IN SOURCE_TABLE
count(*)
FROM emp
SELECT
'IP_PROFILE'
(
WHERE
e.deptno =
,'SEQ#12-IP_NAME'
AS SOURCE_COLUMN
SELECT
EMP.DEPTNO
FROm
EMP
d.deptno)
AS "Num
,CURRENT_DATE
AS Dept"
DAY_EXECUTED
FROM deptAS
d;COUNTER
,COUNT(*)
);
,CASE
WHEN
COUNT(*)
=0 FROM
THEN 'PASS'
SELECT empno,
ename,sal
emp e
ELSE
'FIAL'
END
AS
REULTS
WHERE sal >
FROM
(
(SELECT AVG(sal) FROM emp WHERE deptno
SELECT
DEPTNO FROM DEPT
= e.deptno
MINUS
)
SELECT DEPTNO FROM EMP
)
Inline View
Scalar Subquery
Correlated Subquery
PURPOSE
The subquery appears in the WHERE clause of the SQL.
The subquery appears in the FROM clause of the SQL.
The subquery appears in the SELECT clause of the SQL
This is a type of nested subquery that uses columns from the
outer query in its WHERE clause. A correlated subquery is
evaluated once for each row.
SubQueries.txt
Topic
ROWID
ROWNUM
EXISTS
NOT EXISTS
ON DELETE CASCADE