-
Notifications
You must be signed in to change notification settings - Fork 17
DOC-561-SQL-Support-Changes #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||
| --- | ||||||
| sidebar_position: 10 | ||||||
| title: Advanced SQL Queries | ||||||
| --- | ||||||
|
|
||||||
| ## Case Sensitivity | ||||||
| Case-sensitivity is supported for attribute names using `"<attribute name>"`. | ||||||
| Case-sensitive collections names can be used as well. | ||||||
|
|
||||||
diana-macrometa marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| Example: | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT "CaseSens" FROM case_sens; | ||||||
| ``` | ||||||
|
|
||||||
| ## Join Support | ||||||
| Macrometa SQL has full support for `left outer join`. | ||||||
|
|
||||||
| Example: | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT category.name, avg(film.length) | ||||||
| FROM film | ||||||
| LEFT OUTER JOIN film_category ON (film.film_id= film_category.film_id) | ||||||
| LEFT OUTER JOIN category ON (film_category.category_id = category.category_id) | ||||||
| GROUP BY category.name | ||||||
| ORDER BY avg(film.length) DESC | ||||||
| ``` | ||||||
|
|
||||||
| ## WHERE and EXISTS operators | ||||||
| `WHERE...EXISTS` operators also supported. It has two variants: | ||||||
| - `WHERE EXISTS` | ||||||
| - `WHERE NOT EXISTS` | ||||||
| Both variants can be used to check subquery result. | ||||||
|
|
||||||
| Example: | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT inventory.inventory_id | ||||||
| FROM inventory JOIN store ON (inventory.store_id = store.store_id) | ||||||
| JOIN film ON (inventory.film_id = film.film_id) | ||||||
| JOIN rental ON (inventory.inventory_id = rental.inventory_id) | ||||||
| WHERE film.title = 'Academy Dinosaur' | ||||||
| AND store.store_id = 1 | ||||||
| AND NOT EXISTS ( | ||||||
| SELECT * | ||||||
| FROM rental | ||||||
| WHERE rental.inventory_id = inventory.inventory_id | ||||||
| AND rental.return_date IS NULL) | ||||||
| ``` | ||||||
|
|
||||||
| ## HAVING clause | ||||||
| `HAVING` clause is supported. | ||||||
|
|
||||||
| Example: | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT inventory.inventory_id | ||||||
| FROM inventory JOIN store ON (inventory.store_id = store.store_id) | ||||||
| JOIN film ON (inventory.film_id = film.film_id) | ||||||
| JOIN rental ON (inventory.inventory_id = rental.inventory_id) | ||||||
| WHERE film.title = 'Academy Dinosaur' | ||||||
| AND store.store_id = 1 | ||||||
| AND NOT EXISTS ( | ||||||
| SELECT * | ||||||
| FROM rental | ||||||
| WHERE rental.inventory_id = inventory.inventory_id | ||||||
| AND rental.return_date IS NULL) | ||||||
| ``` | ||||||
|
|
||||||
| :::note | ||||||
| Users can't use `HAVING` clause with subqueries. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always write to "you," not "user." |
||||||
| ::: | ||||||
|
|
||||||
| ## SQL Functions | ||||||
| Macrometa SQL supports following functions: | ||||||
| - STDDEV | ||||||
| - STDDEV_POP | ||||||
| - STDDEV_SAMP | ||||||
| - VAR_POP | ||||||
| - VAR_SAMP | ||||||
| - MEDIAN | ||||||
| - PERCENTILE | ||||||
| - PERCENTILE_CONT | ||||||
|
|
||||||
| :::note | ||||||
| Users can use mathematical functions such as degrees and logarithm with arbitrary base. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ::: | ||||||
|
|
||||||
| Examples: | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT fl.title, FLOOR(fl.length/2.34) fl, fl.length/2.34 nfl, fl.length | ||||||
| FROM film AS fl; | ||||||
| ``` | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT fl.title, FLOOR(fl.length/2.34) fl, ROUND(fl.length/2.34) rnd, | ||||||
| fl.length/2.34 nfl, fl.length, LOG(fl.length, 2) lg2, DEGREES(pi()) pi, | ||||||
| DEGREES(pi()/4) deg45 | ||||||
| FROM film AS fl; | ||||||
| ``` | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT STDDEV(fl.length) div, VAR_POP(fl.length) var | ||||||
| FROM film AS fl; | ||||||
| ``` | ||||||
|
|
||||||
| :::note | ||||||
| Percentile function is no **aggregation function**. We can't use it in groupings in the similar manner `STDDEV` and `VAR_POP`. | ||||||
| ::: | ||||||
|
|
||||||
| Example on how to use percentile function: | ||||||
|
|
||||||
| ```sql | ||||||
| select PERCENTILE((select fl.length from film as fl), 0.5) med; | ||||||
| ``` | ||||||
|
|
||||||
| Absolute value operator is supported. | ||||||
|
|
||||||
| Example: | ||||||
|
|
||||||
| ```sql | ||||||
| SELECT @ -123.45; | ||||||
| ``` | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things: