The CHECKS table:
CHECK# PAYEE AMOUNT REMARKS
--------- -------------------- ------ ---------------------
1 Ma Bell 150 Have sons next time
2 Reading R.R. 245.34 Train to Chicago
3 Ma Bell 200.32 Cellular Phone
4 Local Utilities 98 Gas
5 Joes Stale $ Dent 150 Groceries
6 Cash 25 Wild Night Out
7 Joans Gas 25.1 Gas
Your First Query
INPUT:
SQL> select * from checks;
OUTPUT:
queries CHECK# PAYEE AMOUNT REMARKS
------ -------------------- ------- - --------------------
1 Ma Bell 150 Have sons next time
2 Reading R.R. 245.34 Train to Chicago
3 Ma Bell 200.32 Cellular Phone
4 Local Utilities 98 Gas
5 Joes Stale $ Dent 150 Groceries
6 Cash 25 Wild Night Out
7 Joans Gas 25.1 Gas
7 rows selected.
Changing the Order of the Columns
The preceding example of an SQL statement used the * to select all columns from a table, the order of their appearance in the output being determined by the database. To specify the order of the columns, you could type something like:
INPUT:
SQL> SELECT payee, remarks, amount, check# from checks;
Notice that each column name is listed in the SELECT clause. The order in which the columns are listed is the order in which they will appear in the output. Notice both the commas that separate the column names and the space between the final column name and the subsequent clause (in this case FROM). The output would look like this:
OUTPUT:
PAYEE REMARKS AMOUNT CHECK#
-------------------- ------------------ --------- ---------
Ma Bell Have sons next time 150 1
Reading R.R. Train to Chicago 245.34 2
Ma Bell Cellular Phone 200.32 3
Local Utilities Gas 98 4
Joes Stale $ Dent Groceries 150 5
Cash Wild Night Out 25 6
Joans Gas Gas 25.1 7
7 rows selected.
Another way to write the same statement follows.
INPUT:
SELECT payee, remarks, amount, check#
FROM checks;
Notice that the FROM clause has been carried over to the second line. This convention is a matter of personal taste when writing SQL code. The output would look like this:
OUTPUT:
PAYEE REMARKS AMOUNT CHECK#
-------------------- -------------------- --------- --------
Ma Bell Have sons next time 150 1
Reading R.R. Train to Chicago 245.34 2
Ma Bell Cellular Phone 200.32 3
Local Utilities Gas 98 4
Joes Stale $ Dent Groceries 150 5
Cash Wild Night Out 25 6
Joans Gas Gas 25.1 7
7 rows selected
****:The output is identical because only the format of the statement changed. Now that you have established control over the order of the columns, you will be able to specify which columns you want to see.
Selecting Individual Columns
Suppose you do not want to see every column in the database. You used SELECT * to find out what information was available, and now you want to concentrate on the check number and the amount. You type
INPUT:
SQL> SELECT CHECK#, amount from checks;
which returns
OUTPUT:
CHECK# AMOUNT
--------- ---------
1 150
2 245.34
3 200.32
4 98
5 150
6 25
7 25.1
7 rows selected. 8)