Complete the code to create a function that returns the sum of two numbers.
CREATE FUNCTION add_numbers(a INT, b INT) RETURNS INT LANGUAGE plpgsql AS $$ BEGIN RETURN a [1] b; END; $$;The plus sign (+) adds two numbers, which is what the function should do.
Complete the code to create a procedure that inserts a new user into the users table.
CREATE PROCEDURE add_user(name VARCHAR) LANGUAGE plpgsql AS $$ BEGIN INSERT INTO users (username) VALUES ([1]); END; $$;The procedure uses the parameter 'name' to insert the username value.
Fix the error in the function that should return the length of a string.
CREATE FUNCTION str_length(s TEXT) RETURNS INT LANGUAGE plpgsql AS $$ BEGIN RETURN [1](s); END; $$;In SQL, the function to get string length is length(), not len().
Fill both blanks to create a procedure that updates a user's email by user ID.
CREATE PROCEDURE update_email(user_id INT, new_email VARCHAR) LANGUAGE plpgsql AS $$ BEGIN UPDATE users SET email = [1] WHERE id = [2]; END; $$;
The procedure uses the parameters new_email and user_id directly without quotes to update the record.
Fill all three blanks to create a function that returns the average score from a scores table for a given player.
CREATE FUNCTION avg_score(player_name VARCHAR) RETURNS NUMERIC LANGUAGE plpgsql AS $$ BEGIN RETURN (SELECT AVG([1]) FROM scores WHERE [3] = [2]); END; $$;
The function calculates the average of the score column, filtering rows where player equals the parameter player_name. The parameter is used without quotes, and the column name player is used in the WHERE clause.