MySQL
- Introduction To MySQL
- MySQL RDBMS
- MySQL SQL
- MySQL SELECT Statement
- MySQL WHERE Clause
- MySQL AND, OR And NOT Operators
- MySQL ORDER BY Keyword
- MySQL INSERT INTO Statement
- MySQL NULL Values
- MySQL UPDATE Statement
- MySQL DELETE Statement
- MySQL LIMIT Clause
- MySQL MIN() And MAX() Functions
- MySQL COUNT(), AVG() And SUM() Functions
- MySQL LIKE Operator
- MySQL Wildcards
- MySQL IN Operator
- MySQL BETWEEN
- MySQL Aliases
- MySQL Joins
- MySQL INNER JOIN Keyword
- MySQL LEFT JOIN Keyword
- MySQL RIGHT JOIN Keyword
- MySQL CROSS JOIN Keyword
- MySQL Self Join
- MySQL UNION Operator
- MySQL GROUP BY Statement
- MySQL HAVING Clause
- MySQL EXISTS Operator
- MySQL ANY And ALL Operators
- MySQL INSERT INTO SELECT Statement
- MySQL CASE Statement
- MySQL NULL Functions
- MySQL Comments
- MySQL Operators
- MySQL CREATE DATABASE Statement
- MySQL DROP DATABASE Statement
- MySQL CREATE TABLE Statement
- MySQL DROP TABLE Statement
- MySQL ALTER TABLE Statement
- MySQL Constraints
- MySQL NOT NULL Constraint
- MySQL UNIQUE Constraint
- MySQL PRIMARY KEY Constraint
- MySQL FOREIGN KEY Constraint
- MySQL CHECK Constraint
- MySQL DEFAULT Constraint
- MySQL CREATE INDEX Statement
- MySQL AUTO INCREMENT Field
- MySQL Working With Dates
- MySQL Views
- MySQL Data Types
- MySQL Functions
MySQL NULL Functions
MySQL IFNULL() And COALESCE() Functions
Look at the following "Products" table:
P_Id | ProductName | UnitPrice | UnitsInStock | UnitsOnOrder |
---|---|---|---|---|
1 | Jarlsberg | 10.45 | 16 | 15 |
2 | Mascarpone | 32.56 | 23 | |
3 | Gorgonzola | 15.67 | 9 | 20 |
Suppose that the "UnitsOnOrder" column is optional, and may contain NULL values.
Look at the following SELECT statement:
SELECT ProductName, UnitPrice * (UnitsInStock + UnitsOnOrder)
FROM Products;
FROM Products;
In the example above, if any of the "UnitsOnOrder" values are NULL, the result will be NULL.
Practice Excercise Practice now
MySQL IFNULL() Function
The MySQL IFNULL() function lets you return an alternative value if an expression is NULL.
The example below returns 0 if the value is NULL:
SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL(UnitsOnOrder, 0))
FROM Products;
FROM Products;
Practice Excercise Practice now
MySQL COALESCE() Function
Or we can use the COALESCE() function, like this:
SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0))
FROM Products;
FROM Products;
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP