The goal of this page — which is a work in progress — is to gather information relevant for people who are porting SQL from one product to another and/or are interested in possibilities and limits of 'cross-product' SQL.
The following tables compare how different DBMS products handle various SQL (and related) features. If possible, the tables also state how the implementations should do things, according to the SQL standard.
I will only write about subjects that I've worked with personally, or subjects which I anticipate to find use for in the near future. Subjects on which there are no significant implementation variances are not covered. Beta-versions of software are not examined.
I'm sorry about the colors. They are a result of wanting to mark each DBMS differently and at the same time wanting to be relatively nice to printers.
If you have corrections or suggestions, please contact me; even notifications about spelling errors are welcome.
The following SQL standard and implementations have been examined, if not otherwise stated:
Standard | The latest official version of SQL is SQL:2008. I don't have access to the official ISO standard text, but Whitemarsh Information Systems Corporation provides a rather final draft as a zip-archive, containing several files. Most important to this page is the file No books cover SQL:2008 yet. Regarding the previous standard, SQL:2003, the only book covering the subject is in German which I was never any good at. Therefore, I also use the following book as reference: |
PostgreSQL | PostgreSQL 8.4.1 on CentOS Linux. Documentation |
DB2 | DB2 Express-C v. 9.1 on Fedora Linux. Note that there are differences between various DB2 flavors ; this page is about DB2 for "LUW" (Linux/Unix/Windows). Documentation |
MS SQL Server | MS SQL Server 2005 on Windows XP. Microsoft's SQL implementation is sometimes named Transact-SQL , or TSQL . In this document, I'll generally write MSSQL as a short-hand for Microsoft's SQL Server product. Documentation |
MySQL | MySQL Database Server 5.0.18 on Fedora Linux (i.e. MySQL AB's "classic" DBMS product—not MaxDB). Documentation |
Oracle | Oracle Database 11g Release 2 on Red Hat Enterprise Linux. Documentation |
Informix | Informix Dynamic Server Workgroup Edition v. 11.50 on Red Hat Enterprise Linux. Documentation |
The products are running with their default settings. This is important for MySQL and MSSQL: Their interpretation of SQL may be changed rather drastically by adjusting certain configuration options, potentially increasing the level of standard compliance (for MySQL, there is a dedicated documentation page about this). However, such non-default configuration options are not of great value for people writing SQL applications because the developer often cannot rely on non-default configuration settings.
Standard | Views are part of the standard, and they may be updated, as long as it 'makes sense'. SQL:2008 has a rather complicated set of rules governing when a view is updatable, basically saying that a view is updatable, as long as the update-operation translates into an unambiguous change. SQL-92 was more restrictive, specifying that updatable views cannot be derived from more than one base table. |
PostgreSQL | Has views. Breaks that standard by not allowing updates to views; offers the non-standard 'rules'-system as a work-around. |
DB2 | Conforms to at least SQL-92. |
MSSQL | Conforms to at least SQL-92. |
MySQL | Conforms to at least SQL-92. |
Oracle | Conforms to at least SQL-92. |
Informix | Conforms to at least SQL-92. |
All the DBMSes support basic INNER JOINs, but vary in their support for other join types.
In the following feature chart, a means yes ; an empty table cell means no .
Natural joins (only tested: NATURAL LEFT JOIN ) | <!-- {12738888657262}-->
<!-- {12738888657263}--> | <!-- {12738888657264}--> | <!-- {12738888657265}--> | <!-- {12738888657266}--> | <!-- {12738888657267}--> | |
USING -clause | <!-- {12738888657268}-->
<!-- {12738888657269}--> | <!-- {127388886572610}--> | <!-- {127388886572611}--> | <!-- {127388886572612}--> | <!-- {127388886572613}--> | |
FULL joins1 (tested: SELECT...FULL JOIN...ON...=... ) | <!-- {127388886572614}-->
<!-- {127388886572615}--> | <!-- {127388886572616}--> | <!-- {127388886572617}--> | <!-- {127388886572618}--> | <!-- {127388886572619}--> | |
Explicit CROSS JOIN (cartesian product) | <!-- {127388886572620}-->
<!-- {127388886572621}--> | <!-- {127388886572622}--> | <!-- {127388886572623}--> | <!-- {127388886572624}--> | <!-- {127388886572625}--> |
Remarks:
FULL
joins may be emulated with a union of a left and a right join .Objective: An existing table, t1 needs to be copied to a new table, t2 , without copying data. I.e., only the structure/definition of the table is copied.
Standard | Optional feature T171 defines LIKE clause in table definition : CREATE TABLE t2 ( LIKE t1 ) The DBMS may support an extension of this (feature T173) which allows for more table properties to be copied: If Triggers, CHECK constraints, and other 'non-trivial' table features are not copied to the new table. |
PostgreSQL | Complies with the core of the feature (T171). The extended T173 feature is only partially supported, and extended with a few non-standard options:
PostgreSQL does not allow you to copy the structure of a view, using |
DB2 | Behaves as if inspired by the standard. I.e., DB2 conforms to the standard, except:
Example: DB2 allows you to copy the structure of a view into a table. |
MSSQL | Does not support the standard. Instead, MSSQL has a special SELECT ... INTO ... FROM ... construct which can be combined with an impossible WHERE-clause to copy structure only: SELECT * INTO t2 FROM t1 WHERE 1<>1 The source (t1 ) may be a view, as well as a table.
|
MySQL | Complies with the core of the feature (T171), but not with the extended features (T173). MySQL does not allow you to copy the structure of a view into a table. |
Oracle | Does not support the standard. Oracle lets you copy a table structure using a special CREATE TABLE ... AS construct, combined with an impossible WHERE -clause: CREATE TABLE t2 AS SELECT * FROM t1 WHERE 1<>1 |
Informix | On my TODO. |
Standard | The SQL-standard states that relations are unordered, but result sets may be ordered when returned to the user through a cursor:
The DBMS may additionally allow The standard doesn't specify how NULLs should be ordered in comparison with non-NULL values, except that any two NULLs are to be considered equally ordered, and that NULLs should sort either above or below all non-NULL values. However, the DBMS may optionally (as part of feature ID T611, "Elementary OLAP operations") allow the user to specify whether NULLs should sort first or last: |
PostgreSQL | As well as in cursor definitions, it allows ORDER BY in other contexts. By default, NULLs are considered higher than any non-NULL value; however,(since version 8.3) this sorting behaviour may be changed by adding |
DB2 | As well as in cursor definitions, it allows ORDER BY in other contexts. NULLs are considered higher than any non-NULL value. |
MSSQL | As well as in cursor definitions, it allows ORDER BY in other contexts. NULLs are considered lower than any non-NULL value. |
MySQL | As well as in cursor definitions, it allows ORDER BY in other contexts. NULLs are considered lower than any non-NULL value, except if a |
Oracle | As well as in cursor definitions, it allows ORDER BY in other contexts. By default, NULLs are considered higher than any non-NULL value; however, this sorting behaviour may be changed by adding Beware of Oracle's strange treatment of empty strings and NULLs as the same 'value'. |
Informix | As well as in cursor definitions, it allows ORDER BY in other contexts. NULLs are considered lower than any non-NULL value. |
Objective: Want to only get n rows in the result set. Usually only makes sense in connection with an ORDER BY
expression.
Note: This is not the same as a top-n query — see next section .
Note also: Some of the queries below may not be legal in all situations, such as in views or sub-queries.
Standard | The SQL standard provides three ways of performing a 'simple limit':
|
PostgreSQL | Supports all standards-based approaches. In old PostgreSQL versions (versions 8.3 and older), a special PostgreSQL (and MySQL) specific method was used:
Note that Documentation: |
DB2 | Supports all standards-based approaches. Documentation:
|
MSSQL | Supports the ROW_NUMBER() (since MSSQL 2005) and cursor standards-based approaches; doesn't support FETCH FIRST . MSSQL 2000 didn't support |
MySQL | Doesn't support the standard. Alternative solution:
|
Oracle | Supports ROW_NUMBER ; doesn't support FETCH FIRST . As Oracle doesn't allow
A reader of this page told me that using the Oracle-specific |
Informix | Doesn't support ROW_NUMBER(), nor FETCH FIRST. Alternative solution (which is illegal in plain sub-queries): |
Objective: Like the simple limit-query above, but include rows with tie conditions. Thus, the query may return more than n rows.
Some call this a quota -query.
The following examples are based on this table:
SELECT * FROM person ORDER BY age ASC; +----------+-------------+-----+ |PERSON_ID | PERSON_NAME | AGE | +----------+-------------+-----+ | 7 | Hilda | 12 | | 8 | Bill | 12 | | 4 | Joe | 23 | | 2 | Veronica | 23 | | 3 | Michael | 27 | | 9 | Marianne | 27 | | 1 | Ben | 50 | | 10 | Michelle | 50 | | 5 | Irene | 77 | | 6 | Vivian | 77 | +----------+-------------+-----+
Now, we only want the three (n =3) youngest persons displayed, i.e. a result set like this:
+----------+-------------+-----+ |PERSON_ID | PERSON_NAME | AGE | +----------+-------------+-----+ | 7 | Hilda | 12 | | 8 | Bill | 12 | | 4 | Joe | 23 | | 2 | Veronica | 23 | +----------+-------------+-----+
Standard | With standard SQL, there are two principal ways to obtain the wanted data:
In the article Going To Extremes by Joe Celko , there is a description of yet another principle for performing quota queries, using scalar subqueries . Scalar subqueries are more tedious to write but might yield better performance on your system. |
PostgreSQL | Supports the fast standard SQL variant. In version 8.3 and older, PostgreSQL only supported the slow standard SQL query variant. In practice, a PostgreSQL-only method was used instead, in order to obtain acceptable query performance: (Change |
DB2 | Supports the fast standard SQL variant. |
MSSQL | Supports the fast standard SQL variant. MSSQL 2000 supported the slow standard SQL variant. In practice, a MSSQL-only expression had to be used instead, in order to obtain acceptable query performance: |
MySQL | Supports the slow standard SQL solution. In practice, this MySQL-specific solution should be used instead, in order to obtain acceptable query performance:
(Change The offset-value 2 is the result of n-1 (remember: n is 3 in these examples). The second argument to the |
Oracle | Supports the fast standard SQL variant. However, as Oracle doesn't like "AS... " after subqueries (and doesn't require naming of subqueries), the query has to be paraphrased slightly:
(Change |
Informix | On my TODO. |
Objective: Want to only get n rows in the result set, and we want the first skip rows in the result set discarded. Usually only makes sense in connection with an ORDER BY
expression.
In the recipes below, basic ordering is ASCending, i.e. lowest-first queries. If you want the opposite, then change ASC->DESC
and DESC->ASC
at the places emphasized like this .
Standard | The SQL standard provides three ways of performing 'limit with offset':
|
PostgreSQL | Supports all the standards-based approaches. In version 8.3 and older, cursors should be used, or a special construct: Documentation: |
DB2 | Supports the window function based approach. Regarding cursors: DB2 for Linux/Unix/Windows doesn't support Documentation : OLAP functions , the FETCH statement . |
MSSQL | Supports the window function and cursor based approaches. MSSQL 2000 didn't support |
MySQL | Doesn't support the standard approaches. Alternative solution: SELECT columns In older versions of MySQL, the LIMIT-syntax is less clear: |
Oracle | Supports ROW_NUMBER() . I'm unsure if Oracle's cursor support is standards-compliant. As Oracle doesn't accept
A reader of this page told me that using the Oracle-specific |
Informix | Supports neither OFFSET ...FETCH FIRST nor ROW_NUMBER . Supports cursors. An alternative to using cursors is to us an Informix-specific construct: |
FETCH FIRST/LIMIT/TOP queries with offset are often used in a result presentation context: To retrieve only—say—30 rows at a time so that the end-user isn't overwhelmed by the complete result set, but instead is offered a paginated result presentation. In this case, be careful not to (only) sort on a non-unique column.
Consider the following example (where PostgreSQL is used):
SELECT * FROM person ORDER BY age ASC; person_id | person_name | age -----------+-------------+----- 7 | Hilda | 12 8 | Bill | 12 4 | Joe | 23 2 | Veronica | 23 3 | Michael | 27 9 | Marianne | 27 1 | Ben | 50 10 | Michelle | 50 5 | Irene | 77 6 | Vivian | 77
When ordering is performed on the non-unique age-value, ties may occur and it's not guaranteed that the DBMS will fetch the rows in the same order every time.
Instead of the above listing, the DBMS is allowed to return the following display order where Michael and Marianne are displayed in the opposite order compared to above:
SELECT * FROM person ORDER BY age ASC; person_id | person_name | age -----------+-------------+----- 7 | Hilda | 12 8 | Bill | 12 4 | Joe | 23 2 | Veronica | 23 9 | Marianne | 27 3 | Michael | 27 1 | Ben | 50 10 | Michelle | 50 5 | Irene | 77 6 | Vivian | 77
Now, suppose the end-user wants the results displayed five rows at a time. The result set is fetched in two queries where the DBMS happens to sort differently, as above. We will use PostgreSQL's legacy syntax in the example:
SELECT * FROM person ORDER BY age ASC LIMIT 5; person_id | person_name | age -----------+-------------+----- 7 | Hilda | 12 8 | Bill | 12 4 | Joe | 23 2 | Veronica | 23 3 | Michael | 27 SELECT * FROM person ORDER BY age ASC LIMIT 5 OFFSET 5; person_id | person_name | age -----------+-------------+----- 3 | Michael | 27 1 | Ben | 50 10 | Michelle | 50 5 | Irene | 77 6 | Vivian | 77
Notice that Marianne was not displayed in any of the two split result set presentations.
The problem could be avoided if the result set ordering had been done in a deterministic way, i.e. where the unique person_id value was considered in case of a tie:SELECT * FROM person ORDER BY age ASC, person_id ASC ...
This is safer than to pray for the DBMS to behave in a predictable way when handling non-unique values.
Note : If the table is updated between parts of the result set pagination, then the user might still get an inconsistent presentation. If you want to guard against this, too, then you should see if use of an insensitive cursor is an option in your application. Use of cursors to paginate result sets usually require that your application is stateful , which is not the case in many web-application settings. Alternatively, you could let the application cache the complete result set (e.g. in a session if your web application environment provides for sessions).
Standard | An optional SQL feature is row value constructors (feature ID F641). One handy use of row value constructors is when inserting several rows at a time, such as:
— which may be read as a shorthand for
|
PostgreSQL | Supported .(since version 8.2) |
DB2 | Supported . |
MSSQL | Supported .(since version 2008) |
MySQL | Supported . |
Oracle | An Oracle-specific kludge:INSERT INTO tablename |
Informix | On my TODO. |
Standard | The BOOLEAN type is optional (has feature ID T031), which is a bit surprising for such a basic type. However, it seems that endless discussions of how NULL is to be interpreted for a boolean value is holding BOOLEAN from becoming a core type. The standard says that a BOOLEAN may be one of the following literals:
The DBMS may interpret NULL as equivalent to UNKNOWN. It is unclear from the specification if the DBMS must support UNKNOWN, NULL or both as boolean literals. In this author's opinion, you should forget about the UNKNOWN literal in order to simplify the situation and let the normal SQL three-way logic apply. It's defined that TRUE>FALSE (true larger than false). |
PostgreSQL | Follows the standard. Accepts NULL as a boolean literal; doesn't accept UNKNOWN as a boolean literal. |
DB2 | Doesn't support the BOOLEAN type. Judging from various JDBC-documentation, it seems that IBM recommends a CHAR(1) field constrained to values '0' and '1' (and perhaps NULL) as the way to store boolean values. |
MSSQL | Doesn't support the BOOLEAN type. Possible alternative type: the BIT type which may have 0 or 1 (or NULL) as value. If you insert an integer value other than these into a field of type BIT, then the inserted value will silently be converted to 1. Rudy Limeback has some notes about oddities with the MSSQL BIT type. |
MySQL | Offers a non-conforming BOOLEAN type. MySQL's BOOLEAN is one of many aliases to its TINYINT(1) type. MySQL accepts the literals TRUE and FALSE as aliases to 1 and 0, respectively. However, you may also assign a value of — e.g. — 9 to a column of type BOOLEAN (which is non-conforming). If you use JDBC with MySQL, then BOOLEAN is the preferred type for booleans: MySQL's JDBC-driver implicitly converts between Java's boolean and MySQL's pseudo-BOOLEAN type. Side note: MySQL has a |
Oracle | Doesn't support the BOOLEAN type. Judging from various JDBC documentation and a discussion at Ask Tom , it seems that Oracle recommends NUMBER(1) as the way to store boolean values; it's probably wise to constrain such columns to values 0 and 1 (and perhaps NULL). |
Informix | On my TODO. |
Warning to JDBC users:
According to the JDBC standard, getBoolean() must convert a SQL-'value' of NULL to the false Java value. To check if the database-value was really NULL, use wasNull() .
For the following section, I have used this test-SQL to try to illuminate differences (unfortunately, even standard SQL as simple as this has to be adjusted for some products):
Test steps:CREATE TABLE chartest (
charval1 CHAR(10) NOT NULL,
charval2 CHAR(10) NOT NULL,
varcharval VARCHAR(30) NOT NULL
);
INSERT INTO chartest VALUES ('aaa','aaa','aaa');
INSERT INTO chartest
VALUES ('aaaaaa','aaa','aaa'); -- should truncate to 'aaaaaa'
INSERT INTO chartest
VALUES ('aaaaaaaaaaaa','aaa','aaa'); -- should raise error
SELECT * FROM chartest; -- should show two rows
DELETE FROM chartest WHERE charval1='aaaaaa';
SELECT * FROM chartest; -- should show one row
SELECT * FROM chartest WHERE charval1=varcharval;
SELECT charval1 || 'X' AS res FROM chartest;
SELECT CHAR_LENGTH(charval1 || charval2) AS res FROM chartest;
SELECT CHAR_LENGTH(charval1) + CHAR_LENGTH(charval2)
AS res
FROM chartest;
Expected results, after CREATE and INSERTs:
SELECT * FROM chartest; -- should show two rows CHARVAL1 CHARVAL2 VARCHARVAL ========== ========== ============================== aaa aaa aaa aaaaaa aaa aaa DELETE FROM chartest WHERE charval1='aaaaaa'; SELECT * FROM chartest; -- should show one row CHARVAL1 CHARVAL2 VARCHARVAL ========== ========== ============================== aaa aaa aaa SELECT * FROM chartest WHERE charval1=varcharval; CHARVAL1 CHARVAL2 VARCHARVAL ========== ========== ============================== aaa aaa aaa SELECT charval1 || 'X' FROM chartest AS res; res =========== aaa X SELECT CHAR_LENGTH(charval1 || charval2) AS res FROM chartest; res =========== 20 SELECT character_length(charval1) + character_length(charval2) AS res FROM chartest; res ============ 20
Standard |
|
PostgreSQL | Stores CHARs in space padded form, but violates the standard by (conceptually) truncating trailing white-space before performing most functions, operators, and comparisons (like the CHARACTER_LENGTH -function and the concatenation(|| ) operator). |
DB2 | Follows the standard. |
MSSQL | Generally follows standard, but (conceptually) truncates trailing white-space before performing some functions (at least before LEN() ). |
MySQL | Breaks the standard by silently inserting the string, truncated to specified column CHAR-length. (It's actually not completely silent, as it issues warnings if values were truncated: If you manually check for warnings, you will know that something bad happened, but not which of the rows are now invalid.) Violates the standard by effectively truncating all trailing spaces. The documentation states that MySQL truncates trailing spaces when CHAR values are retrieved . That may be true, but |