Skip to content

Commit

Permalink
updating with proper headings, and note about building on Apache Data…
Browse files Browse the repository at this point in the history
… Fusion
  • Loading branch information
digadeesh committed Jul 15, 2024
1 parent a5c3180 commit dd8310b
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions spiceaidocs/docs/reference/sql/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ pagination_prev: 'reference/index'
pagination_next: null
---

# SELECT syntax
Spice is built on [Apache DataFusion](https://datafusion.apache.org/) and uses the PostgreSQL dialect, even when querying datasources with different SQL dialects.

## SELECT syntax

The queries in Spice scan data from tables and return 0 or more rows.
Please be aware that column names in queries are made lower-case, but not on the inferred schema. Accordingly, if you
want to query against a capitalized field, make sure to use double quotes.
Please be aware that column names in queries are made lower-case, but not on the inferred schema. Accordingly, if you want to query against a capitalized field, make sure to use double quotes.

Spice supports the following syntax for queries:

Expand All @@ -26,7 +27,7 @@ Spice supports the following syntax for queries:
[ [LIMIT](#limit-clause) count ]
[ [EXCLUDE | EXCEPT](#exclude-and-except-clause) ]

## WITH clause
### WITH clause

A with clause allows to give names for queries and reference them by name.

Expand All @@ -35,7 +36,7 @@ WITH x AS (SELECT a, MAX(b) AS b FROM t GROUP BY a)
SELECT a, b FROM x;
```

## SELECT clause
### SELECT clause

The `SELECT` clause is used to select data from a database by defining the colummns it returns. Each `select_expr` in the
SELECT list can be an expression or wildcards.
Expand All @@ -53,7 +54,7 @@ By default `ALL` will be used, which returns all the rows.
SELECT DISTINCT person, age FROM employees
```

## FROM clause
### FROM clause

The `FROM` clause is used to specify which table to select data from.

Expand All @@ -63,7 +64,7 @@ Example:
SELECT t.a FROM table AS t
```

## WHERE clause
### WHERE clause

The `WHERE` clause is used define the conditions to filter the query results.

Expand All @@ -73,7 +74,7 @@ Example:
SELECT a FROM table WHERE a > 10
```

## JOIN clause
### JOIN clause

Spice supports `INNER JOIN`, `LEFT OUTER JOIN`, `RIGHT OUTER JOIN`, `FULL OUTER JOIN`, `NATURAL JOIN` and `CROSS JOIN`.

Expand All @@ -88,7 +89,7 @@ select * from x;
+----------+----------+
```

### INNER JOIN
#### INNER JOIN

The keywords `JOIN` or `INNER JOIN` define a join that only shows rows where there is a match in both tables.

Expand All @@ -101,7 +102,7 @@ select * from x inner join x y ON x.column_1 = y.column_1;
+----------+----------+----------+----------+
```

### LEFT OUTER JOIN
#### LEFT OUTER JOIN

The keywords `LEFT JOIN` or `LEFT OUTER JOIN` define a join that includes all rows from the left table even if there
is not a match in the right table. When there is no match, null values are produced for the right side of the join.
Expand All @@ -115,7 +116,7 @@ select * from x left join x y ON x.column_1 = y.column_2;
+----------+----------+----------+----------+
```

### RIGHT OUTER JOIN
#### RIGHT OUTER JOIN

The keywords `RIGHT JOIN` or `RIGHT OUTER JOIN` define a join that includes all rows from the right table even if there
is not a match in the left table. When there is no match, null values are produced for the left side of the join.
Expand All @@ -129,7 +130,7 @@ select * from x right join x y ON x.column_1 = y.column_2;
+----------+----------+----------+----------+
```

### FULL OUTER JOIN
#### FULL OUTER JOIN

The keywords `FULL JOIN` or `FULL OUTER JOIN` define a join that is effectively a union of a `LEFT OUTER JOIN` and
`RIGHT OUTER JOIN`. It will show all rows from the left and right side of the join and will produce null values on
Expand All @@ -145,7 +146,7 @@ select * from x full outer join x y ON x.column_1 = y.column_2;
+----------+----------+----------+----------+
```

### NATURAL JOIN
#### NATURAL JOIN

A natural join defines an inner join based on common column names found between the input tables. When no common
column names are found, it behaves like a cross join.
Expand All @@ -159,7 +160,7 @@ select * from x natural join x y;
+----------+----------+
```

### CROSS JOIN
#### CROSS JOIN

A cross join produces a cartesian product that matches every row in the left side of the join with every row in the
right side of the join.
Expand All @@ -173,7 +174,7 @@ select * from x cross join x y;
+----------+----------+----------+----------+
```

## GROUP BY clause
### GROUP BY clause

The `GROUP BY` clause groups together input rows that have the same value into summary rows.

Expand All @@ -195,7 +196,7 @@ Example:
SELECT a, b, ARRAY_AGG(c, ORDER BY d) FROM table GROUP BY a, b
```

## HAVING clause
### HAVING clause

The `HAVING` clause can be used with `GROUP BY` to eliminate groups that don't satisfy the condition given.

Expand All @@ -205,7 +206,7 @@ Example:
SELECT a, b, MAX(c) FROM table GROUP BY a, b HAVING MAX(c) > 10
```

## UNION clause
### UNION clause

The `UNION` clause combines the results of two or more `SELECT` statments. By default `UNION` removes
duplicates. To include duplicates, use `UNION ALL`.
Expand All @@ -226,7 +227,7 @@ SELECT
FROM table2
```

## ORDER BY clause
### ORDER BY clause

Orders the results by the referenced expression. By default it uses ascending order (`ASC`).
This order can be changed to descending by adding `DESC` after the order-by expressions.
Expand All @@ -239,7 +240,7 @@ SELECT age, person FROM table ORDER BY age DESC;
SELECT age, person FROM table ORDER BY age, person DESC;
```

## LIMIT clause
### LIMIT clause

Limits the number of rows to be a maximum of `count` rows. `count` should be a non-negative integer.

Expand All @@ -250,7 +251,7 @@ SELECT age, person FROM table
LIMIT 10
```

## EXCLUDE and EXCEPT clause
### EXCLUDE and EXCEPT clause

Excluded named columns from query results.

Expand Down

0 comments on commit dd8310b

Please sign in to comment.