Lecture 8 Flashcards
SELECT & aggregation
SELECT statement aggregates
- Count,
- sum,
- average,
- min & max
aggregate functions
- aggregate functions perform a calculation on a set of values, and returns a single value
- 4+2+3+1+7=16
- Each aggregate function operates on a single column of a table and returns a single value
- used with the GROUP BY clause of the SELECT statement
- except for COUNT, aggregate functions ignore null values
ISO standard defines five aggregate functions:
- COUNT
- SUM (numeric only)
- AVG (numeric only)
- MIN
- MAX
COUNT(*)
COUNT(*) counts all rows of a table, regardless of whether nulls or duplicate values occur
DISTINCT
use DISTINCT before column name to eliminate duplicates
GROUP BY & aggregate functions
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result set by one or more columns
SELECT column_name(s)
FROM Table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
HAVING & aggregrate functions
enables you to specify conditions that filter which group results appear in the results
Similar to WHERE, but WHERE filters individual rows whereas HAVING filters groups
(the HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions)
where can aggregate functions be used
Only in SELECT list and in HAVING claus
If SELECT list includes an aggregate function and there is no GROUP BY clause, SELECT list cannot reference a column out with an aggregate function
example use of COUNT(*)
Example of Having
JOIN
They are used to join two or more different tables on a point in which both the tables match the same value and property
inner, outer joins, etc. check the ppt
inner, outer joins, etc. check the ppt