README.select_expressions 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ----------------------------------------
  2. Select statements and select expressions
  3. ----------------------------------------
  4. Semantics:
  5. A select statement is used to return data to the caller (PSQL module or the
  6. client program). Select expressions retrieve parts of data that construct both
  7. the final the result set or any of the intermediate sets. Select expressions
  8. are also known as subqueries.
  9. Syntax rules:
  10. <select statement> ::=
  11. <select expression> [FOR UPDATE] [WITH LOCK]
  12. <select expression> ::=
  13. <query specification> [UNION [{ALL | DISTINCT}] <query specification>]
  14. <query specification> ::=
  15. SELECT [FIRST <value>] [SKIP <value>] <select list>
  16. FROM <table expression list>
  17. WHERE <search condition>
  18. GROUP BY <group value list>
  19. HAVING <group condition>
  20. PLAN <plan item list>
  21. ORDER BY <sort value list>
  22. ROWS <value> [TO <value>]
  23. <table expression> ::=
  24. <table name> | <joined table> | <derived table>
  25. <joined table> ::=
  26. {<cross join> | <qualified join>}
  27. <cross join> ::=
  28. <table expression> CROSS JOIN <table expression>
  29. <qualified join> ::=
  30. <table expression> [{INNER | {LEFT | RIGHT | FULL} [OUTER]}] JOIN <table expression>
  31. ON <join condition>
  32. <derived table> ::=
  33. '(' <select expression> ')'
  34. Conclusions:
  35. * FOR UPDATE mode and row locking can only be performed for a final dataset,
  36. they cannot be applied to a subquery
  37. * Unions are allowed inside any subquery
  38. * Clauses FIRST, SKIP, PLAN, ORDER BY, ROWS are allowed for any subquery
  39. Notes:
  40. * Either FIRST/SKIP or ROWS is allowed, they cannot be mixed together
  41. (a syntax error is thrown).
  42. * INSERT statement accepts a select expression to create a dataset to be
  43. inserted into a table. So its SELECT part supports all the features defined
  44. above.
  45. * UPDATE and DELETE statements are always based on an implicit cursor
  46. iterating through its target table and limited with the WHERE clause. You
  47. may also specify the final parts of the select expression syntax to limit
  48. the number of affected rows or optimize the statement. The following clauses
  49. are allowed at the end of the UPDATE/DELETE statements: PLAN, ORDER BY and
  50. ROWS.