Query Syntax – an Introduction to SQL SELECT Statements


When you’re first learning SQL, or Structured Query Language, the first thing you’ll want to practice is querying a database for information. The SQL syntax you’ll write to get this information is called a SELECT statement. SELECT statements just return data that’s already in the database, protecting you from any accidental changes or deletions to your information. Let’s walk through some basic SELECT statements using the SalesTable dataset shown below:


View all data in the database table – To see all the data in a table, use this syntax:

SELECT * FROM SalesTable;

The keyword “SELECT” tells the database that you want to see the data in the table as opposed to creating, updating, or deleting it. The “*” symbol directs the database engine to return all the columns and rows in the table. In other words, using “*” is telling the database to return everything in the table. “FROM” is a required keyword to alert the database that the next word in the statement will be the table name where the data you wish to see is stored. “SalesTable” is the name of our table in this example. The semicolon lets the database know that your statement is finished.

View only certain columns from a database table – Now let’s use the same data, but assume that we only want to see the “Category” and “Sales” columns. Our syntax is almost the same as before, but we need to replace the “*” with a comma-delimited list of columns, like this:

SELECT Category, Sales FROM SalesTable;

We should see the same results as before, but without the “Store” column.

View only data that matches certain criteria – In a large database, selecting all the rows can return a set of data that is too large to be useful. We can tell the database to only return data based on criteria. Let’s select all data from the table for the golf category:

SELECT * FROM SalesTable WHERE Category = ‘Golf’;

You can see that we needed some new syntax at the end of the statement to specify our criteria. The “WHERE” keyword alerts the database that the requirements for the query are about to be defined. After “WHERE”, we simply list the column and the value we’re looking for in that column of data.

View data that matches multiple criteria – We can add multiple criteria to limit our query results further. For example, we may want to know which stores had golf sales over $2,000. To get this data, we’ll add a second requirement to the SQL statement in the last example:

SELECT * FROM SalesTable WHERE Category = ‘Golf’ AND Sales > ‘2000’;

The “AND” keyword tells the database to return only the records with the value “Golf” in the Category column and a value greater than “2000” in the Sales column. SQL statements can contain multiple criteria that allow you to filter the results more narrowly to find the exact information you need.

Learning SELECT statements is the first step to becoming proficient in SQL. There are many more complex query structures available, but learning the basics covered here will help you get started and give you the tools you need to answer many common business questions.