If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > basic sql question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-29-06, 09:54
payawaljohn payawaljohn is offline
Registered User
 
Join Date: Oct 2006
Location: amara no meikyuu
Posts: 15
basic sql question

using only the keywords
a. insert
b. select
c. delete
d. update
e. where
how do i sort a list.

(note that the keyword "sort" cannot be used)

take for example a list with 5 elements
a. 1782^12 + 1841^12 = 1922^12 (which disproves fermats last theorem)
b. 3987^12 + 4365^12 = 4472^12 (another proof of fermats error)
c. 1729 (smallest number expressible as the sum of two cubes)
d. largest known mersenne prime
e. pi*(10^40000) (the digit in the units place is 1)

it should yield c, a, b, e, d.
Reply With Quote
  #2 (permalink)  
Old 10-29-06, 10:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
is this some kind of homework question?

there is no "sort" keyword in SQL

however, there is the ORDER BY clause -- give that a try
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-29-06, 10:55
payawaljohn payawaljohn is offline
Registered User
 
Join Date: Oct 2006
Location: amara no meikyuu
Posts: 15
"order by" also not allowed
Reply With Quote
  #4 (permalink)  
Old 10-29-06, 11:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
tell your teacher that ORDER BY is allowed in the real world
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-29-06, 11:24
payawaljohn payawaljohn is offline
Registered User
 
Join Date: Oct 2006
Location: amara no meikyuu
Posts: 15


sure ill tell him...
Reply With Quote
  #6 (permalink)  
Old 10-29-06, 11:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
when your class gets around to reviewing the possible solutions to this question, i would be very interested in seeing the so-called "correct" answer
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 10-30-06, 02:18
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
What about "SELECT DISTINCT column_name FROM tbl" ?
Doesn't a DISTINCT guarantee ordering ?
(I know for sure it does on all RDBMS systems I have seen, but is this required behaviour in terms of the SQL language specs?)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #8 (permalink)  
Old 10-30-06, 02:36
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Oracle must be one of the databases you haven't seen, Peter
Code:
SQL> select distinct ename from emp;

ENAME
----------
ALLEN
JONES
TIGER
FORD
BURGLAR
CLARK
MILLER
SMITH
SCOTT
TURNER
ADAMS
BLAKE
KING
JAMES

14 rows selected.

SQL>
Distinct didn't order those names.
Reply With Quote
  #9 (permalink)  
Old 10-30-06, 02:39
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Remarkable!

Just curious: do you know how Oracle is implementing the DISTINCT in this case?
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #10 (permalink)  
Old 10-30-06, 03:23
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Hm, now you got me thinking ... I checked Oracle documentation, but it says that DISTINCT does, well, what it does - ordering is never mentioned (or I couldn't find it).

However, Googling around found this Tuning queries page. It says something like this:
Quote:
Oracle (and some other systems) may produce results in lname order. If you get the results you want, use your performance tools to measure the difference between this technique and using ORDER BY. However, this technique can fail if the indexes change. Be sure to document it and note it's a trick.
So I tried it:
Code:
select distinct ename from emp where ename >= ' ';
- nothing happened; records were still shuffled. Then I grasped for "... if the indexes change" sentence, and created an index on 'ename' column and - voila!
Code:
SQL> create index i1 on emp (ename);

Index created.

SQL> select distinct ename from emp where ename >= ' ';

ENAME
----------
ADAMS
ALLEN
BLAKE
BURGLAR
CLARK
FORD
JAMES
JONES
KING
MILLER
SCOTT
SMITH
TIGER
TURNER

14 rows selected.

SQL>
Records really are ordered!

When I ran the same query WITHOUT the DISTINCT keyword, the result was the same - records were ordered by 'ename'. So I broadened it with another column, added another condition into the WHERE clause and got different results: if the second column from the WHERE clause was indexed, records were NOT ordered. If I dropped that index, they were again ordered. Etc. etc.

It is a nice little trick; I should take some time to research it better (what happens when index changes, use of a more complicated WHERE clause, etc.), but - as a trick, it surely is interesting!

Thank you, Peter!
Reply With Quote
  #11 (permalink)  
Old 10-30-06, 05:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Peter.Vanroose
... but is this required behaviour in terms of the SQL language specs?
the SQL standard is pretty clear: if you want a particular ordering, you must use ORDER BY
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 10-30-06, 05:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oh, and you guys should take your DISTINCT idea and apply it to the 5 elements in the list as given in post #1

i still don't see how e. comes before d.
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 10-30-06, 09:44
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
What about "GROUP BY"?? Won't that force a sort??
Reply With Quote
  #14 (permalink)  
Old 10-30-06, 10:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by urquel
What about "GROUP BY"?? Won't that force a sort??
i cannot speak for oracle, but as this is the SQL forum and not the oracle forum, the answer, again, is "not necessarily"

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 10-30-06, 15:12
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Can you post the URL or a JPG of the assignment? I suspect that there are one or more critical pieces missing that are keeping us from finding a satisfactory solution.

I've found several possible solutions that acheive the desired results, but none of them satisfy me... Something is missing, subtly wrong, and that is bugging me!

-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On