Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > The three smallest numbers
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

The three smallest numbers

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 16 December 2005

Find the three smallest numbers from a column of numbers.



Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

Oracle and SQL
Oracle tutorial library: SearchOracle.com's learning guides
Can I specify Oracle column order in my database table?
Review: Oracle's 11g R2 database has some good and bad
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
Stored procedures in PL/SQL
PL/SQL functions and triggers in Oracle
Do I need a license for SQL Developer Data Modeler in Oracle?
Using the SQL GROUP BY clause for counting combinations
Oracle and SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


On several occasions, I have remarked on how much I really like short questions. Perhaps the person who asked this question knew this about me, but perhaps not.

Perhaps not, because this question sure looks like a homework assignment. The imperative Find is a dead giveaway.

There are two things I want to say about homework questions. First, I need to remind you that here at "Ask The Expert" we won't answer them. It's not ethical. Earn your marks by doing the work. You'll learn while you're doing, and that's the point.

The second thing I want to mention about homework questions is to encourage you not to give up. Sometimes SQL can be infuriatingly difficult. At those times, don't just post the question somewhere and see if it gets an answer. In the first place, it usually takes too long, especially here at "Ask The Expert" where the turnaround time is usually a week or three, and where the chances are, if it smells like homework, it will not be answered.

Don't post homework questions on Internet discussion forums either, because you're running the risk that you will actually get an answer, but the answer might have a high NZDF (Non-Zero Deviosity Factor©). An answer can be considered "devious" if it actually works but is either fatally flawed in some way, or uses some method which the student couldn't possibly have come up with alone, since it would indicate a mastery of the subject much greater than would be required to solve the simple question being answered.

Here's an example. Suppose you run this query:

select min(mycolumn) from mytable

This will give you the lowest value in the column. Write it down in case you forget it. Now use that value in the following query:

select min(mycolumn) from mytable
 where mycolumn <> value

Here value is the value you got from the first query. This second query will return the second lowest value. Write this down, too. Now run the following query:

select min(mycolumn) from mytable
 where mycolumn <> value1
   and mycolumn <> value2

Here, value1 and value2 are the values you wrote down from the first two queries. Write down what you got from this third query, et voilà, you will see that you have written down the three smallest numbers.

What's wrong with this solution? Well, it isn't exactly devious per se, but it's flawed. It does work, but it's clumsy and takes several steps. Would it be a good solution in the real world? Almost assuredly, assuming you only needed to do it once. Is it a good answer to hand in for a homework assignment? Well, you could try it, and see what happens.

But isn't there an elegant way to solve this problem using SQL? Yes, there is. This is why I encourage you not to give up. Keep trying.

One way to look at this question involves sorting the entire column. If you sort all the numbers in the column into ascending sequence, the smallest three numbers will come up first. This isn't the most elegant way to solve the problem, but it's practical. Now all you have to do is figure out how to sort a column of numbers into ascending sequence and take the top three values.

And that's as much of a hint as I'm willing to give for this homework assignment.




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts