# Example Queries


``` python
import ibis
from strawberry.utils.str_converters import to_camel_case

from graphique import GraphQL


def execute(query):
    result = app.schema.execute_sync(query, root_value=app.root_value)
    for error in result.errors or []:
        raise ValueError(error)
    return result.data


table = ibis.read_parquet("../../tests/fixtures/zipcodes.parquet")
# example projection: camel-cased fields (not relevant in this dataset)
_ = table.select({to_camel_case(name): table[name] for name in table.columns})
app = GraphQL(table)
```


## View schema.


``` python
execute("""{
  count
  schema {
    names
    types
    partitioning
  }
}""")
```


    {'count': 41700,
     'schema': {'names': ['latitude',
       'longitude',
       'state',
       'city',
       'county',
       'zipcode'],
      'types': ['float64', 'float64', 'string', 'string', 'string', 'int32'],
      'partitioning': []}}


## California counties with the most cities.

- [filter](../reference/Dataset.md#graphique.Dataset.filter) state by "CA"
- [group](../reference/Dataset.md#graphique.Dataset.group) by county
  - aggregate distinct cities
- [project](../reference/Dataset.md#graphique.Dataset.project) city count
- [order](../reference/Dataset.md#graphique.Dataset.order) by city counts descending
- access [columns](../reference/Dataset.md#graphique.Dataset.columns)
  - `county` is still known in the schema
  - cities is a new [column](../reference/Dataset.md#graphique.Dataset.column) accessed through an inline fragment


``` python
execute("""{
  filter(state: {eq: "CA"}) {
    group(by: "county", aggregate: {collect: {name: "city", distinct: true}}) {
      project(columns: {alias: "cities", array: {length: {name: "city"}}}) {
        order(by: "-cities", limit: 5) {
          columns {
            county {
              values
            }
          }
          cities: column(name: "cities") {
            ... on BigIntColumn {
              values
            }
          }
        }
      }
    }
  }
}""")
```


    {'filter': {'group': {'project': {'order': {'columns': {'county': {'values': ['Los Angeles',
            'San Bernardino',
            'San Diego',
            'Orange',
            'Riverside']}},
         'cities': {'values': [132, 73, 52, 47, 42]}}}}}}


## States with cities which match the name of their county.

- [filter](../reference/Dataset.md#graphique.Dataset.filter) using `where` expression, because comparing two columns is not a "simple" query
- `Column.distinct` instead of [group](../reference/Dataset.md#graphique.Dataset.group), because no other aggregates are needed


``` python
execute("""{
  filter(where: {eq: [{name: "county"}, {name: "city"}]}) {
    columns {
      state {
        distinct {
          values
        }
      }
    }
  }
}""")
```


    {'filter': {'columns': {'state': {'distinct': {'values': ['VA',
          'OH',
          'IN',
          'WI',
          'MO',
          'NE',
          'LA',
          'NM',
          'HI',
          'OR',
          'VT',
          'WV',
          'NC',
          'FL',
          'MN',
          'KS',
          'AR',
          'OK',
          'TX',
          'ID',
          'AK',
          'PR',
          'MA',
          'RI',
          'NH',
          'ME',
          'CT',
          'MD',
          'KY',
          'SD',
          'CO',
          'UT',
          'AZ',
          'NV',
          'CA',
          'WA',
          'NJ',
          'NY',
          'PA',
          'DE',
          'SC',
          'AL',
          'TN',
          'MS',
          'MI',
          'IA',
          'ND',
          'MT',
          'IL',
          'WY']}}}}}


## States which have no cities which match the name of their county.

The opposite of the previous example. Filtering rows would drop needed data; the "zeros" have to be counted.

- [project](../reference/Dataset.md#graphique.Dataset.project) column matching names instead of filtering
- [group](../reference/Dataset.md#graphique.Dataset.group) by state
  - aggregate whether there are [any](../reference/Dataset.md#graphique.Dataset.any) matches
- [filter](../reference/Dataset.md#graphique.Dataset.filter) for no matches
- access column


``` python
execute("""{
  project(columns: {alias: "match", eq: [{name: "county"}, {name: "city"}]}) {
    group(by: "state", aggregate: {any: {name: "match"}}) {
      filter(where: {inv: {name: "match"}}) {
        columns {
          state {
            values
          }
        }
      }
    }
  }
}""")
```


    {'project': {'group': {'filter': {'columns': {'state': {'values': ['GA',
           'DC']}}}}}}
