The Select statement is a very simple but important part of SQL. At this point, if you read the What is SQL? article, I presume you have a bunch of test data to work with. The important thing about data is not always the data itself but what insights you can draw from the data. The Select statement will help you comb through that data so you find what you’re looking for.
You may recall, in the What is SQL? article, we mentioned creating, reading, writing, and deleting as fundamental SQL actions. Using Select statements will allow you to accomplish the “reading” action, so you can display your data in different ways, such as:
- A list blog posts
- A group of customers
- A bunch of cities
No matter what kind of data you’ve collected, Select statements will help you sort it and read it.
Selecting *All* Newsletter Respondents
Imagine you had a signup form on your website. Visitors provide their name and email. And the form itself grabs their IP address.
Your database may hold this information in a table called “respondents”. Each respondent will have a unique identification number. So that amounts to five columns in your table:
- first name
- last name
- IP address
For our first example, let’s say you just want to see all respondents. We can select all respondents using an
* in our statement:
SELECT * FROM respondents;
“Respondents” being the name of our table.
Make note of the syntax here. The SQL statement parts are capitalized, but this is not required. And a semi-color
; ends the statement.
Select Less Data
Often, it’s beneficial to be more selective with our Select statements. In the following example, let’s suppose you only want to see the names of our respondents. You could use the following statement:
SELECT "first_name", "last_name" FROM respondents;
This will produce the first and last name records for all of our respondents.
You can now see the basic syntax for Select statements in SQL. This is only an introduction. These statements can grow quickly in complexity. However, the basic syntax for selecting information from your database remains the same. As your need for more complicated records grows, your statements will get larger and more complex. Stay tuned in our SQL series, as we move on to discuss creating and deleting tables in our database.