What is the function of COALESCE()?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 3 )
COALESCE()is used when we want to return a default value when there is null in the the table
Eg: Select revenue – Coalesce (cost price, 0) as profit from table_name
Any amateur SQL person would think Why to use COALESCE , when we have ISNULL function in SQL. This is because when we have multiple values , and not sure they might be null , then we opt for COALESCE. It return the first non-null value in a list.
For e.g.
emp_vehicle_count = NULL
emp_mobile_count = NULL
emp_count = 200
SELECT COALESCE(emp_vehicle_count, emp_mobile_count,emp_count);
Output : 200
Coalesce() function is used to handle NULL values.
During the evaluation process, the NULL values are replaced with the user-defined value.