Home » Oracle SQL » Operator Precedence

Operator Precedence

BODMAS

Bodmas is an acronym for Brackets, Orders, Division, Multiplication, Addition and Subtraction. Any arithmetic operation in SQL follows this fundamental maths logic.

We use this rule to decide, which mathematical operator should be given precedence first while doing an arithmetic calculation involving multiple arithmetic operators.

B Brackets first
O Orders (i.e. Powers and Square Roots, etc.)
DM Division and Multiplication (left-to-right)
AS Addition and Subtraction (left-to-right)

Divide and Multiply rank equally (and go left to right).

Add and Subtract rank equally (and go left to right)

Bodmas Examples:

Example 1:

SELECT 8 ÷ 2 + 6 × 4 FROM dual;

Result:

Example 1 Operator Precedence

We need to do division and multiplication first, but we have one of each.

Start from the left and work across to the right, which means that
you start with 8 ÷ 2 = 4. Then do the multiplication, 6 × 4 = 24.

The final result we get from this is 24 + 4 = 28.

Example 2:

SELECT 2 + 10 × (20 ÷ 5) FROM dual;

Result:

Example 2 Operator Precedence

Similarly in this example, Oracle looks at the brackets first.

So we get, 2 + 10 × 4.

Then finally , 2 + 40 = 42