> For the complete documentation index, see [llms.txt](https://qb.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qb.ortusbooks.com/query-builder/building-queries/raw-expressions.md).

# Raw Expressions

Raw expressions are the qb escape hatch. While qb strives to provide ways to execute the majority of queries, you will occasionally need to provide raw sql values that are not processed by qb. These SQL snippets are called `raw` or `Expressions` in qb.

{% hint style="warning" %}
`raw` expressions are useful, but shoud be used only if there is not another way to accomplish the same action using other qb methods. This is because a `raw` expression has the potential to use syntax specific to one database grammar or another, preventing you from easily switching from one grammar to another, one of the major benefits of using qb.
{% endhint %}

The first way to retrieve an `Expression` is to call the `raw` method on the `QueryBuilder` object.

## raw

| Name | Type   | Required | Default | Description                              |
| ---- | ------ | -------- | ------- | ---------------------------------------- |
| sql  | string | true     |         | The raw sql to wrap up in an Expression. |

The sql snippet passed to `raw` is not processed by qb at all. With that in mind, it is important to follow all best practices and security recommendations with the sql you use with `raw`.

{% code title="QueryBuilder" %}

```javascript
query.from( "users" ).select( query.raw( "MAX(created_date)" ) );
```

{% endcode %}

{% code title="MySQL" %}

```sql
SELECT MAX(created_date) FROM `users`
```

{% endcode %}

Expressions can be passed to most qb methods, like `select`, `from`, `where`, or `orderBy`, among others. Additionally, qb provides some convenience methods to add raw values in different parts of the query:

* [selectRaw](/query-builder/building-queries/selects.md#get-3)
* [fromRaw](/query-builder/building-queries/from.md#get-2)
* [joinRaw](/query-builder/building-queries/joins.md#joinraw)
* [leftJoinRaw](/query-builder/building-queries/joins.md#leftjoinraw)
* [rightJoinRaw](/query-builder/building-queries/joins.md#rightjoinraw)
* [crossJoinRaw](/query-builder/building-queries/joins.md#crossjoinraw)
* [whereRaw](/query-builder/building-queries/wheres.md#whereraw)
* forRaw


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://qb.ortusbooks.com/query-builder/building-queries/raw-expressions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
