Which SQL aggregate function gives an output with the number of rows that contain non null values for a given column?

Aggregate functions operate on values across rows to perform mathematical calculations such as sum, average, counting, minimum/maximum values, standard deviation, and estimation, as well as some non-mathematical operations.

An aggregate function takes multiple rows (actually, zero, one, or more rows) as input and produces a single output. In contrast, scalar functions take one row as input and produce one row (one value) as output.

An aggregate function always returns exactly one row, even when the input contains zero rows. Typically, if the input contained zero rows, the output is NULL. However, an aggregate function could return 0, an empty string, or some other value when passed zero rows.

In this Topic:

Introductory Example¶

The following example illustrates the difference between an aggregate function (AVG) and a scalar function (COS). The scalar function returns one output row for each input row, while the aggregate function returns one output row for multiple input rows:

The table contains the following data:

SELECT x, y FROM simple ORDER BY x,y; +----+----+ | X | Y | |----+----| | 10 | 20 | | 20 | 44 | | 30 | 70 | +----+----+

The scalar function returns one output row for each input row.

SELECT COS(x) FROM simple ORDER BY x; +---------------+ | COS(X) | |---------------| | -0.8390715291 | | 0.4080820618 | | 0.1542514499 | +---------------+

The aggregate function returns one output row for multiple input rows:

SELECT SUM(x) FROM simple; +--------+ | SUM(X) | |--------| | 60 | +--------+

Aggregate Functions and NULL Values¶

Some aggregate functions ignore NULL values. For example, AVG calculates the average of values 1, 5, and NULL to be 3, based on the following formula:

(1 + 5) / 2 = 3

In both the numerator and the denominator, only the two non-NULL values are used.

If all of the values passed to the aggregate function are NULL, then the aggregate function returns NULL.

Some aggregate functions can be passed more than one column. For example:

SELECT COUNT(col1, col2) FROM table1;

In these instances, the aggregate function ignores a row if any individual column is NULL.

For example, in the following query, COUNT returns 1, not 4, because three of the four rows contain at least one NULL value in the selected columns:

Create a table and populate it with values:

CREATE TABLE t (x INT, y INT); INSERT INTO t (x, y) VALUES (1, 2), -- No NULLs. (3, NULL), -- One but not all columns are NULL. (NULL, 6), -- One but not all columns are NULL. (NULL, NULL); -- All columns are NULL.

Query the table:

SELECT COUNT(x, y) FROM t; +-------------+ | COUNT(X, Y) | |-------------| | 1 | +-------------+

Similarly, if SUM is called with an expression that references two or more columns, and if one or more of those columns is NULL, then the expression evaluates to NULL, and the row is ignored:

SELECT SUM(x + y) FROM t; +------------+ | SUM(X + Y) | |------------| | 3 | +------------+

Note that this behavior differs from the behavior of GROUP BY, which does not discard rows when some columns are NULL:

SELECT x AS X_COL, y AS Y_COL FROM t GROUP BY x, y; +-------+-------+ | X_COL | Y_COL | |-------+-------| | 1 | 2 | | 3 | NULL | | NULL | 6 | | NULL | NULL | +-------+-------+

Which is the basic SQL aggregate function that gives the number of rows containing not NULL values for the given column?

The SQL COUNT function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. The SQL AGGREGATE SUM() function returns the sum of all selected column.

Which is the aggregate function returns the number of table rows?

COUNT() Function in SQL. The COUNT() aggregate function returns the total number of rows from a database table that matches the defined criteria in the SQL query. COUNT(*) returns the total number of rows in a given table.

Which aggregate function counts the number of rows including rows with null value?

Answer: B. The COUNT(*) counts the number of rows including duplicates and NULLs. Use DISTINCT and ALL keyword to restrict duplicate and NULL values.

What does COUNT (*) do in SQL?

COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.