Java Reference
In-Depth Information
A.2.1
SELECT
As its name implies, this statement is used to select values from a table. It is by far
the most commonly used SQL statement. Basic syntax:
SELECT <fi eldName> {,<fi eldName>} FROM <TableName>
[WHERE <condition>]};
This will return the named attribute(s) for all rows in the named table satisfying
the specifi ed condition. Often, all attributes are required, so the asterisk character
(*) is provided to allow this requirement to be expressed in a shorthand form.
Examples
1 . SELECT * FROM Stock;
Here, all attributes in all rows are returned.
2 . SELECT stockCode, description FROM Stock;
Result returned for our test data:
111111 Pencil
333333 A4 pad narrow feint
444444 A4 pad wide feint
555555 Ruler
666666 Stapler
3 . SELECT stockCode,currentLevel,reorderLevel FROM Stock
WHERE currentLevel <= reorderLevel;
Result returned for our test data:
333333 121 150
555555 80 80
Keywords AND and OR can also be used, to produce compound conditions.
For example:
SELECT stockCode, unitPrice FROM Stock
WHERE unitPrice> 1 AND unitPrice <1.5;
(Does not have to be the same attribute in both sub-conditions.)
Result returned:
333333 1.45
444444 1.45
By default, the order will be ascending (which can be specifi ed explicitly by
adding the ORDER BY clause with the qualifi er ASC ). If we want descending
order, then we can use the ORDER BY clause with the specifi er DESC . For
example:
SELECT * FROM Stock ORDER BY unitPrice DESC;
Search WWH ::




Custom Search