One of the best ways to test your knowledge of SQL is by taking the SQL Quiz. The SQL test will allow you to see how well you understand certain areas of SQL as well as telling you the areas that you need to improve. The test requires moderate knowledge of the database language, but the advanced parts have been omitted because these tutorials are introductory.
To begin the quiz, click SQL quiz. If you see any parts that confuse you or you disagree with the answer on one of the quiz questions, use the contact form to tell me about it. The SQL quiz is intended to be only for practice and is by no means a testing tool for job opportunities.
The SQL language used is MySQL, which might effect some of the answers on the quiz.
SQL is a language that interacts with the database on the server-side. This dynamite duo has changed the structure of the internet forever. Sure, programming languages like PHP, ASP, and ColdFusion made the internet dynamic, but the efficiency of SQL has revolutionized how user’s interact with websites. Get on Facebook, post a status saying “I love AfterHoursProgramming.com!!!” and you just found yourself indirectly using SQL. Any good website that permits users to provide content to that site should use SQL. We have client-side languages and server-side languages, but where is SQL?
Is SQL a Database?
No! I know it is somewhat complicated, but SQL just talks to a database and tells it what to do and exactly what it needs. Think of how PHP, ASP, or ColdFusion interact with files. SQL is just like that, but it was created for that very purpose unlike the other languages mentioned. SQL is much smaller and faster than them also. It has the power to index and to make itself faster. It can handle large amounts of data and you can organize the data into specific tables, columns, and rows.
This will be an introduction to SQL and will not cover everything about SQL, but we will get some solid knowledge on how to work with it and learn how to become a query master.. However, you don’t need to be crazy advanced to utilize the majority of SQL’s power. Let’s get to it.
SQL’s syntax is a lot different than other web programming languages. It is extremely readable and very specific. In the most basic form, we will only be using SQL to create queries to select or change data (data manipulation). A query is just SQL code designed to do just that. So, we will not deal with creating tables, databases, or indexes and we won’t be deleting them either. I may add a part about them later, but the majority of web hosting companies offer fantastic tools that do those tasks for you. Let’s get to your first SQL query::
One of the most simple queries you will ever write. Look at how easily that can be read. SELECT some asterisk thing FROM some table_name. First, I will be using things like table_name, where that will be a real table name like users, reviews, items, etc. The * (asterisk) simply means everything in SQL. So, in this case, the asterisk means select every column in that table.
What about the capitalized words? I don’t know of exactly what to call these things because they are so arbitrary, they are keywords really. SQL is made up from these keywords and functions, and it is a relatively tiny language compared to server side languages. SELECT tells SQL that this is a statement, but FROM tells SQL where to perform the statement.
Notice how SELECT and FROM and in uppercase. While I should tell you that is how it must be written, it isn’t. SQL isn’t case sensitive in terms of keywords and functions, but please, please, please always write the actual SQL syntax in uppercase. It helps distinguish the SQL from the column names and values. If you do this one simple trick, people will think you know what you are doing. Go ahead, pretend like you do. They won’t notice.
With an intent of data manipulation, you really only have 4 types of SQL statements you can make: SELECT, INSERT, UPDATE, or DELETE. We will first discuss the only way to get information from a database using the SQL command SELECT. SELECT is probably the most common query that you will use. You probably are more concerned with displaying data to the user as opposed to get data from them. Luckily in SQL, the SELECT statement has the easiest syntax to master. First up, we need some information to retrieve. Think of this following table as a table in your database that you just created. Now we are ready to perform master SQL ninja skills.
SQL SELECT STATEMENT
So this is our default table. Nothing really to special about it. It is just a tiny little users with a bad way to store passwords (you should look into hashing passwords if you plan to create a real users table). Now let’s get some data from our table.
We already know that SELECT simply tells SQL we want to get information from the database. Then, username is just the name of the column that we want returned. So, if we would have put the column name as “id” we would have returned the 1, 2, and 3. You can add extra column names by putting a comma after the column before the new column name. Next, we have to tell SQL where SELECT data with the FROM keyword, which just gives SQL a table name. That is one simply query, but let’s get more specific
SQL has a built in keyword WHERE that you can give a condition. Pseudo code here: WHERE column_name = “value”. The WHERE keyword is another term you will use very often in your queries, because you probably don’t want to work with every record in the table all the time. WHERE allows you to work with a selected set of records. Let’s steal “theFlanders” password:
WHERE is fairly straight forward when you are searching for single exact matches. What if I had 2 or more conditions? We’ll get into the more advanced stuff like that after we introduce all of the SQL Statements. All you would really need to do is add an AND or an OR after the WHERE statement. You can add as many as you want. Go ahead and go crazy.
The SQL INSERT statement allows you to insert data into your tables. While, it is certainly possible that a website won’t require and INSERT Statement, it isn’t very likely. Giving the user the ability to put fresh data into your tables is extremely useful if done correctly. The SELECT statement doesn’t need as much caution as the next three statements because you aren’t actually mutating the data.
You may be thinking what could INSERT possibly hurt? I mean it only puts new data in a table, it doesn’t delete anything. You would be correct, but if you gave me the chance, I could change your mind very quickly. Ever heard of spam? Think about an INSERT query that repeats itself over and over, while you squirm around trying to figure out why your database has so much duplicated data that it is virtually useless. While in coding you should always be careful, be extra careful for the rest of these tutorials involving INSERT, UPDATE, and DELETE statements. All warnings aside, let’s get to it:
Here is our table before do the insert:
INSERT INTO table_name(username, password, birthday)
VALUES( ‘rustyMeerkat’,’digholes’, ‘1995-09-15’)
Don’t freak out. I know it’s a lot different than the SELECT, but it is still really easy. INSERT INTO signals SQL that we are putting new stuff in a table. Of course, table_name is the name of our table. Now, it gets weird. In pseudo code, it is (column_name, column_name, column_name). We just put our column names separated by a comma. Then, we use the keyword VALUES followed by parentheses. Inside the parentheses, we have various values. The order of these values is directly related to the way we put in the columns. For instance, we will be inserting the value ‘rustyMeerkat’ into the username column, ‘digholes’ into the password column, etc. Don’t worry much about the birthday and the date looking value. Dates are a real pain unless they are done right, which we’ll get into later.
But, why is there only 3 values when we have 4 columns? Typically, a good table design will have an ID column. This should also be set to “auto_increment” which tells the database to add one each time. This gives us unique IDs for every record. This unique id will save your life whenever you run into duplicate data situations. Next!