Count Student Number in Departments - Problem

๐ŸŽ“ Count Student Number in Departments

You're working as a database administrator for a university that needs to generate a comprehensive department report. The university maintains two tables: one tracking students and their department assignments, and another containing all department information.

Your task is to create a report showing every department along with the number of students currently enrolled in each one. This includes departments that might have zero students - the administration wants a complete picture of all departments, regardless of enrollment status.

Key Requirements:

  • Show all departments from the Department table
  • Count students in each department (including 0 for empty departments)
  • Sort by student count (highest first), then by department name alphabetically for ties

The result should help administrators identify popular programs and departments that might need more marketing or resources.

Input & Output

Basic Example
$ Input: Student: | student_id | student_name | gender | dept_id | |------------|--------------|--------|---------| | 1 | Jack | M | 1 | | 2 | Jane | F | 1 | | 3 | Mark | M | 2 | Department: | dept_id | dept_name | |---------|-------------| | 1 | Engineering | | 2 | Science | | 3 | Law |
โ€บ Output: | dept_name | student_number | |-------------|----------------| | Engineering | 2 | | Science | 1 | | Law | 0 |
๐Ÿ’ก Note: Engineering has 2 students (Jack and Jane), Science has 1 student (Mark), and Law has 0 students. Results are ordered by student count descending.
Tied Departments
$ Input: Student: | student_id | student_name | gender | dept_id | |------------|--------------|--------|---------| | 1 | Alice | F | 1 | | 2 | Bob | M | 2 | Department: | dept_id | dept_name | |---------|----------| | 1 | Math | | 2 | Art | | 3 | Biology |
โ€บ Output: | dept_name | student_number | |-----------|----------------| | Art | 1 | | Math | 1 | | Biology | 0 |
๐Ÿ’ก Note: Math and Art both have 1 student, so they're ordered alphabetically (Art before Math). Biology has 0 students and comes last.
All Empty Departments
$ Input: Student: | student_id | student_name | gender | dept_id | |------------|--------------|--------|---------| (empty table) Department: | dept_id | dept_name | |---------|----------| | 1 | Physics | | 2 | Chemistry |
โ€บ Output: | dept_name | student_number | |-----------|----------------| | Chemistry | 0 | | Physics | 0 |
๐Ÿ’ก Note: No students exist, so all departments have 0 students. With equal counts, departments are sorted alphabetically.

Visualization

Tap to expand
All Departments๐Ÿ“š Engineering๐Ÿ”ฌ Scienceโš–๏ธ LawStudents๐Ÿ‘จ Jack (Eng)๐Ÿ‘ฉ Jane (Eng)๐Ÿ‘จ Mark (Sci)JOINFinal Report๐Ÿ“š Engineering: 2๐Ÿ”ฌ Science: 1โš–๏ธ Law: 0โ†‘ Sorted by countCOUNTโœ“All depts includedโšกSingle pass๐Ÿ“ŠProper ordering
Understanding the Visualization
1
Gather Department List
Start with complete list of all university departments
2
Match Students
For each department, find all students belonging to it
3
Count & Record
Count students per department (0 for empty departments)
4
Sort Report
Order by student count (highest first), then alphabetically
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN ensures all departments appear in the result (preserving departments with 0 students), while GROUP BY and COUNT efficiently aggregate student numbers in a single database operation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Single pass through departments (n) and students (m) with efficient JOIN operation

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

No additional space needed beyond result set and join processing

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Department table rows โ‰ค 100
  • 0 โ‰ค Student table rows โ‰ค 1000
  • All department IDs in Student table exist in Department table
  • Must include departments with 0 students
  • Department names are unique and non-null
Asked in
Amazon 45 Microsoft 38 Oracle 32 Google 28
24.4K Views
High Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen