Ask the Expert

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.


    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

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: