qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K
In SQL, what function do I use to return the first non-null column per row?
- User
In SQL, what function do I use to return the first non-null column per row?
- Assistant
In SQL, the function you use to return the first non-null column per row is:
COALESCE
🔹 Syntax:
COALESCE(value1, value2, ..., valueN)
🔹 Description:
COALESCEreturns the first non-null expression among its arguments, evaluated from left to right.- It's commonly used to substitute default values for
NULLs or to pick the first available (non-null) value from multiple columns.
🔹 Example:
Suppose you have a table like this:
id phone_home phone_work phone_mobile 1 NULL '555-1234' NULL 2 '555-5678' NULL NULL 3 NULL NULL '555-9876' You can get the first available phone number with:
SELECT id, COALESCE(phone_home, phone_work, phone_mobile) AS contact_number FROM contacts;✅ Result:
id contact_number 1 '555-1234' 2 '555-5678' 3 '555-9876'
📝 Notes:
COALESCEis part of the SQL standard and works in most RDBMS (PostgreSQL, MySQL, SQL Server, Oracle, etc.).- All arguments must be of compatible data types.
Let me know if you want an alternative like
ISNULL,IFNULL, orNVL— those are vendor-specific and only take two arguments.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.