December 14, 2015

Mafia Games Monday (MGM) – Writing SQL 101

Writing queries is something that we all as game owners and/or programmers have to do. It’s just a fact of life – almost all applications have at least one place where data is stored and has to be written and/or retrieved through that application.

For many, it is a database and the language of choice is SQL.

SQL stands for Structured Query Language. There are several types of SQL, the most popular of which is MySQL. The majority of them are quite the same, so these tools will work for them as well.

Take this example below:

A student (ID 54356) wants to check what courses they are currently taking (SEMESTER_VAL 22015). The following are the relational tables below:

STUDENT(ID, LAST_NAME, FIRST_NAME, MIDDLE_INIT, SSN, DOB)
COURSE(ID, NAME, DESCR)
STUDENT_COURSE(SID, CID, SEMESTER_VAL)

Now would be a good time to get a good handle on what the syntax of a relational table means if you don’t know… 🙂

Anyways, so to do this, you would want to show the student those courses.

There are some SQL keywords that you should know:

SELECT: This keyword is for those data fields that you will want to output in the results of your query.
* : The asterisk after the select keyword essentially means that you want to display all of the data fields in the table(s) that you mention after the FROM keyword.
FROM: This keyword is for those tables that you will need to get those data fields from that you want to display.
WHERE: This keyword is for all of the conditions that you will need in order to display the information that you are looking for.

So to make a query, the syntax will look something like this:

SELECT data
FROM table
WHERE 1=1;

So take our example that I mentioned earlier. To return the courses that they are currently taking, you can do the following query:

SELECT CID
FROM STUDENT_COURSE
WHERE SID = 54356
AND SEMESTER_VAL = 22015;

And there you go! You now returned the course IDs for the courses that the student is currently taking. However, displaying course IDs is not only ugly, but it does not provide the student with information that they would understand. If you asked a question like that and you were given ID numbers, you would be pretty frustrated, right? What do those numbers mean??

So, to make it more user-friendly to the student, we will have to add in the COURSE table. To do that, we will utilize the JOIN keyword.

JOIN: This keyword within the FROM clause will allow you to join two tables together.

Now this keyword is not required, but it will show you what MySQL is doing for you.

The syntax for the join will look something like this:

SELECT data
FROM table1 JOIN table2 ON value1 = value2
WHERE 1=1;

So, applying this to our example, we can now do this:

SELECT NAME, DESCR
FROM STUDENT_COURSE JOIN COURSE ON CID = ID
WHERE SID = 54356
AND SEMESTER_VAL = 22015;

And now, you will be displaying to the student the course name and description instead of the ID. This will be much easier for them to read!

If you don’t want to use the JOIN keyword, you can also do the following to get the same result:

SELECT NAME, DESCR
FROM STUDENT_COURSE, COURSE
WHERE CID = ID
AND SID = 54356
AND SEMESTER_VAL = 22015;

As with any programming language, there are multiple ways to write them and get the same results. 😉

So for those of you with no SQL experience, you now have a little bit to start with. There will be more of these SQL tutorials to come!

That’s all for now, until next time…keep that tech gear ticking.

~Goliath7470