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
19.2 Sample Questions Explained

19.2 Sample Questions Explained

Key Concepts

1. SQL Query Execution

Understanding how SQL queries are executed in Oracle Database is crucial for optimizing performance. This includes understanding the query optimizer, execution plans, and the role of indexes.

Example:

Consider a query to retrieve employee details:

SELECT * FROM employees WHERE department_id = 10;

The query optimizer determines the best execution plan, which might involve using an index on the department_id column to speed up the query.

2. Data Types and Conversion

Oracle Database supports various data types, and understanding how to convert between them is essential. This includes implicit and explicit data type conversion.

Example:

Converting a string to a date:

SELECT TO_DATE('2023-10-01', 'YYYY-MM-DD') FROM dual;

This converts the string '2023-10-01' to a date data type.

3. Joins and Subqueries

Joins and subqueries are fundamental for combining data from multiple tables. Understanding the different types of joins (INNER, OUTER, etc.) and how to use subqueries effectively is key.

Example:

Using an INNER JOIN to combine data from two tables:

SELECT e.employee_id, e.first_name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;

This query retrieves employee details along with their department names.

4. Aggregation Functions

Aggregation functions like SUM, AVG, COUNT, etc., are used to perform calculations on groups of rows. Understanding how to use these functions and the GROUP BY clause is important.

Example:

Calculating the average salary by department:

SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;

This query returns the average salary for each department.

5. Transactions and Concurrency

Transactions ensure data integrity by grouping SQL statements into logical units of work. Understanding transaction control statements (COMMIT, ROLLBACK) and concurrency issues is crucial.

Example:

Using COMMIT and ROLLBACK:

BEGIN UPDATE employees SET salary = salary + 1000 WHERE employee_id = 101; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; END;

This block updates the salary of an employee and commits the transaction, rolling back if an error occurs.

6. Indexing and Performance Tuning

Indexes improve query performance by providing faster access to data. Understanding how to create and use indexes effectively is essential for performance tuning.

Example:

Creating an index on a column:

CREATE INDEX idx_employee_name ON employees(first_name);

This index speeds up queries that filter or sort by the first_name column.

7. PL/SQL Basics

PL/SQL is Oracle's procedural language extension for SQL. Understanding basic PL/SQL constructs like blocks, variables, and control structures is important.

Example:

A simple PL/SQL block:

DECLARE v_name VARCHAR2(50); BEGIN SELECT first_name INTO v_name FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE(v_name); END;

This block retrieves the first name of an employee and prints it.

8. Triggers and Constraints

Triggers are PL/SQL blocks that execute automatically in response to specific events. Constraints enforce data integrity rules. Understanding both is important for maintaining data integrity.

Example:

Creating a trigger:

CREATE OR REPLACE TRIGGER check_salary_before_insert BEFORE INSERT ON employees FOR EACH ROW BEGIN IF :NEW.salary < 1000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary too low'); END IF; END;

This trigger prevents the insertion of employees with a salary less than 1000.

9. Views and Materialized Views

Views are virtual tables based on the result of a query. Materialized views store the result of a query physically. Understanding their use cases and differences is important.

Example:

Creating a view:

CREATE VIEW employee_details AS SELECT employee_id, first_name, last_name, department_id FROM employees;

This view provides a simplified way to access employee details.

10. Stored Procedures and Functions

Stored procedures and functions are PL/SQL blocks stored in the database. They can be called with parameters and are useful for encapsulating business logic.

Example:

Creating a stored procedure:

CREATE OR REPLACE PROCEDURE increase_salary (emp_id IN NUMBER, inc_amount IN NUMBER) IS BEGIN UPDATE employees SET salary = salary + inc_amount WHERE employee_id = emp_id; END;

This procedure increases the salary of an employee by a specified amount.

11. Cursors and Cursor Variables

Cursors are used to handle the result sets of SQL queries. Cursor variables allow for more dynamic and flexible handling of result sets.

Example:

Using a cursor:

DECLARE CURSOR emp_cur IS SELECT * FROM employees; emp_rec emp_cur%ROWTYPE; BEGIN OPEN emp_cur; LOOP FETCH emp_cur INTO emp_rec; EXIT WHEN emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE(emp_rec.first_name); END LOOP; CLOSE emp_cur; END;

This block fetches and prints the first names of all employees.

12. Dynamic SQL

Dynamic SQL allows for the construction and execution of SQL statements at runtime. This is useful when the SQL statement is not known until runtime.

Example:

Using dynamic SQL:

DECLARE sql_stmt VARCHAR2(100); emp_name VARCHAR2(50); BEGIN sql_stmt := 'SELECT first_name FROM employees WHERE employee_id = :id'; EXECUTE IMMEDIATE sql_stmt INTO emp_name USING 101; DBMS_OUTPUT.PUT_LINE(emp_name); END;

This block retrieves the first name of an employee using dynamic SQL.

13. Bulk Operations

Bulk operations allow for processing multiple rows of data in a single operation, improving performance by reducing context switches between PL/SQL and SQL engines.

Example:

Using BULK COLLECT:

DECLARE TYPE emp_id_table IS TABLE OF employees.employee_id%TYPE; emp_ids emp_id_table; BEGIN SELECT employee_id BULK COLLECT INTO emp_ids FROM employees WHERE department_id = 10; FOR i IN 1..emp_ids.COUNT LOOP DBMS_OUTPUT.PUT_LINE(emp_ids(i)); END LOOP; END;

This block retrieves all employee IDs in department 10 using BULK COLLECT.

14. User-Defined Exceptions

User-defined exceptions allow for creating custom error conditions that can be raised and handled within PL/SQL code.

Example:

Using a user-defined exception:

DECLARE invalid_salary EXCEPTION; v_salary NUMBER(8,2); BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 101; IF v_salary < 1000 THEN RAISE invalid_salary; END IF; EXCEPTION WHEN invalid_salary THEN DBMS_OUTPUT.PUT_LINE('Invalid Salary'); END;

This block raises a user-defined exception if the salary is less than 1000.

15. Pipelined Functions

Pipelined functions return a set of values one at a time, allowing for efficient processing of large datasets.

Example:

Creating a pipelined function:

CREATE OR REPLACE FUNCTION get_employees RETURN SYS.ODCINUMBERLIST PIPELINED IS BEGIN FOR rec IN (SELECT employee_id FROM employees) LOOP PIPE ROW(rec.employee_id); END LOOP; RETURN; END;

This function returns employee IDs one at a time.

16. Object-Oriented Features

Oracle Database supports object-oriented programming (OOP) features like objects, methods, and inheritance.

Example:

Creating an object type:

CREATE OR REPLACE TYPE t_employee AS OBJECT ( id NUMBER, name VARCHAR2(50), MEMBER FUNCTION get_details RETURN VARCHAR2 );

This creates an object type for an employee.

17. Collections

Collections in PL/SQL are similar to arrays in other programming languages. They allow for storing and manipulating groups of related data items.

Example:

Using a collection (VARRAY):

DECLARE TYPE name_array IS VARRAY(5) OF VARCHAR2(50); names name_array := name_array('Alice', 'Bob', 'Charlie', 'David', 'Eve'); BEGIN FOR i IN 1..names.COUNT LOOP DBMS_OUTPUT.PUT_LINE(names(i)); END LOOP; END;

This block prints the names stored in a VARRAY.

18. Records

Records are composite data types that allow for grouping related data items into a single variable.

Example:

Using a record:

DECLARE TYPE emp_rec IS RECORD ( emp_id NUMBER, name VARCHAR2(50), salary NUMBER(8,2) ); emp emp_rec; BEGIN SELECT employee_id, first_name, salary INTO emp FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE(emp.name); END;

This block retrieves and prints the name of an employee using a record.

19. Security and Privileges

Security in Oracle Database involves controlling access to database objects and ensuring that sensitive data is protected. This includes using roles, privileges, and encryption.

Example:

Granting and revoking privileges:

GRANT SELECT ON employees TO hr_user; REVOKE SELECT ON employees FROM hr_user;

This grants and revokes SELECT privilege on the employees table to a user.