Preserve student occurrences with zero participation
Mediumsql
Learn the Concept: EntityReturn every studentid and enrollmentcount. Include students with no enrollment as zero and count each supplied enrollment occurrence separately.
MySQL dialect.
sqlCREATE TABLE students (student_id INT PRIMARY KEY, student_name VARCHAR(80) NOT NULL); CREATE TABLE enrollments (enrollment_id INT PRIMARY KEY, student_id INT NOT NULL); INSERT INTO students VALUES (1,'Asha'),(2,'Asha'),(3,'Mira'); INSERT INTO enrollments VALUES (1,1),(2,1),(3,2);
Required ordering: Ascending student_id.
Edge cases:
- Repeated names remain separate identities.
- A student can have zero or several relationships.
Example 1
Input
INSERT INTO students VALUES (1,'Asha'),(2,'Asha'),(3,'Mira'); INSERT INTO enrollments VALUES (1,1),(2,1),(3,2);
Output
student_id enrollment_count 1 2 2 1 3 0
Explanation: The report returns one row per student occurrence while counting related enrollment facts at their separate grain.
- Every supplied enrollment references a supplied student; native FK enforcement is not part of these temporary mirrors.
Hint 1
Preserve the student population.
Hint 2
Count the nonmissing enrollment identity.
The report returns one row per student occurrence while counting related enrollment facts at their separate grain.
The report returns one row per student occurrence while counting related enrollment facts at their separate grain.
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- Select one student occurrence by identityEasy · sqlSolve Problem
- Duplicate EnrollmentsMedium · sqlSolve Problem
- Employees With No Real DepartmentMedium · sqlSolve Problem