COLUMN FUNCTIONS (AGGREGATE FUNCTIONS)

Aggregate functions allow you to calculate values based upon all data in an attribute of a table. The SQL aggregate functions are: Max, Min, Avg, Sum, Count, StdDev, Variance. Note that AVG and SUM work only with numeric values and both exclude NULL values from the calculations.

1. Example 1

How many students are there? SELECT COUNT(*) FROM STUDENT;

COUNT(*)

6

NOTE: COUNT can be used in two ways. COUNT(*) is used to count the number of tuples that satisfy a query. COUNT with DISTINCT is used to count the number of unique values in a named column.

2. Example 2

Find the number of departments that have faculty in them. SELECT COUNT(DISTINCT DEPT) FROM FACULTY;

COUNT(DISTINCT)

4

3. Example 3

Find the average number of credits for students who major in math. SELECT AVG(CREDITS) FROM STUDENT WHERE MAJOR = 'Math';

AVG(CREDITS)

29

Komentar 0

Related Posts