← Back to MySQL Course | Chapter 8: Operators & Conditional Logic | Lesson 5 of 5

IFNULL और COALESCE

IFNULL और COALESCE 'अगर यहाँ कोई answer नहीं है, इसके बजाय यह backup answer इस्तेमाल करो' कहने जैसे हैं। COALESCE लगातार कई backups try कर सकता है।
Syntax
sql
IFNULL(expression, replacement_value)

COALESCE(expression1, expression2, ..., default_value)

IFNULL Function

IFNULL function दो arguments लेता है और MySQL-specific है। अगर पहला argument NULL है, यह दूसरे argument को एक fallback default की तरह return करता है, नहीं तो यह पहले argument को बिना बदले return करता है।

उदाहरण: The IFNULL Function

sql
CREATE TABLE users (id INT, nickname TEXT);
INSERT INTO users VALUES (1, NULL), (2, 'Champ');
SELECT id, IFNULL(nickname, 'Guest') AS display_name FROM users;

COALESCE Function Basics

COALESCE function दो या ज़्यादा arguments accept करता है और SQL standard का हिस्सा है। यह list में सबसे पहली non-NULL value return करता है, उन्हें left से right check करते हुए जब तक एक मिल न जाए।

उदाहरण: The COALESCE Function Basics

sql
CREATE TABLE users (id INT, nickname TEXT, username TEXT, email TEXT);
INSERT INTO users VALUES (1, NULL, NULL, '[email protected]'), (2, 'Champ', 'c123', '[email protected]');
SELECT id, COALESCE(nickname, username, email) AS display_name FROM users;

IFNULL बनाम COALESCE अंतर

IFNULL MySQL के लिए specific है और exactly दो parameters accept करता है, common two-value case के लिए इसे थोड़ा ज़्यादा concise बनाते हुए। COALESCE standard SQL है और कई parameters handle करता है, इसलिए यह अलग-अलग database systems में ज़्यादा portable है।

उदाहरण: IFNULL vs COALESCE Differences

sql
SELECT IFNULL(NULL, 'fallback') AS ifnull_result;
SELECT COALESCE(NULL, NULL, 'third option', 'fourth') AS coalesce_result;

Nested IFNULL Expressions

आप sequence में दो से ज़्यादा values check करने के लिए कई IFNULL functions nest कर सकते हैं। हालाँकि, तीन या ज़्यादा fallbacks के लिए आमतौर पर COALESCE इस्तेमाल करना cleaner है, क्योंकि यह repeated IFNULL calls की माँग वाले deeply nested parentheses से बचाता है।

उदाहरण: Nested IFNULL Expressions

sql
CREATE TABLE users (id INT, nickname TEXT, username TEXT);
INSERT INTO users VALUES (1, NULL, NULL);
SELECT id, IFNULL(nickname, IFNULL(username, 'Guest')) AS via_ifnull FROM users;
SELECT id, COALESCE(nickname, username, 'Guest') AS via_coalesce FROM users;

Math Calculations में Practical Uses

NULL शामिल math calculations पूरे expression के लिए NULL output करती हैं, भले ही सिर्फ एक operand missing हो। math calculations को पहले zero जैसी एक default value substitute करके safely चलने के लिए IFNULL या COALESCE इस्तेमाल करें।

उदाहरण: Practical Uses in Math Calculations

sql
CREATE TABLE items (id INT, price INT, discount INT);
INSERT INTO items VALUES (1, 100, NULL), (2, 50, 10);
SELECT id, price - IFNULL(discount, 0) AS final_price FROM items;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. IFNULL को दो से ज़्यादा arguments के साथ इस्तेमाल करना, जबकि यह exactly दो लेता है (COALESCE इस्तेमाल करें)।
  2. यह उम्मीद करना कि IFNULL(col, 0) एक empty string '' को replace करेगा, जबकि यह सिर्फ NULL को replace करता है।
  3. types को mix करना, जैसे IFNULL(price, 'N/A'), ताकि result type बदल जाए।
चैप्टर सारांश
  • Arithmetic, comparison, और logical operators queries में expressions बनाते हैं।
  • CASE expression किसी query के अंदर conditional logic add करता है।
  • IFNULL और COALESCE data के NULL होने पर fallback values देते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.