Basic Query Syntax

Learn how to structure a query using working examples.

Let’s work on some exercises and see how each query works. Each exercise gives an overview and examples before we start applying what we learn in each section.

An example of the “Products” table is given below, which we’ll be using in the next few exercises.

Products Table

ProductID

Name

ProductNumber

Color

StandardCost

ListPrice

SIZE

317

LL Crankarm

CA-5965

Black

1000.0

1000.0

NULL

318

ML Crankarm

CA-6738

Black

500

455.0

NULL

319

HL Crankarm

CA-7457

Black

1500

1500.0

NULL

320

Chainring Bolts

CB-2903

Silver

2000

2000.0

NULL

321

Chainring Nuts

CN-6137

Silver

750

1800.0

NULL

The query structure

Understanding the syntax and its structure is extremely helpful for the future. Let’s delve into the basics of one of the most common statements, the SELECT statement, which we use to retrieve data.

Press + to interact
SELECT Column_Name1, Column_Name2
FROM Table_name

In the query above, we select two columns and all rows from the table.

Since we’re selecting two columns, the query would perform much faster than if we were to select all of the columns like this:

Press + to interact
SELECT *
FROM tableName
GO

The WHERE clause

We use the WHERE clause to filter the number of rows a query can return. This clause uses a condition, such as if a column is equal to, greater than, less than, between, or like a certain value.

When writing the syntax, it’s important to remember that the WHERE condition comes after the FROM statement. The types of operators vary based on the type of data that we’re filtering.

The EQUALS operator

We use the EQUALS operator when finding an exact match. The syntax below uses the WHERE clause for a column that contains the exact string of 'Value'.

Note: The strings need single quotes around them. However, numeric values do not.

Press + to interact
SELECT ColumnName1, ColumnName2
FROM TableName
WHERE ColumnName = 'Value'
GO

The BETWEEN operator

Typically, we use the BETWEEN operator for finding values between a certain range of numbers or date ranges. It’s important to note that the first value in the BETWEEN comparison operator should be lower than the value on the right—for example, comparison between 10 ...

Get hands-on with 1400+ tech skills courses.