Question

i have following two case-tables. tbl_emp is master table, say:-

 ----------------------------------------------------------
   tbl_emp
 ----------------------------------------------------------
    emp_id            emp_name
     1                  Peter
     2                  Matt
     3                  Jacob
 ----------------------------------------------------------

and the detail table has the family details for employees.......

 -----------------------------------------------------------------
  tbl_family
 ----------------------------------------------------------------
   family_id         emp_id        relation       name       age
 -----------------------------------------------------------------
     1                 1            WIFE          Susan       32
     2                 1            SON           Jack        3
     3                 2            DAUGHTER      Hannah      4
     4                 2            WIFE          Leah        29
     5                 1            WIFE          Anna        38
     6                 3            MOTHER        Loran       73
     7                 2            MOTHER        Sofia       81
 ------------------------------------------------------------------

I want a query to know who all employees have a specific 'relation' entry in the tbl_family and who DONT have. eg i managed following query for the employess having WIFE entry

 select * from tbl_emp, tbl_family where
 tbl_emp.emp_id = tbl_family.emp_id and
 tbl_family.relation = 'WIFE'

this query returns Peter and Matt correctly. But i need queries for three issues. firstly. to give me employees with no WIFE entry in tbl_family. i.e the out put should be

  ---------------------------------------
    emp_id        emp_name
  ---------------------------------------
      3            Jacob
  ---------------------------------------

seconldy, records with two WIFE entries (or any other relation) for the dataset it would give

 -----------------------------------------
   emp_id            emp_name
 -----------------------------------------
     1                Peter
 -----------------------------------------

and lastly, all those employees who have WIFE and MOTHER entries. Thise query would return

 -----------------------------------------
   emp_id            emp_name
 -----------------------------------------
     2               Matt
 -----------------------------------------

I have edited question with all result outputs. Thanks.

Was it helpful?

Solution

First point:

 --wihout wife
 select tbl_emp.* 
 from tbl_emp  
 left join tbl_family 
 on 
   tbl_emp.emp_id = tbl_family.emp_id and tbl_family.relation = 'WIFE'
 where tbl_family.emp_id IS NULL;

Second point:

 --having any relation at least twice
 select tbl_emp.id, tbl_emp.name, tbl_family.relation
 from tbl_emp  
 left join tbl_family 
 on 
   tbl_emp.emp_id = tbl_family.emp_id 
 group by tbl_emp.id, tbl_emp.name, tbl_family.relation
 having count(tbl_family.emp_id) > 1;

Third point:

 --having WIFE, SON AND DAUGHTER.
 select tbl_emp.id, tbl_emp.name 
 from tbl_emp  
 left join tbl_family 
 on 
   tbl_emp.emp_id = tbl_family.emp_id and (
     tbl_family.relation = 'WIFE' or 
     tbl_family.relation = 'SON' or 
     tbl_family.relation = 'DAUGHTER'
     )
 group by tbl_emp.id, tbl_emp.name
 having count(distinct tbl_family.relation) >= 3;

OTHER TIPS

Third point:

SELECT tbl_emp.*
  FROM tbl_emp
   INNER JOIN tbl_family tf1
      ON tbl_emp.emp_id = tf1.emp_id AND tf1.relation = 'WIFE'
   INNER JOIN tbl_family tf2
      ON tbl_emp.emp_id = tf2.emp_id AND tf2.relation = 'MOTHER';

You should play with tbl_family.relation = 'WIFE'

You can use e.g. (tbl_family.relation = 'WIFE' OR tbl_family.relation = 'MOTHER' OR tbl_family.relation = 'DAUGHTER')

Also you can use OUTER JOIN to find persons without family members

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top