---
title: "SurrealQL for SQL users"
description: "What SurrealQL shares with SQL and where it differs: record IDs, schemaless and schemafull tables, NONE and NULL, links and references instead of joins, aggregates, upserts and subqueries, with tested examples."
canonical_url: "https://surrealdb.com/docs/reference/query-language/for-sql-users"
section: "SurrealQL"
surrealdb_version: "4.x"
last_modified: "2026-12-03T03:13:31Z"
---

> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# SurrealQL for SQL users

What SurrealQL shares with SQL and where it differs: record IDs, schemaless and schemafull tables, NONE and NULL, links and references instead of joins, aggregates, upserts and subqueries, with tested examples.

SurrealQL reads like SQL. `SELECT`, `WHERE`, `GROUP BY`, `ORDER BY` and `LIMIT` do what a SQL reader expects, and statements end with a semicolon. The differences come from how SurrealDB stores data: a table holds records, and a record holds fields of any type, including arrays, objects and links to other records. This page maps familiar SQL to SurrealQL, then takes each difference in turn with an example that runs as written.

Each example has a SurrealQL tab and a PostgreSQL tab with the SQL that does the same job, and the tab that is chosen stays chosen down the page. To move an existing database, see the guides for [PostgreSQL](/docs/integrate/migrating/from-other-databases/from-postgresql.md) and [MySQL](/docs/integrate/migrating/from-other-databases/from-mysql.md). To send SQL itself to SurrealDB, the [compatibility matrix](/docs/reference/protocols/sql-compatibility.md) lists what the PostgreSQL and MySQL dialects run.

## SQL and SurrealQL side by side

| SQL | SurrealQL | Difference |
| --- | --- | --- |
| `SELECT * FROM person WHERE age > 30` | `SELECT * FROM person WHERE age > 30` | Identical. |
| `LIMIT 10 OFFSET 20` | `LIMIT 10 START 20` | `START` takes the place of `OFFSET`. |
| `INSERT INTO person (name) VALUES ('Ada')` | `CREATE person SET name = 'Ada'` or `INSERT INTO person { name: 'Ada' }` | A record gets a generated ID unless the statement gives one. |
| `INSERT ... ON CONFLICT DO UPDATE` | `UPSERT person:ada SET ...` or `INSERT ... ON DUPLICATE KEY UPDATE` | See [Writing data](#writing-data). |
| `JOIN` | `author.name` through a record link, or `->` through a graph edge | See [Links and references instead of joins](#links-and-references-instead-of-joins). |
| `WHERE post.author_id = 1` to find the posts of an author | `author:ada<~post` | The field needs `REFERENCE`. |
| `COUNT(*)` | `count()` with `GROUP ALL` | `COUNT(*)` does not parse. |
| `AVG(x)`, `SUM(x)`, `MIN(x)`, `MAX(x)` | `math::mean(x)`, `math::sum(x)`, `math::min(x)`, `math::max(x)` | Used with `GROUP BY` or `GROUP ALL`. |
| `COALESCE(a, b)` | `a ?? b` | |
| `CAST(x AS int)` | `<int> x` | |
| `WITH name AS (...)` | `LET $name = ...;` | See [Subqueries](#subqueries-and-stored-results). |
| `x IS NULL` for a missing value | `x IS NONE` | See [NONE and NULL](#none-and-null). |
| `BEGIN; ... COMMIT;` | `BEGIN; ... COMMIT;` | Identical. |

## Tables and schemas

In SQL a table is created with its columns before it takes data. In SurrealQL a table is created with [`DEFINE TABLE`](/docs/reference/query-language/statements/define/table.md), or by the first record written to it, in which case SurrealDB runs the definition itself: `INFO FOR DB` then shows `DEFINE TABLE person TYPE ANY SCHEMALESS PERMISSIONS NONE`. A schemaless table takes any fields, including nested arrays and objects. A database [defined with `STRICT`](/docs/reference/query-language/statements/define/database.md) refuses to create tables this way, and every table has to be defined first.

A table that should behave like a SQL table is declared `SCHEMAFULL` with one [field](/docs/reference/query-language/statements/define/field.md) for each column. A field has a type, `ASSERT` takes the place of `CHECK`, and a field that is not defined is refused.

**SurrealQL**

```surql
DEFINE TABLE person SCHEMAFULL;
DEFINE FIELD name ON person TYPE string;
DEFINE FIELD age ON person TYPE int ASSERT $value >= 0;

CREATE person:ada SET name = 'Ada', age = 36;
//- [{ age: 36, id: person:ada, name: 'Ada' }]

CREATE person:bob SET name = 'Bob', age = '41';
//- "Couldn't coerce value for field `age` of `person:bob`: Expected `int` but found `'41'`"

CREATE person:eve SET name = 'Eve', age = -1;
//- 'Found -1 for field `age`, with record `person:eve`, but field must conform to: $value >= 0'

CREATE person:zed SET name = 'Zed', age = 29, nickname = 'z';
//- "Found field 'nickname', but no such field exists for table 'person'"
```

**PostgreSQL**

```sql
CREATE TABLE person (
    id   text PRIMARY KEY,
    name text NOT NULL,
    age  integer NOT NULL CHECK (age >= 0)
);

INSERT INTO person (id, name, age) VALUES ('ada', 'Ada', 36);

-- Accepted: the text literal is converted to 41
INSERT INTO person (id, name, age) VALUES ('bob', 'Bob', '41');

-- Refused: the CHECK constraint fails
INSERT INTO person (id, name, age) VALUES ('eve', 'Eve', -1);

-- Refused: the table has no column named nickname
INSERT INTO person (id, name, age, nickname) VALUES ('zed', 'Zed', 29, 'z');
```

A record ID has the form `table:key`, such as `person:ada`. A statement that names no key, as in `CREATE person SET name = 'Ada'`, gets a random one, so there is no auto-incrementing column. The [record IDs page](/docs/reference/query-language/language-primitives/data-types/record-ids.md) covers the other key types.

A field's type also converts a value into it where no data is lost. A `float` field takes `1` and stores `1f`, and an `int` field takes `2.0` and stores `2`, but it refuses `2.5` rather than round it.

**SurrealQL**

```surql
DEFINE TABLE reading SCHEMAFULL;
DEFINE FIELD value ON reading TYPE float;
DEFINE FIELD count ON reading TYPE int;

CREATE reading:1 SET value = 1, count = 2.0;
//- [{ count: 2, id: reading:1, value: 1f }]

CREATE reading:2 SET value = 1, count = 2.5;
//- "Couldn't coerce value for field `count` of `reading:2`: Expected `int` but found `2.5f`"
```

**PostgreSQL**

```sql
CREATE TABLE reading (id integer PRIMARY KEY, value double precision, count integer);

INSERT INTO reading VALUES (1, 1, 2.0);
-- Stored as 2

INSERT INTO reading VALUES (2, 1, 2.5);
-- Stored as 3: the value is rounded to fit the column
```

## NONE and NULL

A field that a record does not have is `NONE`, which is a different value from `NULL`, a stored empty value. A SQL query that tests a missing value with `IS NULL` therefore needs `IS NONE` in SurrealQL, and `= NULL` matches nothing here. [None and null](/docs/reference/query-language/language-primitives/data-types/none-and-null.md) describes both values, and `??` supplies a default for a missing field.

**SurrealQL**

```surql
-- Setup
CREATE person:ada SET name = 'Ada', age = 36;
CREATE person:bob SET name = 'Bob';

SELECT name FROM person WHERE age IS NONE;
//- [{ name: 'Bob' }]

SELECT name FROM person WHERE age = NULL;
//- []

SELECT name, age ?? 0 AS age FROM person ORDER BY name;
//- [{ age: 36, name: 'Ada' }, { age: 0, name: 'Bob' }]
```

**PostgreSQL**

```sql
-- Assumes person(name text, age integer), with Bob's age left NULL
SELECT name FROM person WHERE age IS NULL;

-- No rows: a comparison with NULL is never true
SELECT name FROM person WHERE age = NULL;

SELECT name, COALESCE(age, 0) AS age FROM person ORDER BY name;
```

A comparison between different types does not convert one to the other, so `1 = '1'` is `false`, and a value must be cast first. Numbers are the exception, because they compare by value whatever their type: `1 = 1.0` is `true`.

**SurrealQL**

```surql
RETURN 1 = '1';
//- false

RETURN <int> '1' = 1;
//- true

RETURN 1 = 1.0;
//- true
```

**PostgreSQL**

```sql
-- true: the text literal is converted to a number
SELECT 1 = '1';

SELECT CAST('1' AS integer) = 1;

SELECT 1 = 1.0;
```

## Links and references instead of joins

A field can hold the ID of another record, which is called a [record link](/docs/reference/query-language/language-primitives/record-links.md). A path through the field reads the linked record, so a query that would need a `JOIN` in SQL names the field instead. The path `author.name` reads one field of the author, and `author.*` reads the whole record.

**SurrealQL**

```surql
-- Setup
CREATE author:ada SET name = 'Ada';
CREATE post:1 SET title = 'Engines', author = author:ada;

SELECT title, author.name AS author FROM post;
//- [{ author: 'Ada', title: 'Engines' }]

SELECT title, author.* AS author FROM post;
//- [{ author: { id: author:ada, name: 'Ada' }, title: 'Engines' }]
```

**PostgreSQL**

```sql
-- Assumes post(id, title, author_id) and author(id, name)
SELECT p.title, a.name AS author
FROM post p
JOIN author a ON a.id = p.author_id;

SELECT p.title, to_jsonb(a) AS author
FROM post p
JOIN author a ON a.id = p.author_id;
```

A link points one way. To read it from the other side, a field defined with [`REFERENCE`](/docs/reference/query-language/language-primitives/record-references.md) also records the link on the record it points at, and `<~` follows it back. In SQL the same lookup is a `WHERE` on the foreign key of the other table. The field has to be defined before the records are written, because the references are recorded as the records are written.

**SurrealQL**

```surql
DEFINE FIELD author ON post TYPE record<author> REFERENCE;

CREATE author:ada SET name = 'Ada';
CREATE post:1 SET title = 'Engines', author = author:ada;
CREATE post:2 SET title = 'Notes', author = author:ada;

SELECT name, <~post.title AS titles FROM author:ada;
//- [{ name: 'Ada', titles: ['Engines', 'Notes'] }]
```

**PostgreSQL**

```sql
-- Assumes post(id, title, author_id) with a foreign key to author(id)
SELECT a.name, array_agg(p.title ORDER BY p.id) AS titles
FROM author a
JOIN post p ON p.author_id = a.id
WHERE a.id = 'ada'
GROUP BY a.name;
```

Many-to-many relationships use graph edges, which [`RELATE`](/docs/reference/query-language/statements/relate.md) creates and `->` follows. The PostgreSQL migration guide shows a four-table `JOIN` rewritten as one traversal.

## Nested values

A field can hold an array or an object, and a query reads inside it in place. The result keeps the nesting, so `address.city` comes back as an `address` object that holds `city`, not as a flat column. [Idioms](/docs/reference/query-language/language-primitives/idioms.md) lists the other path forms.

**SurrealQL**

```surql
-- Setup
CREATE person:ada SET name = 'Ada', tags = ['maths', 'engines'], address = { city: 'London' };
CREATE person:bob SET name = 'Bob', tags = ['rust'], address = { city: 'Paris' };

SELECT name FROM person WHERE 'rust' IN tags;
//- [{ name: 'Bob' }]

SELECT name, address.city FROM person ORDER BY name;
//- [{ address: { city: 'London' }, name: 'Ada' }, { address: { city: 'Paris' }, name: 'Bob' }]
```

**PostgreSQL**

```sql
-- Assumes person(name text, tags text[], address jsonb)
SELECT name FROM person WHERE 'rust' = ANY (tags);

-- One flat column for the value, where SurrealQL returns the nested object
SELECT name, address ->> 'city' AS city FROM person ORDER BY name;
```

## Aggregates

An aggregate function needs a [`GROUP`](/docs/reference/query-language/clauses/group.md) clause, which is `GROUP BY` for one result per group and `GROUP ALL` for one result in total. `count()` takes the place of `COUNT(*)`, and the other aggregates are in the [`math` functions](/docs/reference/query-language/functions/database-functions/math.md).

**SurrealQL**

```surql
-- Setup
CREATE product:a SET name = 'Shirt', category = 'clothing', price = 6;
CREATE product:b SET name = 'Hat', category = 'clothing', price = 10;
CREATE product:c SET name = 'Mug', category = 'kitchen', price = 4;

SELECT count() AS products, math::max(price) AS highest FROM product GROUP ALL;
//- [{ highest: 10, products: 3 }]

SELECT category, count() AS products, math::mean(price) AS average FROM product GROUP BY category ORDER BY category;
//- [{ average: 8f, category: 'clothing', products: 2 }, { average: 4f, category: 'kitchen', products: 1 }]
```

**PostgreSQL**

```sql
-- Assumes product(name, category, price)
SELECT count(*) AS products, max(price) AS highest FROM product;

SELECT category, count(*) AS products, avg(price) AS average
FROM product
GROUP BY category
ORDER BY category;
```

## Writing data

The write statements differ from SQL in what they do when the record exists or does not. [`CREATE`](/docs/reference/query-language/statements/create.md) fails if the record is already there. [`UPDATE`](/docs/reference/query-language/statements/update.md) changes records that exist and returns nothing for an ID that does not, and an `UPDATE` with no `WHERE` clause changes every record in the table. [`UPSERT`](/docs/reference/query-language/statements/upsert.md) creates the record or updates it, which is the closest match to `INSERT ... ON CONFLICT`. [`INSERT`](/docs/reference/query-language/statements/insert.md) also takes `ON DUPLICATE KEY UPDATE`, with the submitted values in `$input`.

**SurrealQL**

```surql
-- Setup
CREATE person:ada SET name = 'Ada', visits = 1;

UPDATE person:nobody SET name = 'Nobody';
//- []

UPSERT person:grace SET name = 'Grace', visits = 1;
//- [{ id: person:grace, name: 'Grace', visits: 1 }]

UPSERT person:grace SET visits += 1;
//- [{ id: person:grace, name: 'Grace', visits: 2 }]

INSERT INTO person { id: 'ada', name: 'Ada Lovelace' } ON DUPLICATE KEY UPDATE name = $input.name, visits += 1;
//- [{ id: person:ada, name: 'Ada Lovelace', visits: 2 }]
```

**PostgreSQL**

```sql
-- Assumes person(id text PRIMARY KEY, name text, visits integer), with Ada stored and visits 1
-- UPDATE 0: no row matches, and none is created
UPDATE person SET name = 'Nobody' WHERE id = 'nobody';

INSERT INTO person (id, name, visits) VALUES ('grace', 'Grace', 1)
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, visits = EXCLUDED.visits;

UPDATE person SET visits = visits + 1 WHERE id = 'grace';

INSERT INTO person (id, name) VALUES ('ada', 'Ada Lovelace')
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, visits = person.visits + 1;
```

## Subqueries and stored results

A subquery goes in parentheses as in SQL. A `WITH name AS (...)` expression does not exist in SurrealQL, because the [`WITH` clause](/docs/reference/query-language/clauses/with.md) chooses indexes. A result that a later statement reuses is stored in a parameter with [`LET`](/docs/reference/query-language/statements/let.md).

**SurrealQL**

```surql
-- Setup
CREATE person:ada SET name = 'Ada', age = 36;
CREATE person:bob SET name = 'Bob', age = 41;

LET $adults = SELECT name, age FROM person WHERE age > 40;
SELECT * FROM $adults;
//- [{ age: 41, name: 'Bob' }]
```

**PostgreSQL**

```sql
WITH adults AS (SELECT name, age FROM person WHERE age > 40)
SELECT * FROM adults;
```

## See also

- [Learn: Querying](/docs/learn/querying.md) is the guided introduction to SurrealQL.
- [`SELECT`](/docs/reference/query-language/statements/select.md) lists every clause of the query statement.
- [Record IDs](/docs/reference/query-language/language-primitives/data-types/record-ids.md), [record links](/docs/reference/query-language/language-primitives/record-links.md) and [record references](/docs/reference/query-language/language-primitives/record-references.md) cover the ideas that replace keys and joins.
- [Migrating from PostgreSQL](/docs/integrate/migrating/from-other-databases/from-postgresql.md) and [from MySQL](/docs/integrate/migrating/from-other-databases/from-mysql.md) cover moving the data.
