Using the NULLIF Function in MySQL
📖 Scenario: You are managing a small online store database. You want to handle cases where the discount price is the same as the original price by showing NULL instead of the discount price.
🎯 Goal: Build a MySQL query that uses the NULLIF function to return NULL when the discount price equals the original price, otherwise return the discount price.
📋 What You'll Learn
Create a table called
products with columns id, name, price, and discount_priceInsert three products with specific prices and discount prices
Write a SELECT query using
NULLIF(discount_price, price) to show NULL when discount price equals priceComplete the query to select
id, name, price, and the discount_price or NULL using NULLIF💡 Why This Matters
🌍 Real World
Online stores often need to show discounts only when they differ from the original price. NULLIF helps handle this cleanly in queries.
💼 Career
Understanding NULLIF is useful for database developers and analysts to write clear queries that handle special cases like equal values.
Progress0 / 4 steps