Start learning free

LESSON 45 OF 173 · BACKEND: SQL

Count Rows

Practice

Sometimes you don't need the rows themselves, only how many there are. COUNT(*) counts the rows: the answer is one row with one number in it.

COUNT with a column's name in the parentheses is pickier: it counts only the rows where that column has a value. A NULL is not counted. For example, this counts all the customers, and then only the customers whose city is filled in:

var query = "SELECT COUNT(*), COUNT(city) " +
	"FROM customers "

A WHERE works first: it keeps some of the rows, and COUNT counts only the rows that are left. In the shop, the 8 products have only 7 categories, because the gift card's category is NULL. 5 products cost more than 10, and the gift card, at 50, is one of them. The check prints your query's result, which must be:

COUNT(*) | COUNT(category)
5 | 4
(1 row)

Your task: in one query, count the products that cost more than 10, and count how many of them have a category.


Assignment
Follow the Coding Tutorial to write the query one clause per line: SELECT the two counts, take them FROM the products table, then keep only the rows WHERE the price is above 10.


Hint
Put a comma between COUNT(*) and COUNT(category). Use >, not >=: a product that costs exactly 10 is not counted. Every string piece but the last ends with +.


STEP 1 OF 3
    Ctrl + Enter to check