I am now at Lesson 3. Yey! Glad that I was able to reach here despite being skeptical at first if I can sustain learning a new programming language considering I have Coursehero tutoring on the side.
I decided to write this blog since upon engaging with the lesson on "JOIN" clause, I can't seem to grasp the lesson. It talked about "cross join" and "inner join" and how inner join is more useful than the other. According to w3schools, the following is the difference between the two.
I am given the following tables:
QUERY RESULTS
id | student_id | test | grade |
---|---|---|---|
1 | 1 | Nutrition | 95 |
2 | 2 | Nutrition | 92 |
3 | 1 | Chemistry | 85 |
4 | 2 | Chemistry | 95 |
id | first_name | last_name | phone | birthdate | |
---|---|---|---|---|---|
1 | Peter | Rabbit | peter@rabbit.com | 555-6666 | 2002-06-24 |
2 | Alice | Wonderland | alice@wonderland.com | 555-4444 | 2002-07-04 |
If I do a cross-join, this will be the result.
Code:
SELECT * FROM student_grades, students;
Query results:
id | student_id | test | grade | id | first_name | last_name | phone | birthdate | |
---|---|---|---|---|---|---|---|---|---|
1 | 1 | Nutrition | 95 | 1 | Peter | Rabbit | peter@rabbit.com | 555-6666 | 2002-06-24 |
1 | 1 | Nutrition | 95 | 2 | Alice | Wonderland | alice@wonderland.com | 555-4444 | 2002-07-04 |
2 | 2 | Nutrition | 92 | 1 | Peter | Rabbit | peter@rabbit.com | 555-6666 | 2002-06-24 |
2 | 2 | Nutrition | 92 | 2 | Alice | Wonderland | alice@wonderland.com | 555-4444 | 2002-07-04 |
3 | 1 | Chemistry | 85 | 1 | Peter | Rabbit | peter@rabbit.com | 555-6666 | 2002-06-24 |
3 | 1 | Chemistry | 85 | 2 | Alice | Wonderland | alice@wonderland.com | 555-4444 | 2002-07-04 |
4 | 2 | Chemistry | 95 | 1 | Peter | Rabbit | peter@rabbit.com | 555-6666 | 2002-06-24 |
4 | 2 | Chemistry | 95 | 2 | Alice | Wonderland | alice@wonderland.com | 555-4444 | 2002-07-04 |
Basically, what happened was every row in the student_grades table was joined with the contents of the students table. Since there are 4 rows in the student_grades table and 2 rows in the students table, when we multiply 4 by 2, we have 8 which is equal to the number of rows when we cross-joined the two tables. If we observe, there are incorrect rows in this table. For example, on the second row, Alice did not score 95 on the test on Nutrition rather she scored 92. Therefore, using cross-join did not preserve the relationship of the data based on common columns. This is not our purpose of joining the two tables. What we need to do is to output a table where the name of the student, email address, test, and grade are displayed. Therefore, we need to use "inner join".
Code:
SELECT * FROM student_grades, students
WHERE student_grades.student_id = students.id;
Query results:
id | student_id | test | grade | id | first_name | last_name | phone | birthdate | |
---|---|---|---|---|---|---|---|---|---|
1 | 1 | Nutrition | 95 | 1 | Peter | Rabbit | peter@rabbit.com | 555-6666 | 2002-06-24 |
2 | 2 | Nutrition | 92 | 2 | Alice | Wonderland | alice@wonderland.com | 555-4444 | 2002-07-04 |
3 | 1 | Chemistry | 85 | 1 | Peter | Rabbit | peter@rabbit.com | 555-6666 | 2002-06-24 |
4 | 2 | Chemistry | 95 | 2 | Alice | Wonderland | alice@wonderland.com | 555-4444 | 2002-07-04 |
Notice that we have only 4 rows in the output. Now, this is fit for our purpose. We have now mapped out the test, grade, names, and email addresses correctly. But notice that there are still redundant columns such as student_id and id, which refer to the same quantity. To improve this, we have:
Code:
SELECT first_name,last_name,email,test,grade FROM student_grades, students
WHERE student_grades.student_id = students.id;
Query results:
first_name | last_name | test | grade | |
---|---|---|---|---|
Peter | Rabbit | peter@rabbit.com | Nutrition | 95 |
Alice | Wonderland | alice@wonderland.com | Nutrition | 92 |
Peter | Rabbit | peter@rabbit.com | Chemistry | 85 |
Alice | Wonderland | alice@wonderland.com | Chemistry | 95 |
Now, this is a lot better than the previous output tables. A much better way to do this is to use the "JOIN" clause.
Code:
SELECT first_name, last_name,email,test,grade FROM students
JOIN student_grades
ON student_grades.student_id = students.id
Query results:
first_name | last_name | test | grade | |
---|---|---|---|---|
Peter | Rabbit | peter@rabbit.com | Nutrition | 95 |
Alice | Wonderland | alice@wonderland.com | Nutrition | 92 |
Peter | Rabbit | peter@rabbit.com | Chemistry | 85 |
Alice | Wonderland | alice@wonderland.com | Chemistry | 95 |
Note: In instances where columns from the different tables have the same names but different meaning, we must specify the table from which we took the data. For example, it will be much better to write:
SELECT students.first_name, students.last_name,students.email,student_grades.test,student_grades.grade FROM students
JOIN student_grades
ON student_grades.student_id = students.id;
###
Comments
Post a Comment