0

if suppose we have written a SQL select command without using order by clause then what will be the column on which the sorting will done while displaying records of select command...

create table test
(
ID int identity (1,1) primary key,
em_id int,
name varchar(20),
address varchar(20)
mobile number int
)

suppose the table is like above structure and select command like

select * from test

then how can i check the column name on which the sorting is done by sql..

2
  • 2
    In SQL, the language, if you want a specific ordering add an ORDER BY clause. There's no other way to specify ordering. Even when a specific storage order will return data in whatever way is cheapest unless an ORDER BY clause is specified. That's quite common eg when parallel processing is used Commented Dec 10, 2018 at 11:58
  • Well, not possible to enforce some default column sorting at the creation of the table. But in Sql Server 2012 I assume you could create a view with an ORDER BY if you combine it with a OFFSET 0 ROWS or a TOP. But unsure if that would work in SQL Server 2008. Commented Dec 10, 2018 at 12:27

2 Answers 2

3

In a query like this:

select *
from test;

The result set is not sorted.

SQL tables represent unordered sets. Running the query multiple times can result in the same result set -- but with the rows in a different order.

If you want a specific ordering in the result set, you need to include an order by.

You may be confusing "ordering" with the clustered index. If so, you can find the keys in a clustered index (if one exists) using the system metadata tables. But the data is not guaranteed to be returned by the clustered index unless you have an order by in the query.

Sign up to request clarification or add additional context in comments.

3 Comments

so how the date will be fetch from the data file? data will be fatch on as per the insert order?
@SarveshBandekar No, as Gordon told you: There is no implicit sort order. The same call can return differently sorted sets. If you need a sort order, you must use an ORDER BY on the outermost query.
okkk thanks i got it.... sorry for this silly question but if suppose i have written a order by clause but then also i want to check the order by column in statistics only then how can i check the column same....
0

If you want a specific ordering in the result set, you need to include an order by clause. By default order by(if you specify order by clause) is in ASC order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.