Skip to content

Select Statement

 

The Select Statement is used to extract or query data from a database and can be broken down into the following core parts:

select [FIELDNAME1, FIELDNAME1, ETC.] or [*= All Fields]
from [TABLENAME1]
join [TABLENAME] on [CONDITIONS]
where [CONDITIONS]
order by [FIELDNAME1, FIELDNAME2, etc.]

Eg. The select statement to return all the Customers in the QuickEasy database, starting with 'A' and sorted by company name, could look like this:

select
CUSID,
COMPANY,
TEL,
EMAIL
from CUSMAIN
where COMPANY starting with 'A'
order by COMPANY

Formatting

Spaces and line breaks are ignored, so leaving blank lines between the different parts of your select statement can make your SQL much more readable.

Another way to make your SQL more readable is the use of UPPER and lower case. In this tutorial, we will be using UPPERCASE for all the field and table names and lowercase for the rest of the statement.

Comments

Comments can be used to explain how you queries work and to highlight where variables might need to be changed. This is especially useful when porting queries across different databases and when other persons need to support your clients. There are two types of comments that can be used:

Line comments -- comment

-- Comments the rest of the line

Block comments /* comment */

/* Block comments can stretch over multiple lines. They are ideal for creating a detailed description of how a query works. 
They are also useful for commenting out blocks of a query that might be returning errors*/