Professional Documents
Culture Documents
LEC06b AdvancedSQL
LEC06b AdvancedSQL
LEC06b AdvancedSQL
Language (SQL)
Lecture 7
1
Learning Outcomes
DROP
Example:
DROP TABLE VENDOR;
Ordering a Listing
ORDER BY clause is useful when listing order is
important
Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[ORDER BY columnlist [ASC | DESC]];
Aggregate Functions
10
DISTINCT V_CODE
DISTINCT V_CODE
COUNT(*)
3
SELECT MAX(P_PRICE)
FROM PRODUCT;
256.99
MIN P_PRICE
SELECT MIN(P_PRICE)
FROM PRODUCT;
4.99
11
P_CODE
P_DESCRIPT
P_PRICE
09-WRE-Q
256.99
SUM
SELECT SUM(P_ONHAND*P_PRICE)
FROM PRODUCT;
AVG
SELECT P_DESCRIPT, P_ONHAND, P_PRICE,
V_CODE
FROM PRODUCT WHERE
P_PRICE > (SELECT AVG(P_PRICE) FROM
PRODUCT)
ORDER BY P_PRICE DESC;
12
13
Grouping Data
14
columnlist
tablelist
conditionlist]
columnlist]
conditionlist]
columnlist [ASC | DESC] ] ;
15
GROUP BY
SELECT P_SALECODE, MIN(P_PRICE)
FROM PRODUCT
GROUP BY P_SALECODE;
16
17
18
Creating a View
19
20
21
22
23
Example:
SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT,
V_AREACODE, V_PHONE
FROM PRODUCT P, VENDOR V
WHERE P.V_CODE = V.V_CODE
ORDER BY P_PRICE;
24
Recursive query
Use aliases to differentiate the table from itself
25
Procedural SQL
Shortcomings of SQL
Solutions
Embedded SQL
Shared Code
26
Procedural SQL
27
Triggers
Statement-level trigger
Row-level trigger
Triggering action
28
Table A
C+1
D+1
Table B
29
Trigger
Role of triggers
30
31
Trigger Example
32
33
34
35
36
37
38
39
40
Stored Procedures
Stored
C+ 1 of application
Minimize chanceAof errors
and cost
Procedure
B
D+ 1
development and maintenance
Table A
Table B
You can manipulate the whole set of records (changing, adding, updating,
deleting) using stored procedure
41
42
43
Embedded SQL
Run-time mismatch
Processing mismatch
44
45
46
47
Dynamic SQL