You are on page 1of 46

IT133 ADVANCED DATABASE MANAGEMENT

Lecture 01: Advanced SQL

Another Introduction

SQL is a common language for ALL Relational Databases, skills can easily be transferred from one database to another. And, programs written in SQL are portable, they can often be moved with very little modification. The most common task/use: Querying data. A query is an operation that retrieves data from one or more tables or views.

Types of SQL Statements

Data Definition Language Data Manipulation Language Transaction Control Statements Session Control Statements Embedded SQL Statements

Data Definition Language (DDL)


DDL lets you perform the following tasks: Create, alter, and drop schema objects (CREATE, ALTER, DROP) Grant and revoke privileges and roles (GRANT, REVOKE) Analyze information on a table, index, or cluster (ANALYZE) Establish auditing options (AUDIT) Add comments to the data dictionary (COMMENT) Oracle implicitly commits the current transaction before and after every DDL.

Data Manipulation Language (DML)


DML statements access and manipulate data in existing schema objects. CALL INSERT SELECT DELETE LOCK TABLE UPDATE EXPLAIN PLAN MERGE DML do not implicitly commit the current transaction.

Transaction Control Statements


Transaction control statements manage changes made by DML statements. COMMIT ROLLBACK SAVEPOINT SET TRANSACTION

Session Control Statements

The single system control statement, ALTER SYSTEM, dynamically manages the properties of an Oracle Database instance. This statement does not implicitly commit the current transaction.

Embedded SQL Statements

Embedded SQL statements place DDL, DML, and transaction control statements within a procedural language program.

Advance SQL
Relational Set Operators Joins Sub-queries Functions

Relational Set Operators


Advanced SQL Part I

UNION UNION ALL INTERSECT MINUS

Relational Set Operators


SQL commands operate over entire sets of rows and columns (tables) at once. Using sets, you can combine two or more sets to create new sets (or relations). NB: The SQL-99 standard defines the operations that all DBMSs must perform on data, but it leaves the implementation details to the DBMS vendors. Some advanced SQL functions may not work on all DBMS implementations and that some DBMS Vendors may implement additional features not found in the SQL standard. UNION, INTERSECT, and MINUS are Oracle implementations EXCEPT is the SQL standard for MINUS

Cont

UNION, INTERSECT, and MINUS will work properly for union-compatible relations. Union-compatible means that the names of the relation attributes must be the same and their data types must be identical. In practice, Oracle requires that the datatypes be compatible but not necessarily exactly the same

Oracle Datatypes

A datatype is either scalar or non-scalar


SCALAR contains an atomic values NON-SCALAR contains sets of values

There are over 20 built-in datatypes in Oracle, categorized into:


Character Datatypes Numeric Datatypes Datetime and Interval Datatypes Long Datatype RAW and LONG RAW Datatypes Large Object (LOB) Datatypes

What we are going to use:

VARCHAR2(size) NUMBER[(precision[,scale])] DATE

Lets keep it that simple for now.

NB: More on comparison rules and conversion when we get to SQL FUNCTIONS and PL/SQL

So going back
Set operators combine the results of two component queries into a single result. Queries containing set operators are called compound queries. UNION UNION ALL INTERSECT MINUS

Restrictions on the Set Operators

Not valid on columns of type BLOB, CLOB, BFILE, VARRAY, or nested table UNION, INTERSECT, and MINUS are not valid on LONG columns If the select list preceding the set operator contains an expression, then you must provide a column alias for the expression in order to refer to it in the ORDER BY clause ORDER BY is not allowed in the subquery of operators

UNION [ALL]

The UNION operator returns only the distinct rows that appear in either result The UNION ALL operator returns all rows does not eliminate duplicate rows
SELECT column_name FROM table_name UNION [ALL] SELECT column_name FROM table_name;

UNION [ALL] example


SELECT location_id, department_name Department, TO_CHAR(NULL) Warehouse FROM departments UNION SELECT location_id, TO_CHAR(NULL) Department, warehouse_name FROM warehouses; -SELECT location_id FROM locations

UNION ALL
SELECT location_id FROM departments;

INTERSECT

INTERSECT returns only the rows returned by both queries


SELECT column_name FROM table_name INTERSECT SELECT column_name FROM table_name

-- example SELECT product_id FROM inventories INTERSECT SELECT product_id FROM order_items;

MINUS

MINUS returns only the unique rows returned by the first query but not by the second query
SELECT FROM MINUS SELECT FROM column_name table_name column_name table_name

-- example SELECT product_id FROM inventories MINUS SELECT product_id FROM order_items;

Sorting Compound Query Results

The ORDER BY clause must specify positions or aliases rather than explicit expressions The ORDER BY clause can appear only in the last component query ORDER BY will sort all rows returned by the entire compound query

Questions so far?

Joins

Join Conditions Equijoin Self Join Cartesian Products Inner Joins Outer Joins Antijoins Semijoins

Advanced SQL Part II

Joins and Join Conditions

A join is a query that combines rows from two or more tables, views, or materialized views. Oracle performs a join whenever multiple tables appear in the FROM clause of the query. If any two of the tables have a column name in common, references to the(se) column (s) must be qualified with table names to avoid ambiguity A join condition is one that compares two columns each from a different table; it identifies which pairs of rows are to be combined from the tables.

EQUIJOINS

An equijoin is a join condition containing an equality operator. Combines rows that have equivalent values for the specified columns

SELECT o.product_id, i.product_name FROM inventories i, order_items o WHERE i.product_id = o.product_id

SELF JOINS

A self join is a join of a table to itself The table appears twice in the FROM clause and must be followed by table aliases that will be used to qualify column names in the entire query

SELECT m.last_name || , || m.first_name FROM employees e, employees m

WHERE e.mgr_id = m.employee_id

Cartesian Products

If two tables in a join query have no join condition, then their Cartesian product is returned. Each row of one table is combined with each row of the second table Generates many rows that are rarely useful

Inner Joins

An inner join (sometimes called a simple join) is a join of two or more tables that returns only those rows that satisfy the join condition

Outer Joins

An outer join extends the result of a simple join; Returns all rows that satisfy the join condition and also returns some or all of the rows from one table for which no rows from the other satisfy the join condition LEFT OUTER JOIN A & B and returns all rows from A RIGHT OUTER JOIN A & B and returns all rows from B FULL OUTER JOIN A and B, extended with nulls if they do not satisfy the join condition The outer join operator (+) may also be used in the join condition

Outer Joins examples


SELECT d.department_id, e.last_name FROM departments d LEFT OUTER JOIN employees e ON d.department_id = e.department_id ORDER BY d.department_id; -- using the outer join operator SELECT d.department_id, e.last_name FROM departments d, employees e WHERE d.department_id=e.department_id(+) ORDER BY d.department_id;

Lets illustrate OUTER JOINs

Recommendation and Restrictions

Oracle recommends the use of the FROM clause OUTER JOIN syntax rather than the Oracle join operator Outer join queries using the Oracle join operator are subject to restrictions, some of which are:
The (+) operator can appear only in the WHERE clause In case of joins with multiple join conditions, the (+) operator must be used in all of the join conditions Does not produce an outer join if you specify one table in the outer query and the other table in an inner query Cannot be used to outer-join a table to itself, although self joins are valid

Antijoin

An antijoin returns rows from the left side of the predicate for which there are no corresponding rows on the right side of the predicate. It returns rows that fail to match (NOT IN) the subquery on the right

SELECT * FROM employees WHERE department_id NOT IN (SELECT department_id FROM departments WHERE location_id = 1700);

Semijoin

A semijoin returns rows that match an EXISTS subquery without duplicating rows from the left side of the predicate when multiple rows on t he right side satisfy the criteria of the subquery.

SELECT * FROM departments d WHERE EXISTS (SELECT * FROM employees e WHERE d.department_id = e.department_id AND e.salary >2500);

Questions?

Really? Last chance

Seatwork!

List the SOLD transactions for customer 1003 for June with the RENTAL transactions for customer 1011 for May. Display Customer Name and product description/movie title with transaction type and month.
Which products were bought by both customers 1003 and 1011? Rented? Which products were sold in June but not sold in May?

Subqueries
Inline View Nested Subquery Correlated Subquery

Using Subqueries

A subquery answers multiple-part questions; i.e. to determine who works in Taylors department, you can first use a subquery to determine the department in which Taylor works, then answer the original question in the parent query A subquery in the FROM clause of a SELECT statement is also called an inline view. A subquery in the WHERE clause of a SELECT statement is also called a nested subquery.

Using Subqueries cont

A subquery can contain another subquery; no limit on the number of subquery levels in the FROM clause of the top-level query You can nest up to 255 levels of subqueries in the WHERE clause If columns in a subquery have the same name as columns in the containing statement, then you must qualify any reference to the column

Correlated Subquery

A correlated subquery is when a nested subquery references a column from a table referred to a parent statement any number of levels above the subquery. It is evaluated once for each row processed by the parent statement Answers a multi-part question whose answer depends on the value in each row processed by the parent statement

Using HR/HR schema

Determine who works in the same department as employee Lorentz Give every employee who have changed jobs (positions) a 10% raise List the details of employees who earn more than the average salaries for their department For every employee, display the difference of his/her pay with that of his/her manager

SQL Functions
Date Time Numeric String Conversion

Date Time

SYSDATE ADD_MONTHS() LAST_DAY() MONTHS_BETWEEN() TRUNC()

Numeric

ROUND() TRUNC() MOD() SQRT()

String

UPPER() LOWER() RTRIM() RPAD() LPAD() REPLACE() SUBSTR() LENGTH()

Conversion

TO_CHAR() TO_NUMBER() TO_DATE() CAST() DECODE() CONVERT() COALESCE()

AGGREGATE FUNCTIONS

SUM() COUNT() AVG() MIN() MAX()

You might also like