Using the IF Function in MySQL
📖 Scenario: You work at a bookstore that wants to classify books based on their price. Books priced above $20 are considered 'Expensive', and others are 'Affordable'.
🎯 Goal: Create a MySQL query that uses the IF function to add a new column called price_category which shows 'Expensive' if the book price is greater than 20, otherwise 'Affordable'.
📋 What You'll Learn
Create a table called
books with columns id, title, and price.Insert exactly three books with given prices.
Write a SELECT query using the
IF function to classify books by price.The output must include
id, title, price, and the new price_category column.💡 Why This Matters
🌍 Real World
Classifying products or items based on conditions like price or stock is common in retail databases.
💼 Career
Knowing how to use conditional logic in SQL queries helps in reporting and data analysis roles.
Progress0 / 4 steps