Using GREATEST and LEAST Functions in MySQL
📖 Scenario: You work at a small online store that tracks product prices from different suppliers. You want to find the highest and lowest prices for each product to make better buying decisions.
🎯 Goal: Build a simple MySQL table with product prices from three suppliers. Then write queries using GREATEST and LEAST functions to find the highest and lowest prices for each product.
📋 What You'll Learn
Create a table named
product_prices with columns product_name (VARCHAR) and three price columns: price_supplier1, price_supplier2, price_supplier3 (all DECIMAL).Insert exactly three products with their prices from each supplier.
Write a SELECT query that uses
GREATEST to find the highest price among the three suppliers for each product.Write a SELECT query that uses
LEAST to find the lowest price among the three suppliers for each product.💡 Why This Matters
🌍 Real World
Stores and businesses often compare prices from multiple suppliers to get the best deals. Using GREATEST and LEAST helps quickly find the best and worst prices.
💼 Career
Knowing how to use these functions is useful for data analysts and database developers who work with pricing data or any scenario needing comparisons across columns.
Progress0 / 4 steps