Lecture 7- Aggregation and Grouping Flashcards

1
Q

What is an aggregate?

A

Aggregate function that take a collection (a set or multiset) of values as input and return a single value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the five standard built in aggregate functions?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe these operations on sets:

  1. COUNT ([DISTINCT] A)
  2. SUM ([DISTINCT] A)
  3. AVG ([DISTINCT] A)
  4. MAX (A)
  5. MIN (A)
A
  1. The number of (unique) values in the A column
  2. The sum of all (unique) values in the A column
  3. The average of all (unique) values in the A column
  4. The maximum value in the A column
  5. The minimum value in the A column
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is basic aggregation?

A
  • Count is frequently used to count the number of tuples in a relation
  • The notation for this function in SQL is count(* )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe the use of basic aggregation and the use of DISTINCT

A
  • It is not allowed in SQL to use DISTINCT with COUNT(asterix)
  • If count does not include DISTINCT then COUNT(asterix) = COUNT(x), where x is any set of attributes
  • Retaining duplicates is important in computing an average
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is this query doing?

customer (cname, street, city)

A

Find the number of customers in Edmonton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is this query doing?

branch (bname, address, city, assets)

A

Find the total assets of branches in Edmonton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is this query doing?

deposit (accno, cname, bname, balance)

A

Find the number of different branches where John Doe has a deposit account

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is this query doing?

loan (accno, cname, bname, amount)

A

Find the name of the customer with the higest loan amount

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is this query doing?

customer (cname, street, city)
deposit (accno, cname, bname, balance)

A

Find the number of customers who have deposit accounts in at least 3 different branches

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is this query doing?

branch (bname, address, city, assets)

A

Find the names of branches which have assets greater than the average assets of all branches

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to compute the number of customers per city?

A
  1. Fire off a seperate query for each city
  2. Define a special grouping operator

SELECT COUNT(asterix)
FROM customer
WHERE city = ‘Edmonton’

SELECT city, COUNT(cname)
FROM customer
GROUP BY city

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is GROUP BY used for?

A
  • The GROUP BY clause is used in SQL to group rows in a relation based on give attributes
  • The attributes given in the GROUP BY clause are used to form groups called subgroups
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If there is a ________ in the SELECT statement, then the GROUP BY clause must be placed after it

A

WHERE clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What type of query is this?

A

GROUP BY with aggragate functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the rule of GROUP BY with aggregate functions?

A

Every column name in the SELECT clause must appear in the GROUP BY clause (this is not required if the column name is only used in aggregate function)

17
Q

What is the HAVING clause used for?

A
  • Used to specify a filter condition for the group generated by the GROUP BY clause
  • It is used in SQL statements to specify a filter conditionfor groups of rows or aggregates
18
Q

What does HAVING clause do?

A

HAVING is applied to each group seperately to determine if the group as a whole qualifies

19
Q

What is this query doing?

branch (bname, address, city, assets)

A
  • Find cities with more than two bank branches
  • GROUP BY city partitions tuples into groups where all tuples in a group have the same values in city column
  • HAVING is applied to each group separately to determine if the group as a whole qualifies
20
Q

What is this query doing?

loan (accno, cname, bname, amount)

A

For every branch, list the branch name and the name of every customer who has more than 3 loans each over $100,000 in that branch

21
Q

Describe the evaluation of GROUP BY with HAVING

A