diff --git a/SS/mezaache-abdessamie/tp1_solution.sql b/SS/mezaache-abdessamie/tp1_solution.sql new file mode 100644 index 0000000..c7dc1b6 --- /dev/null +++ b/SS/mezaache-abdessamie/tp1_solution.sql @@ -0,0 +1,200 @@ +-- TP1/// University Management System (Written for Sqlite) +-- please madame use tp1_university.sql | sqlite3 database_one.db OR add "cat" at the beggining if you are using powershell to create tables for this exact script +PRAGMA foreign_keys = ON; + +DROP TABLE IF EXISTS grades; +DROP TABLE IF EXISTS enrollments; +DROP TABLE IF EXISTS courses; +DROP TABLE IF EXISTS professors; +DROP TABLE IF EXISTS students; +DROP TABLE IF EXISTS departments; + +-- Tables +CREATE TABLE departments ( + department_id INTEGER PRIMARY KEY AUTOINCREMENT, + department_name TEXT NOT NULL, + building TEXT, + budget REAL, + department_head TEXT, + creation_date TEXT +); + +CREATE TABLE professors ( + professor_id INTEGER PRIMARY KEY AUTOINCREMENT, + last_name TEXT NOT NULL, + first_name TEXT NOT NULL, + email TEXT UNIQUE NOT NULL, + phone TEXT, + department_id INTEGER, + hire_date TEXT, + salary REAL, + specialization TEXT, + FOREIGN KEY (department_id) REFERENCES departments(department_id) ON DELETE SET NULL ON UPDATE CASCADE +); + +CREATE TABLE students ( + student_id INTEGER PRIMARY KEY AUTOINCREMENT, + student_number TEXT UNIQUE NOT NULL, + last_name TEXT NOT NULL, + first_name TEXT NOT NULL, + date_of_birth TEXT, + email TEXT UNIQUE NOT NULL, + phone TEXT, + address TEXT, + department_id INTEGER, + level TEXT CHECK (level IN ('L1', 'L2', 'L3', 'M1', 'M2')), + enrollment_date TEXT DEFAULT CURRENT_DATE, + FOREIGN KEY (department_id) REFERENCES departments(department_id) ON DELETE SET NULL ON UPDATE CASCADE +); + +CREATE TABLE courses ( + course_id INTEGER PRIMARY KEY AUTOINCREMENT, + course_code TEXT UNIQUE NOT NULL, + course_name TEXT NOT NULL, + description TEXT, + credits INTEGER NOT NULL CHECK (credits > 0), + semester INTEGER CHECK (semester BETWEEN 1 AND 2), + department_id INTEGER, + professor_id INTEGER, + max_capacity INTEGER DEFAULT 30, + FOREIGN KEY (department_id) REFERENCES departments(department_id) ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (professor_id) REFERENCES professors(professor_id) ON DELETE SET NULL ON UPDATE CASCADE +); + +CREATE TABLE enrollments ( + enrollment_id INTEGER PRIMARY KEY AUTOINCREMENT, + student_id INTEGER NOT NULL, + course_id INTEGER NOT NULL, + enrollment_date TEXT DEFAULT CURRENT_DATE, + academic_year TEXT NOT NULL, + status TEXT DEFAULT 'In Progress' CHECK (status IN ('In Progress', 'Passed', 'Failed', 'Dropped')), + UNIQUE (student_id, course_id, academic_year), + FOREIGN KEY (student_id) REFERENCES students(student_id) ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (course_id) REFERENCES courses(course_id) ON DELETE CASCADE ON UPDATE CASCADE +); + +CREATE TABLE grades ( + grade_id INTEGER PRIMARY KEY AUTOINCREMENT, + enrollment_id INTEGER NOT NULL, + evaluation_type TEXT CHECK (evaluation_type IN ('Assignment', 'Lab', 'Exam', 'Project')), + grade REAL CHECK (grade BETWEEN 0 AND 20), + coefficient REAL DEFAULT 1.00, + evaluation_date TEXT, + comments TEXT, + FOREIGN KEY (enrollment_id) REFERENCES enrollments(enrollment_id) ON DELETE CASCADE ON UPDATE CASCADE +); + +-- Indexes +CREATE INDEX idx_student_department ON students(department_id); +CREATE INDEX idx_course_professor ON courses(professor_id); +CREATE INDEX idx_enrollment_student ON enrollments(student_id); +CREATE INDEX idx_enrollment_course ON enrollments(course_id); +CREATE INDEX idx_grades_enrollment ON grades(enrollment_id); + +-- Test Data +INSERT INTO departments (department_name, building, budget) VALUES +('Computer Science', 'Building A', 500000), +('Mathematics', 'Building B', 350000), +('Physics', 'Building C', 400000), +('Civil Engineering', 'Building D', 600000); + +INSERT INTO professors (last_name, first_name, email, department_id, salary, specialization) VALUES +('Bensaid', 'Amine', 'a.bensaid@uni.edu', 1, 75000, 'AI'), +('Ziani', 'Rachid', 'r.ziani@uni.edu', 1, 68000, 'Databases'), +('Kacimi', 'Lydia', 'l.kacimi@uni.edu', 1, 70000, 'Cybersecurity'), +('Khelifi', 'Sofia', 's.khelifi@uni.edu', 2, 60000, 'Algebra'), +('Ould', 'Lamia', 'l.ould@uni.edu', 3, 62000, 'Optics'), +('Haddad', 'Mehdi', 'm.haddad@uni.edu', 4, 72000, 'Structures'); + +INSERT INTO students (student_number, last_name, first_name, email, level, department_id) VALUES +('S001', 'Meziane', 'Yacine', 'y.mez@mail.com', 'L2', 1), +('S002', 'Kaci', 'Rania', 'r.kaci@mail.com', 'L3', 2), +('S003', 'Saad', 'Karim', 'k.saad@mail.com', 'M1', 1), +('S004', 'Yahia', 'Lina', 'l.yahia@mail.com', 'L3', 3), +('S005', 'Benali', 'Omar', 'o.ben@mail.com', 'L2', 1), +('S006', 'Cherif', 'Meriem', 'm.cherif@mail.com', 'M1', 4), +('S007', 'Zouaoui', 'Samir', 's.zou@mail.com', 'L3', 2), +('S008', 'Djerbi', 'Sofia', 's.djer@mail.com', 'L2', 1); + +INSERT INTO courses (course_code, course_name, credits, semester, department_id, professor_id, max_capacity) VALUES +('CS101', 'Intro to SQL', 6, 1, 1, 2, 50), +('CS102', 'Algorithms', 5, 2, 1, 1, 40), +('MA101', 'Calculus', 5, 1, 2, 4, 60), +('PH101', 'Mechanics', 5, 1, 3, 5, 30), +('CE101', 'Statics', 6, 1, 4, 6, 25), +('CS201', 'Security', 6, 1, 1, 3, 30), +('MA201', 'Algebra II', 5, 2, 2, 4, 50); + +INSERT INTO enrollments (student_id, course_id, academic_year, status) VALUES +(1,1,'2024-2025','Passed'), (1,2,'2024-2025','In Progress'), (2,3,'2024-2025','Passed'), +(3,1,'2024-2025','Passed'), (4,4,'2024-2025','In Progress'), (5,1,'2024-2025','Failed'), +(6,5,'2024-2025','Passed'), (7,3,'2024-2025','Passed'), (8,1,'2024-2025','In Progress'), +(1,6,'2023-2024','Passed'), (2,7,'2023-2024','Passed'), (3,6,'2023-2024','Passed'), +(5,2,'2023-2024','Passed'), (7,7,'2023-2024','Passed'), (8,6,'2024-2025','In Progress'); + +INSERT INTO grades (enrollment_id, evaluation_type, grade, coefficient) VALUES +(1,'Exam',15,1.5), (3,'Exam',12,1.5), (4,'Exam',18,1.5), (6,'Exam',8,1.5), (7,'Exam',14,1.5), +(8,'Exam',16,1.5), (10,'Exam',17,1.5), (11,'Exam',13,1.5), (12,'Project',15,1.0), +(13,'Assignment',14,0.5), (14,'Lab',12,1.0), (1,'Assignment',14,0.5); + +-- Solutions Q1-Q30 +-- Q1 +SELECT last_name, first_name, email, level FROM students; +-- Q2 +SELECT p.last_name, p.first_name, p.email, p.specialization FROM professors p JOIN departments d ON p.department_id = d.department_id WHERE d.department_name = 'Computer Science'; +-- Q3 +SELECT course_code, course_name, credits FROM courses WHERE credits > 5; +-- Q4 +SELECT student_number, last_name, first_name, email FROM students WHERE level = 'L3'; +-- Q5 +SELECT course_code, course_name, credits, semester FROM courses WHERE semester = 1; +-- Q6 +SELECT c.course_code, c.course_name, p.last_name || ' ' || p.first_name AS professor_name FROM courses c LEFT JOIN professors p ON c.professor_id = p.professor_id; +-- q7 +SELECT s.last_name || ' ' || s.first_name AS student_name, c.course_name, e.enrollment_date, e.status FROM enrollments e JOIN students s ON e.student_id = s.student_id JOIN courses c ON e.course_id = c.course_id; +-- Q8 +SELECT s.last_name || ' ' || s.first_name AS student_name, d.department_name, s.level FROM students s JOIN departments d ON s.department_id = d.department_id; +-- Q9 +SELECT s.last_name || ' ' || s.first_name AS student_name, c.course_name, g.evaluation_type, g.grade FROM grades g JOIN enrollments e ON g.enrollment_id = e.enrollment_id JOIN students s ON e.student_id = s.student_id JOIN courses c ON e.course_id = c.course_id; +-- Q10 +SELECT p.last_name || ' ' || p.first_name AS professor_name, COUNT(c.course_id) AS number_of_courses FROM professors p LEFT JOIN courses c ON p.professor_id = c.professor_id GROUP BY p.professor_id; +-- Q11 +SELECT s.last_name || ' ' || s.first_name AS student_name, ROUND(AVG(g.grade), 2) AS average_grade FROM students s JOIN enrollments e ON s.student_id = e.student_id JOIN grades g ON e.enrollment_id = g.enrollment_id GROUP BY s.student_id; +-- Q12 +SELECT d.department_name, COUNT(s.student_id) AS student_count FROM departments d LEFT JOIN students s ON d.department_id = s.department_id GROUP BY d.department_id; +-- Q13 +SELECT SUM(budget) AS total_budget FROM departments; +-- Q14 +SELECT d.department_name, COUNT(c.course_id) AS course_count FROM departments d LEFT JOIN courses c ON d.department_id = c.department_id GROUP BY d.department_id; +-- Q15 +SELECT d.department_name, AVG(p.salary) AS average_salary FROM departments d JOIN professors p ON d.department_id = p.department_id GROUP BY d.department_id; +-- Q16 +SELECT s.last_name || ' ' || s.first_name AS student_name, AVG(g.grade) AS average_grade FROM students s JOIN enrollments e ON s.student_id = e.student_id JOIN grades g ON e.enrollment_id = g.enrollment_id GROUP BY s.student_id ORDER BY average_grade DESC LIMIT 3; +-- q17 +SELECT course_code, course_name FROM courses WHERE course_id NOT IN (SELECT course_id FROM enrollments); +-- Q18 +SELECT s.last_name || ' ' || s.first_name AS student_name, COUNT(*) AS passed_courses_count FROM students s JOIN enrollments e ON s.student_id = e.student_id WHERE e.status = 'Passed' GROUP BY s.student_id HAVING COUNT(*) = (SELECT COUNT(*) FROM enrollments e2 WHERE e2.student_id = s.student_id); +-- Q19 +SELECT p.last_name || ' ' || p.first_name AS professor_name, COUNT(c.course_id) AS courses_taught FROM professors p JOIN courses c ON p.professor_id = c.professor_id GROUP BY p.professor_id HAVING courses_taught > 2; +-- Q20 +SELECT s.last_name || ' ' || s.first_name AS student_name, COUNT(e.enrollment_id) AS enrolled_courses_count FROM students s JOIN enrollments e ON s.student_id = e.student_id GROUP BY s.student_id HAVING enrolled_courses_count > 2; +-- Q21 +SELECT s.last_name || ' ' || s.first_name AS student_name, AVG(g.grade) AS student_avg, (SELECT AVG(g2.grade) FROM grades g2 JOIN enrollments e2 ON g2.enrollment_id = e2.enrollment_id JOIN students s2 ON e2.student_id = s2.student_id WHERE s2.department_id = s.department_id) AS department_avg FROM students s JOIN enrollments e ON s.student_id = e.student_id JOIN grades g ON e.enrollment_id = g.enrollment_id GROUP BY s.student_id HAVING student_avg > department_avg; +-- Q22 +SELECT c.course_name, COUNT(e.enrollment_id) AS enrollment_count FROM courses c JOIN enrollments e ON c.course_id = e.course_id GROUP BY c.course_id HAVING enrollment_count > (SELECT COUNT(*) * 1.0 / (SELECT COUNT(DISTINCT course_id) FROM enrollments) FROM enrollments); +-- q3 +SELECT p.last_name || ' ' || p.first_name AS professor_name, d.department_name, d.budget FROM professors p JOIN departments d ON p.department_id = d.department_id WHERE d.budget = (SELECT MAX(budget) FROM departments); +-- 24 +SELECT s.last_name || ' ' || s.first_name AS student_name, s.email FROM students s WHERE s.student_id NOT IN (SELECT e.student_id FROM enrollments e JOIN grades g ON e.enrollment_id = g.enrollment_id); +-- Q25 +SELECT d.department_name, COUNT(s.student_id) AS student_count FROM departments d JOIN students s ON d.department_id = s.department_id GROUP BY d.department_id HAVING student_count > (SELECT AVG(cnt) FROM (SELECT COUNT(*) as cnt FROM students GROUP BY department_id)); +-- Q26 +SELECT c.course_name, COUNT(g.grade_id) AS total_grades, SUM(CASE WHEN g.grade >= 10 THEN 1 ELSE 0 END) AS passed_grades, ROUND(SUM(CASE WHEN g.grade >= 10 THEN 1 ELSE 0 END) * 100.0 / COUNT(g.grade_id), 2) AS pass_rate_percentage FROM courses c JOIN enrollments e ON c.course_id = e.course_id JOIN grades g ON e.enrollment_id = g.enrollment_id GROUP BY c.course_id; +-- Q27 +SELECT RANK() OVER(ORDER BY AVG(g.grade) DESC) as rank, s.last_name || ' ' || s.first_name AS student_name, AVG(g.grade) AS average_grade FROM students s JOIN enrollments e ON s.student_id = e.student_id JOIN grades g ON e.enrollment_id = g.enrollment_id GROUP BY s.student_id; +-- Q28 +SELECT c.course_name, g.evaluation_type, g.grade, g.coefficient, (g.grade * g.coefficient) AS weighted_grade FROM grades g JOIN enrollments e ON g.enrollment_id = e.enrollment_id JOIN courses c ON e.course_id = c.course_id WHERE e.student_id = 1; +-- Q29 +SELECT p.last_name || ' ' || p.first_name AS professor_name, SUM(c.credits) AS total_credits FROM professors p JOIN courses c ON p.professor_id = c.professor_id GROUP BY p.professor_id; +-- Q30 +SELECT c.course_name, COUNT(e.enrollment_id) AS current_enrollments, c.max_capacity, (COUNT(e.enrollment_id) * 100.0 / c.max_capacity) AS percentage_full FROM courses c JOIN enrollments e ON c.course_id = e.course_id GROUP BY c.course_id HAVING percentage_full > 80; \ No newline at end of file diff --git a/SS/mezaache-abdessamie/tp2_solution.sql b/SS/mezaache-abdessamie/tp2_solution.sql new file mode 100644 index 0000000..2e3dede --- /dev/null +++ b/SS/mezaache-abdessamie/tp2_solution.sql @@ -0,0 +1,223 @@ + +-- TP2: Hospital Management System +-- please madame use tp2_hospital.sql | sqlite3 database_two.db OR add "cat" at the beggining if you are using powershell to create tables for this exact script +PRAGMA foreign_keys = ON; + +DROP TABLE IF EXISTS prescription_details; +DROP TABLE IF EXISTS prescriptions; +DROP TABLE IF EXISTS consultations; +DROP TABLE IF EXISTS medications; +DROP TABLE IF EXISTS patients; +DROP TABLE IF EXISTS doctors; +DROP TABLE IF EXISTS specialties; + +-- Tables +CREATE TABLE specialties ( + specialty_id INTEGER PRIMARY KEY AUTOINCREMENT, + specialty_name TEXT UNIQUE NOT NULL, + description TEXT, + consultation_fee REAL NOT NULL +); + +CREATE TABLE doctors ( + doctor_id INTEGER PRIMARY KEY AUTOINCREMENT, + last_name TEXT NOT NULL, + first_name TEXT NOT NULL, + email TEXT UNIQUE NOT NULL, + phone TEXT, + specialty_id INTEGER NOT NULL, + license_number TEXT UNIQUE NOT NULL, + hire_date TEXT, + office TEXT, + active INTEGER DEFAULT 1, + FOREIGN KEY (specialty_id) REFERENCES specialties(specialty_id) ON DELETE RESTRICT ON UPDATE CASCADE +); + +CREATE TABLE patients ( + patient_id INTEGER PRIMARY KEY AUTOINCREMENT, + file_number TEXT UNIQUE NOT NULL, + last_name TEXT NOT NULL, + first_name TEXT NOT NULL, + date_of_birth TEXT NOT NULL, + gender TEXT CHECK(gender IN ('M', 'F')), + blood_type TEXT, + email TEXT, + phone TEXT NOT NULL, + address TEXT, + city TEXT, + province TEXT, + registration_date TEXT DEFAULT CURRENT_DATE, + insurance TEXT, + insurance_number TEXT, + allergies TEXT, + medical_history TEXT +); + +CREATE TABLE consultations ( + consultation_id INTEGER PRIMARY KEY AUTOINCREMENT, + patient_id INTEGER NOT NULL, + doctor_id INTEGER NOT NULL, + consultation_date TEXT NOT NULL, + reason TEXT NOT NULL, + diagnosis TEXT, + observations TEXT, + blood_pressure TEXT, + temperature REAL, + weight REAL, + height REAL, + status TEXT DEFAULT 'Scheduled' CHECK(status IN ('Scheduled','In Progress','Completed','Cancelled')), + amount REAL, + paid INTEGER DEFAULT 0, + FOREIGN KEY (patient_id) REFERENCES patients(patient_id) ON DELETE CASCADE, + FOREIGN KEY (doctor_id) REFERENCES doctors(doctor_id) ON DELETE RESTRICT +); + +CREATE TABLE medications ( + medication_id INTEGER PRIMARY KEY AUTOINCREMENT, + medication_code TEXT UNIQUE NOT NULL, + commercial_name TEXT NOT NULL, + generic_name TEXT, + form TEXT, + dosage TEXT, + manufacturer TEXT, + unit_price REAL NOT NULL, + available_stock INTEGER DEFAULT 0, + minimum_stock INTEGER DEFAULT 10, + expiration_date TEXT, + prescription_required INTEGER DEFAULT 1, + reimbursable INTEGER DEFAULT 0 +); + +CREATE TABLE prescriptions ( + prescription_id INTEGER PRIMARY KEY AUTOINCREMENT, + consultation_id INTEGER NOT NULL, + prescription_date TEXT DEFAULT CURRENT_TIMESTAMP, + treatment_duration INTEGER, + general_instructions TEXT, + FOREIGN KEY (consultation_id) REFERENCES consultations(consultation_id) ON DELETE CASCADE +); + +CREATE TABLE prescription_details ( + detail_id INTEGER PRIMARY KEY AUTOINCREMENT, + prescription_id INTEGER NOT NULL, + medication_id INTEGER NOT NULL, + quantity INTEGER NOT NULL CHECK (quantity > 0), + dosage_instructions TEXT NOT NULL, + duration INTEGER NOT NULL, + total_price REAL, + FOREIGN KEY (prescription_id) REFERENCES prescriptions(prescription_id) ON DELETE CASCADE, + FOREIGN KEY (medication_id) REFERENCES medications(medication_id) ON DELETE RESTRICT +); + +-- Indexes +CREATE INDEX idx_patient_name ON patients(last_name, first_name); +CREATE INDEX idx_consult_date ON consultations(consultation_date); +CREATE INDEX idx_consult_patient ON consultations(patient_id); +CREATE INDEX idx_consult_doctor ON consultations(doctor_id); +CREATE INDEX idx_medication_name ON medications(commercial_name); +CREATE INDEX idx_prescription_consult ON prescriptions(consultation_id); + +-- Test Data +INSERT INTO specialties (specialty_name, description, consultation_fee) VALUES +('General Medicine', 'routine checkups', 1500), ('pediatrics', 'kids doctor', 1200), +('Cardiology', 'heart stuff', 3000), ('Dermatology', 'skin and hair', 2000), +('orthopedics', 'bones', 2500), ('GYNECOLOGY', 'womens health', 2500); + +INSERT INTO doctors (last_name, first_name, email, specialty_id, license_number, office) VALUES +('bensalem', 'Noureddine', 'nourredine.b@hosp.dz', 1, 'lic-001', 'Room A1'), ('Saidi', 'amina', 'a.saidi@hospital.dz', 2, 'lic-002', 'B202'), +('RAHMANI', 'Karim', 'k.rahmani@hosp.dz', 3, 'lic-003', 'off-303'), ('Belkacem', 'Lina', 'linabelk@gmail.com', 4, 'lic-004', 'D404'), +('zitouni', 'sofiane', 's.zitouni@hosp.dz', 5, 'lic-005', 'E-505'), ('moussa', 'NADIA', 'nadiam@hosp.dz', 6, 'lic-006', 'F606'); + +INSERT INTO patients (file_number, last_name, first_name, date_of_birth, gender, phone, city, allergies) VALUES +('p_001', 'Meziane', 'yacine', '1988-05-12', 'M', '06612233', 'Algeirs', 'none'), +('p_002', 'kaci', 'Rania', '2012-11-30', 'F', '06614455', 'oran', 'Penicillin'), +('p_003', 'SAAD', 'Karim', '1975-02-20', 'M', '06616677', 'Setif', 'None'), +('p_004', 'Yahia', 'Lina', '1999-07-07', 'F', '06618899', 'Blida', 'sulfa meds'), +('p_005', 'Benali', 'Omar', '2016-01-15', 'M', '05501122', 'bejaia', 'None'), +('p_006', 'cherif', 'Meriem', '1995-04-22', 'F', '05503344', 'Algeirs', 'N/A'), +('p_007', 'Zouaoui', 'samir', '2010-12-01', 'M', '05505566', 'Annaba', 'none'), +('p_008', 'Djerbi', 'sofia', '1985-08-08', 'F', '05507788', 'Oran', 'aspirine'); + +INSERT INTO medications (medication_code, commercial_name, unit_price, available_stock, minimum_stock) VALUES +('m-101', 'paracetamol 500', 450, 100, 20), ('m-102', 'Amoxicilline', 1200, 50, 10), +('m-103', 'ibuprofene', 300, 200, 30), ('m-104', 'Omeprazole', 2000, 5, 10), +('m-105', 'Aspirine', 250, 10, 20), ('m-106', 'salbutamol spray', 5000, 15, 5), +('m-107', 'Metformine', 800, 80, 20), ('m-108', 'Lisinopril', 1100, 25, 10), +('m-109', 'Cetirizine', 600, 40, 10), ('m-110', 'ventoline', 1500, 12, 5); + +INSERT INTO consultations (patient_id, doctor_id, consultation_date, reason, status, amount, paid) VALUES +(1, 1, '2025-01-10', 'Monthly checkup', 'Completed', 1500, 1), (2, 2, '2025-01-15', 'flu and fever', 'Completed', 1200, 1), +(3, 3, '2025-01-20', 'chest pain', 'Completed', 3000, 1), (4, 4, '2025-01-25', 'skin rash', 'Completed', 2000, 1), +(5, 2, '2025-02-01', 'checkup', 'Scheduled', 1200, 0), (6, 6, '2025-02-05', 'consultation', 'Completed', 2500, 1), +(7, 5, '2025-02-10', 'broken arm', 'Completed', 2500, 1), (8, 1, '2025-02-12', 'stomach ache', 'Completed', 1500, 1); + +INSERT INTO prescriptions (consultation_id, treatment_duration) VALUES +(1, 7), (2, 5), (3, 30), (4, 10), (6, 7), (7, 15), (8, 5); + +INSERT INTO prescription_details (prescription_id, medication_id, quantity, dosage_instructions, duration) VALUES +(1, 1, 10, '1 pill twice a day', 5), (1, 9, 5, 'at night', 5), (2, 2, 14, '2 caps morning/night', 7), +(3, 8, 30, 'once daily', 30), (3, 7, 60, 'morning and evening', 30), (4, 3, 20, 'when needed', 10), +(5, 1, 10, 'twice a day', 5), (6, 5, 15, 'daily', 15), (7, 10, 1, '2 inhalations', 5), +(7, 6, 1, '1 puff', 5), (1, 3, 10, 'twice a day', 5), (2, 1, 10, 'twice a day', 5); + +-- Solutions Q1-Q30 +-- Q1 +SELECT file_number, last_name || ' ' || first_name AS full_name, date_of_birth, phone, city FROM patients; +-- Q2 +SELECT d.last_name || ' ' || d.first_name AS doctor_name, s.specialty_name, d.office, d.active FROM doctors d JOIN specialties s ON d.specialty_id = s.specialty_id; +-- Q3 +SELECT medication_code, commercial_name, unit_price, available_stock FROM medications WHERE unit_price < 500; +-- Q4 +SELECT consultation_date, p.last_name || ' ' || p.first_name AS patient_name, d.last_name || ' ' || d.first_name AS doctor_name, status FROM consultations c JOIN patients p ON c.patient_id = p.patient_id JOIN doctors d ON c.doctor_id = d.doctor_id WHERE consultation_date LIKE '2025-01%'; +-- Q5 +SELECT commercial_name, available_stock, minimum_stock, (minimum_stock - available_stock) AS difference FROM medications WHERE available_stock < minimum_stock; +-- Q6 +SELECT consultation_date, p.last_name || ' ' || p.first_name AS patient_name, d.last_name || ' ' || d.first_name AS doctor_name, diagnosis, amount FROM consultations c JOIN patients p ON c.patient_id = p.patient_id JOIN doctors d ON c.doctor_id = d.doctor_id; +-- Q7 +SELECT pr.prescription_date, p.last_name || ' ' || p.first_name AS patient_name, m.commercial_name, pd.quantity, pd.dosage_instructions FROM prescription_details pd JOIN prescriptions pr ON pd.prescription_id = pr.prescription_id JOIN medications m ON pd.medication_id = m.medication_id JOIN consultations c ON pr.consultation_id = c.consultation_id JOIN patients p ON c.patient_id = p.patient_id; +-- Q8 +SELECT p.last_name || ' ' || p.first_name AS patient_name, MAX(c.consultation_date) AS last_consultation_date, d.last_name || ' ' || d.first_name AS doctor_name FROM patients p JOIN consultations c ON p.patient_id = c.patient_id JOIN doctors d ON c.doctor_id = d.doctor_id GROUP BY p.patient_id; +-- Q9 +SELECT d.last_name || ' ' || d.first_name AS doctor_name, COUNT(c.consultation_id) AS consult_count FROM doctors d LEFT JOIN consultations c ON d.doctor_id = c.doctor_id GROUP BY d.doctor_id; +-- Q10 +SELECT s.specialty_name, SUM(c.amount) AS total_revenue FROM specialties s JOIN doctors d ON s.specialty_id = d.specialty_id JOIN consultations c ON d.doctor_id = c.doctor_id WHERE c.paid = 1 GROUP BY s.specialty_name; +-- Q11 +SELECT AVG(amount) AS average_consultation_cost FROM consultations; +-- Q12 +SELECT city, COUNT(*) AS patient_count FROM patients GROUP BY city; +-- Q13 +SELECT m.commercial_name, COUNT(pd.detail_id) AS times_prescribed FROM medications m JOIN prescription_details pd ON m.medication_id = pd.medication_id GROUP BY m.medication_id; +-- Q14 +SELECT COUNT(*) AS unpaid_consultations_count FROM consultations WHERE paid = 0; +-- Q15 +SELECT p.last_name || ' ' || p.first_name AS patient_name, SUM(pd.quantity * m.unit_price) AS total_medication_cost FROM patients p JOIN consultations c ON p.patient_id = c.patient_id JOIN prescriptions pr ON c.consultation_id = pr.consultation_id JOIN prescription_details pd ON pr.prescription_id = pd.prescription_id JOIN medications m ON pd.medication_id = m.medication_id GROUP BY p.patient_id; +-- Q16 +SELECT d.last_name || ' ' || d.first_name AS doctor_name, s.specialty_name FROM doctors d JOIN specialties s ON d.specialty_id = s.specialty_id WHERE d.active = 1; +-- Q17 +SELECT * FROM consultations WHERE diagnosis IS NULL OR diagnosis = ''; +-- Q18 +SELECT p.last_name || ' ' || p.first_name AS patient_name, c.consultation_date, c.amount FROM patients p JOIN consultations c ON p.patient_id = c.patient_id WHERE c.amount > (SELECT AVG(amount) FROM consultations); +-- Q19 +SELECT s.specialty_name, COUNT(d.doctor_id) AS doctor_count FROM specialties s LEFT JOIN doctors d ON s.specialty_id = d.specialty_id GROUP BY s.specialty_id; +-- Q20 +SELECT commercial_name, expiration_date, (julianday(expiration_date) - julianday('now')) AS days_until_expiration FROM medications WHERE days_until_expiration < 180; +-- Q21 +SELECT p.last_name || ' ' || p.first_name AS patient_name, COUNT(c.consultation_id) AS consultation_count FROM patients p JOIN consultations c ON p.patient_id = c.patient_id GROUP BY p.patient_id HAVING consultation_count > (SELECT AVG(cnt) FROM (SELECT COUNT(*) as cnt FROM consultations GROUP BY patient_id)); +-- Q22 +SELECT commercial_name, unit_price FROM medications WHERE unit_price > (SELECT AVG(unit_price) FROM medications); +-- Q23 +SELECT d.last_name || ' ' || d.first_name AS doctor_name, s.specialty_name FROM doctors d JOIN specialties s ON d.specialty_id = s.specialty_id WHERE s.specialty_id = (SELECT specialty_id FROM doctors JOIN consultations ON doctors.doctor_id = consultations.doctor_id GROUP BY specialty_id ORDER BY COUNT(*) DESC LIMIT 1); +-- Q24 +SELECT consultation_date, p.last_name || ' ' || p.first_name AS patient_name, amount FROM consultations c JOIN patients p ON c.patient_id = p.patient_id WHERE amount > (SELECT AVG(amount) FROM consultations); +-- Q25 +SELECT p.last_name || ' ' || p.first_name AS patient_name, p.allergies FROM patients p WHERE p.allergies IS NOT NULL AND p.patient_id IN (SELECT patient_id FROM consultations JOIN prescriptions ON consultations.consultation_id = prescriptions.consultation_id); +-- Q26 +SELECT d.last_name || ' ' || d.first_name AS doctor_name, COUNT(c.consultation_id) AS total_consultations, SUM(c.amount) AS total_revenue FROM doctors d JOIN consultations c ON d.doctor_id = c.doctor_id WHERE c.paid = 1 GROUP BY d.doctor_id; +-- Q27 +SELECT RANK() OVER(ORDER BY SUM(c.amount) DESC) as rank, s.specialty_name, SUM(c.amount) AS total_revenue FROM specialties s JOIN doctors d ON s.specialty_id = d.specialty_id JOIN consultations c ON d.doctor_id = c.doctor_id WHERE c.paid = 1 GROUP BY s.specialty_id LIMIT 3; +-- Q28 +SELECT commercial_name, available_stock, minimum_stock, (minimum_stock - available_stock) AS quantity_needed FROM medications WHERE available_stock < minimum_stock; +-- Q29 +SELECT AVG(med_count) AS avg_meds_per_prescription FROM (SELECT COUNT(medication_id) as med_count FROM prescription_details GROUP BY prescription_id); +-- Q30 +SELECT CASE WHEN (strftime('%Y', 'now') - strftime('%Y', date_of_birth)) <= 18 THEN '0-18' WHEN (strftime('%Y', 'now') - strftime('%Y', date_of_birth)) <= 40 THEN '19-40' WHEN (strftime('%Y', 'now') - strftime('%Y', date_of_birth)) <= 60 THEN '41-60' ELSE '60+' END AS age_group, COUNT(*) AS patient_count FROM patients GROUP BY age_group; \ No newline at end of file diff --git a/submissions/AI/README.md b/submissions/AI/README.md deleted file mode 100644 index f2bd62b..0000000 --- a/submissions/AI/README.md +++ /dev/null @@ -1,48 +0,0 @@ - -# πŸ“ Submissions - AI Specialty (Artificial Intelligence) - -## πŸ‘₯ students_AI - -### Expected structure - -Each student must create their own folder following this format: - -AI/ -β”œβ”€β”€ lastname_firstname/ -β”‚ β”œβ”€β”€ tp1_solutions.sql -β”‚ └── tp2_solutions.sql - -### Example - -AI/ -β”œβ”€β”€ benali_ahmed/ -β”‚ β”œβ”€β”€ tp1_solutions.sql -β”‚ └── tp2_solutions.sql -β”œβ”€β”€ kaddour_fatima/ -β”‚ β”œβ”€β”€ tp1_solutions.sql -β”‚ └── tp2_solutions.sql - -## How to submit? - -1. **Fork** this repository -2. Create your folder: submissions/AI/your_lastname_firstname/ -3. Add your solution files -4. Create a **Pull Request** with title: [AI] TP1 - LASTNAME Firstname - -## ⚠️ Important rules - -- Folder name: **lowercase**, **no spaces**, **no accents** -- Good: benali_ahmed -- Bad: Benali Ahmed, benali-ahmed -- Files: tp1_solutions.sql and tp2_solutions.sql -- Test your code before submission -- No plagiarism (automatic detection) - -## πŸ“Š Progress - -- **TP1 submitted:** 0/35 -- **TP2 submitted:** 0/35 - -*Last update: February 02, 2026* - -**Good luck! ** diff --git a/submissions/SS/README.md b/submissions/SS/README.md deleted file mode 100644 index 3054cff..0000000 --- a/submissions/SS/README.md +++ /dev/null @@ -1,111 +0,0 @@ -# πŸ“ Submissions - SS Specialty (Cyber Security) - -## students - -### Expected structure - -Each student must create a folder with their name containing 2 SQL files. - -**Folder name format:** `lastname_firstname/` -**Required files:** -- `tp1_solutions.sql` -- `tp2_solutions.sql` - -### Example - -**Student Riad MEZIANE creates:** -- Folder: `meziane_riad/` -- File 1: `meziane_riad/tp1_solutions.sql` -- File 2: `meziane_riad/tp2_solutions.sql` - -**Student Nadia TALEB creates:** -- Folder: `taleb_nadia/` -- File 1: `taleb_nadia/tp1_solutions.sql` -- File 2: `taleb_nadia/tp2_solutions.sql` - ---- - -## βœ… How to submit your work? - -### Step 1: Fork the repository -1. Click the **"Fork"** button at the top right of this page -2. GitHub will create a copy in your account - -### Step 2: Create your folder -1. In YOUR fork, go to `submissions/SS/` -2. Click **"Add file"** β†’ **"Create new file"** -3. Type: `your_lastname_firstname/tp1_solutions.sql` - - Example: `meziane_riad/tp1_solutions.sql` -4. Paste your SQL solutions -5. Click **"Commit new file"** -6. Repeat for `tp2_solutions.sql` - -### Step 3: Submit (Pull Request) -1. Go to your fork's main page -2. Click **"Contribute"** β†’ **"Open pull request"** -3. Title: `[SS] TP1 - MEZIANE Riad` -4. Click **"Create pull request"** - - **Done!** Your professor will receive your submission. - ---- - -## ⚠️ Important Rules - -### Folder naming -- Format: `lastname_firstname` -- All **lowercase** -- Use **underscore** `_` between lastname and firstname -- NO spaces -- NO accents or special characters -- NO hyphens `-` - -**Good examples:** -- `meziane_riad` -- `taleb_nadia` -- `cherif_kamel` - -**Bad examples:** -- `Meziane Riad` (has space and capital letters) -- `meziane-riad` (uses hyphen instead of underscore) -- `MezianeRiad` (no underscore, capital letters) -- `mΓ©ziane_riad` (has accents) - -### File naming -- `tp1_solutions.sql` (exactly this name) -- `tp2_solutions.sql` (exactly this name) - -### Code quality -- Test all queries before submitting -- Add comments to explain your logic -- Use proper SQL formatting -- **NO plagiarism** (automatic detection) - ---- - -## πŸ“Š Progress Tracking - -| Status | TP1 | TP2 | -|--------|-----|-----| -| Submitted | 0/35 | 0/35 | -| Under Review | 0/35 | 0/35 | -| Approved | 0/35 | 0/35 | - -*Last update: February 02, 2026* - ---- - -## πŸ“… Deadlines - -| Assignment | Due Date | Status | -|------------|----------|--------| -| TP1 | February 17, 2026 | Open | -| TP2 | March 17, 2026 | Open | - -⚠️ **No late submissions accepted** - ---- - ---- - -**Good luck! πŸš€**