<p>I have 3 tables</p>

<ol>
<li><b>Links</b><br/>
Link ID<br/>
Link Name<br/>
GroupID (FK into Groups)<br/>
SubGroupID (FK into Subgroups)<br/>
<br/></li>
<li><p><b>Groups</b><br/>
GroupID<br/>
GroupName<br/></p></li>
<li><p><b>SubGroup</b><br/>
SubGroupID<br/>
SubGroupName<br/>
GroupID (FK into Groups)<br/></p></li>
</ol>

<p>Every link needs to have a GroupID but teh SubGroupID is optional.  How do i write a SQL query to show: <br/></p>

<p><b>Links.LinkName, Groups.GroupName, SubGroup.SubGroupName<br/></b></p>

<p>For the records with no subgroup just put a blank entry in that field.  If i have 250 link rows, i should get back 250 reecords from this query.</p>

<p>Is there a way to do this in one query or do i need to do multiple queries?</p>

```
```
SELECT 
  links.linkname
  , groups.groupname
  , subgroup.groupname
FROM
  links 
  JOIN groups ON links.groupid = groups.groupid
  LEFT OUTER JOIN subgroups ON links.subgroupid = subgroup.subgroupid
```
```
(re-added to address OP's question)
incidentally, why not keep groups and subgroups in the same table, and use a self-referential join?
Akantro:
You'd have something like this:
    create table groups(
    groupid integer primary key,
    parentgroupid integer foreign key references groups (groupid),
    groupname varchar(50))
your query would then be
```
```
SELECT 
  links.linkname
  , groups.groupname
  , SUBGROUPS.groupname
FROM
  links 
  JOIN groups ON links.groupid = groups.groupid
  LEFT OUTER JOIN groups SUBGROUPS ON links.subgroupid = subgroup.groupid
```
```
there's no functional difference to keeping the tables like this, but the benefit is you only have to go to one place to edit the groups/subgroups