# QUERY Function

Canonical page: https://www.adaptabletools.com/docs/adaptable-ql-expression-advanced-query-function

- The `QUERY` Expression Function in AdapTableQL supports referencing Named Queries in Expressions
- Named Queries can return either any value, boolean, numeric, text, data etc. 

The `QUERY` Expression Function enables referencing [Named Queries](https://www.adaptabletools.com/docs/handbook-named-queries/index.md) inside Expressions.

- A Named Query is an Expression that has been named and saved into Query State for subsequent re-use
- It can resolve to any value - a Boolean, number, text or date

The referenced Query is identified by its name and it is evaluated on the fly by AdapTableQL.

`QUERY` returns whatever value the referenced Named Query resolves to. 

So a Boolean Named Query can be combined with other Boolean logic, while a scalar (e.g. numeric) Named Query can be used anywhere a value is expected - for example inside a [Calculated Column](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md).

So the Expression below could be saved as a `Named Query` called 'Big Orders'

```ts 
 [ItemCount] > 30 OR [OrderCost] > 500
```

And this Named Query can now be referenced as follows:

```ts [[1, 1, "QUERY"]]
 QUERY("Big Orders") AND [Currency] = 'USD'
```

A scalar Named Query works the same way — save a value Expression, then reference it with `QUERY` wherever a value is expected (including inside another Boolean Expression):

```ts [[1, 2, "QUERY", 0], [1, 2, "QUERY", 32]]
 // Named Query "Popularity Score" = PRODUCT([github_stars], [github_watchers], [open_issues_count])
 QUERY("Popular Frameworks") AND QUERY("Popularity Score") > 1000000000
```

- A Named Query is treated as boolean unless it declares a `ReturnType`
- A scalar Named Query must set its return type, e.g. `ReturnType: 'number'` or `ReturnType: 'text'`

```ts {5}
NamedQuery: {
  NamedQueries: [{
    Name: 'Popularity Score',
    Expression: 'PRODUCT([github_stars], [github_watchers], [open_issues_count])',
    ReturnType: 'number',
  }],
},
```

**Example: AdapTableQL: QUERY Expression Function**

Using QUERY to reference other Expressions

 

- This Example demonstrates how to use the `QUERY` Expression Function
- 2 [Named Queries](https://www.adaptabletools.com/docs/handbook-named-queries/index.md) are provided:
  - `Popular Frameworks` (boolean): `[github_watchers] > 2000 OR [github_stars] > 14500`
  - `Popularity Score` (numeric scalar): `PRODUCT([github_stars], [github_watchers], [open_issues_count])`
- These Named Queries are then referenced using the `QUERY` keyword:
  - A [Custom Report](https://www.adaptabletools.com/docs/handbook-exporting/index.md) has the Expression: `QUERY("Popular Frameworks")`
  - A [Calculated Column](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md) has the Expression: `QUERY("Popularity Score")`
  - A [Format Column Condition](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions/index.md) has the Expression: `QUERY("Popular Frameworks") AND QUERY("Popularity Score") > 1000000000 AND [has_wiki]`
  - A [Data Change Alert](https://www.adaptabletools.com/docs/handbook-alerting-data-change/index.md) has the Expression: `QUERY("Popular Frameworks") AND [language]="JavaScript"`

- Edit either Named Query (through the `Named Query` Section in [Settings Panel](https://www.adaptabletools.com/docs/ui-settings-panel/index.md)) and see how the Style / Calculated Column values change
- Change `Has Wiki` in rows with JavaScript and see the Alert appear; do the same in rows with *TypeScript* and note there is no Alert

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'QUERY Expression Function',
  initialState: {
    Dashboard: {
      ModuleButtons: ['FormatColumn', 'CalculatedColumn'],
      Tabs: [
        {
          Name: 'Expressions',
          Toolbars: ['Alert', 'Export', 'GridFilter'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    NamedQuery: {
      NamedQueries: [
        {
          Name: 'Popular Frameworks',
          Expression:
            '[github_watchers] > 2000 OR [github_stars] > 14500',
          ReturnType: 'boolean',
        },
        {
          Name: 'Popularity Score',
          Expression:
            'PRODUCT([github_stars], [github_watchers], [open_issues_count])',
          ReturnType: 'number',
        },
      ],
    },
    CalculatedColumn: {
      CalculatedColumns: [
        {
          ColumnId: 'popularity-score',
          FriendlyName: 'Popularity Score',
          Expression: 'QUERY("Popularity Score")',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
      ],
    },
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-frameworks-javascript',
          Scope: {
            All: true,
          },
          MessageType: 'Warning',
          Rule: 'QUERY("Popular Frameworks") AND [language]="JavaScript" ',
          AlertProperties: {
            DisplayNotification: true,
          },
        },
      ],
    },
    Export: {
      Reports: [
        {
          Name: 'Popular',
          ReportColumnScope: 'AllColumns',
          ReportRowScope: 'ExpressionRows',
          Expression: 'QUERY("Popular Frameworks")',
        },
      ],
      CurrentReport: 'Popular',
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'style-frameworks-wiki',
          Scope: {All: true},
          Rule: 'QUERY("Popular Frameworks") AND QUERY("Popularity Score") > 1000000000 AND [has_wiki]',
          Style: {
            ForeColor: 'var(--ab-color-palette-14)',
            BackColor: 'var(--ab-color-palette-17)',
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'language',
            'popularity-score',
            'github_watchers',
            'github_stars',
            'has_wiki',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
            'updated_at',
            'pushed_at',
            'description',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
