Graded Quiz: Functions, Multiple Tables, and Sub-queries :Databases and SQL for Data Science with Python (IBM Data Analyst Professional Certificate) Answers 2025
1. Question 1
Query that returns employees belonging to the department with the highest department ID:
-
❌ Query 1
-
❌ Query 2
-
✅ SELECT * FROM EMPLOYEES WHERE DEP_ID = ( SELECT MAX(DEPT_ID_DEP) FROM DEPARTMENTS )
-
❌ Query 4
Explanation:
MAX(DEPT_ID_DEP) gives the highest department ID, and the subquery returns that value.
2. Question 2
Correct way to join EMPLOYEES and DEPARTMENTS:
-
❌ Query 1 (incorrect column names)
-
❌ Query 2
-
❌ Query 3 (uses implicit join but ambiguous columns)
-
✅ SELECT E.F_NAME, D.DEP_NAME FROM EMPLOYEES E, DEPARTMENTS D WHERE E.DEP_ID = D.DEPT_ID_DEP
Explanation:
You must join E.DEP_ID with D.DEPT_ID_DEP, selecting employee name and department name.
(Note: Query 4 has no join condition → Cartesian product. Incorrect.)
3. Question 3
Correct query for total cost with alias Total_Cost:
-
❌ SELECT SUM(Cost)
-
✅ SELECT SUM(Cost) AS Total_Cost FROM PetRescue
-
❌ SUM(Total_Cost)
-
❌ SELECT Total_Cost
Explanation:
AS Total_Cost renames the output column.
4. Question 4
Correct query to compute total days lived:
-
❌ SELECT (CURRENT_DATE – DOB)
-
✅ SELECT DATEDIFF(CURRENT_DATE, DOB) FROM Employees
-
❌ FROM_DAYS(DATEDIFF()) gives years, not days lived
-
❌ Incorrect syntax
Explanation:
DATEDIFF(date1, date2) returns the number of days between two dates.
5. Question 5
Correct query to compute DOE = DOM + 1 year:
-
❌ DATEADD (not MySQL)
-
❌ INTERVAL 1 YEARS (incorrect plural)
-
❌ DATEADD again incorrect
-
✅ SELECT NAME, DATE_ADD(DOM, INTERVAL 1 YEAR) AS DOE FROM MEDS
Explanation:
MySQL uses DATE_ADD(date, INTERVAL value unit).
🧾 Summary Table
| Q | Correct Answer | Key Concept |
|---|---|---|
| 1 | SELECT … WHERE DEP_ID = (SELECT MAX…) | Subquery with MAX |
| 2 | SELECT E.F_NAME, D.DEP_NAME … WHERE E.DEP_ID = D.DEPT_ID_DEP | Joining tables |
| 3 | SELECT SUM(Cost) AS Total_Cost | Aggregate with alias |
| 4 | DATEDIFF(CURRENT_DATE, DOB) | Date difference in MySQL |
| 5 | DATE_ADD(DOM, INTERVAL 1 YEAR) AS DOE | MySQL date arithmetic |