Create a PL/pgSQL function price_band(p numeric) returning 'budget' (<12), 'standard' (<20), or 'premium', then show each book's title and price_band(price) as band. Order by id.
Create a PL/pgSQL function price_band(p numeric) returning 'budget' (<12), 'standard' (<20), or 'premium', then show each book's title and price_band(price) as band. Order by id.
Answer
CREATE FUNCTION price_band(p numeric) RETURNS text AS BEGIN IF p < 12 THEN RETURN 'budget'; ELSIF p < 20 THEN RETURN 'standard'; ELSE RETURN 'premium'; END IF; END; LANGUAGE plpgsql; SELECT title, price_band(price) AS band FROM books ORDER BY id;