# Lesson 6.4: Executing queries

## [](#_data_read_queries)Data read queries

Now that we know how to instantiate driver objects and open transactions, we can begin running queries. We’ll begin with the following Fetch query.

```typeql
match $book isa book;
fetch { "title": $book.title, "page-count": $book.page-count };
```

Let’s run this query within a **read transaction** since we’re only reading data.

```python
from typedb.driver import TypeDB, TransactionType, Credentials, DriverOptions, DriverTlsConfig

DB = "bookstore"
address = "localhost:1729"
credentials = Credentials("admin", "password")
options = DriverOptions(DriverTlsConfig.enabled_with_native_root_ca())

# Note: options can be omitted if you want to use the default options of True/None
with TypeDB.driver(address, credentials, options) as driver:
    with driver.transaction(DB, TransactionType.READ) as tx:
        docs = tx.query(
            """
            match $book isa book;
            fetch { "title": $book.title, "page-count": $book.page-count };
            """
        ).resolve().as_concept_documents()
        for doc in docs:
            print(doc)
```

You should see the results printed out in the same JSON format. The `resolve()` call is ensuring the query promise is finished, and we receive any relevant errors, or the answers.

We will examine query response type, and explain the `resolve()` and `as_concept_documents()` method call further, in [Lesson 6.5](../6.5-processing-results/index.md).

## [](#_data_write_queries)Data write queries

To run queries that do writes, we should use a **write** transaction.

```python
with TypeDB.driver(address, credentials, options) as driver:
    with driver.transaction(DB, TransactionType.WRITE) as tx:
        tx.query(
            """
            insert $new_user isa user,
                   has id "u0014",
                   has name "Jaiden Hurst",
                   has birth-date 1950-03-03;
            """
        ).resolve()
        # we don't care about the answers, so we don't have read them back out
        tx.commit()
```

Note that if we don’t care about the answers to our query and are confident we won’t get any errors, we don’t even need to resolve the query - we can just submit it and the transaction commit will wait until all queries are completed.

Queries submitted, even without resolving, are submitted in order on the server side:

```python
with TypeDB.driver(address, credentials, options) as driver:
    with driver.transaction(DB, TransactionType.WRITE) as tx:
        # make two changes in same transaction, in order of submission
        tx.query(
            """
            match $retracted_review isa review, has id "r0001";
                  $rel links ($retracted_review);
            delete $rel;
            """
        )
        tx.query(
            """
            match $retracted_review isa review, has id "r0001";
            delete $retracted_review;
            """
        )
        tx.commit()
```

## [](#_schema_write_queries)Schema write queries

To run Define queries, we should use a **schema transaction**. As with all previous queries, we issue the query with a method of the transaction’s `query` method. In the following code snippet, we create a new database for a simplified bookstore and define a rudimentary schema.

```python
DB = "bookstore-2"
with TypeDB.driver(address, credentials, options) as driver:
    try:
        driver.databases.create(DB)
    except Exception:
        pass
    with driver.transaction(DB, TransactionType.SCHEMA) as tx:
        tx.query("""
            define
            entity book @abstract,
              owns price,
              plays order-line:item;

            entity hardback,
              sub book,
              owns stock;

            entity paperback,
              sub book,
              owns stock;

            entity ebook sub book;

            entity order,
              owns id @key,
              owns status,
              plays order-line:order;

            relation order-line,
                relates order,
                relates item,
                owns quantity,
                owns price;

            attribute id, value string;
            attribute price, value double;
            attribute quantity, value integer;
            attribute stock, value integer;
            attribute status, value string @values("paid", "dispatched", "delivered",             "returned", "canceled");
        """).resolve()
        tx.commit()
```

As when writing data, we must **commit** the transaction when defining schemas in order for the changes to be persisted.

[Transactions](../6.3-transactions/index.md) [Processing results](../6.5-processing-results/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/academy/modules/ROOT/pages/6-building-applications/6.4-executing-queries.adoc) Edit this page on GitHub.