Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b03b944

Browse files
Update readme.md
1 parent a37cf6b commit b03b944

File tree

1 file changed

+192
-0
lines changed
  • LeetCode SQL 50 Solution/1280. Students and Examinations

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
Here is the well-structured `README.md` file for **LeetCode 1204 - Find the Number of Times Each Student Attended Each Exam**, formatted for a GitHub repository:
2+
3+
```md
4+
# 🎓 Find the Number of Times Each Student Attended Each Exam - LeetCode 1204
5+
6+
## 📌 Problem Statement
7+
You are given three tables: **Students**, **Subjects**, and **Examinations**, which contain information about students, subjects, and exam attendance.
8+
9+
### 📊 Students Table
10+
| Column Name | Type |
11+
| ------------ | ------- |
12+
| student_id | int |
13+
| student_name | varchar |
14+
- `student_id` is the **primary key**.
15+
- Each row represents a **unique student**.
16+
17+
### 📊 Subjects Table
18+
| Column Name | Type |
19+
| ------------ | ------- |
20+
| subject_name | varchar |
21+
- `subject_name` is the **primary key**.
22+
- Each row represents a **unique subject**.
23+
24+
### 📊 Examinations Table
25+
| Column Name | Type |
26+
| ------------ | ------- |
27+
| student_id | int |
28+
| subject_name | varchar |
29+
- **No primary key** (may contain duplicates).
30+
- Each row indicates that a student attended an exam for a specific subject.
31+
32+
### 🔢 Goal:
33+
Find the **number of times each student attended each exam**.
34+
If a student did **not attend an exam**, return `0`.
35+
Return the results **ordered by** `student_id` and `subject_name`.
36+
37+
---
38+
39+
## 📊 Example 1:
40+
### Input:
41+
42+
### **Students Table**
43+
| student_id | student_name |
44+
| ---------- | ------------ |
45+
| 1 | Alice |
46+
| 2 | Bob |
47+
| 13 | John |
48+
| 6 | Alex |
49+
50+
### **Subjects Table**
51+
| subject_name |
52+
| ------------ |
53+
| Math |
54+
| Physics |
55+
| Programming |
56+
57+
### **Examinations Table**
58+
| student_id | subject_name |
59+
| ---------- | ------------ |
60+
| 1 | Math |
61+
| 1 | Physics |
62+
| 1 | Programming |
63+
| 2 | Programming |
64+
| 1 | Physics |
65+
| 1 | Math |
66+
| 13 | Math |
67+
| 13 | Programming |
68+
| 13 | Physics |
69+
| 2 | Math |
70+
| 1 | Math |
71+
72+
### Output:
73+
| student_id | student_name | subject_name | attended_exams |
74+
| ---------- | ------------ | ------------ | -------------- |
75+
| 1 | Alice | Math | 3 |
76+
| 1 | Alice | Physics | 2 |
77+
| 1 | Alice | Programming | 1 |
78+
| 2 | Bob | Math | 1 |
79+
| 2 | Bob | Physics | 0 |
80+
| 2 | Bob | Programming | 1 |
81+
| 6 | Alex | Math | 0 |
82+
| 6 | Alex | Physics | 0 |
83+
| 6 | Alex | Programming | 0 |
84+
| 13 | John | Math | 1 |
85+
| 13 | John | Physics | 1 |
86+
| 13 | John | Programming | 1 |
87+
88+
---
89+
90+
## 🖥 SQL Solution
91+
92+
### 1️⃣ Standard MySQL Query
93+
#### **Explanation:**
94+
- **Use `CROSS JOIN`** to generate **all possible student-subject combinations**.
95+
- **Use `LEFT JOIN`** to attach attendance records from `Examinations`.
96+
- **Use `COUNT(e.subject_name)`** to count how many times a student attended an exam.
97+
- **Sort results by** `student_id` and `subject_name`.
98+
99+
```sql
100+
SELECT s.student_id, s.student_name, sb.subject_name,
101+
COUNT(e.subject_name) AS attended_exams
102+
FROM Students s
103+
CROSS JOIN Subjects sb
104+
LEFT JOIN Examinations e
105+
ON s.student_id = e.student_id AND sb.subject_name = e.subject_name
106+
GROUP BY s.student_id, sb.subject_name
107+
ORDER BY s.student_id, sb.subject_name;
108+
```
109+
110+
---
111+
112+
### 2️⃣ Alternative SQL Query (Using `COALESCE`)
113+
```sql
114+
SELECT s.student_id, s.student_name, sb.subject_name,
115+
COALESCE(COUNT(e.subject_name), 0) AS attended_exams
116+
FROM Students s
117+
CROSS JOIN Subjects sb
118+
LEFT JOIN Examinations e
119+
ON s.student_id = e.student_id AND sb.subject_name = e.subject_name
120+
GROUP BY s.student_id, sb.subject_name
121+
ORDER BY s.student_id, sb.subject_name;
122+
```
123+
- **Uses `COALESCE(COUNT(...), 0)`** to explicitly handle `NULL` values.
124+
125+
---
126+
127+
### 3️⃣ Alternative SQL Query (Using `WITH ROLLUP`)
128+
```sql
129+
SELECT s.student_id, s.student_name, sb.subject_name,
130+
COUNT(e.subject_name) AS attended_exams
131+
FROM Students s
132+
CROSS JOIN Subjects sb
133+
LEFT JOIN Examinations e
134+
ON s.student_id = e.student_id AND sb.subject_name = e.subject_name
135+
GROUP BY s.student_id, sb.subject_name WITH ROLLUP
136+
HAVING GROUPING(subject_name) = 0
137+
ORDER BY s.student_id, sb.subject_name;
138+
```
139+
- **Uses `WITH ROLLUP`** to generate **aggregated results**.
140+
141+
---
142+
143+
## 🐍 Pandas Solution (Python)
144+
#### **Explanation:**
145+
- **Generate all possible (student, subject) pairs** using `pd.merge()`.
146+
- **Group by student_id and subject_name**, then count occurrences.
147+
- **Fill missing values with `0`**.
148+
149+
```python
150+
import pandas as pd
151+
152+
def student_exam_attendance(students: pd.DataFrame, subjects: pd.DataFrame, exams: pd.DataFrame) -> pd.DataFrame:
153+
# Create all possible student-subject combinations
154+
all_combinations = students.merge(subjects, how="cross")
155+
156+
# Merge with exam data
157+
merged = all_combinations.merge(exams, on=["student_id", "subject_name"], how="left")
158+
159+
# Count the number of times each student attended each exam
160+
result = merged.groupby(["student_id", "student_name", "subject_name"]).size().reset_index(name="attended_exams")
161+
162+
return result.sort_values(["student_id", "subject_name"])
163+
```
164+
165+
---
166+
167+
## 📁 File Structure
168+
```
169+
📂 Student-Exam-Attendance
170+
│── 📜 README.md
171+
│── 📜 solution.sql
172+
│── 📜 solution_pandas.py
173+
│── 📜 test_cases.sql
174+
```
175+
176+
---
177+
178+
## 🔗 Useful Links
179+
- 📖 [LeetCode Problem](https://leetcode.com/problems/find-the-number-of-times-each-student-attended-each-exam/)
180+
- 📚 [SQL `CROSS JOIN` Documentation](https://www.w3schools.com/sql/sql_join_cross.asp)
181+
- 🐍 [Pandas Merge Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)
182+
```
183+
184+
### Features of this `README.md`:
185+
✅ **Clear problem statement with table structure**
186+
✅ **Examples with detailed calculations**
187+
✅ **SQL and Pandas solutions with explanations**
188+
✅ **Alternative SQL query for flexibility**
189+
✅ **File structure for GitHub organization**
190+
✅ **Useful reference links**
191+
192+
Would you like any refinements? 🚀

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /