Informatics Practices

An organization ABC maintains a database EMP-DEPENDENT to record the following details about its employees and their dependents.

EMPLOYEE(AadhaarNo, Name, Address, Department, EmpID)  
DEPENDENT(EmpID, DependentName, Relationship)  

Use the EMP-DEPENDENT database to answer the following SQL queries:

(i) Find the names of the employees with their dependents' names.

(ii) Find employee details working in a department, say, 'PRODUCTION'.

(iii) Find employee names having no dependents.

(iv) Find the names of employees working in a department, say, 'SALES' and having exactly two dependents.

SQL Queries

1 Like

Answer

(i)

SELECT e.Name, d.DependentName
FROM EMPLOYEE e, DEPENDENT d 
WHERE e.EmpID = d.EmpID;

(ii)

SELECT *
FROM EMPLOYEE
WHERE Department = 'PRODUCTION';

(iii)

SELECT e.Name
FROM EMPLOYEE e, DEPENDENT d 
WHERE e.EmpID = d.EmpID AND d.DependentName IS NULL;

(iv)

SELECT Name
FROM EMPLOYEE
WHERE Department = 'SALES' AND EmpID IN (
    SELECT EmpID
    FROM DEPENDENT
    GROUP BY EmpID
    HAVING COUNT(*) = 2
);

Answered By

1 Like


Related Questions