INNER JOIN
{{< svg svg="static/svgs/join-diagram.svg" class="inner small center" >}} {{< /flex-content >}} {{< flex-content "quarter" >}}LEFT [OUTER] JOIN
{{< svg svg="static/svgs/join-diagram.svg" class="left small center" >}} {{< /flex-content >}} {{< flex-content "quarter" >}}RIGHT [OUTER] JOIN
{{< svg svg="static/svgs/join-diagram.svg" class="right small center" >}} {{< /flex-content >}} {{< flex-content "quarter" >}}FULL [OUTER] JOIN
{{< svg svg="static/svgs/join-diagram.svg" class="full small center" >}} {{< /flex-content >}} {{< /flex >}} {{< expand-wrapper >}} {{% expand "INNER JOIN" %}} Inner joins combine rows from tables on the left and right side of the join based on common column values defined in the `ON` clause. Rows that don't have matching column values are not included in the output table. ```sql SELECT * FROM home INNER JOIN home_actions ON home.room = home_actions.room AND home.time = home_actions.time; ``` {{% /expand %}} {{% expand "LEFT [OUTER] JOIN" %}} A left outer join returns all rows from the left side of the join and only returns data from the right side of the join in rows with matching column values defined in the `ON` clause. ```sql SELECT * FROM home LEFT OUTER JOIN home_actions ON home.room = home_actions.room AND home.time = home_actions.time; ``` {{% /expand %}} {{% expand "RIGHT [OUTER] JOIN" %}} A right outer join returns all rows from the right side of the join and only returns data from the left side of the join in rows with matching column values defined in the `ON` clause. ```sql SELECT * FROM home RIGHT OUTER JOIN home_actions ON home.room = home_actions.room AND home.time = home_actions.time; ``` {{% /expand %}} {{% expand "FULL [OUTER] JOIN" %}} A full outer join returns all data from the left and right sides of the join and combines rows with matching column values defined in the `ON` clause. ```sql SELECT * FROM home FULL OUTER JOIN home_actions ON home.room = home_actions.room AND home.time = home_actions.time; ``` {{% /expand %}} {{< /expand-wrapper >}} ### GROUP BY clause Use the `GROUP BY` clause to group query results based on specified column values. `GROUP BY` **requires** an aggregate or selector function in the `SELECT` statement. #### Examples ```sql SELECT MEAN("water_level"), "location" FROM "h2o_feet" GROUP BY "location" ``` ### HAVING clause Use the `HAVING` clause to filter query results based on a specified condition. The `HAVING` clause must _follow_ the `GROUP BY` clause, but _precede_ the `ORDER BY` clause. #### Examples ```sql SELECT MEAN("water_level"), "location" FROM "h2o_feet" GROUP BY "location" HAVING MEAN("water_level") > 4 ORDER BY "location" ``` ### UNION clause The `UNION` clause combines the results of two or more `SELECT` statements without returning any duplicate rows. `UNION ALL` returns all results, including duplicates. #### Examples ```sql SELECT 'pH' FROM "h2o_pH" UNION ALL SELECT "location" FROM "h2o_quality" ``` ### ORDER BY clause The `ORDER BY` clause orders results by specified columns and order. Sort data based on fields, tags, and timestamps. The following orders are supported: - `ASC`: ascending _(default)_ - `DESC`: descending #### Examples ```sql SELECT "water_level", "location" FROM "h2o_feet" ORDER BY "location", "time" DESC ``` ### LIMIT clause The `LIMIT` clause limits the number of rows to return. The defined limit should be a non-negative integer. #### Examples ```sql SELECT "water_level", "location" FROM "h2o_feet" LIMIT 10 ``` ### WITH clause The `WITH` clause provides a way to write auxiliary statements for use in a larger query. It can help break down large, complicated queries into simpler forms. ```sql WITH summary_data as (SELECT degrees, location, time FROM average_temperature) SELECT * FROM summary_data ``` ### OVER clause The `OVER` clause is used with SQL window functions. A **window function** performs a calculation across a set of table rows that are related in some way to the current row. While similar to aggregate functions, window functions output results into rows retaining their separate identities. ```sql SELECT time, water_level FROM ( SELECT time, "water_level", row_number() OVER ( order by water_level desc ) as rn FROM h2o_feet ) WHERE rn <= 3; ``` ## Comments Use comments to describe and add detail or notes to your queries. - Single line comments use the double hyphen `--` symbol. Single line comments end with a line break. - Multi-line comments begin with `/*` and end with ` */`. ```sql -- Single-line comment /* * Multi-line comment */ ``` ## Schema information {{% product-name %}} supports the following metadata schema queries: ```sql SHOW tables SHOW columns FROM