Request for Sql Joins

I have three tables.

tblLink: (LinkId, LinkName, GroupId, SubGroupId)
          GroupId and SubGroupId are foreign keys in tblGroup and tblSubGroup
tblGroup: (GroupId, GroupName)
tblSubGroup: (SubGroupId, SubGroupName)

in tblLink SubGroupId is allowed Null, but GroupId is required.

I want to get LinkName, GroupName, SubGroupName for each LinkId in tblLink
I wrote a request

SELECT L.LinkName, G.GroupName, SG.SubGroupName FROM tblLink L
                                        Left Join tblSubGroup SG
                                        for sale (L.SubGroupId = SG.SubGroupId)
                                        Internal registration
                                                    tblGroup G
                                        for sale (L.GroupId = G.GroupId)

If there is no subgroup for any LinkId, I want to show NotExist instead of Null

+2
1
SELECT
    L.LinkName, G.GroupName, 
    ISNULL(SG.SubGroupName, 'NotExist') AS SubGroupName
FROM
    Link L
....
+4

Source: https://habr.com/ru/post/1727769/


All Articles