Example: group the employee tuples based on their values for the sex attribute:
|
Example: suppose we only want to consider groups where the size of the group is at least 2:
|
Group condition:
|
|
Note:
|
SELECT grouping-attributes or set-functions FROM relation-list WHERE tuple-boolean-condition GROUP BY grouping-attribute-list HAVING SetFunction( .. ) RelOp ... <---- Group condition |
RelOp is a a relational operator (such as <, ≤, > ≥, =, ≠)
|
The employee relation:
+--------+---------+-----+
| fname | lname | dno |
+--------+---------+-----+
| James | Borg | 1 |
| Alicia | Zelaya | 4 |
| Jennif | Wallace | 4 |
| Ahmad | Jabbar | 4 |
| John | Smith | 5 |
| Frankl | Wong | 5 |
| Ramesh | Narayan | 5 |
| Joyce | English | 5 |
+--------+---------+-----+
|
|
|
Query:
SELECT dno, sum(salary)
FROM employee
WHERE sex = 'M'
GROUP BY dno
HAVING count(*) > 2
|
The employee relation:
ssn fname lname sex dno
--------- ------ -------- --- -----------
123456789 John Smith M 5
333445555 Frankl Wong M 5
999887777 Alicia Zelaya F 4
987654321 Jennif Wallace F 4
666884444 Ramesh Narayan M 5
453453453 Joyce English F 5
987987987 Ahmad Jabbar M 4
888665555 James Borg M 1
|