Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Using Database in Web Development – Part 6

Monday, June 2nd, 2014 by Servage

c

Now we have tables with stuff in them. The next logical step is getting stuff back out. In this lesson you will learn about the most used statement in SQL, the SELECT.

What is a SELECT statement?

 

So, what does SELECT do? Very simply, it tells the DBMS that you want to get some data out of the database.

So, how does it work? SELECT can range from very simple to painfully complex. With SELECT there are a whole host of different options available to you that will allow you specify, calculate and arrange your data in an infinite number of ways.

Using a SELECT statement obviously begins with the SELECT command. Next, you will need to specify what column(s) of data you will be retrieving. For example, using our test table that we created say we want to get someone’s email address and first name. You would let the DBMS know what you are looking for by starting your SELECT statement like this:

  SELECT first_name, email

This tells the DBMS that we are getting information only from the “first_name” and “email” columns of our table.

So, what if I wanted to select all the columns in the table? Would I have to spell out each and every column in a table? Wouldn’t that be a pain with a table that has a bunch of columns?

Yes, it would be a pain and no you don’t have to specify each column. If you want to get all of the columns out of any table you only need to use just one character, “*”, like this:

  SELECT *

The “*” tells the DBMS to send every column in the table. Making use of the asterisk is very common but shouldn’t be overused. Some developers have a tendency to use the asterisk almost exclusively even when they don’t intend to use most of the information they are retrieving. While this may cut down on the amount of code you have to type in, it can be an incredible waste of system resources if it is abused.

Alright, what if I want to get stuff out of a few different tables? Do I have to make a SELECT statement for each table?

No, you can actually combine data from several different tables into one SELECT statement. However, this particular topic we are going to save for a bit later in the series.

The next part of the SELECT statement is the FROM clause.

What is a FROM clause?

As you might have guessed, the FROM clause tells the DBMS what table to look in. Using our table we created in Part 2, our SELECT statement should look something like this now:

  SELECT first_name, email
    FROM contacts

Can I use more than one table in the FROM clause?

Yes, you can. This can be a more complex issue which I will explain later in the series along with selecting multiple column names from different tables.

Now, if you wanted to you could stop right here. Just add a semicolon to the end of the SELECT statement above and you would get every first name and email in the “contacts” table.

But what if you wanted to get data for people with the first name of “Paul”? Well, that’s when the WHERE clause comes in to play.

Sources for Further Reading

Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.