Performing Single Table SELECT Statements


The SELECT statement is used to retrieve information from tables in the database. In its simplest form, you specify the table from which you want to retrieve data, and specify a list of column names to retrieve. The SELECT statement in the following example retrieves the customer_id , first_name , last_name , dob , and phone columns from the customers table:

 SELECT customer_id, first_name, last_name, dob, phone FROM customers; 

Immediately after the SELECT keyword, you supply the column names that you want to retrieve; after the FROM keyword, you supply the table name from which the columns are to be retrieved. The SQL statement is ended using a semicolon ( ; ).

Note  

You don t tell the database management system software exactly how to access the information you want. You just tell it what you want and let the software worry about how to actually get it. More generally , the items that immediately follow the SELECT statement needn t always be columns from a table. They can be any valid expressions, and you will see examples of expressions later in this chapter.

After you press ENTER at the end of the SQL statement, the statement is executed and the results are returned to SQL*Plus for display on the screen as shown in the following output:

 CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE ----------- ---------- ---------- --------- ------------  1 John Brown 01-JAN-65 800-555-1211  2 Cynthia Green 05-FEB-68 800-555-1212  3 Steve White 16-MAR-71 800-555-1213  4 Gail Black 800-555-1214  5 Doreen Blue 20-MAY-70 

The rows returned by the database are known as a result set . As you can see, the Oracle database converts the column names that you specify into their uppercase equivalents. Character and date columns are left-justified; number columns are right-justified. By default, the Oracle database displays dates in the format DD-MON-YY , where DD is the day number, MON is the first three characters of the month (in uppercase), and YY is the last two digits of the year. The database actually stores all four digits for the year, but by default it only displays the last two digits.

Note  

Your DBA can change the default display format for dates by setting the Oracle database initialization parameter NLS_DATE_FORMAT . You ll learn more about this in Chapter 4.

Although you can specify column names and table names using either lowercase or uppercase, it is better to stick with one style. The examples in this book use uppercase for SQL and Oracle keywords, and lowercase for everything else.




Oracle Database 10g SQL
Oracle Database 10g SQL (Osborne ORACLE Press Series)
ISBN: 0072229810
EAN: 2147483647
Year: 2004
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net