|
SELECT..FROM
SELECT..FROM is
the simplest form of SELECT statement. The SELECT
keyword indicates that the statement will
retrieve information and the
followed the SELECT keyword
indicates what are the data to be retrieved. The
FROM indicates from where
the data will be gathered.
The following SQL statement returns all fields and
all records from Customers table. The results are shown in
ResultSet1 as below.
SELECT
CustID,
Name,
Address,
City,
Country,
PostalCode,
Telephone,
Fax
FROM
Customers
* Note: SQL statement is not
case sensitive. The SELECT and FROM in capital are just good programming
practice to indicate they are SQL keywords and make the the statement more
readable.The indentation and spreading the SELECT statement into multiple lines
are not mandatory. You can write the statement in one straight line.
Table: Customers
| CustID |
Name |
Address |
City |
Country |
PostalCode |
Telephone |
Fax |
| CUST001 |
Maria Anders |
Obere Str. 57 |
Berlin |
Germany |
12209 |
030-0074321 |
030-0076545 |
| CUST002 |
Ernst Handel |
Kirchgasse 6 |
Graz |
Austria |
8010 |
7675-3425 |
7675-3426 |
| CUST003 |
Helen Bennett |
Garden House Crowther Way |
Cowes |
UK |
PO31 7PJ |
(198) 555-8888 |
(198) 555-8889 |
| CUST004 |
Paula Wilson |
2817 Milton Dr. |
Albuquerque |
USA |
87110 |
(505) 555-5939 |
(505) 555-5940 |
| CUST005 |
Liu Wong |
55 Grizzly Peak Rd. |
Butte |
USA |
59801 |
(406) 555-5834 |
(406) 555-8083 |
Resultset1
| CustID |
Name |
Address |
City |
Country |
PostalCode |
Telephone |
Fax |
| CUST001 |
Maria Anders |
Obere Str. 57 |
Berlin |
Germany |
12209 |
030-0074321 |
030-0076545 |
| CUST002 |
Ernst Handel |
Kirchgasse 6 |
Graz |
Austria |
8010 |
7675-3425 |
7675-3426 |
| CUST003 |
Helen Bennett |
Garden House Crowther Way |
Cowes |
UK |
PO31 7PJ |
(198) 555-8888 |
(198) 555-8889 |
| CUST004 |
Paula Wilson |
2817 Milton Dr. |
Albuquerque |
USA |
87110 |
(505) 555-5939 |
(505) 555-5940 |
| CUST005 |
Liu Wong |
55 Grizzly Peak Rd. |
Butte |
USA |
59801 |
(406) 555-5834 |
(406) 555-8083 |
Instead of writing all column names in the SELECT statement, we can use *
to represent all columns in the table. The following command also return the
same result in the ResultSet1
SELECT * FROM Customers
Most of the time, you can will need to limit the columns to be
returned so that you won't waste resources on the columns that you
don't need. To do so, you need only enter the column names that you
want to return after the SELECT keyword.
SELECT Name, Telephone, Fax FROM Customers
Resultset2
| Name |
Telephone |
Fax |
| Maria Anders |
030-0074321 |
030-0076545 |
| Ernst Handel |
7675-3425 |
7675-3426 |
| Helen Bennett |
(198) 555-8888 |
(198) 555-8889 |
| Paula Wilson |
(505) 555-5939 |
(505) 555-5940 |
| Liu Wong |
(406) 555-5834 |
(406) 555-8083 |
|