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