Welcome to my ASP Code Website

Syntax of a SQL Select Statement



The most common thing to do with ASP is show information stored in a database. Here are the basics of the SQL Select statement, to get that information.

SELECT field1, field2 FROM tablename WHERE condition

So that is the basic syntax. Say you had a table called WRITERS and you wanted to select the fields called first_name and last_name. You would use

SELECT first_name, last_name FROM writers

Now say you wanted to only get the writers whose last name was SMITH. You would use

SELECT first_name, last_name FROM writers WHERE last_name = 'SMITH'

You can do multiple conditions using AND and OR if you want. Let's say you only want the writers whose first name is JULIE and last name is SMITH. You would use

SELECT first_name, last_name FROM writers WHERE first_name = 'JULIE' and last_name = 'SMITH'

You can use > and < on fields that are numeric. Let's say the writers table has another field called hourly_rate that is a number. You can select all writers that charge less than $100 an hour with this select statement -

SELECT first_name, last_name FROM writers WHERE hourly_rate < 100

You can use wildcards on STRING FIELDS ONLY - not on text fields. The wildcard symbol is a % and you have to use it in conjunction with the keyword LIKE. So to find all writers whose last names begin with the letter S, you would say:

SELECT first_name, last_name FROM writers WHERE last_name LIKE 'S%'

Basics of SQL Commands