SQL | More exotic joins exercise | Create self-joins between 3 levels of family hierarchies

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend one of more of the courses listed below!

Software ==> SQL  (198 exercises)
Version ==> Any version of SQL Server
Topic ==> More exotic joins  (2 exercises)
Level ==> Harder than average
Courses ==> Introduction to SQL  /  Fast-track SQL
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help). Once you've done this:
  1. Go into SQL Server Management Studio;
  2. Open the SQL file you've just unzipped (you can press CTRL + O to do this); then
  3. Execute this script.

This will generate the database that you'll need to use in order to do this exercise (note that the database and script are only to be used for exercises published on this website, and may not be reused or distributed in any form without the prior written permission of Wise Owl).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

First open the SQL file in the above folder in Management Studio, and execute it.  This should generate a table called tblFamily.

Don't forget that you may need to refresh your tables to see this.

Each row in this table contains a column called ParentFamilyId, which tells you which parent family any family belongs to (the All categories family - number 25 - has no parent, and so sits at the top of the hierarchy).

Create a query which links 3 tables using outer joins as follows:

Table Alias
tblFamily Family
tblFamily ParentFamily
tblFamily TopFamily

Add calculated columns to your query so that it displays all 25 familes:

Families listed

If you display the families in FamilyName order, you should see something like this.

You'll need to trap for nulls, for those occasions when a family doesn't have a parent.

Save this query as Happy families, then close it down.

This page has 2 threads Add post
20 Feb 19 at 06:12

This question is tricky because there are many ways you can do it. In fact, you don't even have to use "null traps"  nor all the aliases and joins mentioned in the problem. However, I am not 100% sure this is the way WiseOwl wanted it to be done nor am I sure this is correct.  Proceed with caution!

ISNULL(Expression, 'All categories') Function

Checks if any of the arguments in the function is NULL, in this case whether if there is a family of a higher hierarchy, and returns the Top Hierarchy 'All categories'.        

Note: The program reads the  hierarchy from Lowest->Highest.  If there areonly two (2) levels the TopFamily becomes NULL  and if only one(1) level the TopFamily and ParentFamily become NULL , so on. Family < Parent Family < Top Family [Lowest< Middle < Highest].

CASE 

I used a CASE here to check if ONLY the TopFamily is NULL, if it's NULL it will return '' and if it's not NULL it will return the rest of the function if it exists.  I did this so that the ISNULL() function does not  make every row with less than 3 levels of hierarchy = 'All categories'.

The rest of the function is simple, a basic string calculation to add the remainder of the FamilyNames in the hierarchy.

LEFT JOIN

TopFamily being the highest in the hierarchy, I set it as ParentFamily's ParentFamilyID and Family's ParentFamilyID as ParentFamily's FamilyID.  Thus, TopFamily.FamilyID = ParentFamily.ParentFamilyID and Family.ParentFamilyID = ParentFamily.FamilyID.

You can see this more clearly by executing the code below, where I wrote it so that it generates all the revelant FamilyIDs and ParentFamilyIDs                    

---SPOILER [ANSWER]---

SELECT
---FOR CLARITY---
TopFamily.FamilyID as [TopFamily FamilyID],
ParentFamily.ParentFamilyID as [ParentFamily ParentFamilyID],
ParentFamily.FamilyID as [ParentFamily FamilyID],
Family.ParentFamilyId as [Family ParentFamilyID],
Family.FamilyID as [Family FamilyID],
Family.FamilyName,

ISNULL(
        CASE 
                  WHEN TopFamily.FamilyName IS NULL 
                              THEN ''
                              ELSE TopFamily.FamilyName + ' > ' 
        END + ParentFamily.FamilyName + ' > ' + Family.FamilyName 
        
        , 'All categories'
      

             ) as 'Family path'
FROM
           tblFamily as family
LEFT JOIN
           tblFamily as ParentFamily
ON  ParentFamily.FamilyID = Family.ParentFamilyID 
LEFT JOIN
           tblFamily as TopFamily
ON TopFamily.FamilyId = ParentFamily.ParentFamilyID
ORDER BY

FamilyName

29 Jun 18 at 14:17

Hello.

I'm having trouble getting this exercise done.

Is there anywhere i can get the solution? 

19 Oct 18 at 21:10

Hello there, this one is kind of funky but here is my solution.

USE WorldEvents

SELECT 
    Family.[FamilyName],
    ISNULL('All categories' + ' > ' +
    IIF(ParentFamily.FamilyId = 25,'',ParentFamily.[FamilyName]) + ' > ' +
    TopFamily.[FamilyName], 'All categories') AS 'Family path'
FROM
    [dbo].[tblFamily] AS Family
    LEFT JOIN [dbo].[tblFamily] AS ParentFamily
        ON Family.ParentFamilyId = ParentFamily.FamilyID    
    LEFT JOIN [dbo].[tblFamily] AS TopFamily
        ON Family.FamilyId = TopFamily.FamilyID    
ORDER BY
    Family.[FamilyName]

13 Jul 18 at 13:30

Hey, 

I came up with this solution:

 

select 
    a.FamilyName,
    isnull(case when b.FamilyName = 'All categories' then '' else 'All categories < 'end +b.FamilyName +' < '+     a.FamilyName, 'All categories') as 'Family path'

from
    tblFamily a
left join tblFamily b on a.ParentFamilyId = b.FamilyID

Order by a.FamilyName
 

29 Jun 18 at 18:43

We don't publish the exercises, I'm afraid - but you never know, some kind person might help ....