0% found this document useful (0 votes)
57 views

Store - Name Sales Store A 300 Store B 200 Store C 100 Store D Null

NULL in SQL represents missing or unknown data, rather than a value like 0. Operations on NULL result in NULL. Aggregate functions like SUM, MAX, MIN exclude NULLs from calculations but AVG and COUNT are affected, counting NULL rows as zero. ISNULL in SQL Server and IFNULL in MySQL replace NULL with a given value, while ISNULL in MySQL and NVL in Oracle test for NULL.

Uploaded by

mkumarshahi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Store - Name Sales Store A 300 Store B 200 Store C 100 Store D Null

NULL in SQL represents missing or unknown data, rather than a value like 0. Operations on NULL result in NULL. Aggregate functions like SUM, MAX, MIN exclude NULLs from calculations but AVG and COUNT are affected, counting NULL rows as zero. ISNULL in SQL Server and IFNULL in MySQL replace NULL with a given value, while ISNULL in MySQL and NVL in Oracle test for NULL.

Uploaded by

mkumarshahi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

NULL

In SQL, NULL means that data does not exist. NULL does not equal to 0 or an empty string. Both 0 and empty string represent a
value, while NULLhas no value.
Any mathematical operations performed on NULL will result inNULL. For example,
10 + NULL = NULL
Aggregate functions such as SUM, COUNT, AVG, MAX, and MIN exclude NULL values. This is not likely to cause any issues for
SUM, MAX, and MIN. However, this can lead to confusion with AVG and COUNT.
Let's take a look at the following example:
Table Sales_Data
store_name Sales
Store A 300
Store B 200
Store C 100
Store D NULL
Below are the results for each aggregate function:
SUM (Sales) = 600
AVG (Sales) = 200
MAX (Sales) = 300
MIN (Sales) = 100
COUNT (Sales) = 3
Note that the AVG function counts only 3 rows (the NULL row is excluded), so the average is 600 / 3 = 200, not 600 / 4 = 150. The
COUNT function also ignores the NULL rolw, which is why COUNT (Sales) = 3.

IS NULL Function
The ISNULL function is available in both SQL Server and MySQL. However, their uses are different:
SQL Server
In SQL Server, the ISNULL() function is used to replace NULL value with another value.
For example, if we have the following table,
Table Sales_Data
store_name Sales
Store A 300
Store B NULL
The following SQL,
SELECT SUM(ISNULL(Sales,100)) FROM Sales_Data;
returns 400. This is because NULL has been replaced by 100 via the ISNULL function.
MySQL
In MySQL, the ISNULL() function is used to test whether an expression is NULL. If the expression is NULL, this function returns
1. Otherwise, this function returns 0.
For example,
ISNULL(3*3) returns 0
ISNULL(3/0) returns 1

IFNULL Function
The IFNULL() function is available in MySQL, and not in SQL Server or Oracle. This function takes two arguments. If the first
argument is not NULL, the function returns the first argument. Otherwise, the second argument is returned. This function is
commonly used to replace NULL value with another value. It is similar to the NVL function in Oracle and the ISNULL Function in
SQL Server.
For example, if we have the following table,
Table Sales_Data
store_name Sales
Store A 300
Store B NULL
The following SQL,
SELECT SUM(IFNULL(Sales,100)) FROM Sales_Data;
returns 400. This is because NULL has been replaced by 100 via the ISNULL function.

NVL Function
The NVL() function is available in Oracle, and not in MySQL or SQL Server. This function is used to replace NULL value with
another value. It is similar to the IFNULL Function in MySQL and the ISNULL Function in SQL Server.
For example, if we have the following table,
Table Sales_Data
store_name Sales
Store A 300
Store B NULL
Store C 150
The following SQL,
SELECT SUM(NVL(Sales,100)) FROM Sales_Data;
returns 550. This is because NULL has been replaced by 100 via the ISNULL function, hence the sum of the 3 rows is 300 +
100 + 150 = 550.

You might also like