SQUARE(number) returns the number times itself: SQUARE(5) is 25, and a negative becomes positive. DuckDB and BigQuery have no SQUARE function, so write x * x or pow(x, 2); Snowflake has SQUARE(). It often sits inside aggregates (variance, root mean square), where AVG(SQUARE(x)) and SQUARE(AVG(x)) are different numbers.
Updated · Checked on the xmetric engine
What is the syntax?
SQUARE(number)
number
a number or a numeric expression.
Returns a number: the input times itself. Null if the input is null.
What does it return, and what is the SQL?
Each example runs on the same 8 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
What is each value squared?
Tableau
SQUARE([Value])
SQL (DuckDB)
SELECT Value * Value AS result
FROM input
ORDERBY id
2.5 and -2.5 both square to 6.25; 16 squares to 256.
Value
Result
2.5
6.25
−2.5
6.25
0.13
0.02
−0.4
0.16
3.75
14.06
null
0
0
16
256
✓ The Tableau formula and the SQL return the same 8 rows.
What is the average squared value per region?
Tableau
AVG(SQUARE([Value]))
SQL (DuckDB)
SELECT Region, avg(Value * Value) AS result
FROM input
GROUPBY Region
ORDERBY Region
East averages 6.25, 6.25 and 0.0156: 4.17.
Region
Result
East
4.17
North
128
West
7.11
✓ The Tableau formula and the SQL return the same 3 rows.
Where does a straight translation go wrong?
Common mistake
Why is the square of the average smaller than the average of the squares?
Squaring the average lets East's 2.5 and -2.5 cancel before squaring, giving 0.0017 instead of 4.17. The difference between the two is the variance, so keep SQUARE inside AVG as Tableau has it.
Tableau
AVG(SQUARE([Value]))
Tempting SQL
SELECT Region, pow(avg(Value), 2) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, avg(Value * Value) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
4.17
0
4.17
North
128
64
128
West
7.11
2.81
7.11
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECTSQUARE(Value) AS result
FROM input
ORDERBY id