# Writing SQL Queries

SQL Queries used within **Databoxes** and **Dynamic Lists** provide you with very powerful features and gives read-only access to a wealth of information that you may wish to use within your scripts. However, using all this power requires some thought and care!

<div align="center"><figure><img src="/files/x38WgX2Pa3v6Y3doHaMj" alt=""><figcaption><p>Example SQL Query Databox</p></figcaption></figure></div>

![](/files/StoXQCAxj2WnMX6DiK8m)**Always** insert a **comment** and maintain the **revision history**.&#x20;

At some point someone will need to know more about this query, what is does, it's origins, any changes, why it's broken (who broke it!) etc and having adequate comments can save a lot of head scratching.

\
![](/files/ZeDFvl9I8pT0zSxnzARV)**Never** embed a **database name** in any SQL Queries, i.e. the above query should read:

<div align="center"><figure><img src="/files/kI78NKJFDpdOceQpu7QP" alt=""><figcaption><p>Don't include the database name itself!</p></figcaption></figure></div>

Embedding the database name in the query is unnecessary and **will cause problems** when moving between environments, e.g. consider what happens when your system is copied into a Test environment and needs to access a database named **SQLActiveH\_Test**; all queries that point at **SQLActiveH** will either fail or retrieve incorrect data! You may experience other problems related to permissions. So, take heed of the following point...

![](/files/5QvQC3spj6QEaq5KWixm) Always select the Database you wish to use from the dropdown list.

If this doesn't show the required database, **please do not** insert a database name in the query, rather, contact Support and they will make the necessary arrangeents to add to the list.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keyfax.biz/product-suite/admin/best-practices/writing-sql-queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
