Problem solve Get help with specific problems with your technologies, process and projects.

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
  cursor cust_cursor is
   select customer_id
     from customer
    order by customer_name;

  cust_record customer_cursor%rowtype;

  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;
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.

Dig Deeper on Oracle and SQL

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.

Please create a username to comment.