160 likes | 186 Views
More sql. Session 4 – subselects, union, aggregate functions. 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. sub-select operators. in and not in
E N D
More sql Session 4 – subselects, union, aggregate functions
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.
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.
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’);
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);
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”
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”;
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
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
Sql functions • String functions : • lower(string) • upper(string) • char_lenth(string) • substring(string,start [,n]) • trim(string) – removes spaces from the string (ltrim, rtrim)
Sql functions (cont) • Numeric functions • +,-,*, / • abs – absolute value • ceil, floor • mod • power • sqrt
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
More information of MySql functions : http://dev.mysql.com/doc/refman/5.0/en/functions.html
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.