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 first published in April 2002

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

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.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close