sql structured query language meizhen huang. content (4.1 – 4.4) background parts of sql basic...

26
SQL Structured Query Language Meizhen Huang

Post on 20-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

SQLStructured Query Language

Meizhen Huang

Page 2: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Content (4.1 –4.4)

Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Page 3: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Background SQL – Structured Query Language

Developed by IBM in the 1970’s, originally called Sequel

The standard relational-database language

Uses relational-algebra and relational-calculus constructs

Page 4: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Parts of SQL DDL: commands for defining relation schemas,

deleting relations, and modifying relation schemas.

DML: based on the relational algebra and the tuple relational calculus.

Integrity: commands for specifying integrity constraints for the data in the DB.

Authorization: commands for specifying access rights to relations and views.

Page 5: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Relations using in Examples Branch-schema = (branch-name, branch-city,

assets) Customer-schema = (customer-name, customer-

street, customer-city) Loan-schema = (loan-number,branch-name,

amount) Borrower-schema = (customer-name, loan-number) Account-schema = (account-number, branch-name,

balance) Depositor-schema = (customer-name, account-

number)

Page 6: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Basic Structure Basic structure of SQL includes three clauses: select, from

and where. A typical SQL has the form select A1, A2, …, An

from r1,r2,…,rm where P

Ai – an attribute ri – a relation p – a predicate This query is equivalent to the relational algebra

expression:

A1, A2, ..., An (P (r1 x r2 x ... x rm))

Page 7: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

select select – projection in RA select without elimination of duplicates “Find the names of all branches in the loan

relation.” select branch-name select all branch-name from loan from loan

select with elimination of duplicates select distinct branch-name from loan

Page 8: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

select “*” can be used to denote “ all attributes”. select * from loan The select clause may also contain arithmetic

expressions involving the operators +, -, *, and / operating on constants or attributes of tuples.

select loan-number, branch-name, amount*100

from loan

Page 9: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

where where – selection predicate of RA e.g. “Find all loan numbers for loans made at the Perryridge

branch with loan amounts greater that $1200.” select loan-number

from loanwhere branch-name = ‘Perryridge’ and amount > 1200Note: SQL uses and, or, and not instead of , v, and in the where clause.

e.g., select loan-number from loan where amount between 90000 and 100000 Note: similarly, we can use the not between comparison

operator.

Page 10: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

from from – Cartesian-product in RA. “Find the Cartesian product borrower x

loan”select from borrower, loan

Page 11: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

from SQL uses relation-name.attribute-name,

as does relational algebra, to avoid ambiguity

“Find the name, loan number and loan amount of all customers having a loan at the Perryridge branch.”

select customer-name, borrower.loan-number, amountfrom borrower, loanwhere borrower.loan-number = loan.loan-number

and branch-name = ‘Perryridge’

Page 12: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Rename Rename can be operated on both

relations and attributes. old-name as new-name e.g., select customer-name,

borrower.loan-number as loan-id, amount

from borrower, loan where borrower.loan-number =

loan.loan-number

Page 13: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Tuple Variables Tuple variables are defined in the from

clause by way of the as clause. “Find all customers who have a loan

from the bank, find their names, loan numbers, and loan amount.”

select customer-name, T.loan-number, S.amount

from borrower as T, loan as S where T.loan-number = S.loan-number

Page 14: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Tuple Variables Tuple variables are most useful for

comparing two tuples in the same relation.

“Find the names of all branches that have assets greater than at least one branch located in Brooklyn.”

select distinct T.branch-name from branch as T, branch as S where T.assets > S.assets and

S.branch-city = ‘Brooklyn’

Page 15: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

String Operations The strings are enclosed by single quotes, for example,

‘Perryridge’. The most commonly used operation on strings is pattern

matching using “like”. Pattern has two special characters:

* Percent(%):matches any substring * Underscore(_): matches any character - ‘Perry%’ matches any string beginning with

“Perry”. - ‘_ _ _ %’matches any string of at least 3 characters. Note: Patterns are case sensitive.

Page 16: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

String Operations “Find the names of all customers whose

street address includes the substring ‘Main’.”

select customer-name from customer where customer-street like ‘%Main%’

Page 17: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Ordering the Display of Tuples The order by clause list the result in sorted order. select distinct customer-name from borrower, loan where borrower.loan-number = loan.loan-number and

branch-name = ‘Perryridge’ order by customer-name Note: by default, the order by clause lists items in

ascending order. ( desc or asc ) select * from loan order by amount desc, loan-number asc

Page 18: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Set Operations The set operations union,

intersect, and except operate on relations and correspond to the relational algebra operations

union all, intersect all and except all.

Page 19: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Union “ Find all customers having a loan,

an account, or both at the bank.” (select customer-name from depositor) union (select customer-name from borrower)

Page 20: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Intersect “ Find all customers who have both

a loan and an account at the bank.” (select distinct customer-name from depositor) intersect (select distinct customer-name from borrower)

Page 21: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Except “Find all customers who have an

account but no loan at the bank.” (select distinct customer-name from depositor) except (select customer-name from borrower)

Page 22: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Aggregation Functions Aggregation functions take a

collection of values as input and return a single value.

* Average: avg (number) * Minimum: min * Maximum: max * Total: sum (number) * Count: count

Page 23: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

avg “Find the average account balance

at the Perryridge branch.” select avg (balance) from account where branch-name =

‘Perryridge’

Page 24: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

group by Aggregation function can be applied

to a group of sets of tuples by using group by clause.

“Find the average account balance at each branch.”

select branch-name, avg(balance)

from account group by branch-name

Page 25: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

having It is useful to state a condition that

applies to groups rather than to tuples. “Find the branches where the average

account balance is more than $1200.” select branch-name, ave(balance) from account group by branch-name having avg(balance) > 1200

Page 26: SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

The End