Java Reference
In-Depth Information
Table 7-1: The CONTACT_INFO Table
FIRST_NAME
MI
LAST_NAME
STREET
CITY
STATE
ZIP
Francis
X
Corleone
17 Main
New York
NY
10005
Vito
G
Corleone
23 Oak St
Newark
NJ
12345
Tom
B
Hagen
37 Chestnut
Newark
NJ
12345
Kay
K
Adams
109 Maple
Newark
NJ
12345
Francis
F
Coppola
123 Sunset
Hollywood
CA
23456
Mario
S
Puzo
124 Vine
Hollywood
CA
23456
To retrieve all records containing the last name Corleone, you could use the following query:
SELECT * FROM Contact_Info WHERE Last_Name = 'Corleone';
The result of this query will be to return all columns from any row containing the Last_Name Corleone.
The order in which the columns are returned will be the order in which they are stored in the database,
although the row order is arbitrary.
Note
Unlike rows in a spreadsheet, records in a database table have no implicit order. You
must specify explicitly any ordering you need.
To retrieve columns in a specific order, the column names must be specified in the query. For example,
to get the data in First_Name, Last_Name order, use the following query:
SELECT First_Name, Last_Name FROM Contact_Info WHERE Last_Name =
'Corleone';
To get the order reversed, use the following query:
SELECT Last_Name, First_Name FROM Contact_Info WHERE Last_Name =
'Corleone';
Formatting SQL Commands
The SQL engine ignores excess white space, so you can and should insert line breaks for clarity.
Conventionally, major clauses such as the FROM clause and the WHERE clause are placed on their own
lines, unless the command is so brief as to be trivial. For example, many Relational Database
Management Systems (RDBMS) such as SQL Server, format commands in the SQL pane
automatically to conform to this style. A good basic approach when you are not quite sure how to
format a command is to aim for readability. Remember, somebody will have to maintain what you write,
so readability is important.
Key words, table names, and column names are not case sensitive, but the contents of the records
within a table are case sensitive. This means that with a little thought, you can use case to help make
your SQL statements more readable.
Search WWH ::




Custom Search