This document discusses SQL's GROUP BY and HAVING clauses. GROUP BY partitions tuples into groups based on attributes in the grouping list. HAVING filters groups. Aggregate functions like MIN can find the youngest sailor per rating level. ORDER BY sorts result tuples. Null values complicate queries by requiring three-valued logic to handle unknown/nonexistent values. SQL provides more expressive power than relational algebra with features like aggregation, arithmetic, sorting, and grouping.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
40 views9 pages
SQL: The Query Language (Part III)
This document discusses SQL's GROUP BY and HAVING clauses. GROUP BY partitions tuples into groups based on attributes in the grouping list. HAVING filters groups. Aggregate functions like MIN can find the youngest sailor per rating level. ORDER BY sorts result tuples. Null values complicate queries by requiring three-valued logic to handle unknown/nonexistent values. SQL provides more expressive power than relational algebra with features like aggregation, arithmetic, sorting, and grouping.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9
SQL: The Query Language (Part III)
GROUP BY and HAVING
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) For i = 1, 2, ... , 10: FROM Sailors S WHERE S.rating = i Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification 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.) Conceptual Evaluation
The cross-product of relation-list is computed, tuples
that fail qualification are discarded, `unnecessary’ fields are deleted, as before. 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. One answer tuple is generated per qualifying group. Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors sid sname rating age SELECT S.rating, MIN (S.age) 22 dustin 7 45.0 FROM Sailors S 31 lubber 8 55.5 WHERE S.age >= 18 71 zorba 10 16.0 GROUP BY S.rating HAVING COUNT (*) > 1 64 horatio 7 35.0 29 brutus 1 33.0 Only S.rating and S.age are 58 rusty 10 35.0 mentioned in the SELECT, rating age GROUP BY or HAVING clauses; 1 33.0 other attributes `unnecessary’. 7 45.0 rating 2nd column of result is 7 35.0 7 35.0 unnamed. (Use AS to name it.) 8 55.5 10 35.0 Answer relation For each red boat, find the number of reservations for this boat SELECT B.bid, COUNT (*) AS rcount FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid
Grouping over a join of three relations.
What if we drop Sailors and the condition involving S.sid? Sorting a Result Names, ages of Sailors, sorted by name: SELECT sname, age FROM Sailors ORDER BY sname Same, ordered by age within a name: SELECT sname, age FROM Sailors ORDER BY sname, age ORDER BY clause evaluated last Nulls Field values in a tuple are sometimes unknown (e.g., a rating has not been assigned) or nonexistent (e.g., no spouse’s name). – SQL provides a special marker null for such situations. The presence of null complicates many issues. E.g.: – Special operators needed to check if field is/is not null. – Is rating>8 true or false when rating is null? What about AND, OR and NOT connectives? – We need a 3-valued logic (true, false and unknown). – Meaning of constructs must be defined carefully. (e.g., WHERE clause eliminates rows that don’t evaluate to true.) – New operators (in particular, outer joins) possible/needed. Summary
Relationally complete; in fact, significantly more
expressive power than relational algebra (aggregates, arithmetic, sorting, grouping, string matching….) Even queries that can be expressed in RA can often be expressed more naturally in SQL. Nulls (unknown or nonexistent) force a 3-valued logic and odd behavior: Be careful!!!