Skip to main content
Expressions are the building blocks of SQL queries in Databas. You can use expressions in SELECT clauses, WHERE conditions, ORDER BY terms, and more.

Expression types

Databas supports several types of expressions:

Literals

Literal values represent constant data:
32-bit signed integers:
SELECT 42;
SELECT -17;
SELECT 0;

Identifiers

Identifiers refer to column names:
SELECT name, age, email FROM users;
SELECT price * quantity FROM order_items;

Wildcard

The * wildcard selects all columns:
SELECT * FROM users;
SELECT COUNT(*) FROM products;

Operators

Databas supports a variety of operators for building complex expressions.

Arithmetic operators

Perform mathematical calculations:
OperatorDescriptionExampleResult
+AdditionSELECT 5 + 3;8
-SubtractionSELECT 10 - 4;6
*MultiplicationSELECT 6 * 7;42
/DivisionSELECT 20 / 4;5
SELECT price + tax FROM products;
SELECT 100 + 50;

Comparison operators

Compare values and return boolean results:
OperatorDescriptionExample
==Equal toage == 18
!=Not equal tostatus != 'inactive'
<Less thanprice < 100
>Greater thanquantity > 0
<=Less than or equalage <= 65
>=Greater than or equalscore >= 90
SELECT * FROM users WHERE age >= 18;
SELECT * FROM products WHERE price < 50.00;
SELECT * FROM orders WHERE status == 'completed';
SELECT * FROM inventory WHERE quantity != 0;
Databas uses == for equality comparison (not =). This follows the convention of many programming languages.

Logical operators

Combine boolean expressions:
OperatorDescriptionExample
ANDLogical ANDage >= 18 AND active == true
ORLogical ORrole == 'admin' OR role == 'moderator'
NOTLogical NOTNOT deleted
SELECT * FROM users 
WHERE age >= 18 AND verified == true;

SELECT * FROM products 
WHERE price > 10 AND price < 100;

Unary operators

Operators that work on a single operand:
  • Negation (-): Negate a number
  • NOT: Negate a boolean
SELECT -price FROM products;
SELECT NOT active FROM users;

Operator precedence

When multiple operators appear in an expression, Databas evaluates them in a specific order:
  1. Unary operators: -, NOT (highest precedence)
  2. Multiplication and division: *, /
  3. Addition and subtraction: +, -
  4. Comparison operators: ==, !=, <, >, <=, >=
  5. Logical AND: AND
  6. Logical OR: OR (lowest precedence)

Precedence examples

-- Multiplication before addition
SELECT 2 + 3 * 4;  -- Result: 14 (evaluated as 2 + (3 * 4))

-- Comparison before AND
SELECT * FROM users WHERE age > 18 AND status == 'active';
-- Evaluated as: (age > 18) AND (status == 'active')

-- AND before OR
SELECT * FROM users WHERE role == 'admin' OR role == 'mod' AND verified == true;
-- Evaluated as: (role == 'admin') OR ((role == 'mod') AND (verified == true))

Using parentheses

You can use parentheses to override the default precedence:
-- Force addition before multiplication
SELECT (2 + 3) * 4;  -- Result: 20

-- Force OR before AND
SELECT * FROM users WHERE (role == 'admin' OR role == 'mod') AND verified == true;
-- Both admins and mods must be verified

-- Complex nested expressions
SELECT * FROM products WHERE (price > 100 OR featured == true) AND in_stock == true;

Precedence table

Here’s the complete precedence from highest to lowest:
PrecedenceOperatorsAssociativity
7- (unary), NOTRight
6-7*, /Left
5-6+, -Left
3-4==, !=, <, >, <=, >=Left
1-2AND, ORLeft
The numbers indicate binding power values used internally by the Pratt parser. Left associativity means a - b - c is evaluated as (a - b) - c.

Aggregate functions

Aggregate functions perform calculations on sets of values:

COUNT

Count the number of values:
SELECT COUNT(*) FROM users;
SELECT COUNT(email) FROM users;

SUM

Calculate the sum:
SELECT SUM(price) FROM products;
SELECT SUM(quantity * price) FROM order_items;

AVG

Calculate the average:
SELECT AVG(price) FROM products;
SELECT AVG(age) FROM users;

MIN and MAX

Find minimum and maximum values:
SELECT MIN(price) FROM products;
SELECT MAX(price) FROM products;
SELECT MIN(age), MAX(age) FROM users;

STDDEV

Calculate standard deviation:
SELECT STDDEV(price) FROM products;

Combining aggregates

You can use multiple aggregate functions in one query:
SELECT 
  COUNT(*), 
  SUM(price), 
  AVG(price), 
  STDDEV(price), 
  MAX(price), 
  MIN(price) 
FROM products;

Complex expression examples

Here are some real-world examples combining multiple operators:

Price calculations

-- Calculate total with tax
SELECT price * 1.08 FROM products;

-- Calculate discount price
SELECT price * (1 - discount / 100) FROM products;

-- Find products within budget
SELECT * FROM products WHERE price * quantity <= 1000;

Boolean logic

-- Premium users or trial users with verification
SELECT * FROM users 
WHERE premium == true OR (trial == true AND verified == true);

-- Active users not in trial
SELECT * FROM users 
WHERE active == true AND NOT trial;

-- Complex eligibility check
SELECT * FROM users 
WHERE (age >= 18 AND verified == true) OR (age >= 13 AND parent_consent == true);

Mathematical expressions

-- Calculate BMI
SELECT weight / (height * height) FROM health_records;

-- Percentage calculation
SELECT (completed / total) * 100 FROM progress;

-- Compound calculations
SELECT (base_salary + bonus) * (1 - tax_rate) FROM employees;

Expression limitations

Databas does not support:
  • String concatenation
  • Type casting or conversion
  • NULL handling with IS NULL or IS NOT NULL
  • CASE expressions
  • BETWEEN operator
  • IN operator for membership testing
  • Pattern matching with LIKE
  • Mathematical functions (SQRT, POW, etc.)

Parsing expressions

The expression parser uses the Pratt parsing algorithm with operator precedence climbing. Here’s how you can parse expressions programmatically:
use databas_sql_parser::parser::Parser;

let expr = "2 + 3 * 4";
let parser = Parser::new(expr);

match parser.expr() {
    Ok(expression) => println!("Parsed: {}", expression),
    Err(error) => eprintln!("Error: {:?}", error),
}
The parser builds an AST that respects operator precedence:
// "2 + 3 * 4" is parsed as:
// BinaryOp(
//   Literal(2),
//   Add,
//   BinaryOp(
//     Literal(3),
//     Mul,
//     Literal(4)
//   )
// )

Build docs developers (and LLMs) love