1

I am trying to write an Oracle SQL query to join two tables that are linked via a link table (by that I mean a table with 2 columns, each a foreign key to the primary tables). A min() function is to be used to limit the results from the left outer join to a single row.

My model consists of "parents" and "nephews". Parents can have 0 or more nephews. Parents can be enabled or disabled. Each nephew has a birthday date. The goal of my query is:

Print a single row for each enabled parent, listing that parent's oldest nephew (ie the one with the min(birthday)).

My problem is illustrated here at sqlfiddle: http://sqlfiddle.com/#!4/9a3be0d/1

I can form a query that lists all of the nephews for the enabled parents, but that is not good enough- I just want one row per parent which includes just the oldest nephew. Forming the where clause to the outer table seems to be my stumbling block.

My tables and sample data:

create table parent (parent_id number primary key, parent_name varchar2(50), enabled int);
create table nephew  (nephew_id number primary key, birthday date, nephew_name varchar2(50));
create table parent_nephew_link  (parent_id number not null, nephew_id number not null);

parent table:
+----+-------------+---------+
| id | parent_name | enabled |
+----+-------------+---------+
| 1  | Donald      | 1       |
+----+-------------+---------+
| 2  | Minnie      | 0       |
+----+-------------+---------+
| 3  | Mickey      | 1       |
+----+-------------+---------+
  nephew table:
  +-----------+------------+-------------+
  | nephew_id | birthday   | nephew_name |
  +-----------+------------+-------------+
  | 100       | 01/01/2017 | Huey        |
  +-----------+------------+-------------+
  | 101       | 01/01/2016 | Dewey       |
  +-----------+------------+-------------+
  | 102       | 01/01/2015 | Louie       |
  +-----------+------------+-------------+
  | 103       | 01/01/2014 | Morty       |
  +-----------+------------+-------------+
  | 104       | 01/01/2013 | Ferdie      |
  +-----------+------------+-------------+
parent_nephew_link table:
+-----------+-----------+
| parent_id | nephew_id |
+-----------+-----------+
| 1         | 100       |
+-----------+-----------+
| 1         | 101       |
+-----------+-----------+
| 1         | 102       |
+-----------+-----------+
| 3         | 103       |
+-----------+-----------+
| 3         | 104       |
+-----------+-----------+

My (not correct) query:

-- This query is not right, it returns a row for each nephew
select parent_name, nephew_name
from parent p
left outer join parent_nephew_link pnl
  on p.parent_id = pnl.parent_id
left outer join nephew n
  on n.nephew_id = pnl.nephew_id
where enabled = 1
--    I wish I could add this clause to restrict the result to the oldest 
--    nephew but p.parent_id is not available in sub-selects.
--    You get an ORA-00904 error if you try this:
-- and n.birthday = (select min(birthday) from nephew nested where nested.parent_id = p.parent_id)

My desired output would be:

+-------------+-------------+
| parent_name | nephew_name |
+-------------+-------------+
| Donald      | Louie       |
+-------------+-------------+
| Mickey      | Ferdie      |
+-------------+-------------+

Thanks for any advice! John

markaaronky's suggestion

I tried using markaaronky's suggestion but this sql is also flawed.

-- This query is not right either, it returns the correct data but only for one parent
select * from (
  select parent_name, n.nephew_name, n.birthday
  from parent p
  left outer join parent_nephew_link pnl
    on p.parent_id = pnl.parent_id
  left outer join nephew n
    on n.nephew_id = pnl.nephew_id
  where enabled = 1
  order by parent_name, n.birthday asc
) where rownum <= 1
2
  • Donald id = 3 references nephew_id = 103 + 104 in the parent_nephew_link table, which in turn reference 103-->Morty and 104-->Ferdie. Could you explain why do you want to get Donald --->Louie in the result but not either Morthy or Ferdie ? I am totally lost, I don't get this logic.
    – krokodilko
    Sep 27, 2017 at 20:22
  • My mistake- I was manually transcribing the data from sqlfiddle into stackoverflow and reversed Donald and Mickey by accident. I have corrected the data shown above. Thanks! Sep 27, 2017 at 21:51

2 Answers 2

0

Why not: (1) include the n.birthday from the nephews table in your SELECT statement (2) add an ORDER BY n.birthday ASC to your query (3) also modify your select so that it only takes the top row?

I tried to write this out in sqlfiddle for you but it doesn't seem to like table aliases (e.g. it throws an error when I write n.birthday), but I'm sure that's legal in Oracle, even though I'm a SQL Server guy.

Also, if I recall correctly, Oracle doesn't have a SELECT TOP like SQL Server does... you have to do something like "WHERE ROWNUM = 1" instead? Same concept... you're just ordering your results so the oldest nephew is the first row, and you're only taking the first row.

Perhaps an undesired side effect is you WOULD get the birthday along with the names in your results. If that's unacceptable, my apologies. It looked like your question has been sitting unanswered for a while and this solution should at least give you a start.

Lastly, since you don't have a NOT NULL constraint on your birthday column and are doing left outer joins, you might make the query safer by adding AND n.birthday IS NOT NULL

0
0

Use:

select parent_name, nephew_name
from parent p
left outer join
(
  SELECT pnl.parent_id, n.nephew_name
    FROM parent_nephew_link pnl
    join nephew n
    on n.nephew_id = pnl.nephew_id
    AND n.BIRTHDAY = ( 
        SELECT min( BIRTHDAY ) 
        FROM nephew n1
        JOIN parent_nephew_link pnl1
        ON pnl1.NEPHEW_ID = n1.NEPHEW_ID
        WHERE pnl1.PARENT_ID = pnl.PARENT_ID
    )
) ppp
on p.parent_id = ppp.parent_id
where p.enabled = 1

Demo: http://sqlfiddle.com/#!4/98758/23

| PARENT_NAME | NEPHEW_NAME |
|-------------|-------------|
|      Mickey |       Louie |
|      Donald |      Ferdie |
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.