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

  • Home
  • SEARCH
  • 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 7937393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:30:49+00:00 2026-06-03T22:30:49+00:00

I have 3 tables: (SELECT DISTINCT ID FROM IDS)a LEFT OUTER JOIN (SELECT NAME,

  • 0

I have 3 tables:

(SELECT DISTINCT ID
FROM IDS)a
LEFT OUTER JOIN
(SELECT NAME, ID
FROM NAMES)b
ON a.ID = b.ID
LEFT OUTER JOIN
(SELECT ADDRESS FROM ADDRESSES
WHERE ROWNUM <2
ORDER BY UPDATED_DATE DESC)c
ON a.ID = c.ID

An ID can have only one name but can have multiple addresses. I only want the latest one. This query returns the address as null even when there is an address I guess cause it only fetches the first address from the table and then tries LEFT JOIN it to the ID of addresses which it canno find. What is the correct way of writing this query?

  • 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-03T22:30:53+00:00Added an answer on June 3, 2026 at 10:30 pm

    Try KEEP DENSE_RANK

    Data source:

    CREATE TABLE person
        (person_id int primary key, firstname varchar2(4), lastname varchar2(9))
    /
    INSERT ALL
        INTO person (person_id, firstname, lastname)
             VALUES (1, 'john', 'lennon')
        INTO person (person_id, firstname, lastname)
             VALUES (2, 'paul', 'mccartney')
    SELECT * FROM dual;
    
    
    
    CREATE TABLE address
        (person_id int, address_id int primary key, city varchar2(8))
    /
    INSERT ALL
        INTO address (person_id, address_id, city)
             VALUES (1, 1, 'new york')
        INTO address (person_id, address_id, city)
             VALUES (1, 2, 'england')
        INTO address (person_id, address_id, city)
             VALUES (1, 3, 'japan')
        INTO address (person_id, address_id, city)
             VALUES (2, 4, 'london')
    SELECT * FROM dual;
    

    Query:

        select  
    
          p.person_id, p.firstname, p.lastname,
    
          x.recent_city
    
        from person p
        left join (
    
            select person_id,      
    
                min(city) -- can change this to max(city). will work regardless of min/max
    
                -- important you do this to get the recent: keep(dense_rank last)
    
                keep(dense_rank last order by address_id) 
                   as recent_city
    
            from address 
            group by person_id
    
    
        ) x on x.person_id = p.person_id
    

    Live test: http://www.sqlfiddle.com/#!4/7b1c9/2


    Not all database has similar functionality with Oracle’s KEEP DENSE_RANK windowing function, you can use plain windowing function instead:

    select  
    
      p.person_id, p.firstname, p.lastname,
    
      x.recent_city, x.pick_one_only
    
    from person p
    left join (
    
        select 
    
            person_id,      
    
            row_number() over(partition by person_id order by address_id desc) as pick_one_only,
            city as recent_city
    
        from address 
    
    
    
    ) x on x.person_id = p.person_id and x.pick_one_only = 1
    

    Live test: http://www.sqlfiddle.com/#!4/7b1c9/48


    Or use tuple testing, shall work on databases that doesn’t support windowing function:

    select  
    
      p.person_id, p.firstname, p.lastname,
    
      x.recent_city
    
    from person p
    left join (
    
        select   
            person_id,city as recent_city    
        from address 
        where (person_id,address_id) in
    
              (select person_id, max(address_id)
               from address
               group by person_id)
    
    
    
    ) x on x.person_id = p.person_id 
    

    Live test: http://www.sqlfiddle.com/#!4/7b1c9/21


    Not all database supports tuple testing like in the preceding code though. You can use JOIN instead:

    select  
    
      p.person_id, p.firstname, p.lastname,
    
      x.recent_city
    
    from person p
    left join (
    
        select 
    
            address.person_id,address.city as recent_city
    
        from address 
        join 
        (
              select person_id, max(address_id) as recent_id
               from address
               group by person_id
        ) r 
        ON address.person_id = r.person_id
        AND address.address_id = r.recent_id
    
    
    
    ) x on x.person_id = p.person_id 
    

    Live test: http://www.sqlfiddle.com/#!4/7b1c9/24

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

Sidebar

Related Questions

I have a query: SELECT DISTINCT countryName,countrySlug FROM countries AS Country INNER JOIN countries_networks
I have multiple tables that need to be merged into one. SELECT name, SUM(money)
I have the following table Select * from Peeps Id Name Age 1 Sam
I want to select all distinct order_ids from my table, and order that list
I have a query that, basically, says this: SELECT DISTINCT [DB_ID] FROM [TableX] WHERE
I am using java/oracle. i have below issue. SELECT DISTINCT id_one, status FROM sometable
I have the following query: SELECT DISTINCT sites.site_id, sites.site_name, sites.site_url, earnings.cust_id FROM sites, earnings
I have two tables that I am joining with the following query... select *
I have two tables - forum_topics and topics_posts . I want to select rows
If we have two tables, say; users, and images, how do we select users

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.