Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query theory question - single-statement vs multi-statement queries

When I write SQL queries, I find myself often thinking that "there's no way to do this with a single query". When that happens I often turn to stored procedures or multi-statement table-valued functions that use temp tables (of one sort or another) and end up simply combining the results and returning the result table.

I'm wondering if anyone knows, simply as a matter of theory, whether it should be possible to write ANY query that returns a single result set as a single query (not multiple statements). Obviously, I'm ignoring relevant points such as code readability and maintainability, maybe even query performance/efficiency. This is more about theory - can it be done... and don't worry, I certainly don't plan to start forcing myself to write a single-statement query when multi-statement will better suit my purpose in all cases, but it might make me think twice or a little bit longer on whether there is a viable way to get the result from a single query.

I guess a few parameters are in order - I'm thinking of a relational database (such as MS SQL) with tables that follow common best practices (such as all tables having a primary key and so forth).

Note: in order to win 'Accepted Answer' on this, you'll need to provide a definitive proof (reference to web material or something similar.)

like image 635
Michael Bray Avatar asked Jan 06 '10 18:01

Michael Bray


People also ask

What is the difference between the single query and the compound query?

The single query is one SELECT statement, whereas the compound query includes two or more SELECT statements. Compound queries are formed by using some type of operator to join the two queries.

Which are the three types of SQL statements?

Data Manipulation Language (DML) Statements. Transaction Control Statements. Session Control Statements.

What is the benefit of multiple SQL statements?

Advantages of PL/SQL It allows the users/developers to run multiple SQL statements at once by wrapping them in a block. It is compatible with SQL. It allows us to use all the SQL statements, data manipulation, cursor handling, transaction statements in PL/SQL blocks.

What are the different types of queries in SQL?

Five types of SQL queries are 1) Data Definition Language (DDL) 2) Data Manipulation Language (DML) 3) Data Control Language(DCL) 4) Transaction Control Language(TCL) and, 5) Data Query Language (DQL)


2 Answers

I believe it is possible. I've worked with very difficult queries, very long queries, and often, it is possible to do it with a single query. But most of the time, it's harder to mantain, so if you do it with a single query, make sure you comment your query carefully.

I've never encountered something that could not be done in a single query.
But sometimes it's best to do it in more than one query.

like image 197
Danielle Paquette-Harvey Avatar answered Sep 23 '22 12:09

Danielle Paquette-Harvey


At least with the a recent version of Oracle is absolutely possible. It has a 'model clause' which makes sql turing complete. ( http://blog.schauderhaft.de/2009/06/18/building-a-turing-engine-in-oracle-sql-using-the-model-clause/ ). Of course this is all with the usual limitation that we don't really have unlimited time and memory.

For a normal sql dialect without these abdominations I don't think it is possible.

A task that I can't see how to implement in 'normal sql' would be: Assume a table with a single column of type integer

For every row 'take the value at the current row and go that many rows back, fetch that value, go that many rows back, and continue until you fetch the same value twice consecutively and return that as the result.'

like image 37
Jens Schauder Avatar answered Sep 21 '22 12:09

Jens Schauder