Today is the intermediate tutorial on MySQL SELECT statements (hence the 201). If you need to review the previous tutorial, you can check that out here.
So take for example, we have the following relational tables below:
STUDENT(ID, LAST_NAME, FIRST_NAME, MIDDLE_INIT, SSN, DOB)
COURSE(ID, NAME, DESCR)
STUDENT_COURSE(SID, CID, SEMESTER_VAL, GRADE)
What if we wanted to get the course name, course description, student name, the most recent time the student took that course, and the grade for student ID 54356 and course ID 444? This information would be useful for an office to get an understanding of whether or not a student took the course more than once, or what they got in that course at that point in time.
Doing this will utilize what’s called a sub-query or nested query. That is essentially a query that when run returns a result that is then used in the parent query to meet a condition.
The syntax looks like this:
SELECT data FROM table WHERE data1 = (SELECT data2 FROM table2 WHERE 1=1);
The SELECT statement that is within the parentheses is known as the sub-query.
Now how does this apply to the scenario above? Well, since we cannot guarantee that a student did not take a course more than once, we will have to get the last time the student took the course. Assuming that the SEMESTER_VAL value increases as the year goes further into the future and, we can write something like this:
SELECT NAME, DESCR, LAST_NAME, FIRST_NAME, MIDDLE_INIT, SEMESTER_VAL FROM STUDENT_COURSE JOIN COURSE ON CID = ID JOIN STUDENT ON SID = ID WHERE SEMESTER_VAL = ( SELECT SEMESTER_VAL FROM STUDENT_COURSE WHERE SID = SID AND CID = CID) AND SID = 54356 AND CID = 444;
Would this query work? Not as it’s currently written, no. There are a few things that we will have to do in order to make this query work.
We will have to use an alias. An alias is another way to identify the table so that the SQL engine knows how to properly run this query. Using aliases is a good practice to get into, especially when you are writing queries that use multiple tables. So, taking this scenario for example, we can then rewrite the query like this:
SELECT C.NAME, C.DESCR, S.LAST_NAME, S.FIRST_NAME, S.MIDDLE_INIT, SC.SEMESTER_VAL FROM STUDENT_COURSE SC JOIN COURSE C ON SC.CID = C.ID JOIN STUDENT S ON SC.SID = S.ID WHERE SC.SEMESTER_VAL = ( SELECT SC2.SEMESTER_VAL FROM STUDENT_COURSE SC2 WHERE SC2.SID = SC.SID AND SC2.CID = SC.CID) AND SC.SID = 54356 AND SC.CID = 444;
Now will the query work? Well, no, because if the student took the course multiple times, that sub-query will return more than one row, which will cause this query to error out. This will require an aggregate function. An aggregate function is a way to perform some mathematical calculation on the value in the select statement before the result is returned. There are several aggregate functions, but today we will be using the MAX() function.
The MAX() function will allow us to get the maximum value for the column that is in the parentheses. So for example, if we did MAX(SID), we would get the largest student ID.
For the query above, we will have to use this function as such below:
SELECT C.NAME, C.DESCR, S.LAST_NAME, S.FIRST_NAME, S.MIDDLE_INIT, SC.SEMESTER_VAL FROM STUDENT_COURSE SC JOIN COURSE C ON SC.CID = C.ID JOIN STUDENT S ON SC.SID = S.ID WHERE SC.SEMESTER_VAL = ( SELECT MAX(SC2.SEMESTER_VAL) FROM STUDENT_COURSE SC2 WHERE SC2.SID = SC.SID AND SC2.CID = SC.CID) AND SC.SID = 54356 AND SC.CID = 444;
And there you have it!
That’s all for now, until next time…keep that tech gear ticking.
~Goliath7470
