7 Week
7 Week
(DATABASE MANAGEMENTS)
4. Implementation
4.1 Introduction to SQL
4.2 Advanced SQL
4.3 Database Application Development
4.4 Data Warehousing
4.2 ADVANCED SQL
OBJECTIVES
Define terms
Write single and multiple table SQL queries
Customer ID
appears twice in the
result
EQUI-JOIN EXAMPLE – ALTERNATIVE
SYNTAX
An INNER join will only return rows from each table that have
matching rows in the other.
Unlike
INNER join,
this will
include
customer
rows with no
matching
order rows
MULTIPLE TABLE JOIN EXAMPLE
Assemble all information necessary to create an invoice for order
number 1006
Four tables
involved in
this join
From Chapter 2
Unary
1:N
PROCESSING MULTIPLE TABLES USING
SUBQUERIES
Subquery–placing an inner query (SELECT
statement) inside an outer query
Options:
In a condition of the WHERE clause
As a “table” of the FROM clause
Within the HAVING clause
Subqueries can be:
Noncorrelated–executed once for the entire outer query
Correlated–executed once for each row returned by the
outer query
SUBQUERY EXAMPLE
Show all customers who have placed an order
The IN operator will test to see if
the CUSTOMER_ID value of a
row is included in the list
returned from the subquery
Join version
Subquery version
Graphical depiction of two ways to answer a query
with different types of joins
Graphical depiction of two ways to answer a query
with different types of joins
CORRELATED VS. NONCORRELATED
SUBQUERIES
Noncorrelated subqueries:
Do not depend on data from the outer query
Execute once for the entire outer query
Correlated subqueries:
Make use of data from the outer query
Execute once for each row of the outer query
Can use the EXISTS operator
Processing a noncorrelated subquery
The WHERE clause normally cannot include aggregate functions, but because
the aggregate is performed in the subquery its result can be used in the outer
query’s WHERE clause.
UNION QUERIES
Combine the output (union of multiple queries)
together into a single result table
First query
Combine
Second query
Combining queries using UNION
Note: With
UNION queries,
the quantity and
data types of the
attributes in the
SELECT clauses
of both queries
must be identical.
CONDITIONAL EXPRESSIONS USING
CASE KEYWORD
This is available with
newer versions of SQL,
previously not part of
the standard
Figure 7-10
MORE COMPLICATED SQL QUERIES
Production databases contain hundreds or
even thousands of tables, and tables could
include hundreds of columns.
So, sometimes query requirements can be very
complex.
Sometimes it’s useful to combine queries,
through the use of Views.
If you use a view (which is a query), you could
have another query that uses the view as if it
were a table.
EXAMPLE OF QUERY USING A VIEW
TIPS FOR DEVELOPING QUERIES
Be familiar with the data model (entities and
relationships)
Understand the desired results
Know the attributes desired in results
Identify the entities that contain desired
attributes
Review ERD
Construct a WHERE equality for each link
Fine tune with GROUP BY and HAVING clauses
if needed
Consider the effect on unusual data
QUERY EFFICIENCY CONSIDERATIONS