# Sample queries

Runnable SurrealQL examples for CREATE, SELECT, UPDATE and DELETE, each editable in an embedded SurrealDB Studio.

This page walks through the four statements that do most of the work in [SurrealQL](/docs/reference/query-language.md): `CREATE`, `SELECT`, `UPDATE` and `DELETE`. Every example runs against a live database in an embedded [SurrealDB Studio](/docs/explore/studio.md), so you can edit a query and run it again to see what changes.

SurrealQL's basic shape is largely equivalent to SQL, but includes additional syntax to allow conveniences such as record links, subqueries and nested field access. The examples below introduce each of those in turn.

### Creating data with CREATE

Before we can start querying data, we need to create some records. This can be done using the [CREATE statement](/docs/reference/query-language/statements/create.md), which is used to add new records to the database.

The following example demonstrates how to create a record in the `category` table, initialised with a `name` field and a `created_at` field. Press the *"Run query"* button to execute the query and view the response.

<br />

```surql
CREATE category SET
	name = 'Technology',
	created_at = time::now();
```

After executing this statement, the `category` record is created in the database, and a randomly generated unique id known as a [Record ID](/docs/reference/query-language/language-primitives/data-types/record-ids.md) is assigned to it. This ID represents the primary key of our record, and can be used to reference the record in future queries.

When creating records, you can also explicitly set the record ID. This can be useful when you are able to use predictable unique record IDs such as `company:surrealdb` or `planet:earth`. In the following example, we create a person record with the ID `john`, and set the `first`, `last`, `age`, `admin`, and `signup_at` fields.

<br />

```surql
CREATE person:john SET
	first = 'John',
	last = 'Adams',
	age = 29,
	admin = true,
	signup_at = time::now();
```

SurrealDB also supports subqueries, used in the following example to populate the `category` field of the `article` record with the ID of the `Technology` category.

<br />

```surql
CREATE article SET
	created_at = time::now(),
	author = person:john,
	title = 'Lorem ipsum dolor',
	text = 'Donec eleifend, nunc vitae commodo accumsan, mauris est fringilla.',
	category = SELECT VALUE id FROM ONLY category WHERE name = 'Technology' LIMIT 1;
```

### Querying data with SELECT

After inserting records into your database, you can now use the [SELECT statement](/docs/reference/query-language/statements/select.md) to retrieve data. While this statement will be familiar to anyone who has used traditional SQL before, SurrealDB's SELECT statement adds features drawn from NoSQL databases.

For example, in addition to selecting records from a single table, you can also select records from multiple tables, or select specific records by their Record ID.

<br />

```surql
-- Select all records from a table
SELECT * FROM article;

-- Select records from multiple tables
SELECT * FROM category, person;

-- Selecting specific records
SELECT * FROM person:john;
```

The [SELECT statement](/docs/reference/query-language/statements/select.md) can filter on fields, resolve the contents of a record link, and reach data through a Record ID directly, with no JOIN planning or indexes needed.

The following query combines a number of such features:
- **Filtering**: Use the `WHERE` clause to only include records where the author's age is less than 30.
- **Fetching**: Use the `.*` idiom to replace record ids with their actual field values.
- **Specific fields**: Only want to retrieve the title and author fields from the article table.
- **Record links**: Structure the field data from the author in a preferred format, including an alias for the field `name.full`.

```surql
SELECT
	title,
	category.*,
	author.{
		age,
		name: name.full,
	}
FROM article
WHERE author.age < 30;
```

### Modifying data with UPDATE

Records can be updated using the [UPDATE](/docs/reference/query-language/statements/update.md) statement, which allows you to modify the contents of existing records.

Much like the `SELECT` statement, you can pass both table names and individual record IDs to the `UPDATE` statement. This allows you to update specific records, or update multiple records at once.

```surql
UPDATE person:john SET
	age += 1,
	admin = false;
```

The `UPDATE` statement offers a variety of features to further filter down records, and apply different update strategies. The following example demonstrates how we can merge new data into records matching a specific condition.

<br />

```surql
UPDATE person MERGE {
	age: 30,
	admin: false
}
```

In addition to the `UPDATE` statement, SurrealDB also offers an [UPSERT statement](/docs/reference/query-language/statements/upsert.md), which has the added functionality of creating a record if it does not already exist. This can be useful when you want to update a record if it exists, or create it if it does not.

### Deleting data with DELETE

You can also delete records from your database using the [DELETE statement](/docs/reference/query-language/statements/delete.md). This statement allows you to remove records from your database, either by specifying the record ID, or by using specific conditions.

The following example demonstrates the use of the `RETURN` clause, which instructs SurrealDB to return the records before they are deleted.

<br />

```surql
DELETE article WHERE author.name.first = 'David' RETURN BEFORE;
```

## Where next

- [Writing SurrealQL](/docs/learn/querying/surrealql/writing-surrealql.md) for the syntax rules behind these examples.
- [Statements and values](/docs/learn/querying/surrealql/statements-and-values.md) for choosing between `CREATE`, `INSERT`, `UPDATE`, `UPSERT` and `RELATE`.
- [Executing queries](/docs/learn/querying/surrealql/executing-queries.md) for running the same statements from the CLI, an SDK or over HTTP.
- [Transactions](/docs/learn/querying/concepts-and-guides/transactions.md) for grouping statements that must succeed or fail together.
