Question: Write SQL query - based on the single-company database - For every dependent, find, and output, the dependent and the number of siblings (brothers and

Write SQL query - based on the single-company database - "For every dependent, find, and output, the dependent and the number of siblings (brothers and sisters) of the dependent who are also dependents of the same employee."

EMPLOYEE FNAME MINIT LNAME DNAME SSN BDATE ADDRESS DEPARTMENT ESSN DNUMBER DEPENDENT

EMPLOYEE FNAME MINIT LNAME DNAME SSN BDATE ADDRESS DEPARTMENT ESSN DNUMBER DEPENDENT PROJECT DNUMBER DEPT LOCATIONS MGRSSN PNAME PNUMBER PLOCATION DLOCATION WORKS ON DEPENDENT_NAME ESSN PNO HOURS MGRSTARTDATE SEX SALARY SUPERSSN SEX BDATE DNUM RELATIONSHIP DNO

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To answer the given question based on the provided database schema we need to identify the dependents of each employee and count how many other dependents are associated with the same employee Given the table DEPENDENT which contains ESSN Employee SSN DEPENDENTNAME SEX BDATE and RELATIONSHIP we can derive a solution SQL Query sql SELECT d1DEPENDENTNAME COUNTd2DEPENDENTNAME AS NUMOFSIBLINGS FROM DEPENDENT d1 JOIN DEPENDENT d2 ON d1ESSN d2ESSN AND d1DEPENDENTNAME d2DEPENDENTNAME GROUP BY d1DEPENDENTNAME Explanation We are using a selfjoin on the DEPENDENT table d1 and d2 to find siblings dependents who share the same employee The condition d1ESSN d2ESSN ensures that we are looking for dependents of the same employee The condition d1DEPENDENTNAME d2DEPENDENTNAME ensures that we do not count the dependent ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!