<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Sybase Default ordering when using distinct clause in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaa-p/13849494#M4880337</link>
    <description>&lt;P&gt;The results should be free to change order unless explicitly directed otherwise.&lt;/P&gt;
&lt;P&gt;As Volker mentioned, the order may depend on what is optimal for that database server. Because of this, different plans (index/table scan, nested join/hash join, etc.) will cause evaluation of the query in different orders and can easily change the order of the output.&lt;/P&gt;
&lt;P&gt;If you want an ordering you can rely on, you should specify this explicitly.&lt;/P&gt;</description>
    <pubDate>Tue, 11 Oct 2011 12:32:19 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2011-10-11T12:32:19Z</dc:date>
    <item>
      <title>Sybase Default ordering when using distinct clause</title>
      <link>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaq-p/13849492</link>
      <description>&lt;P&gt;Hello, We would like to know what is the default order sybase would use when using distinct in the select query.
Here is the use case (Simple table with an int, decimal, string and date columns)&lt;/P&gt;
&lt;P&gt;create table ##tab1 (intcol int, decicamlcol decimal(8,4), stringcol varchar(100), datecol date)
go&lt;/P&gt;
&lt;P&gt;Query 1:
select intcol, datecol  from  ##tab1&lt;/P&gt;
&lt;P&gt;Query 2:
select distinct  intcol , strcol , datecol, decimalcol from ##tab1&lt;/P&gt;
&lt;P&gt;Query 1 - sorts the results first by the intcol and then on the datecol as given in the select clause.&lt;/P&gt;
&lt;P&gt;However Query 2 - (which has a strcol in the selcet),  orders the results in the reverse fashion starting with the decicamlcol, datecol, then on the strcol followed  by the intcol.
We did see this observation only when a string column is used in the select.&lt;/P&gt;
&lt;P&gt;Can some please comment if there is a pattern sybase uses to order results when distinct is used and with out explicitly mentioning "order by" clause.&lt;/P&gt;
&lt;P&gt;Thank you.
Naresh&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2011 12:12:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaq-p/13849492</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-10-11T12:12:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sybase Default ordering when using distinct clause</title>
      <link>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaa-p/13849493#M4880336</link>
      <description>&lt;P&gt;Why would you like to rely on that?&lt;/P&gt;
&lt;P&gt;AFAIK, the query optimizer is &lt;EM&gt;totally free&lt;/EM&gt; in returning the result set in whatever "order" it considers optimal from a performance reason (including in no obvious order at all) - unless you specify an explicit ORDER BY clause.&lt;/P&gt;
&lt;P&gt;Without an ORDER BY the same query may return the result set in different "orders" for different runs - as the query optimizer is free to choose different plans for each run.&lt;/P&gt;
&lt;P&gt;Therefore I guess your observations might vary when used with different machines, different table contents and the like...&lt;/P&gt;
&lt;P&gt;BTW: Ary you using SQL Anywhere or Sybase ASE? - My comment (and the whole forum) is focussed on SQL Anywhere...&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2011 12:24:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaa-p/13849493#M4880336</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2011-10-11T12:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Sybase Default ordering when using distinct clause</title>
      <link>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaa-p/13849494#M4880337</link>
      <description>&lt;P&gt;The results should be free to change order unless explicitly directed otherwise.&lt;/P&gt;
&lt;P&gt;As Volker mentioned, the order may depend on what is optimal for that database server. Because of this, different plans (index/table scan, nested join/hash join, etc.) will cause evaluation of the query in different orders and can easily change the order of the output.&lt;/P&gt;
&lt;P&gt;If you want an ordering you can rely on, you should specify this explicitly.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2011 12:32:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaa-p/13849494#M4880337</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-10-11T12:32:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sybase Default ordering when using distinct clause</title>
      <link>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaa-p/13849495#M4880338</link>
      <description>&lt;P&gt;The underlying assumption in the original question is that sorting is used for duplicate elimination.&lt;/P&gt;
&lt;P&gt;While a SQL Anywhere server does support sort-based duplicate elimination, more often the optimizer will pick the (more efficient) hash-based duplicate-elimination strategy, which inherently does not generate a duplicate-free result set in a reliable order.&lt;/P&gt;
&lt;P&gt;To guarantee the ordering of tuples in a result set, one MUST use an ORDER BY clause.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2011 17:15:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sybase-default-ordering-when-using-distinct-clause/qaa-p/13849495#M4880338</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-10-12T17:15:41Z</dc:date>
    </item>
  </channel>
</rss>

