CodeBlanks Logo Icon CodeBlanks

Summary - Function

Compute tax with CREATE FUNCTION.

Summary - Function

A function is SQL code that returns a value. Created with CREATE FUNCTION, called in SELECT.

CREATE FUNCTION get_tax(price DECIMAL)
RETURNS DECIMAL
AS $$
  SELECT price * 0.18;
$$;

Summary function.

The function get_tax is created. The expression price * 0.18 is evaluated and returned.

Practice Summary - Function

Summary: create the get_tax function.

Starter code

query.sql
-- summary function
CREATE

Sample solution

query.sql
CREATE FUNCTION get_tax(price DECIMAL)
RETURNS DECIMAL
AS $$
  SELECT price * 0.18;
$$;

Expected output

Function oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a function return?

  • A table
  • An index
  • A value Correct answer
  • A view

Explanation: A function returns a value.

2 Summary - Function: match the terms

Term Meaning
function returns value
CREATE FUNCTION create
RETURNS type
SELECT call

All lessons in Advanced