Fit_Not_Quit
Fit_Not_Quit

Reputation: 23

Listing only records of a particular attribute that are linked with ALL other records of another particular attribute

Basically I have to come up with a query to list only which species are found in all forests

So far I have

SELECT Species.Sp_name
FROM Species
LEFT JOIN Tree ON Tree.Tr_species=Species.Sp_name
LEFT JOIN Forest ON Forest.FO_name=tree.Tr_forest

But in all honesty I am lost

Species Table

+--------------+------------------+------+--------------------------------+
| Field        | Type             |  Key | Glossary                       |
+--------------+------------------+------+--------------------------------+
| sp_name      | C(10)            |  PK  | Species Name                   |
| sp_woodtype  | C(10)            |      | Wood Yielded by tree           |
| sp_maxht     |  I               |      | Max. Height                    |
+--------------+------------------+------+--------------------------------+

Forest table

+--------------+------------------+------+--------------------------------+
| Field        | Type             |  Key | Glossary                       |
+--------------+------------------+------+--------------------------------+
| Fo_name      | C(10)            |  PK  | Forest Name                    |
| Fo_size      |   I              |      | Forest Area                    |
| Fo_loc       | C(10)            |      | Geographical Area              |
| Fo_comp      | C(10)            |      | Forest Owner                   |
+--------------+------------------+------+--------------------------------+

Tree table

+--------------+------------------+------+---------------------------------------------+
| Field        | Type             |  Key | Glossary                                    |
+--------------+------------------+------+---------------------------------------------+
| Tr_species   | C(10)            |  FK  | (FK  of species.sp_name                     |
| Tr_forest    | C(10)            |  FK  | (FK of forest.fo_name                       |
| Tr_numb      |   I              |  PK  | Sequence number                             |
| Tr_planted   | Date             |      | Date of planting                            | 
| Tr_loc       | C(10)            |      | Forest quadrant                             |
| Tr_parent    |   I              |  FK  | (FK of tree.numb) procreating tree reference|
+--------------+------------------+------+---------------------------------------------+

Measure Table

+--------------+------------------+------+---------------------------------------------+
| Field        | Type             |  Key | Glossary                                    |
+--------------+------------------+------+---------------------------------------------+
| Me_trnumb    |   I              |  FK  | (FK  of Tree.tr_numb                        |
| Me_numb      |   I              |  PK  | Sequence Number                             |
| Me_result    |   I              |      | Test`s measure                              |
| Me_date      | Date             |      | Measure taken on                            | 
| Me_type      | C(10)            |      | Type of Measurevv                           |
+--------------+------------------+------+---------------------------------------------+

(I means integer and C10 means character(10)

Upvotes: 0

Views: 166

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269953

You can do this by counting the number of forests for each species and comparing to the total number of forests. You can do this with a having clause:

SELECT s.Sp_name
FROM Species s LEFT JOIN
     Tree t 
     ON t.Tr_species = s.Sp_name
GROUP BY s.Sp_name
HAVING COUNT(DISTINCT t.Tr_Forest) = (SELECT COUNT(*) FROM Forest)

Upvotes: 2

Related Questions