Oracle Database SQL Certified Associate
1 Introduction to SQL
1-1 Overview of SQL
1-2 History of SQL
1-3 SQL Standards
2 SQL Data Types
2-1 Numeric Data Types
2-2 Character Data Types
2-3 Date and Time Data Types
2-4 Large Object (LOB) Data Types
2-5 Miscellaneous Data Types
3 Creating and Managing Tables
3-1 Creating Tables
3-2 Modifying Tables
3-3 Dropping Tables
3-4 Table Constraints
3-5 Temporary Tables
4 Data Manipulation Language (DML)
4-1 Inserting Data
4-2 Updating Data
4-3 Deleting Data
4-4 Selecting Data
4-5 Using Subqueries
5 Data Control Language (DCL)
5-1 Granting Privileges
5-2 Revoking Privileges
6 Data Definition Language (DDL)
6-1 Creating Tables
6-2 Altering Tables
6-3 Dropping Tables
6-4 Creating Indexes
6-5 Dropping Indexes
6-6 Creating Views
6-7 Dropping Views
7 SQL Functions
7-1 Single-Row Functions
7-2 Aggregate Functions
7-3 Group Functions
7-4 Analytical Functions
8 Joins and Subqueries
8-1 Inner Joins
8-2 Outer Joins
8-3 Self-Joins
8-4 Cross Joins
8-5 Subqueries
9 Set Operators
9-1 UNION
9-2 UNION ALL
9-3 INTERSECT
9-4 MINUS
10 Grouping and Aggregation
10-1 GROUP BY Clause
10-2 HAVING Clause
10-3 ROLLUP and CUBE
10-4 GROUPING SETS
11 Transactions and Concurrency
11-1 Transaction Control Statements
11-2 Locking and Concurrency
11-3 Isolation Levels
12 Oracle SQL Developer
12-1 Overview of Oracle SQL Developer
12-2 Using SQL Worksheet
12-3 Managing Connections
12-4 Running Scripts
13 Advanced SQL Topics
13-1 Recursive Queries
13-2 Model Clause
13-3 PIVOT and UNPIVOT
13-4 Flashback Query
14 Performance Tuning
14-1 Query Optimization
14-2 Indexing Strategies
14-3 Analyzing Query Performance
15 Security and Auditing
15-1 User Management
15-2 Role Management
15-3 Auditing SQL Statements
16 Backup and Recovery
16-1 Backup Strategies
16-2 Recovery Strategies
16-3 Using RMAN
17 Oracle Database Architecture
17-1 Overview of Oracle Database Architecture
17-2 Memory Structures
17-3 Process Structures
17-4 Storage Structures
18 PLSQL Basics
18-1 Introduction to PLSQL
18-2 PLSQL Block Structure
18-3 Variables and Data Types
18-4 Control Structures
18-5 Exception Handling
19 Oracle SQL Certification Exam Preparation
19-1 Exam Objectives
19-2 Sample Questions
19-3 Practice Tests
19-4 Exam Tips
Advanced SQL Topics in Oracle Database

Advanced SQL Topics in Oracle Database

Key Concepts

1. Analytical Functions

Analytical functions in Oracle SQL perform calculations across a set of table rows that are somehow related to the current row. They are used to compute aggregates, rankings, and other complex calculations without the need for self-joins.

Example:

Using the RANK() function to rank employees by salary within each department:

SELECT EmployeeID, Department, Salary, RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS Rank FROM Employees;

2. Recursive WITH Clause (Common Table Expressions)

The recursive WITH clause allows you to perform recursive queries, which are useful for hierarchical data structures like organizational charts or bill of materials.

Example:

Finding all subordinates of a manager in an organizational hierarchy:

WITH Subordinates (EmployeeID, ManagerID, Level) AS ( SELECT EmployeeID, ManagerID, 1 AS Level FROM Employees WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID, e.ManagerID, s.Level + 1 FROM Employees e JOIN Subordinates s ON e.ManagerID = s.EmployeeID ) SELECT * FROM Subordinates;

3. PIVOT and UNPIVOT

The PIVOT operator transforms rows into columns, and the UNPIVOT operator transforms columns back into rows. These operators are useful for creating cross-tabulation reports.

Example:

Pivoting sales data by quarter:

SELECT * FROM Sales PIVOT (SUM(Amount) FOR Quarter IN ('Q1', 'Q2', 'Q3', 'Q4'));

4. MERGE Statement

The MERGE statement allows you to perform insert, update, and delete operations in a single statement based on the results of a join with a source table.

Example:

Merging data from a source table into a target table:

MERGE INTO TargetTable t USING SourceTable s ON (t.ID = s.ID) WHEN MATCHED THEN UPDATE SET t.Value = s.Value WHEN NOT MATCHED THEN INSERT (ID, Value) VALUES (s.ID, s.Value);

5. Flashback Query

Flashback Query allows you to query data as it existed at a specific point in time, which is useful for auditing and data recovery.

Example:

Querying data as it was yesterday:

SELECT * FROM Employees AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '1' DAY;

6. Materialized Views

Materialized views store the results of a query in a table-like structure, which can improve query performance for complex or frequently executed queries.

Example:

Creating a materialized view for sales data:

CREATE MATERIALIZED VIEW SalesSummary AS SELECT Product, SUM(Sales) AS TotalSales FROM Sales GROUP BY Product;

7. External Tables

External tables allow you to access data stored in files outside the database as if they were regular database tables, which is useful for integrating data from different sources.

Example:

Creating an external table for data stored in a CSV file:

CREATE TABLE ExternalSales ( Product VARCHAR2(50), Sales NUMBER ) ORGANIZATION EXTERNAL ( TYPE ORACLE_LOADER DEFAULT DIRECTORY data_dir ACCESS PARAMETERS ( RECORDS DELIMITED BY NEWLINE FIELDS TERMINATED BY ',' ) LOCATION ('sales.csv') );

8. SQL Macros

SQL Macros allow you to define reusable SQL expressions that can be used in queries, which can simplify complex queries and improve code maintainability.

Example:

Defining a SQL macro for calculating the total sales:

CREATE OR REPLACE FUNCTION TotalSales(product IN VARCHAR2) RETURN NUMBER SQL_MACRO IS BEGIN RETURN q'{ SELECT SUM(Sales) FROM Sales WHERE Product = product }'; END;

9. SQL Plan Management

SQL Plan Management ensures that SQL statements execute using a known, stable execution plan, which helps in maintaining performance stability over time.

Example:

Enabling SQL plan management for a specific SQL statement:

ALTER SYSTEM SET SQLTUNE_CATEGORY = 'MY_CATEGORY'; EXEC DBMS_SQLTUNE.CREATE_STGTAB_SQLSET('MY_SQLSET_TABLE'); EXEC DBMS_SQLTUNE.PACK_STGTAB_SQLSET('MY_SQLSET', 'MY_CATEGORY', 'MY_SQLSET_TABLE');

10. SQL Tuning Advisor

SQL Tuning Advisor analyzes SQL statements and provides recommendations for improving performance, such as index creation, SQL restructuring, and more.

Example:

Running SQL Tuning Advisor on a specific SQL statement:

DECLARE my_task VARCHAR2(30); BEGIN my_task := DBMS_SQLTUNE.CREATE_TUNING_TASK( sql_id => 'my_sql_id' ); DBMS_SQLTUNE.EXECUTE_TUNING_TASK(task_name => my_task); DBMS_SQLTUNE.REPORT_TUNING_TASK(task_name => my_task); END;

11. SQL Performance Analyzer

SQL Performance Analyzer helps in understanding the impact of changes in the database environment on SQL performance, such as upgrades or parameter changes.

Example:

Running SQL Performance Analyzer to compare performance before and after an upgrade:

DECLARE my_task VARCHAR2(30); BEGIN my_task := DBMS_SQLPA.CREATE_ANALYSIS_TASK( sqlset_name => 'my_sqlset' ); DBMS_SQLPA.EXECUTE_ANALYSIS_TASK( task_name => my_task, execution_type => 'COMPARE PERFORMANCE' ); DBMS_SQLPA.REPORT_ANALYSIS_TASK(task_name => my_task); END;

12. SQL Access Advisor

SQL Access Advisor provides recommendations for optimizing the physical database design, such as creating indexes, materialized views, and partitioning strategies.

Example:

Running SQL Access Advisor to get recommendations for optimizing a workload:

DECLARE my_task VARCHAR2(30); BEGIN my_task := DBMS_ADVISOR.CREATE_TASK('SQL Access Advisor'); DBMS_ADVISOR.CREATE_OBJECT( task_name => my_task, object_type => 'SQLSET', attribute1 => 'my_sqlset' ); DBMS_ADVISOR.SET_TASK_PARAMETER( task_name => my_task, parameter => 'ANALYSIS_SCOPE', value => 'ALL' ); DBMS_ADVISOR.EXECUTE_TASK(task_name => my_task); DBMS_ADVISOR.GET_TASK_REPORT(task_name => my_task); END;

13. SQL Monitoring

SQL Monitoring provides real-time performance statistics for long-running SQL statements, helping in identifying and resolving performance issues.

Example:

Monitoring a specific SQL statement in real-time:

SELECT * FROM V$SQL_MONITOR WHERE SQL_ID = 'my_sql_id';