Back to top
Documentation SurrealQL Statements IFELSE statement

IF ELSE statement

The IF ELSE statement can be used as a main statement, or within a parent statement, to return a value depending on whether a condition, or a series of conditions match. The statement allows for multiple ELSE IF expressions, and a final ELSE expression, with no limit to the number of ELSE IF conditional expressions.

Statement syntax

IF @condition [ THEN ]
	@expression
[ ELSE IF @condition [ THEN ]
	@expression ... ]
[ ELSE
	@expression ]
[ END ]

Example usage

The following query shows example usage of this statement.

IF $scope = "admin" THEN
	( SELECT * FROM account )
ELSE IF $scope = "user" THEN
	( SELECT * FROM $auth.account )
ELSE
	[]
END

If-else statements can also be used as subqueries within other statements.

UPDATE person SET railcard =
	IF age <= 10 THEN
		'junior'
	ELSE IF age <= 21 THEN
		'student'
	ELSE IF age >= 65 THEN
		'senior'
	ELSE
		NULL
	END
;