SELECT bouquets.name, bouquets.price
FROM bouquets
WHERE bouquets.price BETWEEN 30.45 AND 40.36
ORDER BY bouquets.name;
We need to help a customer find bouquets within their budget, specifically between $30.45 and $40.36. This is one of the most common types of database queries in e-commerce - letting customers find products in their price range. Think of it like a customer telling a florist: "Show me all bouquets between thirty and forty dollars, and list them alphabetically."
This query is similar to many everyday situations:
Imagine you're house hunting and tell your realtor: "I want to see all houses between $300,000 and $350,000, listed alphabetically by street name." The realtor would:
SELECT bouquets.name, bouquets.price
This determines what information we want to see about each matching bouquet. We're using the full table name (bouquets.name instead of just name) for clarity and to prevent any potential ambiguity if we later join with other tables. Notice that we're only selecting what the customer needs - the name and price. This follows the principle of returning only necessary data.
FROM bouquets
This tells the database which table to search in. In our case, it's simple - we only need the bouquets table. We don't need to join with the flowers or flower_varieties tables because we're only interested in bouquet-level information.
WHERE bouquets.price BETWEEN 30.45 AND 40.36
This is where the filtering magic happens. The BETWEEN operator is inclusive, meaning it includes bouquets that cost exactly $30.45 or $40.36. We could have written this as:
WHERE bouquets.price >= 30.45 AND bouquets.price <= 40.36
But BETWEEN is more readable and concise. It's especially useful when working with:
ORDER BY bouquets.name
This sorts our results alphabetically by bouquet name. Without this, the results might come back in any order (often by the internal database ID). Consistent ordering is important for:
1. Start with all bouquets:
name | price
Get Well Soon | 59.99
Mother's Day | 39.99
2. Apply price filter (BETWEEN 30.45 AND 40.36):
name | price
Mother's Day | 39.99
3. Sort by name (ORDER BY):
name | price
Mother's Day | 39.99
The test specs show this query works with different price ranges. This is possible because the query is properly structured. When the test changes the price range to $50-$60, it finds:
name | price
Get Well Soon | 59.99
While our solution uses BETWEEN, here are other valid ways to write this query:
-- Using >= and <=
SELECT bouquets.name, bouquets.price
FROM bouquets
WHERE bouquets.price >= 30.45
AND bouquets.price <= 40.36
ORDER BY bouquets.name;
-- Using decimal comparison
SELECT bouquets.name, bouquets.price
FROM bouquets
WHERE CAST(bouquets.price AS DECIMAL(10,2)) >= 30.45
AND CAST(bouquets.price AS DECIMAL(10,2)) <= 40.36
ORDER BY bouquets.name;
When working with price ranges, watch out for:
This query performs well because:
Notice that our prices are stored with two decimal places (39.99). This is important for:
This query demonstrates several fundamental SQL concepts: