Which database invented the "limit" SQL query syntax? - sql

MySQL has a nice feature (although non standard) which allow to query resultsets' limit, offset as
SELECT * FROM TABLE LIMIT M, N;
Is it created by MySQL? or Postgres?

According to Wikipedia, Rasmus Lerdorf (the original creator of PHP) first used the "LIMIT x" syntax in the mSQL database:
He has contributed to the Apache HTTP Server and he also came up with the LIMIT clause and added it to the mSQL Database in 1995. It is the origin of the LIMIT clauses found in MySQL and PostgreSQL.
Limiting the result set is now also standardized, but with a more verbose syntax:
SELECT *
FROM T
FETCH FIRST 10 ROWS ONLY

Postgres added the LIMIT syntax in v6.5, released on June 9th, 1999.
Based on the documentation, MySQL had LIMIT syntax starting at v3.23 (production release Jan, 2001). But the docs in the URL are for 4.1, which wasn't released until 2004.
SQL Server didn't have TOP until SQL Server 2000, shipping in late 2000.
Oracle has had ROWNUM since Oracle 6, released in 1988. Scarier still, is that it can perform better in cases than ROW_NUMBER!

MySQL copied it from mSQL, which Rasmus Lerdorf lays claim to implementing:
http://itc.conversationsnetwork.org/shows/detail3298.html
That's not to say mSQL was the first, but it looks like earliest of those mentioned so far.

Between PostgreSQL and MySQL, PostgreSQL copied the syntax LIMIT from MySQL (in v6.5), and added the OFFSET syntax (it may be that that was copied as well, but I think mysql only had the comma-syntax back then). It was then (7.2) changed to only allow "LIMIT foo OFFSET bar" because the MySQL syntax was unclear.

I know that Rdb (originally a DEC Corporation product, now available from Oracle) had LIMIT TO n ROWS back in 1991.
Share and enjoy.

Related

Is SELECT INTO T-SQL?

I'm working in a project where I have been explicitly required to not use T-SQL syntax. The application we are using supports T-SQL but we are not allowed to use it to avoid potential migration issues.
My question is: is the SELECT ... INTO statement T-SQL or SQL? If it is T-SQL, is there a specific SQL query to copy an existing table into a new one? (I have tried with CREATE TABLE AS .. FROM but it doesn't work).
Sounds like a very basic question but I haven't been able to find the answer anywhere. Thus, in addition to the question above, it would be very helpful to know if there is a guide/dictionary/website that collects only the standard SQL syntax.
Thanks!
I think they recommend you to use ANSI SQL, instead of T-SQL (SQL Server) or PL-SQL (ORACLE). Considering it as common requirement, every database vendor provide their own way of implementing this requirement. When you use ANSI SQL, you will not have migration issues, when you move from one database vendor to another database vendor.
SQL SERVER
SELECT * INTO new_table
FROM existing_table
ORACLE & ANSI-SQL
CREATE TABLE new_table
AS SELECT * FROM existing_table
is SELECT INTO TSQL or SQL?
Neither. The MySQL documentation claims that SELECT INTO is a Sybase extension to standard sql. As such I don't think you can accurately say it's either of these, but you can say that it's neither. It is indeed used in T-SQL, as well as some other database vendor products, to create a table from a query. The SQL standard says that queries with that goal should be formed as CREATE TABLE blah AS SELECT .... Oracle/MySQL, for example, use the standard form though you can see them use SELECT INTO in a different context, to assign data to variables in stored procedures
If you want to avoid use of this non standard syntax when creating and populating a table then you'll have to:
CREATE TABLE blah (column spec to match query output)
INSERT blah (select query here)
But then you run into nuances like "sqlserver calls it datetime/datetime2 but oracle calls it date/timestamp"
And ultimately you'll probably get into a situation where you just can't use one form of sql to do all you want..
I'd imagine most libraries that do data access on multiple underlying databases have mechanisms to use vendor specific terminology where required
From the answers, it appears you might need to specify which SELECT INTO you're talking about. The other answers seem to suggest there exists some kind of SELECT ... INTO <table-name> when there is also a kind of SELECT ... INTO <local-variable-name list>. The latter is used in embedded SQL for making SQL interact with variables of the host language program. I'm not certain but that variant may also be used in the part of the SQL language that deals with procedures written in SQL (the SQL/PSM part of the standard).
A "reference" that covers "only the standard SQL syntax" is, in principle, the ISO standard document itself, only available by purchase from ISO (and yes, it's ISO not ANSI - ANSI does nothing more than rubberstamping the ISO document after removing all the names of non-US contributors). And not the easiest kind of literature. There are "draft" versions floating around on the internet that might deviate from the published final standards. E.g. http://www.wiscorp.com/sql200n.zip. Note that this is a SQL:2008 draft. Current standard version is SQL:2011. And it's several thousands of pages, so I guess that covers your question "Is all the syntax covered in w3schools standard SQL". (Hint : no)

Microsoft Query; use of SubString in Excel

I'm trying to filter results from a Query i have created in Microsoft Query to pull data from a database into my Excel sheet. Specifically I'm trying to filter out based on the nth character of a string.
I can easily filter out the based on the first char:
SOPOrderReturnLine.ItemCode Like 'A25%'
But I have no idea how I could filter to show only entries where the 10th char = "A". I'm sure I have to use a Substring function, but it's not familiar to me and I'm struggling to get it to work.
Try to edit your sql query and enter the following statement:
select * from SOPOrderReturnLine where substring(SOPOrderReturnLine.ItemCode,10,1) = 'A';
The statement should work for a MySql database as well as for an Sql Server in the background; (I've tested it with an MySql database).
Hope this helps.
In MSQuery (Jet under the covers, I think), the function is Mid.
SELECT * FROM tblLocation WHERE (Mid(LocationName,2,1)='e')
to find a lower case 'e' in the second location.
I assume when you say MS Query, you are running a query against a DBMS (SQL Server or some other via ODBC).
The use of substr, substring or mid should work, depending on which DBMS. That said, unless you're using MS Access, I think most DBMSs will support the underscore character as "any single character." It might even work in Access, but I don't know for sure. Therefore, I think in addition to the suggestions you've gotten, this will also work in most cases:
SOPOrderReturnLine.ItemCode Like '_________A%'
If you want to use substring, don't hold me to these, but I think:
Oracle / DB2 / SQLite - substr
Microsoft SQL Server / Sybase / MySQL - substring
MS Access - mid
PostgreSQL -substr or substring

SQL error while using limit keyword in Derby

I have used limit keyword as:
"select * from empl3 limit "+4
But I am getting the error as:
Syntax error: Encountered "4" at line 1, column 27.
I am using Derby database.
As documented in the manual there is no LIMIT clause in Derby.
Derby uses the SQL standard for limiting the number of rows:
select *
from empl3
fetch first 4 rows only;
I believe the +4 after the select statement is giving you the error. You should return all of your data necessary for you page select * from empl3, but handle the paging in your page itself.
The key is in your words I am using derby database - see Does Derby support a LIMIT command.
You can either use the workaround suggested in the linked FAQ, or implement paging on your own, as suggested by WEI_DBA.

Documentum Query Language Syntax

I would like to know if there is a way in DQL to fetch the rows based on the start and end row values. (Like row number 1 - 1000, 1001 - 2000). ( Similar to what rownumber in oracle queries).
This input will be of great help.
For Documentum DQL query Pagination you can (should) use RETURN RANGE hint, like this
select * from dm_document where object_name like 'ABC%' enable(RETURN_RANGE 1001 2000 1000 'object_name ASC' )
it will sort documents by object_name and then return up to 1K rows, starting from the row number 1001 ending to 2000, optimized for 1K top (sorted) rows.
Syntax is RETURN_RANGE starting_row ending_row [optimize_top_row] 'sorting_clause'
It works since Content Server CS 6.6 with any underlying database.
Documentum Community Ref
I do not believe this is possible using DQL. However, you can consult the DQL Reference Guide (check Powerlink), which contains information about DQL hints (there is a section on them). There is a discussion of passthrough hints that allow you to pass hints through to the underlying RDBMS. The hints available depend on whether it is Oracle, SQL Server, DB2, etc.
This is an excerpt from that section:
Passthrough hints are hints that are passed to the RDBMS server. They
are not handled by Content Server.
SQL Server and Sybase have two
kinds of hints: those that apply to individual tables and those that
apply globally, to the entire statement. To accommodate this, you can
include passthrough hints in either a SELECT statement’s source list
or at the end of the statement. The hints you include in the source
list must be table‑specific hints. The hints you include at the end of
the statement must be global hints. For example, the following
statement includes passthrough hints for Sybase at the table level and
the statement level:
SELECT "r_object_id" FROM "dm_document" WITH
(SYBASE('NOHOLDLOCK')) WHERE "object_name"='test' ENABLE (FORCE_PLAN)
For DB2 and Oracle, include passthrough hints only at the end of the
SELECT statement.

Hibernate setMaxResults() not working for Sybase database query

Either of the following two approaches to executing a simple Hibernate query with limited results do not work in Sybase. They both result in a SybSQLException: Incorrect syntax near '#p0'.
Query q = session.createQuery( "from Record" );
q.setMaxResults( 50 );
q.list();
or
Criteria criteria = session.createCriteria( Record.class );
criteria.setMaxResults( 50 );
criteria.list();
It appears the actual SQL generated in both of these cases looks like...
select top ? record_id, etc...
and Sybase is balking at the ?, which Hibernate is not filling in with the value 50 (this is my guess). I've searched everywhere and while others have encountered a similar error, it was not due to attempting to limit the results.
I can execute a direct SQL statement such as 'select top 50 from Record' and it works perfectly, so I know my version of Sybase supports the syntax.
I'm using Hibernate 3.2 and Sybase ASE 15.0.2
Perhaps you configured Hibernate to use a wrong SQL dialect.
It looks like HSQLDialect is the only dialect that can produce limit ? ?, and it's definitely a wrong choice for Sybase.
See also:
3.4.1. SQL Dialects
Try putting setFirstResult(1) as well like:
Criteria criteria = session.createCriteria(Record.class);
criteria.setFirstResult(1);
criteria.setMaxResults(50);
criteria.list();
Do you still get the same error?
setMaxResults() is typically used together with setFirstResult() to implement paging. Eg. the first query returns records 1 to 1000, the second query returns 1001 to 2000, and so on. Try using together.
setFetchSize() control how many rows are fetched at a time by the JDBC driver.(if implemented) So, if you for example have setMaxResults(1000) and setFetchSize(100) the query will return no more than 1000 rows, and will do so in batches of 100 rows at a time.
You can use createSQLQuery option which works with TOP.