Ordering in selection result. Postgres - sql

Does the sequence of records in SELECT QUERY in result always the same?
I mean that if the first result of operation returns the following sequence:
first record
second record
third record
All other select * from t queries always return records in the same sequence.

A SQL query -- like a SQL table -- represents an unordered set. There is no ordering, unless an ORDER BY is present for the outermost SELECT.
As an unordered set, the same query can return results in a different order each time it is run.
So, if you want results in a particular order, use ORDER BY.
I should add that if multiple rows have the same key, then these rows can appear in any order, even with an ORDER BY. In general, you should ensure that the keys in the ORDER BY uniquely define each row (say by including the primary key as the final key).

Related

Are big-query results always ordered, that is: using OFFSET makes sense to skip rows?

In other words does a select query order results every time, so these 2 will always produce unique values:
select *
from bigquery-public-data.crypto_ethereum.balances
limit 10 OFFSET 100
select *
from bigquery-public-data.crypto_ethereum.balances
limit 10 OFFSET 2000
Assuming of course the table has unique values...I am just curious if without using "order" clause the table is always deterministic/consequetive or can the results duplicate if they're returned indeed at random? 10x!
I am just curious if without using "order" clause the table is always deterministic/consequetive or can the results duplicate if they're returned indeed at random.
No. SQL tables represent unordered set of rows. There is no inherent ordering of the rows. Unless an order by clause is specified, there is no guarantee that two consequent executive of the same query would yield an indentical result. The database is free to return the rows in whatever order it likes.
As a consequence, the results of a query with a row-limiting clause but no order by clause are not deterministic. Do add an order by clause the these queries, or you will sooner or later run into suprising and hard-to-debug behaviors.

Auto Increment selection SQLite

I have a column named id in my SQLite database which is auto-increment, Primary Key, Unique.
Is the result of the following query guaranteed to be the smallest value of id in the database and does this correspond to the "oldest" (as in a FIFO) row to be inserted?
SELECT id FROM table LIMIT 1
The SQLite documentation is quite explicit:
If a SELECT statement that returns more than one row does not have an
ORDER BY clause, the order in which the rows are returned is
undefined. Or, if a SELECT statement does have an ORDER BY clause,
then the list of expressions attached to the ORDER BY determine the
order in which rows are returned to the user.
The LIMIT is applied after an ORDER BY would be, so I don't think it affects the application of this statement.
Hence, if you want the first row, use ORDER BY:
SELECT id
FROM table
ORDER BY id
LIMIT 1;
Note that if id is a primary key, this will add basically no overhead.
I should emphasize that in practice you are probably going to get the smallest id without the ORDER BY. However, it is a really, really bad idea to depend on behavior that directly contradicts the documentation.
Is the result of the following query guaranteed to be the smallest value of id in the database
Yes. However if the table is empty or the id column is NULL, it could also return NULL
and does this correspond to the "oldest" (as in a FIFO) row to be inserted?
No, there's no guarantee of that.

Table Value parameter cursor order

I'm passing a single string column table parameter (no other option, must be one column). I've read that there's no guarantee of order on a TVP, but all the articles I've read are referring to a select statement. If I run a cursor on a TVP, will it always read from first record to last?
Table-valued parameter is a table, a temporary table.
Any table, including temporary tables in SQL Server is an unordered set of rows.
When you write
SELECT Column1 FROM Table1
the server can return rows in any order it chooses. It will return all rows of a table, of course, you just can't predict in which order.
If you need some specific order, then you have to specify ORDER BY clause, like this:
SELECT Column1 FROM Table1 ORDER BY Column2
All this applies to TVP and cursors.
If you define a cursor without ORDER BY, it will read through all rows, but in some unspecified order, which may vary with each run. If you need the cursor to process rows of TVP in some specific order, then specify the ORDER BY clause in the SELECT statement of the cursor definition.
In other words, the cursor will always read from the "first" record to the "last". If it is important to the logic of the cursor to have defined what is "first" and what is "last" you need to add ORDER BY clause.

ORDER BY query giving two different outputs

I have a student table with 8 records in columns [name, address, TotalFees].
Even though I execute two same ORDER BY queries on single table I get two different outputs. I have attached the screenshot below.
Same query on same table with different output:
The issue that you have is that the keys for the order by do not uniquely define each row. In particular, "khar" is repeated for most of the rows (your results suggest that you are using a case-insensitive collation).
In SQL databases, the sort is not stable. A stable sort is one that preserves the original ordering of values with the same key. Because the sort is not stable, the results -- for identical keys -- can come out in any order.
Why isn't the sort stable? That is easy in retrospect. SQL tables and result sets represent unordered sets. There is no initial ordering, so the sorting is not stable.
The normal way to address this is to include a unique id as the final key in the order by: order by address, id. With your data, though, it is unclear what the unique key is.
Combination of Address and Name won't produce duplicates so you can try this,
select *From student
order by address,name
select top 3 * From student
order by address,name

hsqldb: do I have to ORDER BY to ensure consistent selection order?

I have a table containing samples. The inserted samples are already naturally ordered by the timestamp.
My question is this - when I SELECT from the table do I have to use the ORDER BY clause to ensure the fetched samples are ordered by the timestamp?
Rows in a relation database are NOT sorted (Picture them as balls in a basket. Which one is the "first"?)
The only way (really, the only) to get a consistently sorted result is to use ORDER BY.
You cannot rely on side effects of joins, group by. UNION, index retrieval or similar operators. They will never guarantee an order. The DBMS is free to choose to return the rows in whatever order it thinks is the fastest unless you specify an ORDER BY.
If an HSQLDB table T has a column C as primary key, or has any index on that column,
SELECT FROM T ORDER BY C
will return ordered rows without extra ORDER BY processing.
If there is a condition on the select, which uses an index on a different column, you can still force the use of the index for ORDER BY:
SELECT FROM T WHERE <some condition> ORDER BY C USING INDEX
But in this case, you should only use USING INDEX if most of the rows of the table will be returned. Otherwise it is better to leave the engine use the other index to reduce the table scan time.
USING INDEX is ignored if there is no index to use for ORDER BY.