Even though SQL is one of the easiest programming languages, there is a lot to learn about it. Understanding the three most basic SQL clauses will help you develop the skills you need to understand the more complex aspects of SQL.
Once you have a basic understanding of the different syntactical structures of SQL, you need to learn the details. This lesson covers the three most basic SQL clauses:
Each of these terms serves a specific purpose within the query. Only the first two are required. The WHERE clause is used when you want to narrow the data that will be pulled.
As you progress through the lesson, you may encounter data issues. This is particularly true for new databases. You should always have a robust database for running queries so that you can see how the queries work. If you find there are problems with the data, contact an Access data recovery specialist. It is better to fix the issues early on so that you can get the full benefit from what you learned.
A Quick Example
The following is the most basic query. It provides everything you need to understand each of the three clauses and the required and recommended punctuation.
This shows you all of the necessary formatting for SQL. Notice that each clause is on a separate line. While it is possible to run all of your query on one line, it is strongly discouraged.
- When you run your SQL query, it will ignore line breaks. The breaks are there for you – once you have written a few queries, you will understand why.
- All SELECT statements must end with a semi-colon (;). If you do not add the semi-colon, the query will not run. Instead you will get an error message, then you will have to figure out why the query did not work.
That is all there is to it.
A Closer Look at a Real Example
While it is difficult to create a query specific to every database, you should be able to easily adjust the information in the following example. Have the name of the field and table you would like to use, then substitute them for the text after the SQL clauses.
SELECT [First Name], Company
Here is what the SQL statement says:
Select all data stored under all fields named First Name and Company on the Clients table, pulling only records where the City field is Vancouver. Notice that the semi-colon appears at the end of the last line. This is perfectly acceptable. Just make sure you remember to add it. (Putting the semi-colon on a line by itself makes it easier to identify the occasions when you have forgotten it.)
That is a lot to take in, so we are going to break it down a little further.
Examining Each Clause
We will look at each line on its own.
The first component is the SELECT clause.
SELECT [First Name], Company
It contains the SELECT operator and two identifiers, [First Name] and Company. First Name is in brackets because it contains a space. Without the brackets, your query will fail because there is no field called First or Name.
Note that the SELECT statement is first. It must always be first.
The next part is the FROM clause.
It contains the FROM operator and one identifier, Clients. This is the name of the table that the query will search.
The last component is the WHERE clause.
It contains the WHERE operator and one expression (City=”Vancouver”). This will direct the query to look at the City field and focus on all data rows where the city is Vancouver. Notice that the specific field data is in quotation marks. You must add these so that SQL will identify it as the data in the specified field.
Now you have the basic information you need to create your own basic query. Select the fields you want, the table, and any specific identifier for one of the fields.
That’s all there is to it. From here, you are just building on this knowledge.
Victor Ren is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including Excel data repair and word recovery software products. For more information visit www.datanumen.com