Download presentation
Presentation is loading. Please wait.
Published byHillary Walker Modified over 8 years ago
1
BBM471 – Database Management Systems Fall 2013 Dr. Nazlı İkizler Cinbiş
2
SQL (Structured Query Language)
3
3 SQL = Structured Query Language “Invented” at IBM’s Almaden Research Centre (San Jose, CA) in the 1970s Standard language for querying and manipulating data Has similar capabilities for queries to those in relational algebra Support statements for modifying a database (e.g., inserting and deleting tuples) and for declaring a database schema Many standards: SQL92, SQL2, SQL3, SQL99
4
4 Why SQL? SQL is a very-high-level language. Say “what to do” rather than “how to do it.” Avoid a lot of data-manipulation details needed in procedural languages like C++ or Java. Database management system figures out “best” way to execute query. Called “query optimization.”
5
5 SQL as DDL (Data Definition Language) The Data Definition Language (DDL) is used to create and destroy databases and database objects. SQL commands as DDL CREATE DROP ALTER These commands will primarily be used by database administrators during the setup and removal phases of a database project.
6
6 CREATE TABLE Specifies a new base relation by giving it a name, and specifying each of its attributes and their data types (INTEGER, FLOAT, CHAR(n), VARCHAR(n))
7
7 Creating Relations in SQL Creates the Students relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Students (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL) CREATE TABLE Enrolled (sid: CHAR(20), cid: CHAR(20), grade: CHAR(2))
8
8 Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine- dependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,n). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits.
9
9 Primary and Candidate Keys in SQL Possibly many candidate keys (specified using UNIQUE ), one of which is chosen as the primary key. CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid) ) “For a given student and course, there is a single grade.” vs. “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.” Used carelessly, an IC can prevent the storage of database instances that arise in practice! CREATE TABLE Enrolled (sid CHAR (20) cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid), UNIQUE (cid, grade) )
10
10 Integrity Constraints in Create Table not null primary key (A 1,..., A n ) foreign key (A m,..., A n ) references r create table instructor ( ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2), primary key (ID), foreign key (dept_name) references department); primary key declaration on an attribute automatically ensures not null
11
11 Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR (20), cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ) Enrolled Students
12
12 And a Few More Relation Definitions create table student ( ID varchar(5), name varchar(20) not null, dept_name varchar(20), tot_cred numeric(3,0), primary key (ID), foreign key (dept_name) references department); create table takes ( ID varchar(5), course_id varchar(8), sec_id varchar(8), semester varchar(6), year numeric(4,0), grade varchar(2), primary key (ID, course_id, sec_id, semester, year), foreign key (ID) references student, foreign key (course_id, sec_id, semester, year) references section); Note: sec_id can be dropped from primary key above, to ensure a student cannot be registered for two sections of the same course in the same semester
13
13 And more still create table course ( course_id varchar(8), title varchar(50), dept_name varchar(20), credits numeric(2,0), primary key (course_id), foreign key (dept_name) references department);
14
14 Referential Integrity in SQL SQL/92 and SQL:1999 support all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected) CASCADE (also delete all tuples that refer to deleted tuple) SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) CREATE TABLE Enrolled (sid CHAR (20), cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT )
15
15 Referential Integrity in SQL CASCADE: Whenever rows in the master (referenced) table are deleted (or updated), the respective rows of the child (referencing) table with a matching foreign key column will get deleted (or updated) as well. SET DEFAULT/SET NULL : the foreign key values in the referencing row are set to the column default/null when the referenced row is updated or deleted.
16
16 Weak Entity Sets On weak entity set and identifying relationship set When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE )
17
17 Destroying and Altering Relations Destroys the relation Students. The schema information and the tuples are deleted. The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field. DROP TABLE Students ALTER TABLE Students ADD COLUMN firstYear: integer
18
18 More on Altering Relations alter table r add A D where A is the name of the attribute to be added to relation r and D is the domain of A. All tuples in the relation are assigned null as the value for the new attribute. alter table r drop A where A is the name of an attribute of relation r Dropping of attributes not supported by many databases.
19
19 SQL as Data Manipulation Language (DML) Data Manipulation is: retrieval of information from the database insertion of new information into the database deletion of information in the database modification of information in the database A DML is a language which enables users to access and manipulate data. The goal is to provide efficient human interaction with the system.
20
20 Adding and Deleting Tuples Can insert a single tuple using: INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2) Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Students S WHERE S.name = ‘Smith’
21
SQL as DML : Queries
22
22 The basic form of a SQL query is select- from-where SELECT desired attributes FROM one or more tables WHERE condition on the rows of the tables Project out everything not in the final answer Every table you want to join, union, or intersect together All the join and selection conditions
23
23 RA SQL SQL SELECT RA Projection SQL WHERE RA Selection SQL FROM RA Join/cross Comma-separated list… SQL renaming RA rho Keep RA in the back of your mind…
24
24 Basic SQL Query relation-list A list of relation names (possibly with a range- variable after each name). target-list A list of attributes of relations in relation-list qualification Comparisons (Attr op const or Attr1 op Attr2, where op is one of ) combined using AND, OR and NOT. DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated! SELECT [DISTINCT] target-list FROM relation-list WHERE qualification
25
25 Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the cross-product of relation-list. Discard resulting tuples if they fail qualifications. Delete attributes that are not in target-list. If DISTINCT is specified, eliminate duplicate rows. This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers.
26
26 The select Clause An asterisk in the select clause denotes “all attributes” select * from instructor The select clause can contain arithmetic expressions involving the operation, +, –, , and /, and operating on constants or attributes of tuples. The query: select ID, name, salary/12 from instructor would return a relation that is the same as the instructor relation, except that the value of the attribute salary is divided by 12.
27
27 Example Instances R1 S1 S2 We will use these instances of the Sailors and Reserves relations in our examples. If the key for the Reserves relation contained only the attributes sid and bid, how would the semantics differ?
28
28 Examples Produce the names of sailors: SELECT sname FROM Sailors Change the column heading: SELECT sname as "Sailor Name“ FROM Sailors Or SELECT sname "Sailor Name“ FROM Sailors Can rename table names as well : SELECT S.sname FROM Sailors S SNAME dustin lubber rusty Sailor Name dustin lubber rusty
29
29 The Rename Operation The SQL allows renaming relations and attributes using the as clause: old-name as new-name (or a range variable) E.g., select ID, name, salary/12 as monthly_salary from instructor Find the names of all instructors who have a higher salary than some instructor in ‘Comp. Sci’. select distinct T. name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’ Keyword as is optional and may be omitted instructor as T ≡ instructor T
30
30 Find names and ages of all sailors. SELECTDISTINCT S.sname, S.age FROMSailors S; SELECTDISTINCT Sailors.sname, Sailors.age FROMSailors; SELECTDISTINCT sname, age FROMSailors;
31
31 Find sailors with rating above 7. SELECTS.sid, S.sname, S.rating, S.age FROMSailors AS S WHERES.rating > 7; SELECT* FROMSailors WHERErating > 7;
32
32 Expressions and Strings Illustrates use of arithmetic expressions and string pattern matching: Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. AS and = are two ways to name fields in result. LIKE is used for string matching. `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters. SELECT S.age, S.age-5 as age1, 2*S.age AS age2 FROM Sailors S WHERE S.sname LIKE ‘B_%B’
33
33 Find sailors that have the character ‘u’ in their names SNAME dustin lubber rusty Brutus SELECT sname from Sailors where sname like '%u%' Find the names of all instructors whose name includes the substring “dar”. select name from instructor where name like '%dar%'
34
34 Using multiple tables (Q1)Find names of sailors who have reserved boat number 103. Sailor names only available at Sailors table We need reservation info as well…
35
35 Example using R1, S1 instances SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103
36
36 A Note on Range Variables Really needed only if the same relation appears twice in the FROM clause. The previous query can also be written as: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 It is good style, however, to use range variables always! OR
37
37 Natural Join SQL:99 has a “natural join” clause: SELECT * FROM Sailors natural join Reserves WHERE bid=103 SIDSNAME RAT ING AGEBIDDAY 22dustin74510308-OCT-98 31lubber855.510306-NOV-98 58rusty103510312-NOV-98
38
38 (Q16)Find sids of sailors who have reserved a red boat. SELECTR.sid FROM Boats B, Reserves R WHEREB.bid = R.bid AND B.color = ‘red’; SELECTsid FROMBoats natural join Reserves WHEREcolour = 'red'
39
39 (Q2)Find names of sailors who have reserved a red boat. SELECTS.sname FROM Sailors S, Reserves R, Boats B WHERES.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’;
40
40 Find sid’s of sailors who’ve reserved a red or a green boat UNION : Can be used to compute the union of any two union- compatible sets of tuples (which are themselves the result of SQL queries). Also available: EXCEPT (What do we get if we replace UNION by EXCEPT ?) SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’) SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ UNION SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
41
41 Find sid’s of sailors who’ve reserved a red and a green boat INTERSECT : Can be used to compute the intersection of any two union-compatible sets of tuples. Included in the SQL/92 standard, but some systems don’t support it. SELECT S.sid FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color=‘red’ AND B2.color=‘green’) SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ Key field!
42
42 Except (minus) (Q19)Find sids of sailors who have reserved red boats but not green boats. SELECTR.sid FROMReserves R, Boats B WHERER.bid = B.bid AND B.color = ‘red’ EXCEPT SELECTR2.sid FROMBoats B2, Reserves R2 WHERER2.bid = B2.bid AND B2.color = ‘green’;
43
43 Set Operations Find courses that ran in Fall 2009 or in Spring 2010 n Find courses that ran in Fall 2009 but not in Spring 2010 (select course_id from section where sem = ‘Fall’ and year = 2009) union (select course_id from section where sem = ‘Spring’ and year = 2010) n Find courses that ran in Fall 2009 and in Spring 2010 (select course_id from section where sem = ‘Fall’ and year = 2009) intersect (select course_id from section where sem = ‘Spring’ and year = 2010) (select course_id from section where sem = ‘Fall’ and year = 2009) except (select course_id from section where sem = ‘Spring’ and year = 2010)
44
44 Set Operations Set operations union, intersect, and except 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
45
45 Ordering the Display of Tuples List in alphabetic order the names of all instructors select distinct name from instructor order by name We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default. Example: order by name desc Can sort on multiple attributes Example: order by dept_name, name
46
46 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 result of any arithmetic expression involving null is null Example: 5 + null returns null The predicate is null can be used to check for null values. Example: Find all instructors whose salary is null. select name from instructor where salary is null
47
47 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
48
48 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:
49
49 Correlated Nested Queries EXISTS is another set comparison operator, like IN, tests whether the set is nonempty. The subquery depends on the current row S and must be re- evaluated for each row in Sailors If UNIQUE is used, and * is replaced by R.bid, finds sailors with at most one reservation for boat #103. ( UNIQUE checks for duplicate tuples; * denotes all attributes. Why do we have to replace * by R.bid?) 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:
50
50 Exercise 5.2 ctd. Consider the following schema. Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog lists the prices charged for parts by Suppliers. Write the following query in SQL: Find the snames of suppliers who supply every part.
51
51 More on Set-Comparison Operators We’ve already seen IN, EXISTS and UNIQUE. Can also use NOT IN, NOT EXISTS and NOT UNIQUE. Also available: op ANY, op ALL, op SOME Find sailors whose rating is greater than that of some sailor called Horatio: SELECT * FROM Sailors S WHERE S.rating > ANY ( SELECT S2.rating FROM Sailors S2 WHERE S2.sname=‘Horatio’)
52
52 Rewriting INTERSECT Queries Using IN Similarly, EXCEPT queries re-written using NOT IN. To find names (not sid’s) of Sailors who’ve reserved both red and green boats, just replace S.sid by S.sname in SELECT clause. (What about INTERSECT query?) Find sid’s of sailors who’ve reserved both a red and a green boat: SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ AND S.sid IN ( SELECT S2.sid FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’)
53
53 (Q6)Find names of sailors who have reserved all boats. SELECTS.snameFROMSailors S WHERENOT EXISTS ( (SELECTB.bid FROMBoats B) EXCEPT (SELECTR.bid FROMReserves R WHERER.sid = S.sid));
54
54 Example Query Find courses offered in Fall 2009 and in Spring 2010 n Find courses offered in Fall 2009 but not in Spring 2010 select distinct course_id from section where semester = ’Fall’ and year= 2009 and course_id in (select course_id from section where semester = ’Spring’ and year= 2010); select distinct course_id from section where semester = ’Fall’ and year= 2009 and course_id not in (select course_id from section where semester = ’Spring’ and year= 2010);
55
55 Set Comparison Find names of instructors with salary greater than that of some (at least one) instructor in the Biology department. n Same query using > some clause select name from instructor where salary > some (select salary from instructor where dept name = ’Biology’); select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept name = ’Biology’;
56
56 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)
57
57 Aggregate Operators Significant extension of relational algebra. SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 SELECT COUNT (*) FROM Sailors S SELECT AVG ( DISTINCT S.age) FROM Sailors S WHERE S.rating=10 SELECT S.sname FROM Sailors S WHERE S.rating= ( SELECT MAX (S2.rating) FROM Sailors S2) COUNT (*) COUNT ( [ DISTINCT ] A) SUM ( [ DISTINCT ] A) AVG ( [ DISTINCT ] A) MAX (A) MIN (A) single column SELECT COUNT ( DISTINCT S.rating) FROM Sailors S WHERE S.sname=‘Bob’
58
58 Aggregate Functions (Cont.) Find the average salary of instructors in the Computer Science department select avg (salary) from instructor where dept_name= ’Comp. Sci.’; Find the total number of instructors who teach a course in the Spring 2010 semester select count (distinct ID) from teaches where semester = ’Spring’ and year = 2010; Find the number of tuples in the course relation select count (*) from course;
59
59 Find name and age of the oldest sailor(s) The first query is illegal! (We’ll look into the reason a bit later, when we discuss GROUP BY.) The third query is equivalent to the second query, and is allowed in the SQL/92 standard, but is not supported in some systems. SELECT S.sname, MAX (S.age) FROM Sailors S SELECT S.sname, S.age FROM Sailors S WHERE S.age = ( SELECT MAX (S2.age) FROM Sailors S2) SELECT S.sname, S.age FROM Sailors S WHERE ( SELECT MAX (S2.age) FROM Sailors S2) = S.age
60
60 Find names of sailors who are older than oldest sailor with rating of 10. Solution using MAX: SELECT S.snameFROMSailors S WHERE S.age > (SELECTMAX(S2.age) FROM Sailors S2 WHERES2.rating = 10); Solution using ALL: SELECT S.sname FROMSailors S WHERE S.age > ALL (SELECTS2.age FROMSailors S2 WHERES2.rating = 10);
61
61 Exercise 5.2 ctd. Consider the following schema. Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog lists the prices charged for parts by Suppliers. Write the following query in SQL: Find the sids of suppliers who charge more for some part than the average cost of that part (averaged over all suppliers who supply that part).
62
62 GROUP BY and HAVING (Grup Fonksiyonları) 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:
63
63 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
64
64 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. (SQL does not exploit primary key semantics here!) One answer tuple is generated per qualifying group.
65
65 Aggregate Functions – Group By Find the average salary of instructors in each department select dept_name, avg (salary) as avg_salary from instructor group by dept_name; avg_salary
66
66 Aggregation (Cont.) Attributes in select clause outside of aggregate functions must appear in group by list /* erroneous query */ select dept_name, ID, avg (salary) from instructor group by dept_name;
67
67 Aggregate Functions – Having Clause Find the names and average salaries of all departments whose average salary is greater than 42000 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 dept_name, avg (salary) from instructor group by dept_name having avg (salary) > 42000;
68
68 Null Values and Aggregates Total all salaries select sum (salary ) from instructor Above statement ignores null amounts Result is null if there is no non-null amount All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes What if collection has only null values? count returns 0 all other aggregates return null
69
69 Only S.rating and S.age are mentioned in the SELECT, GROUP BY or HAVING clauses; other attributes `unnecessary’. 2nd column of result is unnamed. (Use AS to name it.) SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors
70
70 SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors. Step 1. Step 1: Apply Where clause.
71
71 SELECT S.rating, MIN ( S.age ) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors. Step 2. Step 2: keep only columns that appear in SELECT, GROUP BY, or HAVING
72
72 SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors. Step 3. Step 4: sort tuples into groups.
73
73 SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors. Step 4. Step 5: apply having clause to eliminate groups.
74
74 SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors. Step 5. Step 6: generate one answer tuple for each group.
75
75 For each red boat, find the number of reservations for this boat What do we get if we remove B.color=‘red’ from the WHERE clause and add a HAVING clause with this condition? SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid
76
76 Exercise 5.2 ctd. Consider the following schema. Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog lists the prices charged for parts by Suppliers. Write the following queries in SQL: 1.For every supplier that supplies only green parts, print the name of the supplier and the total number of parts that she supplies. 2.For every supplier that supplies a green part and a red part, print the name and price of the most expensive part that she supplies.
77
77 Find the age of the youngest sailor with age > 18, for each rating with at least 2 sailors (of any age) Shows HAVING clause can also contain a subquery. Compare this with the query where we considered only ratings with 2 sailors over 18! SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age > 18 GROUP BY S.rating HAVING 1 < ( SELECT COUNT (*) FROM Sailors S2 WHERE S.rating=S2.rating)
78
78 Find those ratings for which the average age is the minimum over all ratings Aggregate operations cannot be nested! WRONG: SELECT S.rating FROM Sailors S WHERE S.age = ( SELECT MIN ( AVG (S2.age)) FROM Sailors S2) SELECT Temp.rating, Temp.avgage FROM ( SELECT S.rating, AVG (S.age) AS avgage FROM Sailors S GROUP BY S.rating) AS Temp WHERE Temp.avgage = ( SELECT MIN (Temp.avgage) FROM Temp) v Correct solution (in SQL/92):
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.