Create Function in Snowflake: SQL & Python UDFs

Formulas & functions · Updated November 23, 2026

To create a function in Snowflake, you use the CREATE FUNCTION command, which defines a user-defined function (UDF) that can be written in SQL, JavaScript, Python, Java, or Scala. The simplest form is a SQL UDF: CREATE OR REPLACE FUNCTION my_func(x NUMBER) RETURNS NUMBER AS 'x * 2';. This article explains the syntax, types, and best practices for creating and using functions in Snowflake.

Background: What Are Snowflake Functions?

Snowflake supports two broad categories of functions: built-in functions (like SUM, DATEADD, or TO_VARCHAR) and user-defined functions (UDFs). UDFs let you encapsulate custom logic that can be reused across queries, similar to stored procedures but returning a single value (scalar) or a table (tabular). They are stored in a schema and can be called like any built-in function within SQL statements.

UDFs are particularly useful for standardizing business calculations (e.g., tax rates, date formatting) or for extending Snowflake’s capabilities with custom algorithms. Unlike stored procedures, UDFs cannot contain transaction control statements (like COMMIT), but they can be used in SELECT, WHERE, and other query clauses.

Types of Functions You Can Create

Snowflake offers several types of UDFs, each suited to different needs:

  • Scalar SQL UDFs: Return a single value. Written in SQL, they are simple and efficient for basic transformations.
  • Tabular SQL UDFs: Return a set of rows (a table). Can be used in the FROM clause.
  • External Functions: Call an external HTTP endpoint (e.g., a REST API) via a proxy service. Useful for integrating with external systems.
  • JavaScript UDFs: Written in JavaScript, useful for complex logic that SQL cannot easily express.
  • Python UDFs: Written in Python, popular for data science and machine learning logic. Requires a Snowpark library.
  • Java/Scala UDFs: For advanced use cases, often using Snowpark.

How to Create a SQL UDF: Step-by-Step

Creating a SQL UDF is straightforward. Here is the basic syntax:

CREATE OR REPLACE FUNCTION function_name (param1 TYPE, param2 TYPE) RETURNS return_type AS 'SELECT expression'

For example, to create a function that calculates the sales tax at 8%:

CREATE OR REPLACE FUNCTION sales_tax(amount NUMBER) RETURNS NUMBER AS 'amount * 0.08'

Then call it in a query:

SELECT sales_tax(100) AS tax;

This returns 8.00.

For a tabular UDF, you specify RETURNS TABLE and include a full SELECT statement. Example:

CREATE OR REPLACE FUNCTION get_high_sales(min_amount NUMBER) RETURNS TABLE (order_id NUMBER, total NUMBER) AS 'SELECT order_id, total FROM orders WHERE total > min_amount'

Then use it in the FROM clause:

SELECT * FROM TABLE(get_high_sales(1000));

Creating a Python UDF (Snowpark)

Python UDFs require the Snowpark library. They are created using the CREATE FUNCTION command with the LANGUAGE PYTHON clause. Here is an example that squares a number:

CREATE OR REPLACE FUNCTION square_py(x NUMBER) RETURNS NUMBER LANGUAGE PYTHON RUNTIME_VERSION = '3.8' HANDLER = 'square' AS 'def square(x): return x * x'

Note that the HANDLER specifies the function name within the Python code. For more complex dependencies, you can use PACKAGES to include libraries like pandas or numpy.

Python UDFs are executed on separate compute resources (Snowpark containers), so they can handle heavier workloads. However, they incur additional cost compared to SQL UDFs.

Choosing Between SQL, JavaScript, and Python

Your choice depends on the complexity and performance needs:

  • SQL UDFs: Best for simple logic that can be expressed in a single SQL expression. They are optimized by Snowflake and have minimal overhead.
  • JavaScript UDFs: Good for procedural logic (loops, conditionals) without external libraries. They run in a sandboxed JavaScript engine.
  • Python UDFs: Ideal for advanced analytics, using libraries like pandas, scikit-learn, or numpy. They are slower to start but more flexible.

As a rule of thumb, if you can write it in SQL, do so—it will be faster and cheaper. Reserve Python for tasks that require libraries or complex algorithmic processing.

Common Mistakes and Troubleshooting

Here are frequent issues users encounter:

  • Missing semicolon in SQL body: In SQL UDFs, the body must be a single expression or a SELECT statement. If you use a multi-statement body, it fails.
  • Data type mismatches: The RETURNS type must match the actual returned value. For example, if you return a decimal, use NUMBER or FLOAT.
  • Python handler not found: Ensure the HANDLER name matches the function defined in the code block.
  • Using UDFs in WHERE clauses with large data: SQL UDFs are inlined, but Python UDFs are not; they may cause performance issues if used on large datasets without filtering.

To debug, use SHOW FUNCTIONS to list all UDFs, and DESCRIBE FUNCTION to see details. Also, test the UDF with a simple SELECT before using it in complex queries.

Managing and Securing Functions

Functions are schema-level objects. You can alter or drop them with ALTER FUNCTION and DROP FUNCTION. To manage permissions, use GRANT USAGE ON FUNCTION to allow other roles to execute them. For example:

GRANT USAGE ON FUNCTION sales_tax(NUMBER) TO ROLE analyst;

You can also set a comment on a function for documentation:

COMMENT ON FUNCTION sales_tax(NUMBER) IS 'Calculates 8% sales tax';

When creating functions, follow these best practices:

  • Use CREATE OR REPLACE to avoid errors on re-creation.
  • Keep function logic simple and testable.
  • Use meaningful names that indicate the function’s purpose.
  • Limit the use of Python UDFs to avoid unnecessary cost.

Frequently Asked Questions

Can I create a function that returns a table?

Yes, use RETURNS TABLE and define the column names and types. Then call it with TABLE(function_name(...)) in the FROM clause.

What is the difference between a UDF and a stored procedure?

A UDF returns a value (scalar) or a table and is used in queries. A stored procedure can perform DML operations (INSERT, UPDATE, DELETE) and manage transactions, but cannot be used directly in a SELECT statement.

Can I use a UDF in a WHERE clause?

Yes, but be cautious with Python UDFs because they may not be inlined and could slow down queries. SQL UDFs are usually inlined and work fine.

How do I pass multiple parameters to a UDF?

Simply list them in the function definition, separated by commas. For example: CREATE FUNCTION add(a NUMBER, b NUMBER) RETURNS NUMBER AS 'a + b'.

Are UDFs cost-effective?

SQL UDFs are essentially free (they are inlined). Python UDFs incur compute costs because they run on separate Snowpark containers. If you use Python UDFs frequently, consider optimizing them or using SQL alternatives.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.