Presentation is loading. Please wait.

Presentation is loading. Please wait.

More sql Session 4 – subselects, union, aggregate functions.

Similar presentations


Presentation on theme: "More sql Session 4 – subselects, union, aggregate functions."— Presentation transcript:

1 More sql Session 4 – subselects, union, aggregate functions

2 Catalog cno varchar(7) ctitle varchar(50) Students sid varchar(5) fname varchar(20) lname varchar(20) minit char Courses term varchar(10) lineno int(4) cno varchar(7) a int(2) b int(2) c int(2) d int(2) Components term varchar(10) lineno int(4) compname varchar(15) maxpoints int(4) weight int(2) Enrolls sid varchar(5) term varchar(10) lineno varchar(4) Scores sid varchar(5) term varchar(10) lineno varchar(4) compname varchar(15) points int(4)

3 Sub-selects The search criteria in the where clause may itself contain a select statement. Such a select statement is referred a a sub- select.

4 sub-select operators in and not in Get the sid values of students who have enrolled in csc226. select fname,lname from students where sid in (select sid from enrolls, courses where enrolls.term = courses.term and enrolls.lineno = courses.lineno and cno = 'csc226'); Ex: Get the names of students who have enrolled in csc226 or csc227.

5 sub-select operators(cont) any – the comparison succeeds if it matches any one value in the subselect all - the comparison succeeds if it matches all the values in the subselect Get the students name with the lowest student id: select fname,lname from students where sid <= all (select sid from students); Get the names of students who took at least one course in the fall semester of 1996 select distinct fname, lname from students, enrolls where students.sid=enrolls.sid and students.sid = any ( select distinct sid from enrolls where term=‘f96’);

6 sub-select operators(cont) exists and not exists –exists predicate is true if the sub-select result is a non-empty set of values and is false otherwise Get the ids of student who did not enroll in any course in the fall 1996 semester. select sid from students where not exists ( select ’a’ from enrolls where term=“f96” and students.sid=enrolls.sid);

7 Union Computes the union of two sub-queries Get the student ids enrolled in f96 or sp97 select sid from enrolls where term=“f96” union select sid from enrolls where term=“sp97” Union all does not remove duplicates select sid from enrolls where term=“f96” Union all select sid from enrolls where term=“sp97”

8 Aggregate functions NameArgument typeResult TypeDescription countany( can be *)numericcount of occurrences sumnumeric sum of arguments avgnumeric Average of arguments maxchar or numericsame as argument maximum value minchar or numericsame as argument minimum value

9 Aggregate functions examples Get the total number of students select count(*) sid from students; Get the un-weighted average of the number of points for all tests in section 1031, fall 1996. select avg(maxpoints) from components where lineno=“1031” and term=“f96”;

10 Group by and having The group by clause is used to form groups of rows of a resulting table based on column values When the group by clause is used all aggregate operations are computed on the individual groups, not on the entire table The having clause is used to eliminate certain groups

11 Group and having example Get the average number of points for all the tests in each term/section select term,lineno,avg(maxpoints) from components group by term,lineno; Get the average number of points for all the tests in each term/section, but only if the average is higher then 99.99 select term,lineno,avg(maxpoints) AVG from components group by term,lineno having AVG >=100

12 Sql functions String functions : –lower(string) –upper(string) –char_lenth(string) –substring(string,start [,n]) –trim(string) – removes spaces from the string (ltrim, rtrim)

13 Sql functions (cont) Numeric functions –+,-,*, / –abs – absolute value –ceil, floor –mod –power –sqrt

14 Sql functions (cont) Date functions –current_date() –+, - '1997-12-31 23:59:59' + INTERVAL 1 SECOND = '1998-01- 01 00:00:00‘ SELECT '1998-01-01' - INTERVAL 1 DAYS = '1997-12-31' –PERIOD_DIFF(P1,P2) – months between P1 and P2 (YYMM) –> < = –trunc(d) returns the same day but with the time truncated to 12:00AM –More information : http://dev.mysql.com/doc/refman/5.0/en/date- and-time-functions.html

15 More information of MySql functions : http://dev.mysql.com/doc/refman/5.0/en/fu nctions.html

16 In class exercises Get the sid values of students who did not enroll in any class during the f96 term. Get the names of students who have enrolled in the highest number of courses.


Download ppt "More sql Session 4 – subselects, union, aggregate functions."

Similar presentations


Ads by Google