Repository navigation
[AI] TP1 & TP2 -BENRAHMOUNE Anes #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,335 @@ | ||||||||||||||||||||||||||||||||||||||
| -- TP1: Gestion de l'Universite | ||||||||||||||||||||||||||||||||||||||
| -- by: BENRAHMOUNE Anes | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- Creation de la base de donnees | ||||||||||||||||||||||||||||||||||||||
| CREATE DATABASE IF NOT EXISTS university_db; | ||||||||||||||||||||||||||||||||||||||
| USE university_db; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- Creation des tables | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- 1. Table Departements | ||||||||||||||||||||||||||||||||||||||
| CREATE TABLE departments ( | ||||||||||||||||||||||||||||||||||||||
| department_id INT PRIMARY KEY AUTO_INCREMENT, | ||||||||||||||||||||||||||||||||||||||
| department_name VARCHAR(100) NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| building VARCHAR(50), | ||||||||||||||||||||||||||||||||||||||
| budget DECIMAL(12, 2), | ||||||||||||||||||||||||||||||||||||||
| department_head VARCHAR(100), | ||||||||||||||||||||||||||||||||||||||
| creation_date DATE | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- 2. Table Professeurs | ||||||||||||||||||||||||||||||||||||||
| CREATE TABLE professors ( | ||||||||||||||||||||||||||||||||||||||
| professor_id INT PRIMARY KEY AUTO_INCREMENT, | ||||||||||||||||||||||||||||||||||||||
| last_name VARCHAR(50) NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| first_name VARCHAR(50) NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| email VARCHAR(100) UNIQUE NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| phone VARCHAR(20), | ||||||||||||||||||||||||||||||||||||||
| department_id INT, | ||||||||||||||||||||||||||||||||||||||
| hire_date DATE, | ||||||||||||||||||||||||||||||||||||||
| salary DECIMAL(10, 2), | ||||||||||||||||||||||||||||||||||||||
| specialization VARCHAR(100), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT fk_prof_dept FOREIGN KEY (department_id) REFERENCES departments(department_id) | ||||||||||||||||||||||||||||||||||||||
| ON DELETE SET NULL ON UPDATE CASCADE | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- 3. Table Etudiants | ||||||||||||||||||||||||||||||||||||||
| CREATE TABLE students ( | ||||||||||||||||||||||||||||||||||||||
| student_id INT PRIMARY KEY AUTO_INCREMENT, | ||||||||||||||||||||||||||||||||||||||
| student_number VARCHAR(20) UNIQUE NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| last_name VARCHAR(50) NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| first_name VARCHAR(50) NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| date_of_birth DATE, | ||||||||||||||||||||||||||||||||||||||
| email VARCHAR(100) UNIQUE NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| phone VARCHAR(20), | ||||||||||||||||||||||||||||||||||||||
| address TEXT, | ||||||||||||||||||||||||||||||||||||||
| department_id INT, | ||||||||||||||||||||||||||||||||||||||
| level VARCHAR(20), | ||||||||||||||||||||||||||||||||||||||
| enrollment_date DATE DEFAULT (CURRENT_DATE), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT chk_level CHECK (level IN ('L1', 'L2', 'L3', 'M1', 'M2')), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT fk_stud_dept FOREIGN KEY (department_id) REFERENCES departments(department_id) | ||||||||||||||||||||||||||||||||||||||
| ON DELETE SET NULL ON UPDATE CASCADE | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- 4. Table Cours | ||||||||||||||||||||||||||||||||||||||
| CREATE TABLE courses ( | ||||||||||||||||||||||||||||||||||||||
| course_id INT PRIMARY KEY AUTO_INCREMENT, | ||||||||||||||||||||||||||||||||||||||
| course_code VARCHAR(10) UNIQUE NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| course_name VARCHAR(150) NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| description TEXT, | ||||||||||||||||||||||||||||||||||||||
| credits INT NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| semester INT, | ||||||||||||||||||||||||||||||||||||||
| department_id INT, | ||||||||||||||||||||||||||||||||||||||
| professor_id INT, | ||||||||||||||||||||||||||||||||||||||
| max_capacity INT DEFAULT 30, | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT chk_credits CHECK (credits > 0), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT chk_semester CHECK (semester BETWEEN 1 AND 2), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT fk_course_dept FOREIGN KEY (department_id) REFERENCES departments(department_id) | ||||||||||||||||||||||||||||||||||||||
| ON DELETE CASCADE ON UPDATE CASCADE, | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT fk_course_prof FOREIGN KEY (professor_id) REFERENCES professors(professor_id) | ||||||||||||||||||||||||||||||||||||||
| ON DELETE SET NULL ON UPDATE CASCADE | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- 5. Table Inscriptions | ||||||||||||||||||||||||||||||||||||||
| CREATE TABLE enrollments ( | ||||||||||||||||||||||||||||||||||||||
| enrollment_id INT PRIMARY KEY AUTO_INCREMENT, | ||||||||||||||||||||||||||||||||||||||
| student_id INT NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| course_id INT NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| enrollment_date DATE DEFAULT (CURRENT_DATE), | ||||||||||||||||||||||||||||||||||||||
| academic_year VARCHAR(9) NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| status VARCHAR(20) DEFAULT 'In Progress', | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT chk_status CHECK (status IN ('In Progress', 'Passed', 'Failed', 'Dropped')), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT fk_enrol_stud FOREIGN KEY (student_id) REFERENCES students(student_id) ON DELETE CASCADE ON UPDATE CASCADE, | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT fk_enrol_course FOREIGN KEY (course_id) REFERENCES courses(course_id) ON DELETE CASCADE ON UPDATE CASCADE, | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT unique_enrollment UNIQUE (student_id, course_id, academic_year) | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- 6. Table Notes | ||||||||||||||||||||||||||||||||||||||
| CREATE TABLE grades ( | ||||||||||||||||||||||||||||||||||||||
| grade_id INT PRIMARY KEY AUTO_INCREMENT, | ||||||||||||||||||||||||||||||||||||||
| enrollment_id INT NOT NULL, | ||||||||||||||||||||||||||||||||||||||
| evaluation_type VARCHAR(30), | ||||||||||||||||||||||||||||||||||||||
| grade DECIMAL(5, 2), | ||||||||||||||||||||||||||||||||||||||
| coefficient DECIMAL(3, 2) DEFAULT 1.00, | ||||||||||||||||||||||||||||||||||||||
| evaluation_date DATE, | ||||||||||||||||||||||||||||||||||||||
| comments TEXT, | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT chk_eval_type CHECK (evaluation_type IN ('Assignment', 'Lab', 'Exam', 'Project')), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT chk_grade_val CHECK (grade BETWEEN 0 AND 20), | ||||||||||||||||||||||||||||||||||||||
| CONSTRAINT fk_grade_enrol FOREIGN KEY (enrollment_id) REFERENCES enrollments(enrollment_id) ON DELETE CASCADE ON UPDATE CASCADE | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- Indexation | ||||||||||||||||||||||||||||||||||||||
| CREATE INDEX idx_stud_dept ON students(department_id); | ||||||||||||||||||||||||||||||||||||||
| CREATE INDEX idx_crs_prof ON courses(professor_id); | ||||||||||||||||||||||||||||||||||||||
| CREATE INDEX idx_enr_stud ON enrollments(student_id); | ||||||||||||||||||||||||||||||||||||||
| CREATE INDEX idx_enr_crs ON enrollments(course_id); | ||||||||||||||||||||||||||||||||||||||
| CREATE INDEX idx_grd_enr ON grades(enrollment_id); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- Insertion des donnees (Contexte Algerien) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| -- Departements (USTHB) | ||||||||||||||||||||||||||||||||||||||
| INSERT INTO departments VALUES (NULL, 'Informatique', 'Bloc A', 500000.00, 'Dr. Mansouri', '1974-09-15'); | ||||||||||||||||||||||||||||||||||||||
| INSERT INTO departments VALUES (NULL, 'Mathematiques', 'Bloc B', 350000.00, 'Pr. Belhadj', '1974-09-15'); | ||||||||||||||||||||||||||||||||||||||
| INSERT INTO departments VALUES (NULL, 'Physique', 'Bloc C', 400000.00, 'Dr. Haddad', '1975-10-01'); | ||||||||||||||||||||||||||||||||||||||
| INSERT INTO departments VALUES (NULL, 'Genie Civil', 'Bloc D', 600000.00, 'Pr. Benali', '1980-01-20'); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+110
to
+113
|
||||||||||||||||||||||||||||||||||||||
| INSERT INTO departments VALUES (NULL, 'Informatique', 'Bloc A', 500000.00, 'Dr. Mansouri', '1974-09-15'); | |
| INSERT INTO departments VALUES (NULL, 'Mathematiques', 'Bloc B', 350000.00, 'Pr. Belhadj', '1974-09-15'); | |
| INSERT INTO departments VALUES (NULL, 'Physique', 'Bloc C', 400000.00, 'Dr. Haddad', '1975-10-01'); | |
| INSERT INTO departments VALUES (NULL, 'Genie Civil', 'Bloc D', 600000.00, 'Pr. Benali', '1980-01-20'); | |
| INSERT INTO departments (department_name, building, budget, department_head, creation_date) | |
| VALUES ('Informatique', 'Bloc A', 500000.00, 'Dr. Mansouri', '1974-09-15'); | |
| INSERT INTO departments (department_name, building, budget, department_head, creation_date) | |
| VALUES ('Mathematiques', 'Bloc B', 350000.00, 'Pr. Belhadj', '1974-09-15'); | |
| INSERT INTO departments (department_name, building, budget, department_head, creation_date) | |
| VALUES ('Physique', 'Bloc C', 400000.00, 'Dr. Haddad', '1975-10-01'); | |
| INSERT INTO departments (department_name, building, budget, department_head, creation_date) | |
| VALUES ('Genie Civil', 'Bloc D', 600000.00, 'Pr. Benali', '1980-01-20'); |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The query uses an old-style implicit join (comma-separated tables in FROM clause) instead of explicit JOIN syntax. While this works, modern SQL best practices recommend using explicit JOIN syntax for better readability and to avoid accidental cross joins. Consider rewriting as: "FROM professors p JOIN departments d ON p.department_id = d.department_id WHERE d.department_name = 'Informatique'"
| FROM professors p, departments d | |
| WHERE p.department_id = d.department_id AND d.department_name = 'Informatique'; | |
| FROM professors p | |
| JOIN departments d ON p.department_id = d.department_id | |
| WHERE d.department_name = 'Informatique'; |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query is incorrect for Q12. According to the assignment (tp1-university-system/queries.sql:59-61), Q12 should "Count the number of students per department" with expected columns: department_name, student_count. The current query counts courses per professor (which is a duplicate of Q10). The correct query should group students by department.
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The subquery in the HAVING clause compares against the average of all students in the same department, but the query groups by both student_id AND department_id. Since each student belongs to only one department, grouping by department_id is redundant. The logic appears correct but could be simplified by removing s.department_id from the GROUP BY clause.
| GROUP BY s.student_id, s.department_id | |
| GROUP BY s.student_id |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assignment (tp1-university-system/queries.sql:100-102), Q21 should return "student_name, student_avg, department_avg". The current query returns student_name and moy (student_avg) but is missing the department_avg as a separate column. Add the department average to the SELECT clause to match the expected output.
| SELECT CONCAT(s.last_name, ' ', s.first_name) AS student, AVG(g.grade) AS moy | |
| FROM students s, enrollments e, grades g | |
| WHERE s.student_id = e.student_id AND e.enrollment_id = g.enrollment_id | |
| GROUP BY s.student_id, s.department_id | |
| HAVING moy > (SELECT AVG(g2.grade) FROM grades g2, enrollments e2, students s2 | |
| WHERE g2.enrollment_id = e2.enrollment_id AND e2.student_id = s2.student_id | |
| AND s2.department_id = s.department_id); | |
| SELECT CONCAT(s.last_name, ' ', s.first_name) AS student, | |
| AVG(g.grade) AS moy, | |
| (SELECT AVG(g2.grade) | |
| FROM grades g2, enrollments e2, students s2 | |
| WHERE g2.enrollment_id = e2.enrollment_id | |
| AND e2.student_id = s2.student_id | |
| AND s2.department_id = s.department_id) AS department_avg | |
| FROM students s, enrollments e, grades g | |
| WHERE s.student_id = e.student_id AND e.enrollment_id = g.enrollment_id | |
| GROUP BY s.student_id, s.department_id | |
| HAVING moy > department_avg; |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assignment (tp1-university-system/queries.sql:104-106), Q22 should return "course_name, enrollment_count". The current query returns these columns correctly, but consider adding a comparison to the average in the output for clarity (e.g., including the average_enrollment as a column), though it's technically correct as-is.
| SELECT c.course_name, COUNT(e.enrollment_id) AS nb | |
| FROM courses c, enrollments e | |
| WHERE c.course_id = e.course_id | |
| GROUP BY c.course_id | |
| HAVING nb > (SELECT AVG(cnt) FROM (SELECT COUNT(*) as cnt FROM enrollments GROUP BY course_id) t); | |
| SELECT c.course_name, | |
| COUNT(e.enrollment_id) AS nb, | |
| (SELECT AVG(cnt) | |
| FROM (SELECT COUNT(*) AS cnt FROM enrollments GROUP BY course_id) t) AS average_enrollment | |
| FROM courses c, enrollments e | |
| WHERE c.course_id = e.course_id | |
| GROUP BY c.course_id | |
| HAVING nb > (SELECT AVG(cnt) FROM (SELECT COUNT(*) AS cnt FROM enrollments GROUP BY course_id) t); |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assignment (tp1-university-system/queries.sql:108-110), Q23 should return "professor_name, department_name, budget". The current query returns professor_name and department_name but is missing the budget column. Add d.budget to the SELECT clause to match the expected output.
| SELECT CONCAT(p.last_name, ' ', p.first_name) AS prof, d.department_name | |
| SELECT CONCAT(p.last_name, ' ', p.first_name) AS prof, d.department_name, d.budget |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assignment (tp1-university-system/queries.sql:122-124), Q26 should return "course_name, total_grades, passed_grades, pass_rate_percentage". The current query returns these columns correctly but uses inconsistent naming. The variable 'total' should be 'total_grades', 'admis' should be 'passed_grades', and 'taux' should be 'pass_rate_percentage' to match the expected column names.
| COUNT(g.grade_id) AS total, | |
| SUM(CASE WHEN g.grade >= 10 THEN 1 ELSE 0 END) AS admis, | |
| (SUM(CASE WHEN g.grade >= 10 THEN 1 ELSE 0 END) / COUNT(g.grade_id)) * 100 AS taux | |
| COUNT(g.grade_id) AS total_grades, | |
| SUM(CASE WHEN g.grade >= 10 THEN 1 ELSE 0 END) AS passed_grades, | |
| (SUM(CASE WHEN g.grade >= 10 THEN 1 ELSE 0 END) / COUNT(g.grade_id)) * 100 AS pass_rate_percentage |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assignment (tp1-university-system/queries.sql:127-129), Q27 should return "rank, student_name, average_grade". The current query returns student_name and moy (average_grade) but is missing the rank column. While the ORDER BY provides implicit ordering, an explicit rank column is expected. Consider adding ROW_NUMBER() OVER (ORDER BY moy DESC) as rank if supported, or use a variable-based ranking.
| SELECT CONCAT(s.last_name, ' ', s.first_name) AS student, AVG(g.grade) AS moy | |
| FROM students s, enrollments e, grades g | |
| WHERE s.student_id = e.student_id AND e.enrollment_id = g.enrollment_id | |
| GROUP BY s.student_id ORDER BY moy DESC; | |
| SELECT | |
| ROW_NUMBER() OVER (ORDER BY t.average_grade DESC) AS rank, | |
| t.student_name, | |
| t.average_grade | |
| FROM ( | |
| SELECT | |
| CONCAT(s.last_name, ' ', s.first_name) AS student_name, | |
| AVG(g.grade) AS average_grade | |
| FROM students s, enrollments e, grades g | |
| WHERE s.student_id = e.student_id AND e.enrollment_id = g.enrollment_id | |
| GROUP BY s.student_id | |
| ) AS t | |
| ORDER BY rank; |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assignment (tp1-university-system/queries.sql:130-132), Q28 should return "course_name, evaluation_type, grade, coefficient, weighted_grade". The current query returns course_name, evaluation_type, grade, and coefficient but is missing weighted_grade (grade * coefficient). Add a calculated column for weighted_grade to match the expected output.
| SELECT c.course_name, g.evaluation_type, g.grade, g.coefficient | |
| SELECT c.course_name, | |
| g.evaluation_type, | |
| g.grade, | |
| g.coefficient, | |
| g.grade * g.coefficient AS weighted_grade |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assignment (tp1-university-system/queries.sql:138-140), Q30 should return "course_name, current_enrollments, max_capacity, percentage_full". The current query uses 'inscrits' instead of 'current_enrollments' and is missing the percentage_full column. Add a calculated column for the percentage and use more descriptive English column names to match the expected output.
| SELECT c.course_name, COUNT(e.enrollment_id) AS inscrits, c.max_capacity | |
| FROM courses c LEFT JOIN enrollments e ON c.course_id = e.course_id | |
| GROUP BY c.course_id | |
| HAVING (inscrits / c.max_capacity) > 0.8; | |
| SELECT c.course_name, | |
| COUNT(e.enrollment_id) AS current_enrollments, | |
| c.max_capacity AS max_capacity, | |
| (COUNT(e.enrollment_id) / c.max_capacity) * 100 AS percentage_full | |
| FROM courses c LEFT JOIN enrollments e ON c.course_id = e.course_id | |
| GROUP BY c.course_id | |
| HAVING percentage_full > 80; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR title includes both TP1 and TP2, but the expected format according to submissions/AI/README.md is "[AI] TP1 - LASTNAME Firstname" (separate PRs for TP1 and TP2, or at least following the single format). While this is functional, it deviates from the documented convention.