Advance SQL
Advance SQL
ADVANCED SQL
ICE BREAKER
4 PICS 1 WORD
INSTRUCTIONS:
EACH ROUND WE WILL PRESENT FOUR PICTURES THAT MAY OR MAY
NOT BE RELATED TO AN ADVANCED SQL CONCEPT.
The relational set operators UNION, UNION ALL, INTERSECT, and MINUS
How to use SQL functions to manipulate dates, strings, and other data
JOINS INCLUDES:
Full / Full
Inner Join
Outer Join
SYNTAX:
CROSS
JOIN
STUDENT RESULT:
MARKS QUERY:
NATURAL
JOIN
Natural Join returns all rows with matching
columns and eliminates duplicate columns.
This style of query is used when the tables
share on or more attributes with common
names.
SYNTAX:
INNER
JOIN
Inner Join combines rows from two or more
tables on a related column between them. It
only returns the rows that have matching
values in specified column.
SYNTAX:
NATURAL & INNER
JOIN
STUDENT QUERY NATURAL JOIN: RESULT:
SYNTAX:
LEFT / LEFT OUTER
JOIN RESULT:
Customer
Invoice
QUERY:
RIGHT / RIGHT OUTER
JOIN
The RIGHT JOIN keyword returns all records
from the right table (table2), and the
matching records from the left table
(table1). The result is 0 records from the left
side, if there is no match.
SYNTAX:
RIGHT / RIGHT OUTER
JOIN RESULT:
Customer
Invoice
QUERY:
FULL / FULL OUTER
JOIN
The FULL OUTER JOIN or FULL JOIN keyword
returns all records when there is a match in
left (table1) or right (table2) table records.
SYNTAX:
FULL / FULL OUTER
JOIN RESULT:
Customer
Invoice
QUERY:
SUBQUERIES &
CORRELATED QUERIES
WHERE ALL
IN FROM
ANY CORRELATED
SUBQUERIES
SUBQUERIES
ANY - Compares a value with any ALL - Compares a value with all
value in the subquery result set. values in the subquery result set
NAME SALARY
2 Bob IT 70000
Eve 80000
3 Carol IT 60000
Grace 90000
4 David HR 55000
Select employees whose salary is greater than all salaries in the HR department.
5 Eve Sales 80000
NAME SALARY
Grace 90000
FROM
SUBQUERY SELECT
FROM
DISTINCT CUSTOMER.CUS_CODE, CUSTOMER.CUS_LNAME
CUSTOMER,
(SELECT INVOICE.CUS_CODE FROM INVOICE NATURAL JOIN
A subquery that generates a temporary LINE WHERE P_CODE = '13-Q2/P2') CP1,
(SELECT INVOICE.CUS_CODE FROM INVOICE NATURAL JOIN
table for use in the main (outer) query.
LINE WHERE P_CODE = '23109-HB') CP2
(1,2)
OUTPUT
CORRELATED
SUBQUERY
Inner query is dependent of outer
query.
OUTPUT
ICE BREAKER
4 PICS 1 WORD
INSTRUCTIONS:
EACH ROUND WE WILL PRESENT FOUR PICTURES THAT MAY OR MAY
NOT BE RELATED TO AN ADVANCED SQL CONCEPT.
NUMERIC FUNCTIONS
STRING FUNCTIONS
CONVERSION FUNCTIONS
SQL FUNCTIONS
used to perform calculations and manipulations on data stored in a relational database.
Functions always use a numerical, date, or string value. The value may be part of the
command itself (a constant or literal) or it may be an attribute located in a table.
Sample Query:
DATE AND TIME
FUNCTIONS
Sample Query:
DATE AND TIME
FUNCTIONS
Sample Query:
DATE AND TIME
FUNCTIONS
DATE() − MS Access
GETDATE() − SQL Server - Returns today’s date
Sample Query:
DATE AND TIME
FUNCTIONS
Sample Query:
DATE AND TIME
FUNCTIONS
Sample Query:
DATE AND TIME
FUNCTIONS
EXAMPLES FUNCTION OF ORACLE
TO_CHAR - Returns a character string or a formatted string from a date value
Syntax: TO_CHAR(date_value, fmt)
fmt = format used; can be: MONTH: name of month, MON: three-letter month
name, MM: two-digit month name, D: number for day of week, DD: number day of
month, DAY: name of day of week, YYYY: four-digit year value, YY: two-digit year
value
Sample Query:
DATE AND TIME
FUNCTIONS
EXAMPLES FUNCTION OF ORACLE
TO_DATE - Returns a date value using a character string and a date format mask
Syntax: TO_DATE(char_value, fmt)
fmt = format used; can be: MONTH: name of month, MON: three-letter month
name, MM: two-digit month name, D: number for day of week, DD: number day of
month, DAY: name of day of week, YYYY: four-digit year value, YY: two-digit year
value
Sample Query:
DATE AND TIME
FUNCTIONS
EXAMPLES FUNCTION OF ORACLE
Sample Query:
DATE AND TIME
FUNCTIONS
EXAMPLES FUNCTION OF ORACLE
Sample Query:
DATE AND TIME
FUNCTIONS
EXAMPLES FUNCTION OF ORACLE
LAST_DAY - Returns the date of the last day of the month given in a date
Syntax: LAST_DAY(date_value)
Sample Query:
NUMERIC
FUNCTIONS
can be grouped in many different ways, such as algebraic, trigonometric, and
logarithmic.
It takes one numeric parameter and return one value.
Sample Query:
NUMERIC
FUNCTIONS
ROUND - Rounds a value to a specified precision (number of digits)
Syntax: ROUND(numeric_value, p)
p = precision
Sample Query:
NUMERIC
FUNCTIONS
CEIL/CEILING/FLOOR - Returns the smallest integer greater than or equal to a
number or returns the largest integer equal to or less than a number respectively
Syntax: CEIL(numeric_value) − Oracle
CEILING(numeric_value) − SQL Server
FLOOR(numeric_value)
Sample Query:
STRING
FUNCTIONS
Are used to perform operations on string data types
String manipulations are among the most-used functions in programming.
Sample Query:
STRING
FUNCTIONS
Sample Query:
STRING
FUNCTIONS
SUBSTRING - Returns a substring or part of a given string parameter
Syntax: SUBSTR(strg_value, p, l) − Oracle
SUBSTRING(strg_value,p,l) − SQL Server
p = start position
l = length of characters
Sample Query:
STRING
FUNCTIONS
LENGTH - Returns the number of characters in a string value
Syntax: LENGTH(strg_value) − Oracle
LEN(strg_value) − SQL Server
Sample Query:
CONVERSION
FUNCTIONS
Allows you to take a value of a given data type and convert it to the equivalent
value in another data type.
Sample Query:
CONVERSION
FUNCTIONS
String to Number: TO_NUMBER - Returns a formatted number from a character
string, using a given format
Syntax: Oracle: TO_NUMBER(char_value, fmt)
fmt = format used; can be:
9: displays a digit Sample Query:
0: displays a leading zero
, : displays the comma
. : displays the decimal point
$: displays the dollar sign
B: leading blank
S: leading sign
MI: trailing minus sign
CONVERSION
FUNCTIONS
CASE − SQL Server
DECODE − Oracle - Compares an attribute or expression with a series of values
and returns an associated value or a default value if no match is found
Syntax: Oracle: DECODE(e, x, y, d)
e : attribute or expression, x: value with which to compare e, y: value to return if
e=x, d: default value to return if e is not equal to x
Syntax: SQL Server: CASE When condition
THEN value1
ELSE value2
END
CONVERSION
FUNCTIONS
Sample Query:
SEQUENCES
DATA_TYPE START
INCREMENT CYCLE
etc.
CREATE
SEQUENCE
EXAMPLE:
CREATE SEQUENCE creates
a new sequence number
generator. This involves CREATE SEQUENCE name
creating and initializing a
new special single-row CREATE SEQUENCE student_id_seq
table with the name name.
IF NOT EXISTS
SEQUENCE
Do not throw an error if a EXAMPLE:
relation with the same name
CREATE SEQUENCE IF NOT EXISTS name
already exists. A notice is
issued in this case. Note that
there is no guarantee that the CREATE SEQUENCE IF NOT EXISTS
existing relation is anything like student_id_seq
the sequence that would have
been created — it might not
even be a sequence.
DATA_TYPE
SEQUENCE
The optional clause AS EXAMPLE:
data_type specifies the data CREATE SEQUENCE IF NOT EXISTS name
type of the sequence. Valid AS data_type
types are smallint, integer,
and bigint. bigint is the CREATE SEQUENCE IF NOT EXISTS
default. The data type student_id_seq
determines the default AS SMALLINT
minimum and maximum
values of the sequence.
INCREMENT
SEQUENCE
The optional clause INCREMENT EXAMPLE:
BY increment specifies which CREATE SEQUENCE IF NOT EXISTS
value is added to the current student_id_seq
sequence value to create a
new value. A positive value will
INCREMENT BY 5
make an ascending sequence,
a negative one a descending
sequence. The default value is 1.
MINVALUE
SEQUENCE
EXAMPLE:
The optional clause MINVALUE
minvalue determines the CREATE SEQUENCE IF NOT EXISTS student_id_seq
minimum value a sequence can
generate. If this clause is not MINVALUE 5
supplied or NO MINVALUE is
specified, then defaults will be
ALTER SEQUENCE student_id_seq
used. The default for an ascending
sequence is 1. The default for a NO MINVALUE;
descending sequence is the
minimum value of the data type.
MAXVALUE
SEQUENCE
The optional clause MAXVALUE EXAMPLE:
maxvalue determines the
maximum value for the CREATE SEQUENCE IF NOT EXISTS
sequence. If this clause is not student_id_seq
supplied or NO MAXVALUE is
specified, then default values will MAXVALUE 100
be used. The default for an
ascending sequence is the ALTER SEQUENCE student_id_seq
maximum value of the data type.
The default for a descending NO MAXVALUE;
sequence is -1.
START
SEQUENCE
The optional clause START EXAMPLE:
WITH start allows the CREATE SEQUENCE IF NOT EXISTS
sequence to begin student_id_seq
anywhere. The default
starting value is minvalue for START 10
ascending sequences and
maxvalue for descending
ones.
CYCLE
SEQUENCE
The CYCLE option allows the EXAMPLE:
sequence to wrap around when CREATE SEQUENCE IF NOT EXISTS
the maxvalue or minvalue has
student_id_seq
been reached by an ascending
or descending sequence
respectively. If the limit is CYCLE
reached, the next number
generated will be the minvalue
or maxvalue, respectively.
NO CYCLE
SEQUENCE
EXAMPLE:
If NO CYCLE is specified, any
calls to nextval after the CREATE SEQUENCE IF NOT EXISTS
sequence has reached its student_id_seq
maximum value will return
NO CYCLE
an error. If neither CYCLE or
NO CYCLE are specified, NO
CYCLE is the default.
ADDITIONAL PARAMETERS
SEQUENCE
OWNED BY TABLE_NAME.COLUMN_NAME
TEMP
OWNED BY NONE
A trigger is procedural SQL code that is automatically invoked by the RDBMS upon
the occurrence of a given data manipulation event. It is useful to remember that:
is assumed if you omit the FOR requires use of the FOR EACH
EACH ROW keywords. This type of ROW keywords. This type of
trigger is executed once, before trigger is executed once for each
or after the triggering statement row affected by the triggering
is completed. This is the default statement. If you update 10 rows,
case. the trigger executes 10 times.
STATEMENT-LEVEL
OLD
UPDATED
ROW-LEVEL
ROW-LEVEL
You could also create triggers whose
actions depend on the type of DML
statement (INSERT, UPDATE, or DELETE)
that fires the trigger.
TRIGGER ACTION BASED
ON CONDITIONAL DML
IF INSERTING THEN ... END IF;
PREDICATES
IF UPDATING THEN ... END IF;
IF DELETING THEN ... END IF;
PROCESSING WITH
CURSORS
allows for processing multiple rows
returned by a query, one row at a
time. This is especially useful for
operations that need to be
performed on each row
individually.
IMPLICIT CURSORS EXPLICIT CURSORS