# NetSuiteSQL

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>

`SELECT` `{`

&#x20; `[ TOP` `<numeric_literal> | DISTINCT` `]`

&#x20; `{`

&#x20;   `*`

&#x20;   `| {`

&#x20;       `<expression> [ [ AS` `] <column_reference> ]`

&#x20;       `| { <table_name> | <correlation_name> } .*`

&#x20;     `} [ , ... ]`

&#x20; `}`

&#x20; `[ INTO` `csv:// [ filename= ] <file_path> [ ;delimiter=tab ] ]`

&#x20; `{`

&#x20;   `FROM` `<table_reference> [ [ AS` `] <identifier> ]`

&#x20; `} [ , ... ]`

&#x20; `[ [`&#x20;

&#x20;     `INNER` `| { { LEFT` `| RIGHT` `| FULL` `} [ OUTER` `] }`

&#x20;   `] JOIN` `<table_reference> [ ON` `<search_condition> ] [ [ AS` `] <identifier> ]`

&#x20; `] [ ... ]`

&#x20; `[ WHERE` `<search_condition> ]`

&#x20; `[ GROUP` `BY` `<column_reference> [ , ... ]`

&#x20; `[ HAVING` `<search_condition> ]`

&#x20; `[ UNION` `[ ALL` `] <select_statement> ]`

&#x20; `[`

&#x20;   `ORDER` `BY`

&#x20;   `<column_reference> [ ASC` `| DESC` `] [ NULLS FIRST` `| NULLS LAST` `]`

&#x20; `]`

&#x20; `[`

&#x20;   `LIMIT <expression>`

&#x20;   `[`

&#x20;     `{ OFFSET | , }`

&#x20;     `<expression>`

&#x20;   `]`

&#x20; `]`

`} | SCOPE_IDENTITY()`

&#x20;

`<expression> ::=`

&#x20; `| <column_reference>`

&#x20; `| @ <parameter>`

&#x20; `| ?`

&#x20; `| COUNT( * | { [ DISTINCT` `] <expression> } )`

&#x20; `| { AVG` `| MAX` `| MIN` `| SUM` `| COUNT` `} ( <expression> )`

&#x20; `| NULLIF` `( <expression> , <expression> )`

&#x20; `| COALESCE` `( <expression> , ... )`

&#x20; `| CASE` `<expression>`

&#x20;     `WHEN` `{ <expression> | <search_condition> } THEN` `{ <expression> | NULL` `} [ ... ]`

&#x20;   `[ ELSE` `{ <expression> | NULL` `} ]`

&#x20;   `END`

&#x20; `| <literal>`

&#x20; `| <sql_function>`

&#x20;

`<search_condition> ::=`

&#x20; `{`

&#x20;   `<expression> { = | > | < | >= | <= | <> | != | LIKE` `| NOT` `LIKE` `| IN` `| NOT` `IN` `| IS` `NULL` `| IS` `NOT` `NULL` `| AND` `| OR` `| CONTAINS` `| BETWEEN` `} [ <expression> ]`

&#x20; `} [ { AND` `| OR` `} ... ]`

&#x20;

| <p></p><p> </p> |
| --------------- |

#### Examples

1. Return all columns:<br>

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

   | `SELECT [AcctName] AS MY_AcctName FROM Account` |
   | ----------------------------------------------- |
3. Cast a column's data as a different data type:<br>

   | `SELECT CAST(AnnualRevenue AS VARCHAR) AS Str_AnnualRevenue FROM Account` |
   | ------------------------------------------------------------------------- |
4. Search data:<br>

   | `SELECT * FROM Account WHERE acctName = 'Checking'` |
   | --------------------------------------------------- |
5. Return the number of items matching the query criteria:<br>

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

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

   | `SELECT DISTINCT AcctName FROM Account` |
   | --------------------------------------- |
8. Summarize data:<br>

   | `SELECT AcctName, MAX(AnnualRevenue) FROM Account GROUP BY AcctName` |
   | -------------------------------------------------------------------- |

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

   | `SELECT n.Note, c.InternalId FROM Note n, Customer c WHERE n.Entity_InternalId = c.InternalId` |
   | ---------------------------------------------------------------------------------------------- |

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

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

    | `SELECT InternalId, AcctName FROM Account 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 Account WHERE acctName = @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 Account WHERE acctName = 'Checking'` |
| ---------------------------------------------------------- |

#### 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 InternalId) AS DistinctValues FROM Account WHERE acctName = 'Checking'` |
| ---------------------------------------------------------------------------------------------- |

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

Returns the average of the column values.<br>

| `SELECT AcctName, AVG(AnnualRevenue) FROM Account WHERE acctName = 'Checking'`  `GROUP BY AcctName` |
| --------------------------------------------------------------------------------------------------- |

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

Returns the minimum column value.<br>

| `SELECT MIN(AnnualRevenue), AcctName FROM Account WHERE acctName = 'Checking'` `GROUP BY AcctName` |
| -------------------------------------------------------------------------------------------------- |

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

Returns the maximum column value.<br>

| `SELECT AcctName, MAX(AnnualRevenue) FROM Account WHERE acctName = 'Checking'` `GROUP BY AcctName` |
| -------------------------------------------------------------------------------------------------- |

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

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

| `SELECT SUM(AnnualRevenue) FROM Account WHERE acctName = 'Checking'` |
| -------------------------------------------------------------------- |

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

The CData ADO.NET Provider for NetSuite 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 n.Note, c.InternalId FROM Note n, Customer c WHERE n.Entity_InternalId = c.InternalId` |
| ---------------------------------------------------------------------------------------------- |

#### Left Join

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

| `SELECT n.Note, c.InternalId FROM Customer c LEFT JOIN Note n ON n.Entity_InternalId = c.InternalId` |
| ---------------------------------------------------------------------------------------------------- |

### 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.

#### L\_TODAY() <a href="#ltoday" id="ltoday"></a>

The current day.<br>

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

#### L\_YESTERDAY() <a href="#lyesterday" id="lyesterday"></a>

The previous day.<br>

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

#### L\_TOMORROW() <a href="#ltomorrow" id="ltomorrow"></a>

The following day.<br>

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

#### L\_LAST\_WEEK() <a href="#llastweek" id="llastweek"></a>

Every day in the preceding week.<br>

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

#### L\_THIS\_WEEK() <a href="#lthisweek" id="lthisweek"></a>

Every day in the current week.<br>

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

#### L\_NEXT\_WEEK() <a href="#lnextweek" id="lnextweek"></a>

Every day in the following week.<br>

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

Also available:

* L\_LAST/L\_THIS/L\_NEXT MONTH
* L\_LAST/L\_THIS/L\_NEXT QUARTER
* L\_LAST/L\_THIS/L\_NEXT YEAR

#### L\_LAST\_N\_DAYS(n) <a href="#llastndaysn" id="llastndaysn"></a>

The previous n days, excluding the current day.


---

# 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/erp/netsuitesql.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.
