Q

Selecting the alphabetically first customer

I need to select alphabetically the first customer. So, first I have to sort the table customer on customer_name...

and then select the first among them. I know Rownum does not work.

My Oracle version is 8.0.6. The following query is also not working:

Select customer_id, customer_name, phone from (select customer_id,
customer_name, phone from customer order by customer_name) where rownum <=1.


The query you are trying to run won't work because that syntax is not supported until 8i. So in 8.0.6 you'll need to do what you need in a function something like the following:

create function get_first_alpha_cust
return number
as
  cursor cust_cursor is
   select customer_id
     from customer
    order by customer_name;

  cust_record customer_cursor%rowtype;

begin
  open cust_cursor;
  for i in 1..1 loop
    fetch cust_cursor into cust_record;
    RETURN (cust_record.customer_id);
  end loop;

  close cust_cursor;
end;
/
This function could then be called to get the customer_id of the first alphabetically sorted customer. Of course, you can tweak the function to return whatever you want... but hopefully this will get you started!

For More Information

  • What do you think about this answer? E-mail the editors at editor@searchDatabase.com with your feedback.
  • The Best Oracle Web Links: tips, tutorials, scripts, and more.
  • Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

This was last published in April 2002

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close