SQL differences

The Caret Operator Is a Power on PostgreSQL and a Bitwise XOR on MySQL

A growth formula written as rate ^ years works on PostgreSQL. On MySQL and MariaDB it still runs and gives numbers that have nothing to do with growth: 2 ^ 3 is 1 there.

PostgreSQL raises the left side to the power of the right one. MySQL and MariaDB combine the bits of both numbers. SQLite has no ^ operator.

SELECT 2 ^ 3, run on four engines: SQLite 3.45.1 error: unrecognized token: "^"; PostgreSQL 16.15 8; MySQL 8.0.46 1; MariaDB 10.11.14 1.

What Each Engine Answered

The example

SELECT 2 ^ 3;

gave these answers:

Engine Answer
SQLite 3.45.1 error: unrecognized token: "^"
PostgreSQL 16.15 8
MySQL 8.0.46 1
MariaDB 10.11.14 1

The SQLite answers assume a build with math functions.

What to Write Instead

This form answers the same on every engine:

SELECT POWER(2, 3);
Engine Answer
SQLite 3.45.1 8.0
PostgreSQL 16.15 8
MySQL 8.0.46 8
MariaDB 10.11.14 8

Use POWER(a, b), which all four engines have (SQLite when it is built with its math functions).

What BareSQL Portable Reports

Saved as example.sql, the example gets this from baresql lint --all:

$ baresql lint --all example.sql
example.sql:1:8: error: 2 ^ 3 is 8 on PostgreSQL, where ^ is a power, 1 on MySQL and MariaDB, where it is a bitwise exclusive or, and an error on SQLite. [caret-operator]
    2 ^ 3
    Use POWER(a, b), which all four engines have (SQLite when it is built with its math functions).

1 problem: 1 error.

baresql rules caret-operator prints the same answers in the terminal, and the BareSQL Portable page lists every difference BareSQL knows.