I have three tables named exam, student and subject.
You can learn more about the data by checking this link: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b80b08d0517678b8da3b70d489462d4f
I want to see the columns test, sub_code, marks from exam table and name column from subject table. I will use WHERE clause to get data of a particular roll number.
I came up with this query:
SELECT exam.test "Test", subject.name "Subject", exam.sub_code "Subject Code", exam.marks "Marks"
FROM exam
INNER JOIN subject ON exam.sub_code = subject.sub_code
WHERE roll_no=1
ORDER BY exam.test,exam.sub_code;
but I am not sure if this is the correct way to join the tables. I just followed help from people in one of the Stack Overflow chatrooms and online articles; so I am not sure if this is the correct JOIN to use.
I tried using this https://joins.spathon.com/ but I could not understand which JOIN is correct because of the huge number of records in exam table (and I am a noob in JOIN statements).
Should I use LEFT JOIN or JOIN or some other method?
Also, is it possible to format SQL output like this:
+------+-----------------------+--------------+-------+
| Test | Subject | Subject Code | Marks |
+------+-----------------------+--------------+-------+
| MT1 | Maths | 041 | 29 |
| | Physics | 042 | 36 |
| | Chemistry | 043 | 42 |
| | Informatics Practices | 065 | 46 |
| | English | 301 | 38 |
+------+-----------------------+--------------+-------+
| MT2 | Maths | 041 | 37 |
| | Physics | 042 | 43 |
| | Chemistry | 043 | 45 |
| | Informatics Practices | 065 | 48 |
| | English | 301 | 40 |
+------+-----------------------+--------------+-------+
| MT3 | Maths | 041 | 42 |
| | Physics | 042 | 40 |
| | Chemistry | 043 | 37 |
| | Informatics Practices | 065 | 45 |
| | English | 301 | 45 |
+------+-----------------------+--------------+-------+
OUTER
joins - I was lazy in my use of terminology and apologise - the only case in whichOUTER
joins would not be applicable is if every student had an exam result in every exam for every subject - that's not the case. I was too hasty in my response - I've put in an explanation of howOUTER
joins could be used with your data at the beginning of my answer!