# Youtube 2020

### SELECT Statements <a href="#default" id="default"></a>

A SELECT statement can consist of the following basic clauses.

* SELECT
* INTO
* FROM
* JOIN
* WHERE
* GROUP BY
* HAVING
* UNION
* ORDER BY
* LIMIT

### SELECT Syntax

The following syntax diagram outlines the syntax supported by the SQL engine of the provider:<br>

\| <p><code>SELECT</code> <code>{</code></p><p>  <code>\[ TOP</code> <code>\<numeric\_literal> | DISTINCT</code> <code>]</code></p><p>  <code>{</code></p><p>    <code>*</code></p><p>    <code>| {</code></p><p>        <code>\<expression> \[ \[ AS</code> <code>] \<column\_reference> ]</code></p><p>        <code>| { \<table\_name> | \<correlation\_name> } .*</code></p><p>      <code>} \[ , ... ]</code></p><p>  <code>}</code></p><p>  <code>\[ INTO</code> <code>csv:// \[ filename= ] \<file\_path> \[ ;delimiter=tab ] ]</code></p><p>  <code>{</code></p><p>    <code>FROM</code> <code>\<table\_reference> \[ \[ AS</code> <code>] \<identifier> ]</code></p><p>  <code>} \[ , ... ]</code></p><p>  <code>\[ \[</code> </p><p>      <code>INNER</code> <code>| { { LEFT</code> <code>| RIGHT</code> <code>| FULL</code> <code>} \[ OUTER</code> <code>] }</code></p><p>    <code>] JOIN</code> <code>\<table\_reference> \[ ON</code> <code>\<search\_condition> ] \[ \[ AS</code> <code>] \<identifier> ]</code></p><p>  <code>] \[ ... ]</code></p><p>  <code>\[ WHERE</code> <code>\<search\_condition> ]</code></p><p>  <code>\[ GROUP</code> <code>BY</code> <code>\<column\_reference> \[ , ... ]</code></p><p>  <code>\[ HAVING</code> <code>\<search\_condition> ]</code></p><p>  <code>\[ UNION</code> <code>\[ ALL</code> <code>] \<select\_statement> ]</code></p><p>  <code>\[</code></p><p>    <code>ORDER</code> <code>BY</code></p><p>    <code>\<column\_reference> \[ ASC</code> <code>| DESC</code> <code>] \[ NULLS FIRST</code> <code>| NULLS LAST</code> <code>]</code></p><p>  <code>]</code></p><p>  <code>\[</code></p><p>    <code>LIMIT \<expression></code></p><p>    <code>\[</code></p><p>      <code>{ OFFSET | , }</code></p><p>      <code>\<expression></code></p><p>    <code>]</code></p><p>  <code>]</code></p><p><code>} | SCOPE\_IDENTITY()</code></p><p> </p><p><code>\<expression> ::=</code></p><p>  <code>| \<column\_reference></code></p><p>  <code>| @ \<parameter></code></p><p>  <code>| ?</code></p><p>  <code>| COUNT( \* | { \[ DISTINCT</code> <code>] \<expression> } )</code></p><p>  <code>| { AVG</code> <code>| MAX</code> <code>| MIN</code> <code>| SUM</code> <code>| COUNT</code> <code>} ( \<expression> )</code></p><p>  <code>| NULLIF</code> <code>( \<expression> , \<expression> )</code></p><p>  <code>| COALESCE</code> <code>( \<expression> , ... )</code></p><p>  <code>| CASE</code> <code>\<expression></code></p><p>      <code>WHEN</code> <code>{ \<expression> | \<search\_condition> } THEN</code> <code>{ \<expression> | NULL</code> <code>} \[ ... ]</code></p><p>    <code>\[ ELSE</code> <code>{ \<expression> | NULL</code> <code>} ]</code></p><p>    <code>END</code></p><p>  <code>| \<literal></code></p><p>  <code>| \<sql\_function></code></p><p> </p><p><code>\<search\_condition> ::=</code></p><p>  <code>{</code></p><p>    <code>\<expression> { = | > | < | >= | <= | <> | != | LIKE</code> <code>| NOT</code> <code>LIKE</code> <code>| IN</code> <code>| NOT</code> <code>IN</code> <code>| IS</code> <code>NULL</code> <code>| IS</code> <code>NOT</code> <code>NULL</code> <code>| AND</code> <code>| OR</code> <code>| CONTAINS</code> <code>| BETWEEN</code> <code>} \[ \<expression> ]</code></p><p>  <code>} \[ { AND</code> <code>| OR</code> <code>} ... ]</code></p> |
\| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Examples

1. Return all columns:<br>

   | `SELECT * FROM Videos` |
   | ---------------------- |
2. Rename a column:<br>

   | `SELECT [ViewCount] AS MY_ViewCount FROM Videos` |
   | ------------------------------------------------ |
3. Cast a column's data as a different data type:<br>

   | `SELECT CAST(ViewCount AS VARCHAR) AS Str_ViewCount FROM Videos` |
   | ---------------------------------------------------------------- |
4. Search data:<br>

   | `SELECT * FROM Videos WHERE Id = 'MyId';` |
   | ----------------------------------------- |
5. Return the number of items matching the query criteria:<br>

   | `SELECT COUNT(*) AS MyCount FROM Videos` |
   | ---------------------------------------- |
6. Return the number of unique items matching the query criteria:<br>

   | `SELECT COUNT(DISTINCT ViewCount) FROM Videos` |
   | ---------------------------------------------- |
7. Return the unique items matching the query criteria:<br>

   | `SELECT DISTINCT ViewCount FROM Videos` |
   | --------------------------------------- |
8. Summarize data:<br>

   | `SELECT ViewCount, MAX(ViewCount) FROM Videos GROUP BY ViewCount` |
   | ----------------------------------------------------------------- |

   See Aggregate Functions below for details.
9. Retrieve data from multiple tables.<br>

   | `SELECT Channels.Title, Playlists.Title FROM Channels, Playlists WHERE Channels.Id = Playlists.ChannelId` |
   | --------------------------------------------------------------------------------------------------------- |

   See JOIN Queries below for details.
10. Sort a result set in ascending order:<br>

    | `SELECT Id, ViewCount FROM Videos  ORDER BY ViewCount ASC` |
    | ---------------------------------------------------------- |
11. Restrict a result set to the specified number of rows:<br>

    | `SELECT Id, ViewCount FROM Videos LIMIT 10` |
    | ------------------------------------------- |
12. Parameterize a query to pass in inputs at execution time. This enables you to create prepared statements and mitigate SQL injection attacks.<br>

    | `SELECT * FROM Videos WHERE Id = @param` |
    | ---------------------------------------- |

### Aggregate Functions <a href="#default" id="default"></a>

#### COUNT <a href="#count" id="count"></a>

Returns the number of rows matching the query criteria.<br>

| `SELECT COUNT(*) FROM Videos WHERE Id = 'MyId'` |
| ----------------------------------------------- |

#### COUNT(DISTINCT) <a href="#countdistinct" id="countdistinct"></a>

Returns the number of distinct, non-null field values matching the query criteria.<br>

| `SELECT COUNT(DISTINCT Id) AS DistinctValues FROM Videos WHERE Id = 'MyId'` |
| --------------------------------------------------------------------------- |

#### AVG <a href="#avg" id="avg"></a>

Returns the average of the column values.<br>

| `SELECT ViewCount, AVG(ViewCount) FROM Videos WHERE Id = 'MyId'`  `GROUP BY ViewCount` |
| -------------------------------------------------------------------------------------- |

#### MIN <a href="#min" id="min"></a>

Returns the minimum column value.<br>

| `SELECT MIN(ViewCount), ViewCount FROM Videos WHERE Id = 'MyId'` `GROUP BY ViewCount` |
| ------------------------------------------------------------------------------------- |

#### MAX <a href="#max" id="max"></a>

Returns the maximum column value.<br>

| `SELECT ViewCount, MAX(ViewCount) FROM Videos WHERE Id = 'MyId'` `GROUP BY ViewCount` |
| ------------------------------------------------------------------------------------- |

#### SUM <a href="#sum" id="sum"></a>

Returns the total sum of the column values.<br>

| `SELECT SUM(ViewCount) FROM Videos WHERE Id = 'MyId'` |
| ----------------------------------------------------- |

### JOIN Queries <a href="#default" id="default"></a>

The Provider for YouTube 2020 supports standard SQL joins like the following examples.

#### Inner Join

An inner join selects only rows from both tables that match the join condition:<br>

| `SELECT Channels.Title, Playlists.Title FROM Channels, Playlists WHERE Channels.Id = Playlists.ChannelId` |
| --------------------------------------------------------------------------------------------------------- |

#### Left Join

A left join selects all rows in the FROM table and only matching rows in the JOIN table:<br>

| `SELECT Channels.Title, Playlists.Title FROM Channels LEFT OUTER JOIN Playlists ON Channels.Id = Playlists.ChannelId` |
| --------------------------------------------------------------------------------------------------------------------- |

### Date Literal Functions <a href="#default" id="default"></a>

The following date literal functions can be used to filter date fields using relative intervals. Note that while the <, >, and = operators are supported for these functions, <= and >= are not.

#### TODAY() <a href="#today" id="today"></a>

The current day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = TODAY()` |
| --------------------------------------------------- |

#### YESTERDAY() <a href="#yesterday" id="yesterday"></a>

The previous day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = YESTERDAY()` |
| ------------------------------------------------------- |

#### TOMORROW() <a href="#tomorrow" id="tomorrow"></a>

The following day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = TOMORROW()` |
| ------------------------------------------------------ |

#### LAST\_WEEK() <a href="#last_week" id="last_week"></a>

Every day in the preceding week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = LAST_WEEK()` |
| ------------------------------------------------------- |

#### THIS\_WEEK() <a href="#this_week" id="this_week"></a>

Every day in the current week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = THIS_WEEK()` |
| ------------------------------------------------------- |

#### NEXT\_WEEK() <a href="#next_week" id="next_week"></a>

Every day in the following week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = NEXT_WEEK()` |
| ------------------------------------------------------- |

Also available:

* LAST/THIS/NEXT MONTH
* LAST/THIS/NEXT QUARTER
* LAST/THIS/NEXT YEAR

#### LAST\_N\_DAYS(n) <a href="#last_n_daysn" id="last_n_daysn"></a>

The previous n days, excluding the current day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = LAST_N_DAYS(3)` |
| ---------------------------------------------------------- |

#### NEXT\_N\_DAYS(n) <a href="#next_n_daysn" id="next_n_daysn"></a>

The following n days, including the current day.<br>

| `SELECT * FROM MyTable WHERE MyDateField = NEXT_N_DAYS(3)` |
| ---------------------------------------------------------- |

Also available:

* LAST/NEXT\_90\_DAYS

#### LAST\_N\_WEEKS(n) <a href="#last_n_weeksn" id="last_n_weeksn"></a>

Every day in every week, starting n weeks before current week, and ending in the previous week.<br>

| `SELECT * FROM MyTable WHERE MyDateField = LAST_N_WEEKS(3)` |
| ----------------------------------------------------------- |

#### NEXT\_N\_WEEKS(n) <a href="#next_n_weeksn" id="next_n_weeksn"></a>

Every day in every week, starting the following week, and ending n weeks in the future.<br>

| `SELECT * FROM MyTable WHERE MyDateField = NEXT_N_WEEKS(3)` |
| ----------------------------------------------------------- |

Also available:

* LAST/NEXT\_N\_MONTHS(n)
* LAST/NEXT\_N\_QUARTERS(n)
* LAST/NEXT\_N\_YEARS(n)


---

# 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.appstrategy.com/apprules-r-documentation/platform/platform-features/system-settings/data-sources/sql-compliance/marketing/youtube.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.
