Q.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:
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Two tables share the column EmpID — that is the bridge for every query here. (a) is a JOIN, (b) a plain WHERE filter, (c) a NOT IN subquery (rows that have no match cannot come from a join alone), and (d) combines JOIN + WHERE + GROUP BY + HAVING to count dependents per employee.
Reading the schema first
EMPLOYEE(AadhaarNo, Name, Address, Department, EmpID)
DEPENDENT(EmpID, DependentName, Relationship)
EmpID identifies an employee in EMPLOYEE and re-appears in DEPENDENT as the link (a foreign key): one employee can have many dependent rows. Whenever a question needs information from both tables, we join them on EmpID.
a) Names of employees with their dependent names
The name lives in EMPLOYEE, the dependent's name in DEPENDENT — so join:
SELECT E.Name, D.DependentName
FROM EMPLOYEE E
JOIN DEPENDENT D ON E.EmpID = D.EmpID;
Why these lines: E and D are table aliases that keep the query readable; the ON E.EmpID = D.EmpID condition pairs each dependent row with its own employee. The result has two columns — Name | DependentName — one row per (employee, dependent) pair. An employee with three dependents appears three times; an employee with none does not appear at all (an inner join keeps only matching rows).
b) Details of employees working in 'PRODUCTION'
Everything asked for is inside EMPLOYEE, so no join is needed:
SELECT *
FROM EMPLOYEE
WHERE Department = 'PRODUCTION';
Why: SELECT * returns all five attributes ("employee details"), and the WHERE clause keeps only rows whose Department equals the string 'PRODUCTION'. The result is a table with columns AadhaarNo | Name | Address | Department | EmpID containing only PRODUCTION employees.
c) Employee names having no dependent
"No dependent" means the employee's EmpID never occurs in DEPENDENT. A join cannot express absence, but a subquery can:
SELECT Name
FROM EMPLOYEE
WHERE EmpID NOT IN (SELECT EmpID FROM DEPENDENT);
Why: the inner query builds the set of all EmpIDs that do have dependents; NOT IN keeps every employee outside that set. The result is a single Name column.
d) 'SALES' employees having exactly two dependents
This needs a count per employee, which is what GROUP BY + HAVING do:
SELECT E.Name
FROM EMPLOYEE E
JOIN DEPENDENT D ON E.EmpID = D.EmpID
WHERE E.Department = 'SALES'
GROUP BY E.EmpID, E.Name
HAVING COUNT(*) = 2;
Why each clause works:
JOIN … ON E.EmpID = D.EmpID— one row per dependent of each employee. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.