Database Systems: Design, Implementation, and Management: Advanced SQL
Database Systems: Design, Implementation, and Management: Advanced SQL
Chapter 8
Advanced SQL
SQL Join Operators
• Join operation merges rows from two tables and
returns the rows with one of the following:
– Inner Join
• Have common values in common columns
– Natural join
• Meet a given join condition
– Equality or inequality
– Outer join
• Have common values in common columns or have no
matching value
– Cross join
• Returns same result as the Cartesian product of two sets
2
SQL Join Operators
3
Cross Join
• Performs relational product of two tables
– Also called Cartesian product
• Syntax:
SELECT column-list FROM table1 CROSS JOIN
table2
• Perform a cross join that yields specified attributes
SELECT INVOICE.INV_NUMBER, CUS_CODE, INV_DATE, P_CODE
FROM INVOICE CROSS JOIN LINE;
OR
FROM INVOICE, LINE;
4
Natural Join
• Not available in MS SQL – use JOIN ON
• Returns all rows with matching values in the matching
columns
– Determines the common attribute(s) by looking for attributes
with identical names and compatible data types
– Select only the rows with common values in the common
attribute(s)
– If there are no common attributes, return the relational product
of the two tables
– Eliminates duplicate columns
• Used when tables share one or more common attributes with
common names
• Syntax:
SELECT column-list FROM table1 NATURAL JOIN table2
5
Natural Join
6
JOIN USING Clause
• Returns only rows with matching values in the
column indicated in the USING clause
– The column must exist in both tables
• Syntax:
SELECT column-list FROM table1 JOIN table2
USING (common-column)
• JOIN USING operand does not require table
qualifiers
– Oracle returns error if table name is specified
7
JOIN USING Clause
8
JOIN ON Clause
10
JOIN ON Clause
11
Outer Joins
12
Outer Joins (cont’d.)
• Left outer join
– Returns rows matching the join condition
– Returns rows in left side table with unmatched values
– Syntax: SELECT column-list FROM table1 LEFT [OUTER] JOIN
table2 ON join-condition
13
Outer Joins (cont’d.)
• Right outer join
– Returns rows matching join condition
– Returns rows in right side table with unmatched values
14
Outer Joins (cont’d.)
• Full outer join
– Returns rows matching join condition
– Returns all rows with unmatched values in either side
table
– Syntax:
SELECT column-list
FROM table1 FULL [OUTER] JOIN table2
ON join-condition
15
16
Relational Set Operators
• UNION
• INTERSECT
• MINUS
• Work properly if relations are union-compatible
– Names of relation attributes must be the same and
their data types must be identical
19
UNION ALL
20
UNION ALL
21
INTERSECT
24
MINUS
25
Syntax Alternatives
26
Subqueries and Correlated Queries
• Often necessary to process data based on
other processed data
• Subquery is a query inside a query, normally
inside parentheses
• First query is the outer query
– Inside query is the inner query
• Inner query is executed first
• Output of inner query is used as input for outer
query
• Sometimes referred to as a nested query
27
Subqueries and Correlated Queries
28
WHERE Subqueries
• Most common type uses inner SELECT subquery on right side of
WHERE comparison
– Requires a subquery that returns only one single value
• Value generated by subquery must be of comparable data type
• Can be used in combination with joins
29
IN Subqueries
• Used when comparing a single attribute to a list
of values
30
HAVING Subqueries
• HAVING clause restricts the output of a
GROUP BY query
– Applies conditional criterion to the grouped rows
31
Multirow Subquery Operators: ANY and ALL
• Allows comparison of single value with a list of values using inequality
comparison
– “Greater than ALL” equivalent to “greater than the highest in list”
– “Less than ALL” equivalent to “less than lowest”
– Using equal to ANY operator equivalent to IN operator
32
SELECT V_CODE FROM VENDOR WHERE V_STATE='FL';
V_CODE
21226
25443
25595
34
Attribute List Subqueries
36
Correlated Subqueries
• Subquery that executes once for each row in the
outer query
• Correlated because inner query is related to the
outer query
– Inner query references column of outer subquery
– Works like a nested loop
for x=1 to 2
for y=1 to 3
print “x=“ x “y=“ y
as x holds steady with 1, y loops from 1 to 3
• Can also be used with the EXISTS special operator
37
Correlated Subqueries
38
EXISTS Correlated Subquery
39
SQL Functions
• View
– Virtual table based on a SELECT query
• Base tables
• Tables on which the view is based
• CREATE VIEW viewname AS SELECT query
50
Triggers
• On an insert or update of the emp table, if the
areacode is not 615 or 901, delete the record
51
Stored Procedures
52
Stored Procedures
• Advantages
– Substantially reduce network traffic and increase
performance
• No transmission of individual SQL statements over
network as procedure is stored at the server and
executed locally on the RDBMS
– Reduce code duplication by means of code
isolation and code sharing
• Minimize chance of errors and cost of application
development and maintenance
53
Stored Procedures
Parameter is optional but allows reuse similar to a
function call
CREATE PROCEDURE sp_GetInventory
@location varchar(10)
AS
SELECT Product, Quantity
FROM Inventory
WHERE Warehouse = @location
To execute
EXECUTE sp_GetInventory 'FL'
54