# Surreal Deal Store

To quickly test out SurrealDB and SurrealQL functionality, we've included demo data which you can download and import into SurrealDB.

To further test out SurrealDB and SurrealQL functionality, we've included two demo datasets here in `.surql` files which you can download and [`import`](/docs/reference/cli/surrealdb-cli/commands/import.md) into SurrealDB using the [CLI](/docs/reference/cli/surrealdb-cli/overview.md).

## Surreal Deal Store - there is a lot in store for you!

Surreal Deal Store is our new and improved demo dataset based on our [SurrealDB Store](https://surrealdb.store/).
The dataset is made up of 12 tables using both [graph relations](/docs/reference/query-language/statements/relate.md) and [record links](/docs/reference/query-language/language-primitives/record-links.md).

In the diagram below, the nodes in pink are the [standard tables](/docs/reference/query-language/statements/define/table.md), the ones in purple represent the [edge tables](/docs/reference/query-language/statements/relate.md) which shows relationships between records and SurrealDB as a graph database. The nodes in grey are the [pre-computed table views](/docs/reference/query-language/statements/define/table.md).

<img src="~/assets/img/image/light/surreal-deal-store-light.png" darkSrc="~/assets/img/image/dark/surreal-deal-store.png" alt="Surreal Deal Data Model" />

### Download

**SurrealDB 3.x**

| Dataset                                                                          | URL                                                       |
| -------------------------------------------------------------------------------- | --------------------------------------------------------- |
| [Surreal Deal Store (mini)](https://datasets.surrealdb.com/datasets/surreal-deal-store/mini-v3.surql) | https://datasets.surrealdb.com/datasets/surreal-deal-store/mini-v3.surql |

**SurrealDB 2.x**

| Dataset                                                                          | URL                                                       |
| -------------------------------------------------------------------------------- | --------------------------------------------------------- |
| [Surreal Deal Store](https://datasets.surrealdb.com/surreal-deal-store.surql)             | https://datasets.surrealdb.com/surreal-deal-store.surql      |
| [Surreal Deal Store (mini)](https://datasets.surrealdb.com/surreal-deal-store-mini.surql) | https://datasets.surrealdb.com/surreal-deal-store-mini.surql |

### Import

Once one of the datasets has been downloaded, it's now time to [start the server](/docs/reference/cli/surrealdb-cli/commands/start.md).

```bash
# Create a new in-memory server
surreal start --user root --pass secret --allow-all
```

Lastly, use the [import command](/docs/reference/cli/surrealdb-cli/commands/import.md) to add the dataset.

Use the command below to import the [surreal deal store dataset](https://datasets.surrealdb.com/surreal-deal-store.surql):

```bash
surreal import --endpoint http://localhost:8000 --user root --pass secret \
  --ns main --db main surreal-deal-store.surql
```

To import the surreal downloaded the [Surreal Deal store (mini)](https://datasets.surrealdb.com/surreal-deal-store-mini.surql) use the command below:

```bash
surreal import --endpoint http://localhost:8000 --user root --pass secret \
  --ns main --db main surreal-deal-store-mini.surql
```

Please be aware that the import process might take a few seconds.

### Using Curl

First, start the SurrealDB server:

```bash
# Create a new in-memory server
surreal start --user root --pass secret --allow-all
```

Then download the file and load it into the database:

```bash
# Download the file
curl -L "https://datasets.surrealdb.com/surreal-deal-store.surql" -o surreal-deal-store.surql

# Load the file into the database using the rest endpoint
curl -v -X POST -u "root:secret" -H "Surreal-NS: main" -H "Surreal-DB: main" -H "Accept: application/json" --data-binary @surreal-deal-store.surql http://localhost:8000/import
```

If you want to use the mini version:

```bash
# Download the file
curl -L "https://datasets.surrealdb.com/surreal-deal-store-mini.surql" -o surreal-deal-store-mini.surql

# Load the file into the database using the rest endpoint
curl -v -X POST -u "root:secret" -H "Surreal-NS: main" -H "Surreal-DB: main" -H "Accept: application/json" --data-binary @surreal-deal-store-mini.surql http://localhost:8000/import
```

### Sample queries

Here are some sample queries you can run on the Surreal Deal Store dataset. We've also included a [SurrealDB Studio Mini](https://app.surrealdb.com/mini) below to help you run these queries.

> [!NOTE]
> The query results below have been limited to 4 rows for brevity. If you remove the `LIMIT 4` clause from the queries, you'll see the full results.

```surql
-- Query 1: Using record links to select from the seller table
SELECT
  name,
  seller.name
FROM product LIMIT 4;

-- Query 2: Using graph relations to select from the person and product table
SELECT
    time.created_at as order_date,
    product_name,
    <-person.name as person_name,
    ->product.details
FROM order LIMIT 4;

-- Query 3: Conditional filtering based on an embedded object property.
SELECT
  name,
  email
FROM person
WHERE address.country ?= "England" LIMIT 4;

-- Query 4: Conditional filtering using relationships.
SELECT * FROM review
WHERE ->product.sub_category ?= "Activewear" LIMIT 4;

-- Query 5: Count orders based on order status
SELECT count() FROM order
WHERE order_status IN [ "processed", "shipped"]
GROUP ALL LIMIT 4;

-- Query 6: Get a deduplicated list of products that were ordered
SELECT
    array::distinct(product_name) as ordered_products
FROM order
GROUP ALL LIMIT 4;

-- Query 7: Get the average price per product category
SELECT
    ->product.category AS product_category,
    math::mean(price) AS avg_price
FROM order
GROUP BY product_category
ORDER BY avg_price DESC LIMIT 4;

-- Query 8: encapsulating logic in a function
RETURN fn::number_of_unfulfilled_orders();

-- Query 9: using a custom fuction for currency conversion
SELECT
    product_name,
    fn::pound_to_usd(price) AS price_usd
FROM order LIMIT 4;
```

[Run this example in SurrealDB Studio](https://app.surrealdb.com/mini?query=--+Query+1%3A+Using+record+links+to+select+from+the+seller+table+%0ASELECT%0A++name%2C%0A++seller.name%0AFROM+product+LIMIT+4%3B%0A%0A%0A--+Query+2%3A+Using+graph+relations+to+select+from+the+person+and+product+table%0ASELECT%0A++++time.created_at as order_date%2C%0A++++product_name%2C%0A++++%3C-person.name+as+person_name%2C%0A++++-%3Eproduct.details%0AFROM+order+LIMIT+4%3B%0A%0A%0A--+Query+3%3A+Conditional+filtering+based+on+an+embedded+object+property.%0ASELECT+%0A++name%2C%0A++email+%0AFROM+person+%0AWHERE+address.country+%3F%3D+%22England%22+LIMIT+4%3B%09%0A%0A%0A--+Query+4%3A+Conditional+filtering+using+relationships.%0ASELECT+*+FROM+review%0AWHERE+-%3Eproduct.sub_category+%3F%3D+%22Activewear%22+LIMIT+4%3B%0A%0A%0A--+Query+5%3A+Count+orders+based+on+order+status%0ASELECT+count%28%29+FROM+order%0AWHERE+order_status+IN+%5B+%22processed%22%2C+%22shipped%22%5D%0AGROUP+ALL+LIMIT+4%3B%0A%0A%0A--+Query+6%3A+Get+a+deduplicated+list+of+products+that+were+ordered%0ASELECT+%0A++++array%3A%3Adistinct%28product_name%29+as+ordered_products%0AFROM+order%0AGROUP+ALL+LIMIT+4%3B%0A%0A%0A--+Query+7%3A+Get+the+average+price+per+product+category%0ASELECT+%0A++++-%3Eproduct.category+AS+product_category%2C%0A++++math%3A%3Amean%28price%29+AS+avg_price%0AFROM+order%0AGROUP+BY+product_category%0AORDER+BY+avg_price+DESC+LIMIT+4%3B%0A%0A%0A--+Query+8%3A+encapsulating+logic+in+a+function%0ARETURN+fn%3A%3Anumber_of_unfulfilled_orders%28%29%3B%0A%0A%0A--+Query+9%3A+using+a+custom+fuction+for+currency+conversion%0ASELECT+%0A++++product_name%2C%0A++++fn%3A%3Apound_to_usd%28price%29+AS+price_usd%0AFROM+order+LIMIT+4%3B&dataset=surreal-deal-store&orientation=horizontal)
