You are on page 1of 15

Hi Guys, in this Exercise we will create a database to store information regarding students, the

subjects they study, the teachers who teach those subjects and also the marks students scored on
each of those subjects.
So the first obvious thing we will do is to create a database named school
Inside our Database we will create four tables:
a) Students: for storing students information like student id, students name, gender, contact
number of parent, etc.
b) Teachers: for storing teacher information like teacher id, teacher name, gender, contact
number of teacher, etc.
c) Subjects: for storing subject name and id of teacher teaching that subject
4) Scores: for storing details like student id (to identify student based on student id of students
table), subject id (to identify subject based on subject id of subjects table) and score (i.e. marks
scored by student on that subject)
IMPORTANT:
So if you take a minute out and think about how the tables are related to each other then we have
students table and teachers table which are independent, then there is subjects table which uses
teacher_id so it is dependent on teachers table and scores table which are connected to two tables
(students table through student_id and subjects table through subject_id)
It is important to remember the relation between tables as they are very useful when we have to
think about joins.
So now that we have the entire picture in mind, lets get started with creating the Database and
tables for it:
1) Create New Database with Name school

CREATE DATABASE school;

2) Create a New Table called 'students' inside Database School with following fields /
columns:
a) student_id (type int(11) , Should be Primary Key + Should Auto Increment on its
own )
b) student_name (type varchar (200) and it cannot be null)
c) student_group (which can be blue, green, yellow or red. Type enum and again it
cannot be null)
d) student_gender (type enum('M','F') Where M stands for Male and F for female
and cannot be NULL )
e) parent_contact_num ( type bigint(10) and cannot be null )

CREATE TABLE students(


student_id INT(10) PRIMARY KEY AUTO_INCREMENT,
student_name VARCHAR(200) NOT NULL,
student_group ENUM('blue','green','yellow','red') NOT NULL,
student_gender ENUM('M','F') NOT NULL,
parent_contact_num BIGINT(10) NOT NULL
);

3) Lets populate the students table by inserting few student details inside the table ( Just
copy the Entire INSERT query below and run it )

INSERT INTO students( student_name , student_group , student_gender , parent_contact_num )


VALUES ( 'Zack' , 'blue' , 'M' , 1234567890 ),
( 'Tina' , 'blue' , 'F' , 3213213210 ),
( 'Robin' , 'red' , 'M' , 9822211122 ),
( 'Ricky' , 'blue' , 'M' , 9898989797 ),
( 'Stuart' , 'green' , 'M' , 3243243240 ),
( 'Jackie' , 'yellow' , 'F' , 5648900000 ),
( 'Priyanka' , 'green' , 'F' , 1231234567 ),
( 'Jing' , 'red' , 'F' , 7008009000 ),
( 'Ronald' , 'yellow' , 'M' , 2122123344 ),
( 'Rina' , 'red' , 'F' , 2211334455 );
Students will automatically be assigned student_id's from 1 to 10
NOTE: The above way is used when you want to insert multiple rows of data simultaneously in
a table

4) Create a New Table Called 'teachers'


a) teacher_id ( type int(10) , should be primary key + should auto increment on its
own )
b) teacher_name ( type varchar(200) and it cannot be null )
c) teacher_gender ( type enum( 'M' , 'F' ) where M stands for Male and F for
Female and cannot be NULL )
d) teacher_contact ( BIGINT(10) NOT NULL )

CREATE TABLE teachers(


teacher_id INT(10) PRIMARY KEY AUTO_INCREMENT,
teacher_name VARCHAR(200) NOT NULL,
teacher_gender ENUM('M','F') NOT NULL,
teacher_contact BIGINT(10) NOT NULL
);

5) Lets Populate the teachers table by inserting few student details inside it ( Copy the
Entire INSERT Query below and run it )
INSERT INTO teachers( teacher_name , teacher_gender , teacher_contact )
VALUES ( 'Janet' , 'F' , 3343345566 ),
( 'John' , 'M' , 7657657650 ),
( 'Marcus' , 'M' , 4343434343 ),
( 'Teresa' , 'F' , 6506507788 ),
( 'Jim' , 'M' , 3243245500 );
Teachers will automatically be assigned teacher_id's from 1 to 5

6) Create a New Table Called 'subjects' inside Database School with following fields /
columns:
a) subject_id ( type int(10) , should be primary key + should auto increment on its
own )
b) teacher_id ( type int(10) )
c) subject_name ( type varchar(200) and it cannot be null )

CREATE TABLE subjects(


subject_id INT(10) PRIMARY KEY AUTO_INCREMENT,
teacher_id INT(10) NOT NULL,
subject_name VARCHAR(200) NOT NULL
);

7) Now we want teacher_id field in table subjects to be foreign key of field teacher_id in
table teachers. Do it using ALTER?

ALTER TABLE subjects


ADD FOREIGN KEY (teacher_id) REFERENCES teachers(teacher_id);
This will ensure that if we try to insert a teacher_id in table subjects which does not correspond
to teacher_id in table teachers then it will not be allowed to be INSERTED.

8) Lets Populate table Subjects? (Copy the Entire INSERT Query below and run it )
INSERT INTO subjects( teacher_id , subject_name )
VALUES( 1 , 'Science' ),
( 1 , 'Computers' ),
( 2 , 'Math' ),
( 3 , 'History' ),
( 2 , 'Economics' ),
( 4 , 'Arts' ),
( 5 , 'Sports' ),
( 3 , 'Geography' );
Subjects will automatically assign subject_id from 1 to 8

9) If you try to run the query:


INSERT INTO subjects( teacher_id , subject_name ) VALUES( 9 , 'Electronics' );
Will it work or not? And why?

It will not work because we have added a FOREIGN KEY Constraint on teacher_id
Column of table subjects which points to teacher_id column of table teachers and since
there is no teacher with teacher_id of 9, it will not be allow it to run.

10) Create another table scores with following fields:

a) score_id INT(10) PRIMARY key and auto increment


b) student_id INT(10) NOT NULL
c) subject_id INT(10) NOT NULL
d) score INT(10) NOT NULL

CREATE TABLE scores(


score_id INT(10) PRIMARY KEY AUTO_INCREMENT,
student_id INT(10) NOT NULL,
subject_id INT(10) NOT NULL,
score INT(10) NOT NULL
);

11) Create FOREIGN Key constraint such that student_id in table scores points to
student_id in students table and subject_id in table scores points to subject_id in table
subjects

ALTER TABLE scores


ADD FOREIGN KEY ( student_id) REFERENCES students( student_id);

ALTER TABLE scores


ADD FOREIGN KEY ( subject_id ) REFERENCES subjects( subject_id );

12) Lets Populate Scores Table i.e. we are inserting the marks scored by students in all
subjects ( Copy the Entire INSERT Query below and run it )

INSERT INTO scores( student_id , subject_id , score )


VALUES ( 1 , 1 , 74 ),
( 1 , 2 , 30 ),
( 1 , 3 , 97 ),
( 1 , 4 , 33 ),
( 1 , 5 , 14 ),
( 1 , 6 , 46 ),
( 1 , 7 , 16 ),
( 1 , 8 , 3 ),
( 2 , 1 , 82 ),
( 2 , 2 , 29 ),
( 2 , 3 , 86 ),
( 2 , 4 , 87 ),
( 2 , 5 , 54 ),
( 2 , 6 , 77 ),
( 2 , 7 , 63 ),
( 2 , 8 , 7 ),
( 3 , 1 , 91 ),
( 3 , 2 , 68 ),
( 3 , 3 , 25 ),
( 3 , 4 , 48 ),
( 3 , 5 , 86 ),
( 3 , 6 , 40 ),
( 3 , 7 , 71 ),
( 3 , 8 , 22 ),
( 4 , 1 , 30 ),
( 4 , 2 , 84 ),
( 4 , 3 , 56 ),
( 4 , 4 , 100 ),
( 4 , 5 , 59 ),
( 4 , 6 , 35 ),
( 4 , 7 , 59 ),
( 4 , 8 , 33 ),
( 5 , 1 , 66 ),
( 5 , 2 , 55 ),
( 5 , 3 , 66 ),
( 5 , 4 , 80 ),
( 5 , 5 , 0 ),
( 5 , 6 , 83 ),
( 5 , 7 , 84 ),
( 5 , 8 , 82 ),
( 6 , 1 , 11 ),
( 6 , 2 , 70 ),
( 6 , 3 , 69 ),
( 6 , 4 , 66 ),
( 6 , 5 , 46 ),
( 6 , 6 , 31 ),
( 6 , 7 , 73 ),
( 6 , 8 , 36 ),
( 7 , 1 , 100 ),
( 7 , 2 , 99 ),
( 7 , 3 , 85 ),
( 7 , 4 , 86 ),
( 7 , 5 , 38 ),
( 7 , 6 , 55 ),
( 7 , 7 , 8 ),
( 7 , 8 , 69 ),
( 8 , 1 , 39 ),
( 8 , 2 , 64 ),
( 8 , 3 , 68 ),
( 8 , 4 , 99 ),
( 8 , 5 , 100 ),
( 8 , 6 , 26 ),
( 8 , 7 , 31 ),
( 8 , 8 , 65 ),
( 9 , 1 , 81 ),
( 9 , 2 , 97 ),
( 9 , 3 , 44 ),
( 9 , 4 , 82 ),
( 9 , 5 , 79 ),
( 9 , 6 , 28 ),
( 9 , 7 , 63 ),
( 9 , 8 , 91 ),
( 10 , 1 , 98 ),
( 10 , 2 , 31 ),
( 10 , 3 , 56 ),
( 10 , 4 , 43 ),
( 10 , 5 , 63 ),
( 10 , 6 , 28 ),
( 10 , 7 , 80 ),
( 10 , 8 , 63 );

13) Write a query which returns students name and gender?

SELECT student_name,
student_gender
FROM students;

14) Find id, name and group of all students whose id is greater than 5 and belongs to group
red?

SELECT student_id,
student_name,
student_group
FROM students
WHERE student_id > 5 AND student_group = 'red';
15) Find all teachers whose id are either 1,3 or 5 and are all male?

SELECT *
FROM teachers
WHERE teacher_id IN ( 1 , 3 , 5 )
AND teacher_gender = 'M';

16) Find all scores where student score more than 70 in subject_id 7 OR students scored
more than 50 in subject_id 5

SELECT *
FROM scores
WHERE ( score > 70 AND subject_id = 7 ) OR
( score > 50 AND subject_id = 5 );

17) List all Score Details in decreasing ORDER of score?

SELECT *
FROM scores
ORDER BY score DESC;

18) Get name of all subject taught by teacher_id 1 or teacher_id 3 in two different ways?

Method # 1:
SELECT subject_name
FROM subjects
WHERE teacher_id = 1 OR teacher_id = 3;

Method # 2:
SELECT subject_name
FROM subjects
WHERE teacher_id IN ( 1 , 3 );

19) Find Details of all subjects that either start with S or end with S?

SELECT *
FROM subjects
WHERE subject_name LIKE 's%' OR subject_name LIKE '%s';

20) Get Details of all subjects which are taught by female Teachers?

SELECT *
FROM teachers t
LEFT JOIN subjects s ON t.teacher_id = s.teacher_id
WHERE t.teacher_gender = 'F';

21) Get List of All scores scored by student with student_id 3?

SELECT *
FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
WHERE stu.student_id = 3;

22) Find name and highest score scored by each student?

SELECT stu.name,
MAX( s.score )
FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
GROUP BY stu.student_id;

23) List Name of each subject and highest score scored for the same subject?

SELECT sub.subject_name,
MAX( s.score )
FROM subjects sub
LEFT JOIN scores s ON sub.subject_id = s.subject_id
GROUP BY sub.subject_id;

24) List name of all subjects, name of student and score for that subject scored by students
whose student_id is either 1 or 3? Do it using UNION?

SELECT sub.subject_name,
stu.student_name,
s.score
FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
LEFT JOIN subjects sub ON s.subject_id = sub.subject_id
WHERE stu.student_id = 1
UNION
SELECT sub.subject_name,
stu.student_name,
s.score
FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
LEFT JOIN subjects sub ON s.subject_id = sub.subject_id
WHERE stu.student_id = 3;

25) Get a total score scored by each student, Average scored by each student along with
their names and order them in descending order of total and we only want to see top 5?

SELECT stu.student_name,
SUM( s.score ) AS total_score ,
AVG( s.score ) AS avg_score
FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
GROUP BY stu.student_id
ORDER BY SUM( s.score ) DESC
LIMIT 5;

26) Get a list of all teachers and avg score scored by students on subject being taught by
that teacher and order them in ascending order by average score?

SELECT t.teacher_name,
AVG( s.score )
FROM teachers t
LEFT JOIN subjects sub ON t.teacher_id = sub.teacher_id
LEFT JOIN scores s ON s.subject_id = sub.subject_id
GROUP BY t.teacher_id
ORDER BY AVG( s.score );

27) Write a Query to update score of student with student_id 5 in subject_id 4 to 95?

UPDATE scores
SET score = 95
WHERE student_id = 5 AND subject_id = 4;

28) Write a Query to Find All Students who have scored an Average of More than 60 and
order them in decreasing order of their average scores?

SELECT stu.student_name,
stu.student_id,
AVG( s.score )
FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
GROUP BY stu.student_id
HAVING AVG( s.score ) > 60
ORDER BY AVG( s.score );

29) Select Name of all Students who have score More than 90 in at least two subjects? We
only want Unique names i.e. even if student has score more than 75 in say three subjects,
still his or her name should appear only once in the result.

SELECT DISTINCT stu.student_name


FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
WHERE s.score > 90
GROUP BY stu.student_id
HAVING COUNT(*) >= 2;

30) Create View using query for Question #29 and name it as bright_students? Also how
can we see the view result?

Creating View:
CREATE VIEW bright_students AS
SELECT DISTINCT stu.student_name
FROM students stu
LEFT JOIN scores s ON stu.student_id = s.student_id
WHERE s.score > 90
GROUP BY stu.student_id
HAVING COUNT(*) >= 2;

View Result:

SELECT *
FROM bright_students;

I am sure you guys were able to do most of it and I can assure you that you WILL NOW be able
to write any Real World query yourself.
Thank you.

You might also like