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 9204009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:36:18+00:00 2026-06-17T23:36:18+00:00

I want to get records from two table that are linked with fk. This

  • 0

I want to get records from two table that are linked with fk. This fk can be null and the child table must have a filter.
I explain it with simple example.

Table Poet
IdPoet Number(5) 
NameVarchar2(250)

Table Poem 
IdPoem Number(5) 
IdPoet Number(5) FK 
Language Number(1) 
Text Varchar2(2000)

The FK is relationship Poet and Poem
Poet-1————–N0—Poem

Poem.IdPoet can be null.

I need to get all records from Poet that don’t have poems, and poets that have poems with language=1 or language=2. Language 1 is more restrictive that 2. This mean that if there are a Poet that have 2 poems (one writes on language 1 and other write on language 2) must show only record with language 1.

    SELECT *
    FROM POET, POEM
    WHERE POET.IDPOET = POEM.IDPOET(+)
    AND
    (
    (POEM.LANGUAGE IS NOT NULL AND POE.LANGUAGE = 1) OR
    (POEM.LANGUAGE IS NOT NULL AND POE.LANGUAGE = 2) OR
    (POEM.LANGUAGE IS = 1) OR
    (POEM.LANGUAGE IS = 2)
    )

This select shows Poets that don’t have poems, that have 1 poem on language 1 or that have 1 poem on language 2. The problem is when poem have 2 poems, one on language 1 and other on language 2, then show 2 records, and i want to show only record with poem on language 1 because is more restrictive than 2.

  • 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-17T23:36:19+00:00Added an answer on June 17, 2026 at 11:36 pm

    You can do this pretty easily using Oracle’s ROW_NUMBER() analytic function:

    SELECT Name, Text FROM (
      SELECT PT.Name, PM.Text, ROW_NUMBER() OVER (PARTITION BY PT.IdPoet ORDER BY PM.Language) RN
      FROM POET PT
      LEFT JOIN POEM PM ON (PT.IdPoet = PM.IdPoet)
      WHERE PM.Language IS NULL OR PM.Language IN (1, 2)
    ) WHERE RN = 1
    

    Note that if there are multiple poems in language 1 for a single poet, it’ll just pick whichever one is returned first from the database. You could add another field to the ORDER BY in the partition clause to sort by poem name, etc. first if you wanted.

    SQL Fiddle


    Edit: Also, if you want the same behavior that alzaimar proposed (i.e., all poems from language 1 are shown), you can use the DENSE_RANK() function instead of ROW_NUMBER().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two employee lists that I want to get only unique records from
I have two jobs pulling from a mysql table. They both want to get
I want to get the count of records from database. In my table this
I have two tables that can have multiple records linking to another table, but
I want to get all the records from the one table which contain at
I have a table that has two records (there will be many at runtime).
I have two tables that I want to fetch data from. Lets call them
I want to insert the records that time want to get the visitNo from
I want to get two field values from a database table and combine them
I want to get the records of last month based on my database table

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.