W3. SQL Null Functions Flashcards
Q: What do SQL functions like IFNULL(), ISNULL(), COALESCE(), and NVL() do?
A: They allow you to return an alternative value when an expression is NULL.
Q: Why would you use functions like IFNULL() in a SQL query?
A: To replace NULL values with an alternative value, avoiding NULL results in calculations.
Q: Write a query using IFNULL() in MySQL to replace NULL in UnitsOnOrder with 0.
SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL(UnitsOnOrder, 0))
FROM Products;
Q: How does the COALESCE() function work, and is it database-specific?
A: COALESCE() returns the first non-NULL value from a list, and it is compatible across multiple databases (MySQL, SQL Server, Oracle).
Q: Write a query using COALESCE() to replace NULL in UnitsOnOrder with 0.
SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0))
FROM Products;
Q: Which function does SQL Server use to handle NULL values similarly to IFNULL()?
A: ISNULL()
Q: Write a query using ISNULL() in SQL Server to replace NULL in UnitsOnOrder with 0.
SELECT ProductName, UnitPrice * (UnitsInStock + ISNULL(UnitsOnOrder, 0))
FROM Products;
Q: How does the IsNull() function work in MS Access?
A: It returns TRUE (-1) if the expression is NULL, and FALSE (0) otherwise.
Q: Write a query using IIF() and IsNull() in MS Access to replace NULL in UnitsOnOrder with 0.
SELECT ProductName, UnitPrice * (UnitsInStock + IIF(IsNull(UnitsOnOrder), 0, UnitsOnOrder))
FROM Products;
Q: What function does Oracle use to handle NULL values, similar to IFNULL()?
A: NVL()
Q: Write a query using NVL() in Oracle to replace NULL in UnitsOnOrder with 0.
SELECT ProductName, UnitPrice * (UnitsInStock + NVL(UnitsOnOrder, 0))
FROM Products;