0% 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.

Uploaded by

Mrinmoy Pathak
Copyright
© © All Rights Reserved
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% 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.

Uploaded by

Mrinmoy Pathak
Copyright
© © All Rights Reserved
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
You are on page 1/ 4

mysql> use assignment_7;

Database changed

mysql> CREATE TABLE Teacher (


-> teacher_id INT PRIMARY KEY,
-> teacher_name VARCHAR(50),
-> address VARCHAR(100)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE Student (


-> s_id INT PRIMARY KEY,
-> name VARCHAR(50),
-> phone VARCHAR(15),
-> dob DATE
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE Course (


-> c_id INT PRIMARY KEY,
-> cname VARCHAR(50),
-> credit INT,
-> teacher_id INT,
-> FOREIGN KEY (teacher_id) REFERENCES Teacher(teacher_id)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE Result (


-> s_id INT,
-> c_id INT,
-> mark INT,
-> PRIMARY KEY (s_id, c_id),
-> FOREIGN KEY (s_id) REFERENCES Student(s_id),
-> FOREIGN KEY (c_id) REFERENCES Course(c_id)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO Teacher (teacher_id, teacher_name, address) VALUES


-> (1, 'Teacher1', 'Address1'),
-> (2, 'Teacher2', 'Address2'),
-> (3, 'Teacher3', 'Address3'),
-> (4, 'Teacher4', 'Address4');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Student (s_id, name, phone, dob) VALUES


-> (1, 'Student1', '123-456-7890', '1985-05-10'),
-> (2, 'Student2', '987-654-3210', '1990-03-15'),
-> (3, 'Student3', '555-555-5555', '1988-12-25'),
-> (4, 'Student4', '111-111-1111', '1995-06-20'),
-> (5, 'Student5', '178-895-7890', '1992-03-03'),
-> (6, 'Student6', '199-455-7400', '1993-08-02'),
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Course (c_id, cname, credit, teacher_id) VALUES


-> (101, 'Course1', 3, 1),
-> (102, 'Course2', 4, 2),
-> (103, 'Course3', 2, 1),
-> (104, 'Course4', 3, 1);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Result (s_id, c_id, mark) VALUES


-> (1, 101, 85),
-> (1, 102, 78),
-> (2, 102, 92),
-> (2, 103, 88),
-> (3, 101, 75),
-> (3, 103, 60),
-> (5, 101, 60),
-> (5, 102, 98),
-> (5, 103, 78),
-> (5, 104, 77),
-> (6, 101, 22),
-> (6, 102, 23),
-> (6, 103, 24),
-> (6, 104, 25);

Query OK, 14 rows affected (0.00 sec)


Records: 14 Duplicates: 0 Warnings: 0

mysql> SELECT DISTINCT s.name


-> 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)

You might also like