Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 SQL HUANG XUEHUA. SQL Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables;

Similar presentations


Presentation on theme: "Chapter 7 SQL HUANG XUEHUA. SQL Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables;"— Presentation transcript:

1 Chapter 7 SQL HUANG XUEHUA

2 SQL

3 Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables; SELECT title, length FROM movies WHERE year =1994; For example:

4 A typical SQL query has the form: select A 1, A 2,..., A n from r 1, r 2,..., r m where C  A i s represent attributes  r i s represent relations  C is a condition. This query is equivalent to the relational algebra expression. A1, A2,..., An (  c (r 1 x r 2 x... x r m )) The result of an SQL query is a relation. Select-From-Where Statements

5 SELECT GRAMMER SELECT [ALL | DISTINCT] [alias] [ , [alias] ]… FROM [ , ]… [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ASC | DESC]] ;

6 SELECT GRAMMER 1. Column can have this pattern : ( 1 ) * ( 2 ).* ( 3 ) COUNT([ALL | DISTINCT] * ) ( 4 ) [.] [alias] [ , [.] [alias]]… 2. WHERE condition expression is very flexible 3. GROUP BY: SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT- clause and each subgroup of tuples consists of the set of tuples that have the same value for the grouping attribute(s) HAVING only that group satisfy the condition can output 4. [ORDER BY [ASC | DESC]] : order

7 SELECT SEARCH 1. search based on single table ( 1 ) choose some columns : some columns ; all columns ; computed columns ( 2 ) choose some rows : eliminate some rows ; the tuples satisify the condition (比较大小,确定范围,确定集合,字符匹配,空值,多条件); ( 3 ) make the result in order 。 ( 4 ) use the aggregate function 。 ( 5 ) group 2. join search (等值与非等值连接;自身连接;外连接;复合条件连 接) 3. nest search (用 IN 子查询;用 = ;用 ANY 和 ALL ;用 EXISTS )

8 Search the Student Database Tables: student , course , sc ( 1 ) student ( sno , sname , ssex , sage,sdept ), ( 2 ) Course (cno , cname , cpno , ccredit) , ( 3 ) SC (sno , cno , grade)

9 STUDENT RELATION

10 SC RELATION

11 1) Search all the student’s information 2) Find the name and the number of the boys who study in ‘IS’ department? Select * from student Select sno , sname from student where ssex=‘m’ and sdept=‘IS’;

12 3)Search the students who have chosen courses. Select sno from sc

13 Search the Tuples Satisfy Condition Search condition operators comparision = , > , = , , ! , NOT+ 上述 符号 scope BETWEEN AND , NOT BETWEEN AND sets IN , NOT IN Letter match LIKE , NOT LIKE ( % , _ ) Null values IS NULL , IS NOT NULL Mult- conditions AND , OR

14 Search the Tuples Satisfy Condition 4)Search the name and sex of the student whose age is between 25 and 30 5)Search the student whose family name is’ 欧阳 ’ 6)Search the student who is from ‘IS’,’MATH’,’CS’ 7)Aggregate function

15 Use the Aggregate Function Aggregate function includes : COUNT ( [DISTINCT | ALL] * ) the numbers of the tuples COUNT ( [DISTINCT | ALL] ) the numbers of the columns SUM ( [DISTINCT | ALL] ) the total value of a column AVG ( [DISTINCT | ALL] ) the average value of a column MAX ( [DISTINCT | ALL] ) the maximum value of a column MAX ( [DISTINCT | ALL] ) the minimum value of a column 8). Search the total numbers of the students 。 select count(*) from student; 9). Search the numbers of the student who has chosen the courses select count(distinct sno) from sc;

16 10) Search the numbers of the students for each course. 11)Search the index number of the student who has chosen more than 4 courses SELECT Sno FROM SC GROUP BY Sno HAVING COUNT(*)>4 SELECT Cno, COUNT(Sno) FROM sc GROUP BY Cno;

17 Join 12) Search the student’s information and include the courses they have chosen and the grade they have got

18 Join Search

19

20

21 12a: 12b: Select student.*,sc.* From student,sc Where student.sno=sc.sno (等值连接)

22 Join Search 13) Search the course name of each course’s direct prerequisites 14)Search the student who have chosen the 2 course and the grade is large than 90

23 Summary From this lecture you can learn the basic syntax of data definition language. search condition Column expression join

24 Any Questions? If there are any outstanding questions you can ask me one-to-one after the lecture OR privately in my office.

25 Exercises Do the search exercises on the machine.


Download ppt "Chapter 7 SQL HUANG XUEHUA. SQL Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables;"

Similar presentations


Ads by Google