(M8-Main) Advanced SQL
(M8-Main) Advanced SQL
ADVANCED SQL
MODULE 8
OBJECTIVES
■At the end of the chapter, the learner should be able to:
• Define terms
• Write single and multiple table SQL queries
• Define and use three types of joins
• Write noncorrelated and correlated subqueries
• Differentiate system privileges from object privileges
• Grant privileges on tables
• Grant roles
• Distinguish between privileges and roles
EMPLOYEES DEPARTMENTS
…
Use a join to query data from more than one table:
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];
Database Systems 1
Join–a relational operation that causes two or more tables
with a common domain to be combined into a single table or
view
• Natural join–an equi-join in which one of the duplicate columns is eliminated in the result table
The common columns in joined tables are usually the primary key
of the dominant table and the foreign key of the dependent table in
1:M relationships.
Database Systems 1
• Outer join–a join in which rows that do not have matching values in
common columns are nonetheless included in the result table (as
opposed to inner join, in which rows must have matching values in
order to appear in the result table)
• Union join–includes all columns from each table in the join, and an
instance for each row of each table
Database Systems 1
Figure 7-2
Visualization of different join types with results returned in shaded area
The following slides create tables for this enterprise data model
Customer ID
appears twice in the
result
Database Systems 1
INNER JOIN clause is an alternative to WHERE clause, and is
used to match primary and foreign keys.
An INNER join will only return rows from each table that have
matching rows in the other.
Database Systems 1
List the customer name, ID number, and order number for all customers.
Include customer information even for customers that do have an order.
Database Systems 1
Outer Join
Results
Unlike
INNER join,
this will
include
customer
rows with
no matching
order rows
Assemble all information necessary to create an invoice for order number
1006
Four
tables
involved
in this
join
Each pair of tables requires an equality-check condition in the
WHERE clause, matching primary keys against foreign keys.
Database Systems 1
Figure 7-4 Results from a four-table join (edited for readability)
Database Systems 1
Show all customers who have placed an order
Database Systems 1
Some queries could be accomplished by either a join or a subquery
Join version
Subquery version
Database Systems 1
Figure 7-6 Graphical depiction of two ways to answer a query
with different types of joins
Figure 7-6 Graphical depiction of two ways to answer a query with
different types of joins
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
Database Systems 1
Figure 7-8a Processing a noncorrelated subquery
26
Database Systems 1
Figure 7-8b
Processing a
correlated Subquery refers to outer-
subquery query data, so executes once
for each row of outer query
28
Show all products whose standard price is higher than the average price
Database Systems 1
Combine the output (union of multiple queries) together into a single result
table
First query
Combine
Second query
Database Systems 1
This is available with newer
versions of SQL, previously
not part of the standard
Figure 7-10
• 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
Database Systems 1
• Instead of SELECT *, identify the specific attributes in the SELECT
clause; this helps reduce network traffic of result set
• Limit the number of subqueries; try to make everything done in a single
query if possible
• If data is to be used many times, make a separate query and store it as a
view
Database Systems 1
• Understand how indexes are used in query processing
• Keep optimizer statistics up-to-date
• Use compatible data types for fields and literals
• Write simple queries
• Break complex queries into multiple simple parts
• Don’t nest one query inside another query
• Don’t combine a query with itself (if possible avoid self-joins)
Database Systems 1
• Create temporary tables for groups of queries
• Combine update operations
• Retrieve only the data you need
• Don’t have the DBMS sort without an index
• Learn!
• Consider the total query processing time for ad hoc queries
Database Systems 1
END OF SUBTOPIC 1
MODULE 8 SUBTOPIC 2
OBJECTIVES
■At the end of the chapter, the learner should be able to:
• Differentiate system privileges from object privileges
• Grant privileges on tables
• Grant roles
• Distinguish between privileges and roles
Database
administrator
Users
• Database security:
- System security
- Data security
• System privileges: Performing a particular action within the
database
• Object privileges: Manipulating the content of the database
objects
• Schemas: Collection of objects such as tables, views, and
sequences
Database Systems 1
•More than 100 privileges are available.
•The database administrator has high-level system privileges
for tasks such as:
• Creating new users
• Removing users
• Removing tables
• Backing up tables
Database Systems 1
• The database administrator (DBA) creates users with the CREATE USER statement.
Database Systems 1
• After a user is created, the DBA can grant specific system privileges to that user.
GRANT privilege [, privilege...]
TO user [, user| role, PUBLIC...];
• An application developer, for example, may have the following system privileges:
• CREATE SESSION
• CREATE TABLE
• CREATE SEQUENCE
• CREATE VIEW
• CREATE PROCEDURE
Database Systems 1
The DBA can grant specific system privileges to a user.
Database Systems 1
Give a user authority to pass along system privileges:
Database Systems 1
Users
Manager
Privileges
Database Systems 1
• Create a role:
CREATE ROLE manager;
Database Systems 1
• The DBA creates your user account and initializes your password.
• You can change your password by using the ALTER USER statement.
Database Systems 1
Object
privilege Table View Sequence
ALTER
DELETE
INDEX
INSERT
REFERENCES
SELECT
UPDATE
• Object privileges vary from object to object.
• An owner has all the privileges on the object.
• An owner can give specific privileges on that owner’s object.
Database Systems 1
• Grant query privileges on the EMPLOYEES table:
GRANT select
ON employees
TO demo;
Database Systems 1
• Give a user authority to pass along privileges:
GRANT select, insert
ON departments
TO demo
WITH GRANT OPTION;
• Allow all users on the system to query data from Alice’s DEPARTMENTS table:
GRANT select
ON alice.departments
TO PUBLIC;
Database Systems 1
Data Dictionary View Description
Database Systems 1
Revoke the SELECT and INSERT privileges given to the demo user on the
DEPARTMENTS table.
Database Systems 1
In this lesson, you should have learned about statements that control
access to the database and database objects.
Statement Action
Introduction to Programming