SlideShare a Scribd company logo
ORACLE SQL - Advanced
By:
Dhananjay Goel
CTO, Alphalogic Inc
www.alphalogicinc.com
Agenda
05
01
02
03
04
06
07
Manipulating Data
Transforming Data
Hierarchical Queries
Table Partitioning
Materialized Views
Regular Expression
Flashback Operations
MANIPULATING DATA
Manipulating Data
Default Feature: In Oracle Data Definition Language (DDL) , we have the
ability to assign default values to columns in table.
Example - 1
Create table customer1 {
customer_id number,
customer_name varchar 2 (20),
exp_date date DEFAULT to_date (‘31-dec-2009’,’dd-mon-yyyy’) }
→ insert into customer1 (customer_id, customer_name) values (1,’Jones’);
→ insert into customer1 (customer_id,customer_name,exp_date) values
(2,’Raj’,’DEFAULT);
Virtual Column
● Virtual Columns appear just like your normal table columns, but their
values are derived at run time then being stored on disc.
● We cannot insert any data into a virtual column
Advantages:
● Saves Disk spaces
● Need not update data if formula changes.
Arithmetic calculations on Null
values
● Any arithmetic expression which involves a NULL value returns a
NULL value as the output.
● Use NVL functions while performing Arithmetic expression on NULL
values in a table.
Arithmetic calculations on Null
values
Example: DO
● select AVG (NVL(sales_amount,0)) from sales
● select COUNT (NVL(sales_amount,0)) from sales
Example : DON’T
● select AVG (sales_amount) FROM sales;
● select COUNT (sales_amount) from sales;
NULL values are not considered while taking Average or count, we have
to use NVL function to the average and count values having nulls.
Multi table Inserts
Oracle INSERT ALL statement is used to add multiple rows with a single
INSERT statement. The rows can be inserted into one table or multiple
tables using only one SQL command.
Inserting into 1 Table
INSERT ALL
INTO customer1 (customer_id,customer_name) values (1,’Kenny’)
INTO customer1 (customer_id,customer_name)values (2,’Peter’)
INTO customer1 (customer_id,customer_name)values (3,’John’)
select* from dual;
Multi table Inserts
Inserting into Multiple Tables
INSERT ALL
INTO customer1(customer_id,customer_name) VALUES (1,’Kenny’)
INTO customer1 (customer_id,customer_name) VALUES (2,’Peter’)
INTO sales1 (sales_date,order_id,total_amount) VALUES (‘12-Jan
-2017’,345,900);
Merge the Data
Merge statement is used to select rows from one or more sources for
update or insertion into table or view.
Merge Statement :-
1) Provides a convenient way to combine multiple operations.
2) Lets you avoid multiple INSERT,UPDATE and DELETE DML statement
If Match found – UPDATE
If Match not found – INSERT
MERGE statement with Conditions We can specify conditions to
determine whether to update or insert into target table or view.
Example of
Analytical Function
Simple Query:
Select sales_date, order_id,product_id,sales_amount,avg_amount from
sales, (select_avg(sales_amount) as avg_amount from sales)
Analytical Query:
Select sales_date, order_id,product_id,sales_amount,
avg(sales_amount) over () from sales
● In Analytical query (2nd ) we are fetching once and where as in 1st
query we are fetching data multiple times. So 1st query burden’s the
database.
Analytical Functions
Used to compute and aggregate values based on group of rows.
The group of rows is called a window and is defined by the
analytic_clause.
Advantages
a) Improve Query Speed
b) Enhanced Developer Productivity
c) Minimized Learning Effort
Types of
Analytical Functions
Four Analytical Families :
1) Ranking Functions
2) Window Aggregate Functions
3) Reporting Aggregate Functions
4) LAG/LEAD Functions
Rank Function
● The RANK function produces an ordered ranking of rows starting with
a rank of one.
● RANK function returns the rank of a value in a group of values.
● The RANK function can be used two ways - as an Aggregate function
or as an Analytic function.
Window Function:
● The GROUP BY clause allows us to apply aggregate functions to
subsets of rows.
● This time AVG is an analytic function, operating on the group of rows
defined by the contents of the OVER clause.
● This group of rows is known as a window, which is why analytic
functions are sometimes referred to as window[ing] functions.
LAG And LEAD Function
● LAG navigates back : Previous month, quarter , year
● LEAD navigates front: Next month, quarter , year
● These functions are used to analyse data over a period of time.
Example this month vs next month,last years vs this years,etc.
Reporting Function:
● After a query has been processed, aggregate values like the number
of resulting rows or an average value in a column can be easily
computed within a partition and made available to other reporting
functions.
● Reporting aggregate functions return the same aggregate value for
every row in a partition.
TRANSFORMING DATA
Case Statement
● Converting the row level data to column level using CASE statement.
● The CASE statement is like a series of IF statements, only using the
key word WHEN. A CASE statement is evaluated from top to bottom.
● If a condition is true, then corresponding THEN clause is executed
and execution jumps to the END CASE (short circuit evaluation)
clause.
Case Statement
Syntax:
CASE [expression]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2 …
WHEN condition_n THEN result_n
ELSE result
END case_name
PIVOT
● Converting the row level data to column level using PIVOT Analytical
function.
● PIVOT is faster than CASE statement.
● PIVOT means that you can aggregate your results and rotate rows
into columns.
PIVOT
Syntax:
Select * from (select column1, column2 from tables where
conditions)
PIVOT
aggregate_function(column2)
FOR column2
IN ( expr1, expr2, ... expr_n) | subquery)
ORDER BY expression [ ASC | DESC ];
PIVOT - Syntax
Parameters:
● aggregate_function: It can be a function such as SUM, COUNT, MIN,
MAX, or AVG functions.
● IN ( expr1, expr2, ... expr_n ): A list of values for column2 to pivot into
headings in the cross-tabulation query results.
● Subquery: It can be used instead of a list of values. In this case, the
results of the subquery would be used to determine the values for
column2 to pivot into headings in the cross-tabulation query results.
LISTAGG
● Converting the row level data to column level using LISTAGG Analytical
function.
● Group all the values and all values are displayed in a single column.
● The nice thing about this function is it also allows us to order the elements
in the concatenated list.
Syntax
LISTAGG (measure_column [, 'delimiter'])
WITHIN GROUP (order_by_clause) [OVER (query_partition_clause)]
LISTAGG - Syntax
Syntax Arguments:
● measure_column:The column or expression whose values you wish
to concatenate together in the result set. Null values in the
measure_column are ignored.
● Delimiter: Optional. It is the delimiter to use when separating the
measure_column values when outputting the results.
● order_by_clause: It determines the order that the concatenated
values (ie: measure_column) are returned.
UNION ALL
● Converting the column level data to row level using UNION ALL
● It returns all rows from the query and it does not remove duplicate
rows between the various SELECT statements.
● Each SELECT statement within the UNION ALL operator must have
the same number of fields in the result sets with similar data types.
UNION ALL
Syntax:
SELECT expression1, expression2, ... expression_n FROM tables
[WHERE conditions]
UNION ALL
SELECT expression1, expression2, ... expression_n
FROM tables [WHERE conditions];
UNPIVOT
● Converting the column level data to row level using
● UNPIVOT.UNPIVOT transposes columns in rows.
Example:
Create table # student
(Student Id int,Marks1 float,
Marks2 float, Marks3 float)
insert into #student
values (1,5.6,7.3,4.2)
insert values for all 4 rows.
UNPIVOT - Syntax
SELECT StudentID, MarksNo, MarksRecd
FROM
(SELECT StudentID,
Marks1, Marks2, Marks3
FROM #Student) stu
UNPIVOT
(MarksRecd FOR MarksNo
IN (Marks1, Marks2, Marks3))
AS marks
HIERARCHICAL QUERIES
Hierarchical Query
● Hierarchical query needs a definition of how each child relates to its
parent.
● This is defined using the CONNECT BY .. PRIOR clause, which
defines how the current row (child) relates to a prior row (parent).
● The START WITH clause can be used to define the root node(s) of
the hierarchy.
Hierarchical Query -
Operators
● LEVEL : The position in the hierarchy of the current row in relation to
the root node.
● CONNECT_BY_ROOT : Returns the root node(s) associated with the
current row.
● SYS_CONNECT_BY_PATH : Returns a delimited breadcrumb from
root to the current row.
● CONNECT_BY_ISLEAF : Indicates if the current row is a leaf node.
● ORDER SIBLINGS BY : Applies an order to siblings, without altering
the basic hierarchical structure of the data returned by the query.
CONNECT BY,PRIOR &
START WITH
● The CONNECT BY clause specifies the relationship between parent
rows and child rows of the hierarchy.
● The connect_by_condition can be any condition, however, it must use
the PRIOR operator to refer to the parent row.
● Restriction on the CONNECT BY clause: The connect_by_condition
cannot contain a regular subquery or a scalar subquery expression.
CONNECT BY,PRIOR &
START WITH
Example:
SELECT employee_id, last_name, manager_id, LEVEL
FROM employees
CONNECT BY PRIOR employee_id = manager_id;
RESULT: Shows employee_id, last_name, manager_id and level in the
tree for the employee hierarchy.
CONNECT BY ROOT
● CONNECT_BY_ROOT is a unary operator that is valid only in
hierarchical queries.
● When you qualify a column with this operator, Oracle returns the
column value using data from the root row.
● This operator extends the functionality of the CONNECT BY [PRIOR]
condition of hierarchical queries.
● Restriction on CONNECT_BY_ROOT: You cannot specify this
operator in the START WITH condition or the CONNECT BY
condition
SYS-CONNECT_BY_PATH
● SYS_CONNECT_BY_PATH is valid only in hierarchical queries.
● It returns the path of a column value from root to node, with column
values separated by char for each row returned by CONNECT BY
condition.
Extension To GROUP BY
● Aggregation is a fundamental part of data warehousing.
● To improve aggregation performance in your warehouse, Oracle
Database provides the following extensions to the GROUP BY
clause:
– CUBE and ROLLUP extensions to the GROUP BY clause
– GROUPING functions
– GROUPING SETS expression
ROLLUP Extension to
GROUP BY
● ROLLUP enables a SELECT statement to calculate multiple levels of
subtotals across a specified group of dimensions.
● It also calculates a grand total.
● The ROLLUP extension is highly efficient, adding minimal overhead
to a query.
● The action of ROLLUP is straightforward: it creates subtotals that roll
up from the most detailed level to a grand total, following a grouping
list specified in the ROLLUP clause.
ROLLUP Extension to
GROUP BY
● First ,it calculates the standard aggregate values specified in the
GROUP BY clause.
● Then, it creates progressively higher-level subtotals, moving from
right to left through the list of grouping columns.
● Finally, it creates a grand total.
● If wanting to compress data when using ROLLUP. This is particularly
useful when there are few updates to older partitions.
WHEN TO USE ROLLUP
● Use the ROLLUP extension in tasks involving subtotals.
● It is very helpful for subtotaling along a hierarchical dimension such
as time or geography. For instance, a query could specify a
ROLLUP(y, m, day) or ROLLUP(country, state, city).
● For data warehouse administrators using summary tables, ROLLUP
can simplify and speed up the maintenance of summary tables.
Syntax:
ROLLUP appears in the GROUP BY clause in a SELECT statement. Its
form is:
SELECT … GROUP BY ROLLUP(grouping_column_reference_list)
CUBE Extension to GROUP BY
● CUBE takes a specified set of grouping columns and creates
subtotals for all of their possible combination.
● In terms of multidimensional
analysis, CUBE generates all
the subtotals that could be
calculated for a data cube with
the specified dimensions.
CUBE Extension to
GROUP BY
● The data needed for cross-tabular reports can be generated with a single
SELECT using CUBE.
● Note that population of summary tables is even faster if the CUBE query
executes in parallel.
● CUBE is typically most suitable in queries that use columns from multiple
dimensions rather than columns representing different levels of a single
dimension.
Syntax:
CUBE appears in the GROUP BY clause in a SELECT statement. Its form is:
SELECT … GROUP BY CUBE(grouping_column_reference_list)
Grouping Functions
● Two challenges arise with the use of ROLLUP and CUBE.
● How can you programmatically determine which result set rows are
subtotals, and how do you find the exact level of aggregation for a
given subtotal?
● What happens if query results contain both stored NULL values and
"NULL" values created by a ROLLUP or CUBE?
● GROUPING handles the above problems
When to use
GROUPING Function
● Using a single column as its argument, GROUPING returns 1 when it
encounters a NULL value created by a ROLLUP or CUBE operation.
● That is, if the NULL indicates the row is a subtotal, GROUPING
returns a 1. Any other type of value, including a stored NULL, returns
a 0.
● The GROUPING function is not only useful for identifying NULLs, it
also enables sorting subtotal rows and filtering results.
When to use
GROUPING Function
Syntax:
GROUPING appears in the selection list portion of a SELECT statement.
Its form is:
SELECT …
[GROUPING(dimension_column)…] …
GROUP BY …
{CUBE | ROLLUP| GROUPING SETS} (dimension_column)
Grouping Functions
● GROUPING_ID Function
● GROUPING SETS FUNCTION
Problem: A four-column GROUP BY clause needs to be analyzed with
four GROUPING functions. This is inconvenient to write in SQL and
increases the number of columns required in the query. When you want
to store the query result sets in tables, as with materialized views, the
extra columns waste storage space.
To address these problems, use the GROUPING_ID function
GROUPING_ID Functions
● GROUPING_ID returns a single number that enables you to
determine the exact GROUP BY level.
● For each row, GROUPING_ID takes the set of 1's and 0's that would
be generated if you used the appropriate GROUPING functions and
concatenates them, forming a bit vector.
● The bit vector is treated as a binary number, and the number's base-
10 value is returned by the GROUPING_ID function.
● If we group with the expression CUBE(a, b) the possible values:
GROUPING_ID
Example for CUBE(a, b)
Aggregation Level Bit Vector GROUPING_ID
a, b 0 0 0
a 0 1 1
b 1 0 2
Grand Total 1 1 3
GROUPING_ID clearly distinguishes groupings created by grouping set
specification, and it is very useful during refresh and rewrite of materialized
views.
GROUP_ID Function
● While the extensions to GROUP BY offer power and flexibility, they
also allow complex result sets that can include duplicate groupings.
● The GROUP_ID function lets you distinguish among duplicate
groupings.
● If there are multiple sets of rows calculated for a given level,
GROUP_ID assigns the value of 0 to all the rows in the first set.
● All other sets of duplicate rows for a particular grouping are assigned
higher values, starting with 1
GROUPING SETS Syntax
● GROUPING SETS syntax lets you define multiple groupings in the
same query.
● GROUP BY computes all the groupings specified and combines them
with UNION ALL.
GROUP BY GROUPING sets (channel_desc, calendar_month_desc,
country_id )
The above statement is equivalent to:
GROUP BY channel_desc UNION ALL
GROUP BY calendar_month_desc UNION ALL GROUP BY country_id
GROUPING SETS Statements and
Equivalent GROUP BY
GROUPING SETS Statement Equivalent GROUP BY Statement
GROUP BY GROUPING SETS(a, b, c) GROUP BY a UNION ALL GROUP BY b UNION ALL GROUP BY c
GROUP BY GROUPING SETS(a, b, (b, c)) GROUP BY a UNION ALL GROUP BY b UNION ALL GROUP BY b, c
GROUP BY GROUPING SETS((a, b, c)) GROUP BY a, b, c
GROUP BY GROUPING SETS(a, (b), ()) GROUP BY a UNION ALL GROUP BY b UNION ALL GROUP BY ()
GROUP BY GROUPING SETS(a, ROLLUP(b, c)) GROUP BY a UNION ALL GROUP BY ROLLUP(b, c)
GROUPING SETS Statements and
Equivalent GROUP BY
● A query based on UNION would need multiple scans of the base
table, sales.
● This could be very inefficient as fact tables will normally be huge.
● Using GROUPING SETS statements, all the groupings of interest are
available in the same query block.
Composite Columns
● A composite column is a collection of columns that are treated as a
unit during the computation of groupings. You specify the columns in
parentheses as in the following statement:
ROLLUP (year, (quarter, month), day)
● Here, (quarter, month) form a composite column and are treated as a
unit.
● Composite columns are useful in ROLLUP, CUBE, GROUPING
SETS, and concatenated groupings.
GROUP BY ROLLUP(a, (b, c))
● Here, (b, c) are treated as a unit and rollup will not be applied across
(b, c)
Concatenated Groupings
● Concatenated Groupings offer a concise way to generate useful
combinations of groupings.
● Concatenation of grouping sets is very helpful for these reasons:
– Ease of query development
● You need not enumerate all groupings manually.
– Use by applications
● Groupings specified with concatenated groupings yield the cross-
product of groupings from each grouping set.
Concatenated Groupings
Syntax:
GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d)
This SQL defines the following groupings:
(a, c), (a, d), (b, c), (b, d)
TABLE PARTITIONING
What is Table Partitioning?
● Table Partitioning is a way to divide a large table into smaller, more
manageable parts without having to create separate tables for each
part. Data in a partitioned table is physically stored in groups of rows
called partitions and each partition can be accessed and maintained
separated.
● Partitioning allows tables and indexes to be partitioned into smaller,
more manageable units, providing database administrators with the
ability to pursue a "divide and conquer" approach to data
management.
Why Table Partitioning?
● Data is growing tremendously in organizations, storing millions of
data.
● Data is growing bigger and bigger.
● Breaking big table into multiple pieces.
● Dividing table into multiple pieces is called 'PARTIONING' With
partitioning, maintenance operations can be focused on particular
portions of tables.
Types of Partitioning
● Single Level Partitioning
– Range Partitioning
– Hash Partitioning
– List Partitioning
– Composite Partitioning
Range Partitioning
● Range Partitioning maps data to partitions based on ranges of
values of the partitioning key that you establish for each partition.
● A table that is partitioned by range is partitioned in such a way that
each partition contains rows for which the partitioning expression
value lies within a given range. Ranges should be contiguous but
not overlapping, and are defined using the VALUES LESS THAN
operator.
Range Partitioning
January to August Data
January
&
February
March
&
April
May
&
June
July
&
August
}
}
}
}
Hash Partitioning
● The hashing algorithm evenly distributes rows among partitions,
giving partitions approximately the same size.
● Hash Partitioning is also an easy-to-use alternative to range
partitioning, especially when the data to be partitioned is not historical
or has no obvious partitioning key.
● To partition a table using HASH partitioning, it is necessary to append
to the CREATE TABLE statement a PARTITION BY HASH (expr)
clause, where expr is an expression that returns an integer.
Hash Partitioning
List Partitioning
● List partitioning enables you to explicitly control how rows map to
partitions by specifying a list of discrete values for the partitioning key
in the description for each partition.
● The advantage of list partitioning is that you can group and organize
unordered and unrelated sets of data in a natural way.
List Partitioning
Composite Partitioning
MATERIALIZED VIEWS
Materialized View
● It is a table segment whose content are periodically refreshed based
on a query, either against a local or remote table.
● A materialized view is a table on disk that contains the result set of a
query.
Advantange:
● Replication of data between sites.
● Improve Performance
Options for Creating
Materialized View
● Immediate - The materialized view is populated with data
immediately because the build method is immediate and it is
available for use by query rewrite.
– Create the materialized view and then populate it with data.
● Deffered - Specify DEFERRED to indicate that the materialized view
is to be populated by the next REFRESH operation.
– Create the materialized view definition but do not populate it with
data.
Types of Refresh
Fast Refresh:
● Refreshes by incrementally adding the new data that has been
inserted into the tables using direct path or from the materialized view
log. Have to create 'LOG'"
● Fast Refresh" means you update (or insert/delete) only the rows
which have been changed on master tables.
Types of Refresh
Complete Refresh:
● "Complete Refresh" means you truncate entire materialized view and
insert new data.
● When a complete refresh occurs the materialized view's defining
query is executed and the entire result set replaces the data currently
residing in the materialized view.
Types of Refresh
Force Refresh:
● FORCE REFRESH is combination of FAST refresh and COMPLETE
refresh
● Applies FAST refresh if possible; otherwise, it applies COMPLETE
refresh.
Options For Refresh
Triggered- On Commit
● Refresh occurs automatically when a transaction that modified one of
the materialized view's detail tables commits.
● This can be specified as long as the materialized view is fast
refreshable (in other words, not complex). The ON COMMIT privilege
is necessary to use this mode.
● Can be used with materialized views on single table aggregates and
materialized views containing joins only.
Options For Refresh
Triggered- On Commit
● The time required to complete the commit may be slightly longer than
usual. This is because the refresh operation is performed as part of
the commit process.
● Therefore, this method may not be suitable if many users are
concurrently changing the tables upon which the materialized view is
based.
Options For Refresh
Triggered- On Demand
● The refresh is initiated by a manual request or a scheduled task.
● Refresh occurs when a user manually executes one of the available
refresh procedures contained in the DBMS_MVIEW package.
Options For Refresh
Triggered- On Demand
● Selecting the ON DEMAND execution mode means that you can take
advantage of the materialized view warehouse refresh facility, which
provides a quick and efficient mechanism for refreshing your
materialized views, either in their entirety or only with the additions to
the detail data.
● Oracle recommends you use ON COMMIT fast refresh rather than
ON DEMAND fast refresh.
REGULAR EXPRESSION
Regular Expression
Regular expressions enable you to search for patterns in string data by
using standardized syntax conventions.
Types of Characters:
● Metacharacters : Operators that specify search algorithms.
● Literals : Characters for which you are searching
Regular Expression
SQL Regular Expression- Functions and Conditions
● REGEXP_LIKE ---> Condition
● REGEXP_REPLACE ---> Function
● REGEXP_INSTR ---> Function
● REGEXP_SUBSTR ---> Function
SQL -REGEXP
● REGEXP_LIKE : REGEXP_LIKE condition allows you to perform
regular expression matching in the WHERE clause of a SELECT,
INSERT, UPDATE, or DELETE statement.
● REGEXP_REPLACE: REGEXP_REPLACE function is an extension
of the REPLACE function. This will allow to replace a sequence of
characters in a string with another set of characters using regular
expression pattern matching.
SQL -REGEXP
● REGEXP_INSTR : REGEXP_INSTR function is an extension of the
INSTR function. It returns the location of a regular expression pattern
in a string. Allows to find a substring in a string using regular
expression pattern matching.
● REGEXP_SUBSTR: REGEXP_SUBSTR function is an extension of
the SUBSTR function. This allows to extract a substring from a string
using regular expression pattern matching.
Meta Characters
FLASHBACK OPERATIONS
Time Stamp
● It is useful to recover from accidental statement failures.
● Flashback feature depends upon on how much undo retention time
you have specified.
Example:
SQL>select * from emp as of timestamp sysdate-1/24;
Or
SQL> SELECT * FROM emp AS OF TIMESTAMP
TO_TIMESTAMP('2007-06-07 10:00:00', 'YYYY-MM-DD
HH:MI:SS')
Undo- Retention
● Flashback feature depends upon on how much undo retention time you
have specifies
Example:
To insert the accidentally deleted rows again in the table type
SQL
insert into emp (select * from emp as of timestamp sysdate-1/24)
THANK YOU!

More Related Content

What's hot

Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sqlUmang Gupta
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In SqlAnurag
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
NITISH KUMAR
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
Edureka!
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
atishupadhyay
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Arzath Areeff
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...while
Jayfee Ramos
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
Infoviaan Technologies
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
Smriti Jain
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
Sachidananda M H
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
Ritwik Das
 

What's hot (20)

Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...while
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
OOP java
OOP javaOOP java
OOP java
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 

Similar to Oracle SQL Advanced

Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
KalyankumarVenkat1
 
Analytic & Windowing functions in oracle
Analytic & Windowing functions in oracleAnalytic & Windowing functions in oracle
Analytic & Windowing functions in oracle
Logan Palanisamy
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
Zohar Elkayam
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Sql DML
Sql DMLSql DML
Sql DML
Vikas Gupta
 
Sql DML
Sql DMLSql DML
Sql DML
Vikas Gupta
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
Hosein Zare
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Boston Institute of Analytics
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatablephanleson
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatableleminhvuong
 
Query parameterization
Query parameterizationQuery parameterization
Query parameterization
Riteshkiit
 
SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...
SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...
SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...
Datavail
 
Bootcamp sql fundamentals bootcamp_part1
Bootcamp   sql fundamentals  bootcamp_part1Bootcamp   sql fundamentals  bootcamp_part1
Bootcamp sql fundamentals bootcamp_part1
varunbhatt23
 
Anchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data typeAnchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data typedhruv patel
 
Presentation1
Presentation1Presentation1
Presentation1Jay Patel
 
Sql Server Query Parameterization
Sql Server Query ParameterizationSql Server Query Parameterization
Sql Server Query Parameterization
Mindfire Solutions
 
Database testing
Database testingDatabase testing
Database testing
Pesara Swamy
 
SQL Windowing
SQL WindowingSQL Windowing
SQL Windowing
Sandun Perera
 
OBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsOBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsMichael Perhats
 
SQL and Integrity Constraints (2).pptx
SQL and Integrity Constraints (2).pptxSQL and Integrity Constraints (2).pptx
SQL and Integrity Constraints (2).pptx
DeepakMishra195232
 

Similar to Oracle SQL Advanced (20)

Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
Analytic & Windowing functions in oracle
Analytic & Windowing functions in oracleAnalytic & Windowing functions in oracle
Analytic & Windowing functions in oracle
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Sql DML
Sql DMLSql DML
Sql DML
 
Sql DML
Sql DMLSql DML
Sql DML
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
 
Query parameterization
Query parameterizationQuery parameterization
Query parameterization
 
SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...
SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...
SQL Pass Summit Presentations from Datavail - Optimize SQL Server: Query Tuni...
 
Bootcamp sql fundamentals bootcamp_part1
Bootcamp   sql fundamentals  bootcamp_part1Bootcamp   sql fundamentals  bootcamp_part1
Bootcamp sql fundamentals bootcamp_part1
 
Anchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data typeAnchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data type
 
Presentation1
Presentation1Presentation1
Presentation1
 
Sql Server Query Parameterization
Sql Server Query ParameterizationSql Server Query Parameterization
Sql Server Query Parameterization
 
Database testing
Database testingDatabase testing
Database testing
 
SQL Windowing
SQL WindowingSQL Windowing
SQL Windowing
 
OBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsOBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic Functions
 
SQL and Integrity Constraints (2).pptx
SQL and Integrity Constraints (2).pptxSQL and Integrity Constraints (2).pptx
SQL and Integrity Constraints (2).pptx
 

Recently uploaded

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 

Recently uploaded (20)

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 

Oracle SQL Advanced

  • 1. ORACLE SQL - Advanced By: Dhananjay Goel CTO, Alphalogic Inc www.alphalogicinc.com
  • 2. Agenda 05 01 02 03 04 06 07 Manipulating Data Transforming Data Hierarchical Queries Table Partitioning Materialized Views Regular Expression Flashback Operations
  • 4. Manipulating Data Default Feature: In Oracle Data Definition Language (DDL) , we have the ability to assign default values to columns in table. Example - 1 Create table customer1 { customer_id number, customer_name varchar 2 (20), exp_date date DEFAULT to_date (‘31-dec-2009’,’dd-mon-yyyy’) } → insert into customer1 (customer_id, customer_name) values (1,’Jones’); → insert into customer1 (customer_id,customer_name,exp_date) values (2,’Raj’,’DEFAULT);
  • 5. Virtual Column ● Virtual Columns appear just like your normal table columns, but their values are derived at run time then being stored on disc. ● We cannot insert any data into a virtual column Advantages: ● Saves Disk spaces ● Need not update data if formula changes.
  • 6. Arithmetic calculations on Null values ● Any arithmetic expression which involves a NULL value returns a NULL value as the output. ● Use NVL functions while performing Arithmetic expression on NULL values in a table.
  • 7. Arithmetic calculations on Null values Example: DO ● select AVG (NVL(sales_amount,0)) from sales ● select COUNT (NVL(sales_amount,0)) from sales Example : DON’T ● select AVG (sales_amount) FROM sales; ● select COUNT (sales_amount) from sales; NULL values are not considered while taking Average or count, we have to use NVL function to the average and count values having nulls.
  • 8. Multi table Inserts Oracle INSERT ALL statement is used to add multiple rows with a single INSERT statement. The rows can be inserted into one table or multiple tables using only one SQL command. Inserting into 1 Table INSERT ALL INTO customer1 (customer_id,customer_name) values (1,’Kenny’) INTO customer1 (customer_id,customer_name)values (2,’Peter’) INTO customer1 (customer_id,customer_name)values (3,’John’) select* from dual;
  • 9. Multi table Inserts Inserting into Multiple Tables INSERT ALL INTO customer1(customer_id,customer_name) VALUES (1,’Kenny’) INTO customer1 (customer_id,customer_name) VALUES (2,’Peter’) INTO sales1 (sales_date,order_id,total_amount) VALUES (‘12-Jan -2017’,345,900);
  • 10. Merge the Data Merge statement is used to select rows from one or more sources for update or insertion into table or view. Merge Statement :- 1) Provides a convenient way to combine multiple operations. 2) Lets you avoid multiple INSERT,UPDATE and DELETE DML statement If Match found – UPDATE If Match not found – INSERT MERGE statement with Conditions We can specify conditions to determine whether to update or insert into target table or view.
  • 11. Example of Analytical Function Simple Query: Select sales_date, order_id,product_id,sales_amount,avg_amount from sales, (select_avg(sales_amount) as avg_amount from sales) Analytical Query: Select sales_date, order_id,product_id,sales_amount, avg(sales_amount) over () from sales ● In Analytical query (2nd ) we are fetching once and where as in 1st query we are fetching data multiple times. So 1st query burden’s the database.
  • 12. Analytical Functions Used to compute and aggregate values based on group of rows. The group of rows is called a window and is defined by the analytic_clause. Advantages a) Improve Query Speed b) Enhanced Developer Productivity c) Minimized Learning Effort
  • 13. Types of Analytical Functions Four Analytical Families : 1) Ranking Functions 2) Window Aggregate Functions 3) Reporting Aggregate Functions 4) LAG/LEAD Functions
  • 14. Rank Function ● The RANK function produces an ordered ranking of rows starting with a rank of one. ● RANK function returns the rank of a value in a group of values. ● The RANK function can be used two ways - as an Aggregate function or as an Analytic function.
  • 15. Window Function: ● The GROUP BY clause allows us to apply aggregate functions to subsets of rows. ● This time AVG is an analytic function, operating on the group of rows defined by the contents of the OVER clause. ● This group of rows is known as a window, which is why analytic functions are sometimes referred to as window[ing] functions.
  • 16. LAG And LEAD Function ● LAG navigates back : Previous month, quarter , year ● LEAD navigates front: Next month, quarter , year ● These functions are used to analyse data over a period of time. Example this month vs next month,last years vs this years,etc.
  • 17. Reporting Function: ● After a query has been processed, aggregate values like the number of resulting rows or an average value in a column can be easily computed within a partition and made available to other reporting functions. ● Reporting aggregate functions return the same aggregate value for every row in a partition.
  • 19. Case Statement ● Converting the row level data to column level using CASE statement. ● The CASE statement is like a series of IF statements, only using the key word WHEN. A CASE statement is evaluated from top to bottom. ● If a condition is true, then corresponding THEN clause is executed and execution jumps to the END CASE (short circuit evaluation) clause.
  • 20. Case Statement Syntax: CASE [expression] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 … WHEN condition_n THEN result_n ELSE result END case_name
  • 21. PIVOT ● Converting the row level data to column level using PIVOT Analytical function. ● PIVOT is faster than CASE statement. ● PIVOT means that you can aggregate your results and rotate rows into columns.
  • 22. PIVOT Syntax: Select * from (select column1, column2 from tables where conditions) PIVOT aggregate_function(column2) FOR column2 IN ( expr1, expr2, ... expr_n) | subquery) ORDER BY expression [ ASC | DESC ];
  • 23. PIVOT - Syntax Parameters: ● aggregate_function: It can be a function such as SUM, COUNT, MIN, MAX, or AVG functions. ● IN ( expr1, expr2, ... expr_n ): A list of values for column2 to pivot into headings in the cross-tabulation query results. ● Subquery: It can be used instead of a list of values. In this case, the results of the subquery would be used to determine the values for column2 to pivot into headings in the cross-tabulation query results.
  • 24. LISTAGG ● Converting the row level data to column level using LISTAGG Analytical function. ● Group all the values and all values are displayed in a single column. ● The nice thing about this function is it also allows us to order the elements in the concatenated list. Syntax LISTAGG (measure_column [, 'delimiter']) WITHIN GROUP (order_by_clause) [OVER (query_partition_clause)]
  • 25. LISTAGG - Syntax Syntax Arguments: ● measure_column:The column or expression whose values you wish to concatenate together in the result set. Null values in the measure_column are ignored. ● Delimiter: Optional. It is the delimiter to use when separating the measure_column values when outputting the results. ● order_by_clause: It determines the order that the concatenated values (ie: measure_column) are returned.
  • 26. UNION ALL ● Converting the column level data to row level using UNION ALL ● It returns all rows from the query and it does not remove duplicate rows between the various SELECT statements. ● Each SELECT statement within the UNION ALL operator must have the same number of fields in the result sets with similar data types.
  • 27. UNION ALL Syntax: SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION ALL SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];
  • 28. UNPIVOT ● Converting the column level data to row level using ● UNPIVOT.UNPIVOT transposes columns in rows. Example: Create table # student (Student Id int,Marks1 float, Marks2 float, Marks3 float) insert into #student values (1,5.6,7.3,4.2) insert values for all 4 rows.
  • 29. UNPIVOT - Syntax SELECT StudentID, MarksNo, MarksRecd FROM (SELECT StudentID, Marks1, Marks2, Marks3 FROM #Student) stu UNPIVOT (MarksRecd FOR MarksNo IN (Marks1, Marks2, Marks3)) AS marks
  • 31. Hierarchical Query ● Hierarchical query needs a definition of how each child relates to its parent. ● This is defined using the CONNECT BY .. PRIOR clause, which defines how the current row (child) relates to a prior row (parent). ● The START WITH clause can be used to define the root node(s) of the hierarchy.
  • 32. Hierarchical Query - Operators ● LEVEL : The position in the hierarchy of the current row in relation to the root node. ● CONNECT_BY_ROOT : Returns the root node(s) associated with the current row. ● SYS_CONNECT_BY_PATH : Returns a delimited breadcrumb from root to the current row. ● CONNECT_BY_ISLEAF : Indicates if the current row is a leaf node. ● ORDER SIBLINGS BY : Applies an order to siblings, without altering the basic hierarchical structure of the data returned by the query.
  • 33. CONNECT BY,PRIOR & START WITH ● The CONNECT BY clause specifies the relationship between parent rows and child rows of the hierarchy. ● The connect_by_condition can be any condition, however, it must use the PRIOR operator to refer to the parent row. ● Restriction on the CONNECT BY clause: The connect_by_condition cannot contain a regular subquery or a scalar subquery expression.
  • 34. CONNECT BY,PRIOR & START WITH Example: SELECT employee_id, last_name, manager_id, LEVEL FROM employees CONNECT BY PRIOR employee_id = manager_id; RESULT: Shows employee_id, last_name, manager_id and level in the tree for the employee hierarchy.
  • 35. CONNECT BY ROOT ● CONNECT_BY_ROOT is a unary operator that is valid only in hierarchical queries. ● When you qualify a column with this operator, Oracle returns the column value using data from the root row. ● This operator extends the functionality of the CONNECT BY [PRIOR] condition of hierarchical queries. ● Restriction on CONNECT_BY_ROOT: You cannot specify this operator in the START WITH condition or the CONNECT BY condition
  • 36. SYS-CONNECT_BY_PATH ● SYS_CONNECT_BY_PATH is valid only in hierarchical queries. ● It returns the path of a column value from root to node, with column values separated by char for each row returned by CONNECT BY condition.
  • 37. Extension To GROUP BY ● Aggregation is a fundamental part of data warehousing. ● To improve aggregation performance in your warehouse, Oracle Database provides the following extensions to the GROUP BY clause: – CUBE and ROLLUP extensions to the GROUP BY clause – GROUPING functions – GROUPING SETS expression
  • 38. ROLLUP Extension to GROUP BY ● ROLLUP enables a SELECT statement to calculate multiple levels of subtotals across a specified group of dimensions. ● It also calculates a grand total. ● The ROLLUP extension is highly efficient, adding minimal overhead to a query. ● The action of ROLLUP is straightforward: it creates subtotals that roll up from the most detailed level to a grand total, following a grouping list specified in the ROLLUP clause.
  • 39. ROLLUP Extension to GROUP BY ● First ,it calculates the standard aggregate values specified in the GROUP BY clause. ● Then, it creates progressively higher-level subtotals, moving from right to left through the list of grouping columns. ● Finally, it creates a grand total. ● If wanting to compress data when using ROLLUP. This is particularly useful when there are few updates to older partitions.
  • 40. WHEN TO USE ROLLUP ● Use the ROLLUP extension in tasks involving subtotals. ● It is very helpful for subtotaling along a hierarchical dimension such as time or geography. For instance, a query could specify a ROLLUP(y, m, day) or ROLLUP(country, state, city). ● For data warehouse administrators using summary tables, ROLLUP can simplify and speed up the maintenance of summary tables. Syntax: ROLLUP appears in the GROUP BY clause in a SELECT statement. Its form is: SELECT … GROUP BY ROLLUP(grouping_column_reference_list)
  • 41. CUBE Extension to GROUP BY ● CUBE takes a specified set of grouping columns and creates subtotals for all of their possible combination. ● In terms of multidimensional analysis, CUBE generates all the subtotals that could be calculated for a data cube with the specified dimensions.
  • 42. CUBE Extension to GROUP BY ● The data needed for cross-tabular reports can be generated with a single SELECT using CUBE. ● Note that population of summary tables is even faster if the CUBE query executes in parallel. ● CUBE is typically most suitable in queries that use columns from multiple dimensions rather than columns representing different levels of a single dimension. Syntax: CUBE appears in the GROUP BY clause in a SELECT statement. Its form is: SELECT … GROUP BY CUBE(grouping_column_reference_list)
  • 43. Grouping Functions ● Two challenges arise with the use of ROLLUP and CUBE. ● How can you programmatically determine which result set rows are subtotals, and how do you find the exact level of aggregation for a given subtotal? ● What happens if query results contain both stored NULL values and "NULL" values created by a ROLLUP or CUBE? ● GROUPING handles the above problems
  • 44. When to use GROUPING Function ● Using a single column as its argument, GROUPING returns 1 when it encounters a NULL value created by a ROLLUP or CUBE operation. ● That is, if the NULL indicates the row is a subtotal, GROUPING returns a 1. Any other type of value, including a stored NULL, returns a 0. ● The GROUPING function is not only useful for identifying NULLs, it also enables sorting subtotal rows and filtering results.
  • 45. When to use GROUPING Function Syntax: GROUPING appears in the selection list portion of a SELECT statement. Its form is: SELECT … [GROUPING(dimension_column)…] … GROUP BY … {CUBE | ROLLUP| GROUPING SETS} (dimension_column)
  • 46. Grouping Functions ● GROUPING_ID Function ● GROUPING SETS FUNCTION Problem: A four-column GROUP BY clause needs to be analyzed with four GROUPING functions. This is inconvenient to write in SQL and increases the number of columns required in the query. When you want to store the query result sets in tables, as with materialized views, the extra columns waste storage space. To address these problems, use the GROUPING_ID function
  • 47. GROUPING_ID Functions ● GROUPING_ID returns a single number that enables you to determine the exact GROUP BY level. ● For each row, GROUPING_ID takes the set of 1's and 0's that would be generated if you used the appropriate GROUPING functions and concatenates them, forming a bit vector. ● The bit vector is treated as a binary number, and the number's base- 10 value is returned by the GROUPING_ID function. ● If we group with the expression CUBE(a, b) the possible values:
  • 48. GROUPING_ID Example for CUBE(a, b) Aggregation Level Bit Vector GROUPING_ID a, b 0 0 0 a 0 1 1 b 1 0 2 Grand Total 1 1 3 GROUPING_ID clearly distinguishes groupings created by grouping set specification, and it is very useful during refresh and rewrite of materialized views.
  • 49. GROUP_ID Function ● While the extensions to GROUP BY offer power and flexibility, they also allow complex result sets that can include duplicate groupings. ● The GROUP_ID function lets you distinguish among duplicate groupings. ● If there are multiple sets of rows calculated for a given level, GROUP_ID assigns the value of 0 to all the rows in the first set. ● All other sets of duplicate rows for a particular grouping are assigned higher values, starting with 1
  • 50. GROUPING SETS Syntax ● GROUPING SETS syntax lets you define multiple groupings in the same query. ● GROUP BY computes all the groupings specified and combines them with UNION ALL. GROUP BY GROUPING sets (channel_desc, calendar_month_desc, country_id ) The above statement is equivalent to: GROUP BY channel_desc UNION ALL GROUP BY calendar_month_desc UNION ALL GROUP BY country_id
  • 51. GROUPING SETS Statements and Equivalent GROUP BY GROUPING SETS Statement Equivalent GROUP BY Statement GROUP BY GROUPING SETS(a, b, c) GROUP BY a UNION ALL GROUP BY b UNION ALL GROUP BY c GROUP BY GROUPING SETS(a, b, (b, c)) GROUP BY a UNION ALL GROUP BY b UNION ALL GROUP BY b, c GROUP BY GROUPING SETS((a, b, c)) GROUP BY a, b, c GROUP BY GROUPING SETS(a, (b), ()) GROUP BY a UNION ALL GROUP BY b UNION ALL GROUP BY () GROUP BY GROUPING SETS(a, ROLLUP(b, c)) GROUP BY a UNION ALL GROUP BY ROLLUP(b, c)
  • 52. GROUPING SETS Statements and Equivalent GROUP BY ● A query based on UNION would need multiple scans of the base table, sales. ● This could be very inefficient as fact tables will normally be huge. ● Using GROUPING SETS statements, all the groupings of interest are available in the same query block.
  • 53. Composite Columns ● A composite column is a collection of columns that are treated as a unit during the computation of groupings. You specify the columns in parentheses as in the following statement: ROLLUP (year, (quarter, month), day) ● Here, (quarter, month) form a composite column and are treated as a unit. ● Composite columns are useful in ROLLUP, CUBE, GROUPING SETS, and concatenated groupings. GROUP BY ROLLUP(a, (b, c)) ● Here, (b, c) are treated as a unit and rollup will not be applied across (b, c)
  • 54. Concatenated Groupings ● Concatenated Groupings offer a concise way to generate useful combinations of groupings. ● Concatenation of grouping sets is very helpful for these reasons: – Ease of query development ● You need not enumerate all groupings manually. – Use by applications ● Groupings specified with concatenated groupings yield the cross- product of groupings from each grouping set.
  • 55. Concatenated Groupings Syntax: GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d) This SQL defines the following groupings: (a, c), (a, d), (b, c), (b, d)
  • 57. What is Table Partitioning? ● Table Partitioning is a way to divide a large table into smaller, more manageable parts without having to create separate tables for each part. Data in a partitioned table is physically stored in groups of rows called partitions and each partition can be accessed and maintained separated. ● Partitioning allows tables and indexes to be partitioned into smaller, more manageable units, providing database administrators with the ability to pursue a "divide and conquer" approach to data management.
  • 58. Why Table Partitioning? ● Data is growing tremendously in organizations, storing millions of data. ● Data is growing bigger and bigger. ● Breaking big table into multiple pieces. ● Dividing table into multiple pieces is called 'PARTIONING' With partitioning, maintenance operations can be focused on particular portions of tables.
  • 59. Types of Partitioning ● Single Level Partitioning – Range Partitioning – Hash Partitioning – List Partitioning – Composite Partitioning
  • 60. Range Partitioning ● Range Partitioning maps data to partitions based on ranges of values of the partitioning key that you establish for each partition. ● A table that is partitioned by range is partitioned in such a way that each partition contains rows for which the partitioning expression value lies within a given range. Ranges should be contiguous but not overlapping, and are defined using the VALUES LESS THAN operator.
  • 61. Range Partitioning January to August Data January & February March & April May & June July & August } } } }
  • 62. Hash Partitioning ● The hashing algorithm evenly distributes rows among partitions, giving partitions approximately the same size. ● Hash Partitioning is also an easy-to-use alternative to range partitioning, especially when the data to be partitioned is not historical or has no obvious partitioning key. ● To partition a table using HASH partitioning, it is necessary to append to the CREATE TABLE statement a PARTITION BY HASH (expr) clause, where expr is an expression that returns an integer.
  • 64. List Partitioning ● List partitioning enables you to explicitly control how rows map to partitions by specifying a list of discrete values for the partitioning key in the description for each partition. ● The advantage of list partitioning is that you can group and organize unordered and unrelated sets of data in a natural way.
  • 68. Materialized View ● It is a table segment whose content are periodically refreshed based on a query, either against a local or remote table. ● A materialized view is a table on disk that contains the result set of a query. Advantange: ● Replication of data between sites. ● Improve Performance
  • 69. Options for Creating Materialized View ● Immediate - The materialized view is populated with data immediately because the build method is immediate and it is available for use by query rewrite. – Create the materialized view and then populate it with data. ● Deffered - Specify DEFERRED to indicate that the materialized view is to be populated by the next REFRESH operation. – Create the materialized view definition but do not populate it with data.
  • 70. Types of Refresh Fast Refresh: ● Refreshes by incrementally adding the new data that has been inserted into the tables using direct path or from the materialized view log. Have to create 'LOG'" ● Fast Refresh" means you update (or insert/delete) only the rows which have been changed on master tables.
  • 71. Types of Refresh Complete Refresh: ● "Complete Refresh" means you truncate entire materialized view and insert new data. ● When a complete refresh occurs the materialized view's defining query is executed and the entire result set replaces the data currently residing in the materialized view.
  • 72. Types of Refresh Force Refresh: ● FORCE REFRESH is combination of FAST refresh and COMPLETE refresh ● Applies FAST refresh if possible; otherwise, it applies COMPLETE refresh.
  • 73. Options For Refresh Triggered- On Commit ● Refresh occurs automatically when a transaction that modified one of the materialized view's detail tables commits. ● This can be specified as long as the materialized view is fast refreshable (in other words, not complex). The ON COMMIT privilege is necessary to use this mode. ● Can be used with materialized views on single table aggregates and materialized views containing joins only.
  • 74. Options For Refresh Triggered- On Commit ● The time required to complete the commit may be slightly longer than usual. This is because the refresh operation is performed as part of the commit process. ● Therefore, this method may not be suitable if many users are concurrently changing the tables upon which the materialized view is based.
  • 75. Options For Refresh Triggered- On Demand ● The refresh is initiated by a manual request or a scheduled task. ● Refresh occurs when a user manually executes one of the available refresh procedures contained in the DBMS_MVIEW package.
  • 76. Options For Refresh Triggered- On Demand ● Selecting the ON DEMAND execution mode means that you can take advantage of the materialized view warehouse refresh facility, which provides a quick and efficient mechanism for refreshing your materialized views, either in their entirety or only with the additions to the detail data. ● Oracle recommends you use ON COMMIT fast refresh rather than ON DEMAND fast refresh.
  • 78. Regular Expression Regular expressions enable you to search for patterns in string data by using standardized syntax conventions. Types of Characters: ● Metacharacters : Operators that specify search algorithms. ● Literals : Characters for which you are searching
  • 79. Regular Expression SQL Regular Expression- Functions and Conditions ● REGEXP_LIKE ---> Condition ● REGEXP_REPLACE ---> Function ● REGEXP_INSTR ---> Function ● REGEXP_SUBSTR ---> Function
  • 80. SQL -REGEXP ● REGEXP_LIKE : REGEXP_LIKE condition allows you to perform regular expression matching in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement. ● REGEXP_REPLACE: REGEXP_REPLACE function is an extension of the REPLACE function. This will allow to replace a sequence of characters in a string with another set of characters using regular expression pattern matching.
  • 81. SQL -REGEXP ● REGEXP_INSTR : REGEXP_INSTR function is an extension of the INSTR function. It returns the location of a regular expression pattern in a string. Allows to find a substring in a string using regular expression pattern matching. ● REGEXP_SUBSTR: REGEXP_SUBSTR function is an extension of the SUBSTR function. This allows to extract a substring from a string using regular expression pattern matching.
  • 84. Time Stamp ● It is useful to recover from accidental statement failures. ● Flashback feature depends upon on how much undo retention time you have specified. Example: SQL>select * from emp as of timestamp sysdate-1/24; Or SQL> SELECT * FROM emp AS OF TIMESTAMP TO_TIMESTAMP('2007-06-07 10:00:00', 'YYYY-MM-DD HH:MI:SS')
  • 85. Undo- Retention ● Flashback feature depends upon on how much undo retention time you have specifies Example: To insert the accidentally deleted rows again in the table type SQL insert into emp (select * from emp as of timestamp sysdate-1/24)