SQL Syntax and Conventions
SQL Syntax and Conventions
SQL statements have keywords that must be spelled following rules. The keywords can be upper or lower
case - SQL is not case sensitive. By convention and to improve readability, this tutorial spells SQL
keywords in upper case.
SQL statements are independent of text lines. A single SQL statement can be placed on one text line or
on multiple. In addition, multiple SQL statements can be combined on a single text line. By convention
and to improve readability, this tutorial does not put more than one SQL statement on a single text line.
Further, SQL statements are often broken into multiple lines.
A SQL statements may be terminated by a semi-colon or the word 'GO'. This tutorial leaves these
terminators out. Please supply as needed.
SQL BASICS
SELECT <columnlist>
FROM <tablename>
<main-statement>
WHERE <conditions>
Each condition tests column(s) using comparison operator(s). The following basic comparison
operators are supported:
Operator Description
= Equal
<> Not Equal
> Greater Than
< Less Than
>= Greater Than Or Equal
<= Less Than Or Equal
The comparison may involve literal value(s) that are constants like:
10
'Minnesota'
-5006.3
UPDATE <table_name>
SET <column_name> = <column_value>
WHERE <where_conditions>
SQL ADMINISTRATION
The number of characters that can make up SQL table names and column names varies by DBMS. In
many cases the limit is 30 characters. The leading character of the name must be alphabetic - not a
number or special character. The name of a new table can not duplicate the name of an existing table
and should not be the same as a SQL reserved word. The underscore character can be used to improve
readability. The same column name can not be repeated within a table. List elements are seperated by
commas.
The number of characters that can make up SQL names for tables, columns and indexes varies by
DBMS. In many cases the limit is 30 characters. The leading character of the name must be alphabetic -
not a number or special character. The name of a new index can not duplicate the name of an existing
index for the same table and should not be the same as a SQL reserved word. The underscore character
can be used to improve readability. List elements are seperated by commas.
The number of characters that can make up SQL names for tables, columns and foreign keys varies by
DBMS. In many cases the limit is 30 characters. The leading character of the name must be alphabetic -
not a number or special character. The name of a new foreign key can not duplicate the name of an
existing foreign key for the database and should not be the same as a SQL reserved word. To make the
foreign key unique it is common practice to include the table and column name as part of the foreign key
name. The underscore character can be used to improve readability. List elements are seperated by
commas.
The number of characters that can make up SQL names for tables, columns and views varies by DBMS.
In many cases the limit is 30 characters. The leading character of the name must be alphabetic - not a
number or special character. The name of a new view can not duplicate the name of an existing view or
table and should not be the same as a SQL reserved word. The underscore character can be used to
improve readability. List elements are seperated by commas.
SQL ADVANCED
<value_1> + <value_2>
The means of achieving concatenation differs by database type. for example:
SELECT LTRIM(<value_1>)
SELECT RTRIM(<value_1>)
SELECT <column_list>
FROM <table_name>
WHERE <condition_1>
AND|OR <condition_2>
SQL IN Syntax
SELECT <column_list>
FROM <table_name>
WHERE <column_name IN (value_list)>
There must be one or more members of the value_list. Numeric and non-numeric values are supported.
SELECT <column_list>
FROM <table_name>
WHERE <column_name> BETWEEN
<lower_value> AND <higher_value>
SELECT <column_list>
FROM <table_name>
WHERE <column_name> LIKE <like_condition>
The word DISTINCT can be placed in front of a single column name or a number of column names. When
in front of multiple column names, a distinct combination is returned.
The GROUP BY clause must follow the FROM and WHERE clauses. The columns in a SELECT clause
must be either group by columns or aggregate function columns.
The SQL Aggregate Functions are functions that provide mathematical operations. The functions include:
Multiple columns can be included in the ORDER BY clause. The direction of the sort is controlled by: