Menu
CoddyTech

Count Rows and Values

Part of the SQL Foundations section of Coddy's PostgreSQL journey. Lesson 11 of 22.

An aggregate combines several input rows into one result. COUNT(*) counts rows; COUNT(column) counts only rows where that column is not NULL. Both return zero for an empty input.

Input table products:

idnamecategorypricestockactivenote
1Maptravel125TrueNULL
2Notebookbooks80TrueRecycled
3Atlasbooks243FalseNULL
4Mughome128TrueBlue
5lamphome302FalseClearance
6Mini maptravel610TrueNULL
SELECT COUNT(*) AS products_count, COUNT(note) AS notes_count FROM products;

There are six products but only three non-null notes. A note that is an empty string would still count because it is present. Without GROUP BY, the aggregate describes the whole filtered input.

COUNT(*) counts rows; COUNT(column) counts non-null values.

challenge icon

Challenge

Medium

Return one row named available_count counting products with stock above zero.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in SQL Foundations

Practice on your own: PostgreSQL playground