|
| 1 | +--- |
| 2 | +title: PIPE SQL Queries |
| 3 | +weight: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +LingoDB supports a subset of Google's PIPE SQL syntax, which allows you to write queries as a series of data transformations. This can make complex queries easier to read and write. A query consists of a data source (e.g., a table) followed by a series of operators, separated by `|>`. |
| 7 | + |
| 8 | +## Simple Data Access |
| 9 | + |
| 10 | +To start a query, specify the data source using the `from` keyword. You can then pipe this data through a series of operators. |
| 11 | + |
| 12 | +```sql |
| 13 | +-- Get all information about a specific student |
| 14 | +from students |
| 15 | +|> where studnr = 12; |
| 16 | + |
| 17 | +-- Selecting specific columns |
| 18 | +from students |
| 19 | +|> select studnr, name |
| 20 | +|> where studnr = 12; |
| 21 | +``` |
| 22 | + |
| 23 | +To create new columns or perform calculations, use the `extend` operator. |
| 24 | + |
| 25 | +```sql |
| 26 | +from students |
| 27 | +|> extend extract(year from birthday) as birthyear |
| 28 | +|> select name, birthyear; |
| 29 | +``` |
| 30 | + |
| 31 | +:::info[Unsupported Features (incomplete list)] |
| 32 | +Expressions in the `select` clause are not yet supported. You must use `extend` to create new columns. |
| 33 | +```sql |
| 34 | +-- This is not supported: |
| 35 | +from students |
| 36 | +|> select name, extract(year from birthday) as birthyear; |
| 37 | +``` |
| 38 | +::: |
| 39 | + |
| 40 | +## Group By/Aggregations |
| 41 | + |
| 42 | +For aggregations and groupings, the `aggregate` operator must be used. `aggregate` produces the grouping columns and then the aggregate columns. Aliases can be assigned directly to grouping expressions. |
| 43 | + |
| 44 | +```sql |
| 45 | +from lectures |
| 46 | +|> aggregate min(weeklyhours) group by given_by as profNr; |
| 47 | + |
| 48 | +-- Filter aggregated values using `where` |
| 49 | +from lectures |
| 50 | +|> aggregate min(weeklyhours) as minWeeklyHoours group by given_by as profNr |
| 51 | +|> where minWeeklyHoours < 5; |
| 52 | +``` |
| 53 | + |
| 54 | +:::info[Unsupported Features (incomplete list)] |
| 55 | +This query fails because the parser cannot resolve the reference to `min(weeklyhours)` in the where clause. |
| 56 | +It does recognizing that both refer to the same computed aggregate. |
| 57 | +```sql |
| 58 | +-- This is not supported: |
| 59 | +from lectures |
| 60 | +|> aggregate min(weeklyhours) group by given_by as profNr |
| 61 | +|> where min(weeklyhours) < 5; |
| 62 | +``` |
| 63 | +::: |
| 64 | + |
| 65 | +## Window Functions |
| 66 | + |
| 67 | +Window functions must be used in the `extend` operator. |
| 68 | + |
| 69 | +```sql |
| 70 | +from lectures |
| 71 | +|> extend row_number() over (partition by given_by order by weeklyhours desc) as rn |
| 72 | +|> where rn = 1; |
| 73 | +``` |
| 74 | + |
| 75 | +## Joins |
| 76 | + |
| 77 | +Pipe SQL introduces a `join` operator to join the input with another table. `LEFT`, `RIGHT`, and `FULL OUTER` joins are also supported. |
| 78 | + |
| 79 | +```sql |
| 80 | +-- Inner join |
| 81 | +from students s |
| 82 | +|> join attend h on h.studnr = s.studnr; |
| 83 | + |
| 84 | +-- Left outer join |
| 85 | +from students s |
| 86 | +|> left join attend h on h.studnr = s.studnr; |
| 87 | + |
| 88 | +-- Right outer join |
| 89 | +from students s |
| 90 | +|> right join attend h on h.studnr = s.studnr; |
| 91 | + |
| 92 | +-- Full outer join |
| 93 | +from students s |
| 94 | +|> full join outer attend h on h.studnr = s.studnr; |
| 95 | +``` |
| 96 | + |
| 97 | +## Sorting and Limiting |
| 98 | + |
| 99 | +You can sort and limit your results using the `order by` and `limit` operators. |
| 100 | + |
| 101 | +```sql |
| 102 | +-- Sort students by name |
| 103 | +from students |
| 104 | +|> order by name; |
| 105 | + |
| 106 | +-- Sort in descending order |
| 107 | +from students |
| 108 | +|> order by name desc; |
| 109 | + |
| 110 | +-- Limit the number of results |
| 111 | +from students |
| 112 | +|> limit 10; |
| 113 | + |
| 114 | +-- Combine sort and limit to get the top 10 students by matriculation number |
| 115 | +from students |
| 116 | +|> order by studnr |
| 117 | +|> limit 10; |
| 118 | +``` |
| 119 | +## Set Operations |
| 120 | + |
| 121 | +LingoDB supports `UNION`, `UNION ALL`, `EXCPET` and `EXCPET ALL` to combine the result sets of two or more `SELECT` statements. |
| 122 | + |
| 123 | +```sql |
| 124 | +from attend |
| 125 | +|> UNION (from attend) |
| 126 | +``` |
| 127 | +:::info[Unsupported Features (incomplete list)] |
| 128 | +Set operation operators currently to not support multiple queries |
| 129 | +```sql |
| 130 | +-- This is not supported: |
| 131 | +from x |
| 132 | +|> UNION (from y), (from z) |
| 133 | +``` |
| 134 | +::: |
| 135 | + |
| 136 | +## List of all supported pipe operators |
| 137 | + |
| 138 | +The following table provides a summary of all supported pipe operators and their functions. These operators allow for a step-by-step transformation of your data. |
| 139 | + |
| 140 | +| Operator | Description | |
| 141 | +|----------------|-------------| |
| 142 | +| `SELECT <expr>, ...`| Keeps only the specified columns. | |
| 143 | +| `EXTEND <expr> AS <name>, ...`| Adds new columns to the result. | |
| 144 | +| `SET <column> = <expr>`| Updates the values in existing columns. | |
| 145 | +| `DROP <column>, ...`| Removes specified columns from the result. | |
| 146 | +| `WHERE <condition>, ...`| Filters rows based on one or more conditions. | |
| 147 | +| `AGGREGATE <aggr> GROUP BY <group> [[AS] alias], ...` | Groups rows and calculates aggregate values. | |
| 148 | +| `LIMIT <n>`| Limits the number of rows in the output. | |
| 149 | +| `ORDER BY <expr> [ASC\|DESC], ...`| Sorts the result based on specified columns. | |
| 150 | +| `{UNION\|INTERSECT\|EXCEPT} [ALL\|DISTINCT] <query>, ... ` | Combines results with other queries using set operations. | |
| 151 | +| `[LEFT\|...] JOIN <table> [ON <condition>]` | Joins the current result with another table. | |
| 152 | + |
| 153 | +## Compatibility with Classic SQL |
| 154 | + |
| 155 | +The pipe syntax can be combined with the classic SQL syntax in any way: |
| 156 | + |
| 157 | +```sql |
| 158 | +select * from students s |
| 159 | +|> where studnr=1 |
| 160 | +|> join (select * from attend) h on h.studnr = s.studnr; |
| 161 | +``` |
0 commit comments