Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:25:24+00:00 2026-06-15T01:25:24+00:00

I am doing a query that finds all cats born between two dates. Each

  • 0

I am doing a query that finds all cats born between two dates. Each cat has a name, or multiple names.

The initial query is something like

SELECT Id, Color FROM Cat WHERE Cat.BirthDate > dat_min AND Cat.BirthDate < dat_max;

I also have a table called CatName which for each Cat Id, has one or more names that this cat has been given by its different owners. I only want to return the first name that matches the Id in the CatName table, as part of the query. So something like:

SELECT Id, Color, Name FROM Cat JOIN CatName on .....

for a cat that has 5 names, will return 5 rows. I only want one row, the first one. If I was only retrieving data for one cat, then I would just use ROWNUM to limit it to 1 query, but I am trying to get a list of all cats including their name, so I can’t do this.

Can anyone offer some guidance? I guess it doesn’t have to be plsql specific, the technique will be the same I imagine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:25:25+00:00Added an answer on June 15, 2026 at 1:25 am

    There are several methods.

    1. You could use an inline query:

      SELECT id, color, 
             (SELECT name FROM CatName cn WHERE cn.id = c.id AND ROWNUM = 1) Name
        FROM cat c
       WHERE ...
      
    2. You could use a join then analytics:

      SELECT id, color, Name
        FROM (SELECT Id, Color, Name,
                     row_number() OVER (PARTITION BY id ORDER BY 1) rn
                FROM Cat JOIN CatName on .....)
       WHERE rn = 1
      
    3. You can use an aggregate:

      SELECT id, color, MAX(name) name
        FROM Cat JOIN CatName on .....
       GROUP BY id, color
      

    From a performance point of view, assuming that CatName is indexed by CatId:

    • if the number of cats returned is smallish, or you only want the very first few cats among many, solution 1 can be really fast,
    • if the dataset returned is large and you want all cats, then solution 2 and 3 can make good use of the efficient HASH JOIN.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about doing a query that returns results of all rows
I have a SQL query that I'm currently solving by doing two queries. I
I am doing a query that converts rows to columns similar to this post
When doing query optimization, the SHOW STATUS query returns values that are easy to
I have a query that I'm trying to figure the django way of doing
I'm doing a query on a set of users that returns the users within
I'm doing this query: SomeObject.objects.annotate(something=Avg('something')).order_by(something).all() I normally have an aggregate field in my model
This is similar to Compute dates and durations in mysql query , except that
How do you write a query that executes for each different result in a
I am trying to write a query that pulls out the names of all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.