Using IF-ELSE in SQL Procedures
📖 Scenario: You work at a bookstore that wants to categorize books by their price range automatically.They want a procedure that takes a book's price and returns a category: 'Cheap', 'Moderate', or 'Expensive'.
🎯 Goal: Create a SQL procedure named categorize_book_price that uses IF-ELSE statements to assign a category based on the price input.
📋 What You'll Learn
Create a procedure named
categorize_book_price with an input parameter book_price of type DECIMAL.Declare a variable
price_category to hold the category as VARCHAR(20).Use
IF-ELSE statements to set price_category to 'Cheap' if book_price is less than 10.Set
price_category to 'Moderate' if book_price is between 10 and 20 inclusive.Set
price_category to 'Expensive' if book_price is greater than 20.Return the
price_category as an output parameter.💡 Why This Matters
🌍 Real World
Bookstores and retail systems often need to categorize products automatically based on price or other attributes to help with pricing strategies and customer information.
💼 Career
Knowing how to write conditional logic in SQL procedures is essential for database developers and analysts to automate data processing and business rules.
Progress0 / 4 steps