CH 4
CH 4
• Basic Structure
• Set Operations
• Aggregate Functions
• Null Values
• Nested Subqueries
• Derived Relations
• Views
• Modification of the Database
• Joined Relations
• Data Definition Language
• Embedded SQL, ODBC and JDBC
Schema Used in Examples
Basic Structure
• SQL is based on set and relational operations with
certain modifications and enhancements
• A typical SQL query has the form:
select A1, A2, ..., An
from r1, r2, ..., rm
where P
– Ais represent attributes
– ris represent relations
– P is a predicate.
• This query is equivalent to the relational algebra
expression.
A1, A2, ..., An(P (r1 x r2 x ... x rm))
• The result of an SQL query is a relation.
The select Clause
• The select clause list the attributes desired in the
result of a query
– corresponds to the projection operation of the relational algebra
• E.g. find the names of all branches in the loan
relation
select branch-name
from loan
• In the “pure” relational algebra syntax, the query
would be:
branch-name(loan)
• NOTE: SQL does not permit the ‘-’ character in
names,
– Use, e.g., branch_name instead of branch-name in a real implementation.
– We use ‘-’ since it looks nicer!
• NOTE: SQL names are case insensitive, i.e. you can
use capital or small letters.
– You may wish to use upper case where-ever we use bold font.
The select Clause (Cont.)
• SQL allows duplicates in relations as well as in query
results.
• To force the elimination of duplicates, insert the
keyword distinct after select.
• Find the names of all branches in the loan relations,
and remove duplicates
select distinct branch-name
from loan
• The keyword all specifies that duplicates not be
removed.
select all branch-name
from loan
The select Clause (Cont.)
• An asterisk in the select clause denotes “all attributes”
select *
from loan
• The select clause can contain arithmetic expressions
involving the operation, +, –, , and /, and operating on
constants or attributes of tuples.
• The query:
select loan-number, branch-name, amount
100
from loan
would return a relation which is the same as the loan
relations, except that the attribute amount is multiplied by
100.
The where Clause
• The where clause specifies conditions that the result must
satisfy
– corresponds to the selection predicate of the relational algebra.
• To find all loan number for loans made at the Perryridge
branch with loan amounts greater than $1200.
select loan-number
from loan
where branch-name = ‘Perryridge’ and amount >
1200
• Comparison results can be combined using the logical
connectives and, or, and not.
• Comparisons can be applied to results of arithmetic
expressions.
The where Clause (Cont.)
• SQL includes a between comparison operator
• E.g. Find the loan number of those loans with
loan amounts between $90,000 and $100,000
(that is, $90,000 and $100,000)
select loan-number
from loan
where amount between 90000 and 100000
The from Clause
• The from clause lists the relations involved in the query
– corresponds to the Cartesian product operation of the relational algebra.
Find the name, loan number and loan amount of all customers
having a loan at the Perryridge branch.
• Find the name, loan number and loan amount of all customers;
rename the column name loan-number as loan-id.
where:
<query expression> is any legal expression
The view name is represented by v
Example Queries
• A view consisting of branches and their customers
create view all-customer as
(select branch-name, customer-name
from depositor, account
where depositor.account-number = account.account-number)
union
(select branch-name, customer-name
from borrower, loan
where borrower.loan-number = loan.loan-number)
Find all customers of the Perryridge branch
select customer-name
from all-customer
where branch-name = ‘Perryridge’
Joined Relations
• Join operations take two relations and return as a result
another relation.
• These additional operations are typically used as
subquery expressions in the from clause
• Join condition – defines which tuples in the two
relations match, and what attributes are present in the
result of the join.
• Join type – defines how tuples in each relation that do
not match any tuple in the other relation (based on the
join condition) are treated.
Join Types Join Conditions
inner join natural
left outer join on <predicate>
right outer join using (A1, A2, ..., An)
full outer join
Joined Relations – Datasets for Examples
• Relation loan
loan-number branch-name amount
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Note: borrower information missing for L-260 and loan
information missing for L-155
Joined Relations – Examples
• loan inner join borrower on
loan.loan-number = borrower.loan-number
loan-number branch-name amount customer-name loan-number
select customer-name
from (depositor natural full outer join borrower)
where account-number is null or loan-number is null
The loan and borrower Relations
The Result of loan inner join borrower
on loan.loan-number =
borrower.loan-number
The Result of loan left outer join
borrower on loan-number
The Result of loan natural inner join
borrower
Join Types and Join Conditions
The Result of loan natural right outer
join borrower
The Result of loan full outer join
borrower using(loan-number)
SQL Data Definition for Part of the Bank Database