Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Today’s Class  Relational Model  SQL CS F212 Database Systems.

Similar presentations


Presentation on theme: "1 Today’s Class  Relational Model  SQL CS F212 Database Systems."— Presentation transcript:

1 1 Today’s Class  Relational Model  SQL CS F212 Database Systems

2 2 Tuple Variables  Tuple variables are defined in the from clause via the use of the as clause.  Find the customer names and their loan numbers and amount for all customers having a loan at some branch. n Find the names of all branches that have greater assets than some 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' n Keyword as is optional and may be omitted borrower as T ≡ borrower T n Some database such as Oracle require as to be omitted select customer_name, T.loan_number, S.amount from borrower as T, loan as S where T.loan_number = S.loan_number

3 3 String Operations  SQL includes a string-matching operator for comparisons on character strings. The operator “like” uses patterns that are described using two special characters:  percent (%). The % character matches any substring.  underscore (_). The _ character matches any character.  Find the names of all customers whose street includes the substring “Main”. select customer_name from customer where customer_street like ' %Main% '  Match the name “Main%” like ' Main\% ' escape ' \ '  SQL supports a variety of string operations such as  concatenation (using “||”)  converting from upper to lower case (and vice versa)  finding string length, extracting substrings, etc.

4 4 Ordering the Display of Tuples List in alphabetic order the names of all customers having a loan in Perryridge branch select distinct customer_name from borrower, loan where borrower loan_number = loan.loan_number and branch_name = ' Perryridge ' order by customer_name We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default. Example: order by customer_name desc

5 5 Set Operations The set operations union, intersect, and except operate on relations and correspond to the relational algebra operations  Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m + n times in r union all s min( m,n) times in r intersect all s max(0, m – n) times in r except all s

6 6 Set Operations Find all customers who have a loan, an account, or both: (select customer_name from depositor) except (select customer_name from borrower) (select customer_name from depositor) intersect (select customer_name from borrower) n Find all customers who have an account but no loan. (select customer_name from depositor) union (select customer_name from borrower) n Find all customers who have both a loan and an account.

7 7 Aggregate Functions  These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values

8 8 Aggregate Functions Find the average account balance at the Perryridge branch. n Find the number of depositors in the bank. n Find the number of tuples in the customer relation. select avg (balance) from account where branch_name = 'Perryridge' select count (*) from customer select count (distinct customer_name) from depositor

9 9 Motivation for Grouping  So far, we’ve applied aggregate operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples.  Consider: Find the age of the youngest sailor for each rating level.  In general, we don’t know how many rating levels exist, and what the rating values for these levels are!  Suppose we know that rating values go from 1 to 10; we can write 10 queries that look like this (!): SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i For i = 1, 2,..., 10:

10 10 Queries With GROUP BY and HAVING  The target-list contains (i) attribute names (ii) terms with aggregate operations (e.g., MIN ( S.age )).  The attribute list (i) must be a subset of grouping-list. Intuitively, each answer tuple corresponds to a group, and these attributes must have a single value per group. (A group is a set of tuples that have the same value for all attributes in grouping-list.) SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification

11 11 Conceptual Evaluation  The cross-product of relation-list is computed, tuples that fail qualification are discarded, ` unnecessary’ fields are deleted, and the remaining tuples are partitioned into groups by the value of attributes in grouping-list.  The group-qualification is then applied to eliminate some groups. Expressions in group-qualification must have a single value per group !  In effect, an attribute in group-qualification that is not an argument of an aggregate op also appears in grouping-list.  One answer tuple is generated per qualifying group.

12 12 Aggregate Functions – Group By  Find the number of depositors for each branch. Note: Attributes in select clause outside of aggregate functions must appear in group by list select branch_name, count (distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name

13 13 Aggregate Functions – Having Clause  Find the names of all branches where the average account balance is more than $1,200. Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups select branch_name, avg (balance) from account group by branch_name having avg (balance) > 1200

14 14 Aggregate Functions – Having and Where Clause  Find the average balance for each customer who lives in Harrison and has at least three accounts. Select d.customer_name, avg (balance) from account a, depositor d, customer c Where d.account_number=a.account_number and d.customer_name=c.customer_name and customer_city=‘Harrison’ group by d.customer_name having count(d.account_number) >= 3

15 15 Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation: Sailors instance:

16 16 Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors.

17 17 Nested Subqueries  SQL provides a mechanism for the nesting of subqueries.  A subquery is a select-from-where expression that is nested within another query.  A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality.

18 18 “In” Construct  Find all customers who have both an account and a loan at the bank. n Find all customers who have a loan at the bank but do not have an account at the bank select distinct customer_name from borrower where customer_name not in (select customer_name from depositor ) select distinct customer_name from borrower where customer_name in (select customer_name from depositor )

19 19 Nested Queries  A very powerful feature of SQL: a WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses.)  To find sailors who’ve not reserved #103, use NOT IN.  To understand semantics of nested queries, think of a nested loops evaluation: For each Sailors tuple, check the qualification by computing the subquery. SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R WHERE R.bid=103) Find names of sailors who’ve reserved boat #103:

20 20 Example Query  Find all customers who have both an account and a loan at the Perryridge branch n Note: Above query can be written in a much simpler manner. The formulation above is simply to illustrate SQL features. select distinct customer_name from borrower, loan where borrower.loan_number = loan.loan_number and branch_name = 'Perryridge' and (branch_name, customer_name ) in (select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number )

21 21 “Some” Construct  Find all branches that have greater assets than some branch located in Brooklyn. n Same query using > some clause select branch_name from branch where assets > some (select assets from branch where branch_city = ' Brooklyn ' ) select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = ' Brooklyn '

22 22 “All” Construct  Find the names of all branches that have greater assets than all branches located in Brooklyn. select branch_name from branch where assets > all (select assets from branch where branch_city = 'Brooklyn')

23 23 Nested Queries with Correlation  EXISTS is another set comparison operator, like IN. SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Find names of sailors who’ve reserved boat #103:

24 24 “Exists” Construct  Find all customers who have an account at all branches located in Brooklyn. select distinct S.customer_name from depositor as S where not exists ( (select branch_name from branch where branch_city = 'Brooklyn') except (select R.branch_name from depositor as T, account as R where T.account_number = R.account_number and S.customer_name = T.customer_name ))

25 25 Division in SQL  Let’s do it the hard way, without EXCEPT: SELECT S.sname FROM Sailors S WHERE NOT EXISTS (( SELECT B.bid FROM Boats B) EXCEPT ( SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( SELECT B.bid FROM Boats B WHERE NOT EXISTS ( SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) Sailors S such that... there is no boat B without... a Reserves tuple showing S reserved B Find sailors who’ve reserved all boats. (1) (2)

26 26 Absence of Duplicate Tuples  The unique construct tests whether a subquery has any duplicate tuples in its result.  Find all customers who have at most one account at the Perryridge branch. select T.customer_name from depositor as T where unique ( select R.customer_name from account, depositor as R where T.customer_name = R.customer_name and R.account_number = account.account_number and account.branch_name = 'Perryridge')

27 27 Example Query  Find all customers who have at least two accounts at the Perryridge branch. select distinct T.customer_name from depositor as T where not unique ( select R.customer_name from account, depositor as R where T.customer_name = R.customer_name and R.account_number = account.account_number and account.branch_name = ' Perryridge ' ) n Variable from outer level is known as a correlation variable

28 28 Derived Relations o SQL allows a subquery expression to be used in the from clause o Find the average account balance of those branches where the average account balance is greater than $1200. select branch_name, avg_balance from ( select branch_name, avg ( balance ) from account group by branch_name ) as branch_avg ( branch_name, avg_balance ) where avg_balance > 1200 o Note that we do not need to use the having clause, since we compute the temporary (view) relation branch_avg in the from clause, and the attributes of branch_avg can be used directly in the where clause.

29 29  Find the maximum total balance across all branches.  Select max(tot_balance)  From ( select branch_name, sum(balance) from account group by branch_name) as branch_total(branch_name,tot_balance)

30 30 Null Values  It is possible for tuples to have a null value, denoted by null, for some of their attributes  null signifies an unknown value or that a value does not exist.  The predicate is null can be used to check for null values.  Example: Find all loan number which appear in the loan relation with null values for amount. select loan_number from loan where amount is null  The result of any arithmetic expression involving null is null  Example: 5 + null returns null  However, aggregate functions simply ignore nulls

31 31 Null Values and Three Valued Logic  Any comparison with null returns unknown  Example : 5 null or null = null  Three-valued logic using the truth value unknown :  OR: ( unknown or true ) = true, ( unknown or false ) = unknown ( unknown or unknown) = unknown  AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown  NOT : ( not unknown) = unknown  “ P is unknown ” evaluates to true if predicate P evaluates to unknown  Result of where clause predicate is treated as false if it evaluates to unknown

32 32 Null Values and Aggregates  Total all loan amounts select sum ( amount ) from loan  Above statement ignores null amounts  All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes.


Download ppt "1 Today’s Class  Relational Model  SQL CS F212 Database Systems."

Similar presentations


Ads by Google