Lecture 8: SQL Select Part 2 and RA Flashcards

1
Q

Explain Aggregate Functions

A

Often require to perform some form of summation or aggregation of data, similar to the totals at the bottom of a report

there are 5 aggregate functions, in SQL that we can use in SELECT command

each operates on a single column of a table and returns a single value

apart from count(*), each function eliminates nulls first and operates only on remaining non-null values

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

list the aggregate functions

A
COUNT
SUM
AVG
MIN
MAX
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

which two aggregate functions are only used on NUMERIC fields

A

SUM and AVG

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

List three aggregate functions that can apply to non-numeric and numeric fields

A

COUNT
MIN
MAX

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

What does count(*) do?

A

counts all rows of a table, regardless of whether nulls or duplicates values occur

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

What does distinct have an effect on?

A

no effect with : MIN/MAX

effect with
SUM/AVG

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

where are aggregate functions location?

A

can only be used in select list and in having clause

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

give an example of a meaningless aggreate function call

A

select staffNo, COUNT(salary)

from staff;

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

how to use count

A
SELECT COUNT(*) AS myCount	 
FROM PropertyForRent		WHERE rent > 350
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to use count and distinct

A
SELECT COUNT(DISTINCT propertyNo)
AS myCount FROM viewing;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

use of count and sum?

A

SELECT COUNT(staffNo) AS myCount,SUM(salary) AS mySum FROM Staff WHERE position = ‘Manager’

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

use of min, max and avg?

A
SELECT 
MIN(salary) AS myMin,
MAX(salary) AS myMax,
AVG(salary) AS myAvg	
FROM Staff;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to use group by

A

SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;

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

Describe the having clause

A

having clause is designed for use with group by to restrict groups that appear in final result table

similar to where, but where filters individual rows whereas HAVING filters groups

column names in HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function

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

Example of using having

A
select branchNo, 
count (staffNo) as mycount
sum(salary) as mysum
from staff
group by branchNo
having count (staffNo) > 1
order by branchoNo;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

explain subqueries

A

some sql statements can have a select embedded within them

a subselect can be used in WHERE and HAVING clauses of an outer select, where it is called a subquery or nested query

subselects may also appear in INSERT, UPDATE and DELETE statements

17
Q

Provide a subquery example

A

SELECT staffNo, fName, lName, position
FROM Staff
WHERE branchNo =

(SELECT branchNo FROM Branch
WHERE street = ‘163 Main St’);

18
Q

example of subquery with aggregate

A

SELECT staffNo, fName, lName, position,
salary –

(SELECT AVG(salary) FROM Staff) As SalDiff
FROM Staff
WHERE salary >

(SELECT AVG(salary)
FROM Staff);

19
Q

is this possible?

WHERE salary > AVG (salary) ?

A

NO

instead:
SELECT staffNo, fName, lName, position, salary – 17000 As salDiff
FROM Staff
WHERE salary > 7000;

20
Q

List the subquery rules

A
  • order by clause may not be used n a subquery
  • subquery select list must consist of a single column, name or expression unless using EXIST
  • must appear on RHS
  • cannot be used as an operand in an expression
21
Q

What are set operations?

A

union
intersection,
difference
except /not in (symbol : -)

two tables must be union compatible

22
Q

Union?

A

union of two tables A and B, it table containing all rows in either A or B or both

23
Q

intersection /JOIN

A

is table containing all rows common to both a and b

mySQL does not use intersect. use join instead

24
Q

difference

A

is table containing all rows in a but not in b