Advanced SQL
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.
Data Definition Language Data Manipulation Language Transaction Control Statements Session Control Statements Embedded SQL 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 place DDL, DML, and transaction control statements within a procedural language program.
Advance SQL
Relational Set Operators Joins Sub-queries Functions
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
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
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
SELECT location_id FROM departments;
INTERSECT
-- 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;
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
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
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
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
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?
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.
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
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
Numeric
String
Conversion
AGGREGATE FUNCTIONS