|
| 1 | +--- |
| 2 | +pcx_content_type: concept |
| 3 | +title: SQL queries supported |
| 4 | +sidebar: |
| 5 | + order: 3 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Details } from "~/components" |
| 9 | + |
| 10 | +This page outlines the SQL features supported by Log Explorer, including common aggregation functions, expressions, and query clauses. |
| 11 | + |
| 12 | +The diagram below illustrates the general shape of a valid query supported in Log Explorer. It shows how standard SQL clauses — such as `SELECT`, `WHERE`, `GROUP BY`, and `ORDER BY` — can be composed to form supported queries. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +Examples of queries include: |
| 17 | + |
| 18 | +- `SELECT * FROM table WHERE (a = 1 OR b = "hello") AND c < 25.89` |
| 19 | +- `SELECT a, b, c FROM table WHERE d >= "GB" LIMIT 10` |
| 20 | + |
| 21 | +:::note |
| 22 | +- A default `LIMIT` of 10,000 is applied if the `LIMIT` clause is omitted. |
| 23 | +- The `WHERE` clause supports up to 25 predicates, which can be grouped using parentheses. |
| 24 | +::: |
| 25 | + |
| 26 | +### SQL Clauses in detail |
| 27 | + |
| 28 | +The following SQL clauses define the structure and logic of queries in Log Explorer: |
| 29 | + |
| 30 | +- `SELECT` - The `SELECT` clause specifies the columns that you want to retrieve from the database tables. It can include individual column names, expressions, or even wildcard characters to select all columns. |
| 31 | +- `FROM` - The `FROM` clause specifies the tables from which to retrieve data. It indicates the source of the data for the `SELECT` statement. |
| 32 | +- `WHERE` - The `WHERE` clause filters the rows returned by a query based on specified conditions. It allows you to specify conditions that must be met for a row to be included in the result set. |
| 33 | +- `SELECT DISTINCT` - Removes duplicate rows from the result set. |
| 34 | +- `GROUP BY` - Groups rows for aggregation. The `GROUP BY` clause is used to group rows that have the same values into summary rows. |
| 35 | +- `ORDER BY` - Sorts the result set. The `ORDER BY` clause is used to sort the result set by one or more columns in ascending or descending order. |
| 36 | +- `LIMIT` - Restricts the number of rows returned. The `LIMIT` clause is used to constrain the number of rows returned by a query. It is often used in conjunction with the `ORDER BY` clause to retrieve the top `N` rows or to implement pagination. |
| 37 | +- `OFFSET` - Skips a specified number of rows before returning results. |
| 38 | + |
| 39 | +The sections that follow break down the remaining components shown in the diagram — such as aggregation functions, string and numeric expressions, and supported operators — in more detail. |
| 40 | + |
| 41 | +## Functions |
| 42 | + |
| 43 | +Log Explorer supports a range of SQL functions to transform, evaluate, or summarize data. These include scalar and aggregation functions. |
| 44 | + |
| 45 | +### Scalar functions |
| 46 | + |
| 47 | +These help manipulate or evaluate values (often strings): |
| 48 | + |
| 49 | +- `ARRAY_CONTAINS(array, element)` – Checks if the array contains the element. |
| 50 | + |
| 51 | + <Details header="Example"> |
| 52 | + `ARRAY_CONTAINS(['US', 'CA'], ClientCountry)` |
| 53 | + |
| 54 | + Returns rows where `ClientCountry` is either `US` or `CA`. |
| 55 | + </Details> |
| 56 | + |
| 57 | +- `SUBSTRING(string, from_number, for_number)` – Extracts part of a string. |
| 58 | + |
| 59 | + <Details header="Example"> |
| 60 | + `SUBSTRING(ClientRequestPath, 0, 5)` |
| 61 | + |
| 62 | + Extracts the first `5` characters from `ClientRequestPath`. |
| 63 | + </Details> |
| 64 | + |
| 65 | +- `LOWER(string)` – Converts to lowercase. |
| 66 | + |
| 67 | + <Details header="Example"> |
| 68 | + `LOWER(ClientRequestUserAgent)` |
| 69 | + |
| 70 | + Converts the user agent string to lowercase. |
| 71 | + </Details> |
| 72 | + |
| 73 | +- `UPPER(string)` – Converts to uppercase. |
| 74 | + |
| 75 | + <Details header="Example"> |
| 76 | + `UPPER(ClientCountry)` |
| 77 | + |
| 78 | + Converts the country code to uppercase. |
| 79 | + </Details> |
| 80 | + |
| 81 | +### Aggregation functions |
| 82 | + |
| 83 | +Used to perform calculations on sets of rows: |
| 84 | + |
| 85 | +- `SUM(expression)` – Total of values. |
| 86 | + |
| 87 | + <Details header="Example"> |
| 88 | + `SUM(ClientRequestBytes)` |
| 89 | + |
| 90 | + Adds up the total number of bytes requested by clients. |
| 91 | + </Details> |
| 92 | + |
| 93 | +- `MIN(expression)` – Minimum value. |
| 94 | + |
| 95 | + <Details header="Example"> |
| 96 | + `MIN(OriginResponseDurationMs)` |
| 97 | + |
| 98 | + Finds the shortest response time from origin servers. |
| 99 | + </Details> |
| 100 | + |
| 101 | +- `MAX(expression)` – Maximum value. |
| 102 | + |
| 103 | + <Details header="Example"> |
| 104 | + `MAX(OriginResponseDurationMs)` |
| 105 | + |
| 106 | + Finds the longest response time. |
| 107 | + </Details> |
| 108 | + |
| 109 | +- `COUNT(expression)` – Number of rows (can be all rows or non-null values). |
| 110 | + |
| 111 | + <Details header="Example"> |
| 112 | + `COUNT(ClientRequestUserAgent)` |
| 113 | + |
| 114 | + Counts how many rows have a user agent value. |
| 115 | + </Details> |
| 116 | + |
| 117 | +- `COUNT(DISTINCT expression)` – Number of distinct non-null values. |
| 118 | + |
| 119 | + <Details header="Example"> |
| 120 | + `COUNT(DISTINCT ClientIP)` |
| 121 | + |
| 122 | + Counts how many unique client IPs made requests. |
| 123 | + </Details> |
| 124 | + |
| 125 | +- `AVG(expression)` – Average of numeric values. |
| 126 | + |
| 127 | + <Details header="Example"> |
| 128 | + `AVG(OriginResponseDurationMs)` |
| 129 | + |
| 130 | + Computes the average origin response time in milliseconds. |
| 131 | + </Details> |
| 132 | + |
| 133 | +The diagram below represents the grammar for SQL expressions including scalar and aggregate functions. |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +## Expressions |
| 138 | + |
| 139 | +Conditions or logic used in queries: |
| 140 | + |
| 141 | +- `CASE WHEN` – Conditional logic (like if-else). |
| 142 | +- `AS` – Alias for columns or tables. |
| 143 | +- `LIKE` – Pattern matching. |
| 144 | +- `IN (list)` – Checks if a value is in a list. |
| 145 | +- `BETWEEN ... AND ...` – Checks if a value is within a range. |
| 146 | +- `Unary operator` – Operates on one operand (for example, `-5`). |
| 147 | +- `Binary operator` – Operates on two operands (for example, `5 + 3`). |
| 148 | +- `Nested Expressions` – Expression wrapped with parentheses, like `( x > y )` or `( 1 )`. |
| 149 | +- `Compound identifier` – Multi-part name (for example, `schema.table.column`). |
| 150 | +- `Array` – A collection of values (supported differently across SQL dialects). |
| 151 | +- `Literals` - represent values such as strings, numbers, or arrays. |
| 152 | + |
| 153 | +The diagram below represents the grammar for SQL expressions, detailing the various forms an expression can take, including columns, literals, functions, operators, and aliases. |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +The diagram below defines the grammar for unary operators, which operate on a single operand (for example, negation or logical `NOT`): |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +## Binary Operators |
| 162 | + |
| 163 | +Used for arithmetic, comparison, logical operations: |
| 164 | + |
| 165 | +- Arithmetic: `+`, `-`, `*`, `/`, `%` (modulo) |
| 166 | +- Comparison: `>`, `<`, `>=`, `<=`, `=`, `!=` (or `<>`)` |
| 167 | +- Logical: `AND`, `OR`, `XOR` |
| 168 | +- Bitwise: `&`, `|`, `^`, `>>`, `<<` |
| 169 | +- String concat: `||` |
| 170 | + |
0 commit comments