---
sidebar_position: 3
---

# Select

Performs a query to retrieve data from a table.

## Methods

### selectDatabase()
&rarr; (`database` = __String__) &rarr; `this`

Selects a different database for this query.

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `database` | __String__ | Name of the database to select |

**Returns**

&rarr; `this`

***

### distinct()
&rarr; () &rarr; `this`

Adds the 'distinct' keyword to this query

- With 'distinct' only unique values are returned

**Returns**

&rarr; `this`

***

### where()
&rarr; (`string` = __String__, `values` = __Array__\<__any__\>) &rarr; `this`

Adds a where-clause to the query

- Values should be set as ? in the string and given in left-to-right order via the 'values'-array to minimize the risk of sql-injection
- If you are using joins, specify the table and column together: table.column

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `string` | __String__ | The where-clause as a string with ? representing each values. |
| `values` | __Array__\<__any__\> | Array containing values replacing the ? in the string (from left to right) |

**Returns**

&rarr; `this`

***

### having()
&rarr; (`string` = __String__, `values` = __Array__\<__any__\>) &rarr; `this`

Same as [where()](#where) but allows for aggregation.

- Values should be set as ? in the string and given in left-to-right order via the 'values'-array to minimize the risk of sql-injection
- If you are using joins, specify the table and column together: table.column

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `string` | __String__ | The having-clause with possible aggregation ? representing each values. |
| `values` | __Array__\<__any__\> | Array containing values replacing the ? in the string (from left to right) |

**Returns**

&rarr; `this`

***

### order()
&rarr; (`column` = __String__, `desc` = __Boolean__, `aggregation` = __Enum__) &rarr; `this`

Adds a new sort order.

- Can be used multiple times to order by multiple columns

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `column` | __String__ | Column to order by |
| `desc` | __Boolean__ | Sort descending? Defaults to `false` |
| `aggregation` | __Enum__ &rarr; `MIN`/`MAX`/`COUNT`/`SUM`/`AVG` | The aggregation type to use |

**Returns**

&rarr; `this`

***

### count()
&rarr; (`doParse` = __Boolean__) &rarr; `this`

Counts the number of entries of the first selected column.

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `doParse` | __Boolean__ | If `true` the query will only return a __Number__ of entries. Defaults to `false`. |

**Returns**

&rarr; `this`

***

### sum()
&rarr; (`doParse` = __Boolean__) &rarr; `this`

Sums numerical rows of the first selected column.

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `doParse` | __Boolean__ | If `true` the query will only return a __Number__ of entries. Defaults to `false`. |

**Returns**

&rarr; `this`

***

### avg()
&rarr; (`doParse` = __Boolean__) &rarr; `this`

Averages numerical rows of the first selected column.

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `doParse` | __Boolean__ | If `true` the query will only return a __Number__ of entries. Defaults to `false`. |

**Returns**

&rarr; `this`

***

### group()
&rarr; (`...columns` = __String__) &rarr; `this`

Groups rows that have the same values into summary rows.

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `...columns` | __String__ | The columns to group by |

**Returns**

&rarr; `this`

***

### join()
&rarr; (`type` = __Enum__, `table` = __String__, `onOriginalColumn` = __String__, `onJoinedColumn` = __String__, `...columns` = __String__) &rarr; `this`

Adds a new join to the query.

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `type` | __Enum__ &rarr; `LEFT`/`INNER`/`RIGHT`/`FULL OUTER` | The join type |
| `table` | __String__ | Table to join on |
| `onOriginalColumn` | __String__ | Column name on the original table to check agains |
| `onJoinedColumn` | __String__ | Column name of the join table to check against |
| `...columns` | __String__ | The columns to join. OG-columns must be set! |

**Returns**

&rarr; `this`

***

### limit()
&rarr; (`number` = __Number__, `offset` = __Number__) &rarr; `this`

Limits the query and specifies an offset to start at.

:::warning
`offset` has no default value and therefore must not be empty!
:::

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `number` | __Number__ | Limits the query by specified rows |
| `offset` | __Number__ | Offset to start at. |

**Returns**

&rarr; `this`

***

### pagination()
&rarr; (`page` = __Number__, `itemsPerPage` = __Number__) &rarr; `this`

Paginates the query.

**Parameters**

| Parameter | Type | Description |
| -- | -- | -- |
| `page` | __Number__ | The page to get (Minimum 1) |
| `itemsPerPage` | __Number__ | How many items a page should have |

**Returns**

&rarr; `this`

***

### execute()
`async` &rarr; () &rarr; __any__

Executes the prepared query.

**Returns**

&rarr; __any__ - Query result

***