The document outlines the creation of a MySQL database named 'assignment_7' with four tables: Teacher, Student, Course, and Result, along with their respective fields and relationships. It includes SQL commands for inserting data into these tables and various queries to retrieve specific information such as students without results, teachers with multiple courses, and students with high marks. The document demonstrates the use of JOINs, GROUP BY, and HAVING clauses to extract meaningful insights from the data.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views4 pages
Assignment 7
The document outlines the creation of a MySQL database named 'assignment_7' with four tables: Teacher, Student, Course, and Result, along with their respective fields and relationships. It includes SQL commands for inserting data into these tables and various queries to retrieve specific information such as students without results, teachers with multiple courses, and students with high marks. The document demonstrates the use of JOINs, GROUP BY, and HAVING clauses to extract meaningful insights from the data.
-> FROM Student s -> LEFT JOIN Result r ON s.s_id = r.s_id -> WHERE r.s_id IS NULL; +----------+ | name | +----------+ | Student4 | +----------+ 1 row in set (0.00 sec)
mysql> SELECT t.teacher_name
-> FROM Teacher t -> JOIN Course c ON t.teacher_id = c.teacher_id -> GROUP BY t.teacher_name -> HAVING COUNT(c.c_id) > 1; +--------------+ | teacher_name | +--------------+ | Teacher1 | +--------------+ 1 row in set (0.00 sec)
mysql> SELECT s.name, r.mark
-> FROM Student s -> JOIN Result r ON s.s_id = r.s_id -> WHERE s.dob < '1989-01-01' AND r.mark > 80; +----------+------+ | name | mark | +----------+------+ | Student1 | 85 | +----------+------+ 1 row in set (0.00 sec)
mysql> SELECT s.name
-> FROM Student s -> JOIN Result r ON s.s_id = r.s_id -> WHERE r.mark >= 50 -> GROUP BY s.s_id, s.name -> HAVING COUNT(r.c_id) > 3; +----------+ | name | +----------+ | Student5 | +----------+ 1 row in set (0.00 sec)
mysql> SELECT s.s_id, SUM(c.credit) AS total_credits
-> FROM Student s -> JOIN Result r ON s.s_id = r.s_id -> JOIN Course c ON r.c_id = c.c_id -> WHERE s.s_id = 1 -> GROUP BY s.s_id; +------+---------------+ | s_id | total_credits | +------+---------------+ | 1 | 7 | +------+---------------+ 1 row in set (0.00 sec)
mysql> SELECT DISTINCT s.name, r.mark
-> FROM Student s -> JOIN Result r ON s.s_id = r.s_id -> WHERE r.mark > 80; +----------+------+ | name | mark | +----------+------+ | Student1 | 85 | | Student2 | 92 | | Student2 | 88 | | Student5 | 98 | +----------+------+ 4 rows in set (0.00 sec)
mysql> SELECT s.s_id, s.name
-> FROM Student s -> JOIN Result r ON s.s_id = r.s_id -> WHERE r.mark < 30 -> GROUP BY s.s_id, s.name -> HAVING COUNT(CASE WHEN r.mark < 30 THEN 1 ELSE NULL END) > 3; +------+----------+ | s_id | name | +------+----------+ | 6 | Student6 | +------+----------+ 1 rows in set (0.00 sec)
mysql> SELECT s.name
-> FROM Student s -> JOIN ( -> SELECT s_id, SUM(mark) AS total_marks -> FROM Result -> GROUP BY s_id -> HAVING SUM(mark) = (SELECT MAX(total_marks) -> FROM (SELECT s_id, SUM(mark) AS total_marks -> FROM Result GROUP BY s_id) AS temp) -> ) max_marks ON s.s_id = max_marks.s_id; +----------+ | name | +----------+ | Student5 | +----------+ 1 row in set (0.00 sec)