Questions tagged [sql-standard]

For questions concerning the SQL Standard itself (also known as the ANSI or ISO SQL Standard) including writing queries that conform to that standard. This tag is NOT to be used for questions about SQL Server Standard Edition.

SQL, or Structured Query Language, is a special-purpose programming language designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS).

Originally based upon relational algebra and tuple relational calculus, SQL consists of a data definition language and a data manipulation language. The scope of SQL includes data insert, query, update and delete, schema creation and modification, and data access control. Although SQL is often described as, and to a great extent is, a declarative language (4GL), it also includes procedural elements.

SQL was one of the first commercial languages for Edgar F. Codd's relational model, as described in his influential 1970 paper, "A Relational Model of Data for Large Shared Data Banks." Despite not entirely adhering to the relational model as described by Codd, it became the most widely used database language.

SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987. Since then, the standard has been revised to include a larger set of features. Despite the existence of such standards, though, most SQL code is not completely portable among different database systems without adjustments.

122 questions
213
votes
15 answers

SQL: SELECT All columns except some

Is there a way to SELECT all columns in a table, except specific ones? IT would be very convenient for selecting all the non-blob or non-geometric columns from a table. Something like: SELECT * -the_geom FROM segments; I once heard that this…
Adam Matan
  • 12,079
  • 30
  • 82
  • 96
33
votes
2 answers

CREATE INDEX vs ALTER TABLE ADD INDEX - MySQLism, or SQL Standard?

Just came across a weird issue, whereby depending on how I create an index, an index name is required. http://dev.mysql.com/doc/refman/5.5/en/create-index.html http://dev.mysql.com/doc/refman/5.5/en/alter-table.html CREATE INDEX `random_name` ON…
Mike Purcell
  • 549
  • 3
  • 8
  • 20
31
votes
4 answers

Where can I find the first standardization of SQL, SQL-86?

This question is different but similar to this request looking for SQL-89. The first draft of SQL is labeled SQL-86. Numerous references are made to it. Is this available to download? Wikipedia doesn't even have a page for it. I'm interested in it…
Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
30
votes
3 answers

Why does ANSI SQL define SUM(no rows) as NULL?

The ANSI SQL standard defines (chapter 6.5, set function specification) the following behaviour for aggregate functions on empty result sets: COUNT(...) = 0 AVG(...) = NULL MIN(...) = NULL MAX(...) = NULL SUM(...) = NULL Returning NULL for AVG, MIN…
Heinzi
  • 3,210
  • 2
  • 32
  • 43
30
votes
1 answer

Using a CREATE TABLE AS SELECT how do I specify a WITH condition (CTE)?

There is an old and deprecated command in PostgreSQL that predates CREATE TABLE AS SELECT (CTAS) called SELECT ... INTO .... FROM, it supports WITH clauses / Common Table Expressions (CTE). So, for instance, I can do this.. WITH w AS ( SELECT * …
Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
27
votes
1 answer

Is the optimisation fence behaviour of a CTE (WITH query) specified in the SQL:2008 standard? If so, where?

I see frequent references to WITH queries (common table expressions, or CTEs) acting as an optimisation fence, where the server isn't permitted to push filters down into the CTE queries, pull common expressions up out of the CTE, etc. It's often…
Craig Ringer
  • 57,821
  • 6
  • 162
  • 193
22
votes
3 answers

What is the correct result for this query?

I came across this puzzle in the comments here CREATE TABLE r (b INT); SELECT 1 FROM r HAVING 1=1; SQL Server and PostgreSQL return 1 row. MySQL and Oracle return zero rows. Which is correct? Or are both equally valid?
Martin Smith
  • 87,941
  • 15
  • 255
  • 354
17
votes
2 answers

Where can I find the SQL standard document?

Where can I find a legal copy of the ISO SQL 2008 standard?
Philᵀᴹ
  • 31,952
  • 10
  • 86
  • 108
12
votes
1 answer

ANSI/ISO plans for LIMIT standardization?

Are there currently plans to standardize one best way of limiting the number of results returned by a query? The stack overflow question at Is there an ANSI SQL alternative to the MYSQL LIMIT keyword? lists the various ways to handle this behavior…
chucksmash
  • 545
  • 1
  • 6
  • 9
12
votes
3 answers

Is MySQL breaking the standard by allowing selecting columns that are not part of the group by clause?

I am used to Microsoft technologies including SQL Server. Today I ran across a Q&A where the following passage from the MySQL documentation was quoted: Standard SQL would reject your query because you can not SELECT non-aggregate fields that are…
Paul White
  • 94,921
  • 30
  • 437
  • 687
12
votes
3 answers

What actually is a SQL clause?

This may sound like an overly simple question but I'm not finding it easy to locate a proper answer. To the question "What are sql clauses?" most of the resources on internet simply provide a list of clauses and explain what they do. But I'm…
Kshitiz Sharma
  • 3,357
  • 9
  • 33
  • 35
11
votes
2 answers

In PostgreSQL, what is the difference between a "Stored Procedure" and other types of functions?

I understand the distinction between: Scalar functions Set-Returning-Functions (SRF)s Internal functions Window functions Aggregate functions of all sorts User-Implemented functions (which in PostgreSQL can be implement in any language) Etc. In…
Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
11
votes
1 answer

Does the SQL Spec require a GROUP BY in EXISTS ()

Microsoft currently permits this syntax. SELECT * FROM ( VALUES (1) ) AS g(x) WHERE EXISTS ( SELECT * FROM ( VALUES (1),(1) ) AS t(x) WHERE g.x = t.x HAVING count(*) > 1 ); Notice that there is no GROUP BY in the EXISTS clause, is that…
Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
11
votes
2 answers

Are there standard SQL functions with side effects?

Do the SQL standards define functions with side-effects? For example, do they have functions for writing into files* or to update values in certain columns of a table when you do something like SELECT myfunction(params...); I've seen these…
tinlyx
  • 3,810
  • 14
  • 50
  • 79
10
votes
3 answers

Is the keyword "ALIAS" actually used?

According to PostgreSQL 7.1 through 9.1 (now unsupported), ALIAS is listed as a reserved word, at least for SQL-99. Later versions do not show it - suggesting that it has been dropped as a reserved word. The old PostgreSQL docs do say "the presence…
user4930
1
2 3
8 9