
use mytools;

DROP FUNCTION IF EXISTS olympic_tier;
DELIMITER //

CREATE FUNCTION olympic_tier(medals INT) 
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
    DECLARE medal_level VARCHAR(20);

    IF medals >= 30 THEN
		SET medal_level = 'TIER 1';
    ELSEIF medals >= 20 THEN
		SET medal_level = 'TIER 2';
    ELSE
		SET medal_level = 'TIER 3';
    END IF;
    -- return the customer level
    RETURN (medal_level);
END //
DELIMITER ;

SELECT country, count, olympic_tier(count)
FROM olympics
ORDER BY country;

