Computer ScienceExam code: 0984

Database Concepts

Databases

Two functions calculate a single value from a whole column of results rather than listing the rows.

SUM

SUM(field) adds up all the values in a numeric field across the records the query selects.

SELECT SUM(DailyFeedKg)
FROM tbl_zoo
WHERE InCaptivity = 'Yes';

This adds the daily feed for every captive species (35 + 3 + 2 + 1 + 25 + 2 + 20 + 4 = 92), so the query returns a single value: 92.

COUNT

COUNT(field) returns how many records the query selects, rather than the records themselves.

SELECT COUNT(Species)
FROM tbl_zoo
WHERE InCaptivity = 'No';

Two species are not in captivity (Anaconda and Tarantula), so this returns 2. Using COUNT(*) counts every selected record in the same way.

Build on this topic