Sie sind auf Seite 1von 9

Listing The Table Contents

If you want to see the contents of a table, use the SELECT command followed by
the desired attributes. If you want to see all the attributes listed, use the *
(asterisk) to indicate "all attributes" For example to list all the attributes and all
the rows of the CUSTOMER Table, use;
SELECT *
FROM CUSTOMER;
Below is the output from the CUSTOMER Table:

Or instead of using the * character (wildcard), each of the table's attributes may
be listed:
SELECT
CUS_CODE, CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM
CUSTOMER
Partial Listing Of Table Contents
You can select partial table contents by naming the desired fields and by placing
restrictions on the rows to be included in the output. The following syntax
enables you to specify which table values are presented:
SELECT <column(s)>
FROM <table name>
WHERE<conditions>
For example, the query:
SELECT CUS_CODE, CUS_LNAME, CUS_FNAME
FROM CUSTOMER

WHERE CUS_CODE = 10018


The query above will return the following results:

Numerous logical restrictions may be placed on the selected table contents. The
SQL command structure thus provides almost limitless query flexibility. For
example, the mathematical symbols shown below may be used to restrict
output:
Mathematical Operators
Symbol
=

Meaning
Equal To

<

Less Than

<=

Less Than or Equal To

>

Greater Than

>=

Greater Than or Equal


To

<>

Not equal to *

* Some SQL versions use != rather than <>


The following example uses the "Not Equal To" operator:
SELECT
CUS_CODE, CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM
CUSTOMER
WHERE
CUS_CODE <> 10018;
The output returns all the rows in the CUSTOMER Table except for where
CUS_CODE = 10018:

Using the PRODUCT Table to follow the example below, we only want to query
data where the Product Price is less than or equal to $ 10.00. See PRODUCT
Table below:

SELECT
PROD_DESCRIPTION, PROD_PRICE, PROD_ON_HAND
FROM
PRODUCT
WHERE
PROD_PRICE <= 10;
The following results are returned:

Using Mathematical Operators on Dates


Use the INVENTORY to query for a records where the Inventory Date is less than
or equal to 11/16/2007

SELECT
INV_NUMBER, CUS_CODE, INV_DATE
FROM
INVENTORY
WHERE
INV_DATE <= '11/16/2007'
The following results are returned:

Logical Operators: AND, OR, and NOT


SQL allows the use of logical restrictions on its queries. For example, if you want
a list of table contents for
Either the CUS_CODE 10013 OR CUS_CODE 10016 using the CUSTOMER Table,
you can use the following command sequence:
SELECT
CUS_CODE, CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM
CUSTOMER
WHERE
CUS_CODE = 10013
OR
CUS_CODE =10016;
The following results are returned:

The logical AND has the same SQL syntax requirement. The following command
generates a list of all rows for which P_PRICE is less than $50.00 AND for which
the P_INDATE is a date is a date occurring after 10/11/2007

SELECT
P_DESCRIPTION, P_INDATE, P_PRICE, V_CODE
FROM
PRODUCT
WHERE
P_PRICE < 50
AND
P_INDATE > '10/11/2007'
The query will return the following results:

You can also combine the logical OR with the logical AND to place further
restrictions on the output.
For example, suppose you want a table listing for the following conditions:

The P_INDATE is after 10/11/2007 and the P_Price is less than $50.00.
Or, the V_CODE is 24288
The required listing can be produced by using:
SELECT
P_DESCRIPTION, P_INDATE, P_PRICE, V_CODE
FROM
PRODUCT
WHERE
(P_PRICE < 50 AND P_INDATE > '10/11/2007')
OR
V_CODE =2144
The following results return:

Special Operators
SQL allows the use of special operators in conjunction with the WHERE clause.
These special operators include:

BETWEEN - used to define a range


IS NULL - used to check whether an attribute value is null.
LIKE - used to check for similar character strings
IN- used to check whether an attribute value matches a value contained
within a (sub)set of listed values
EXIST- used to check whether an attribute has a value. In effect, EXIST is the
opposite of IS NULL
If you wanted to see a listing for all products whose prices are between $50.00
and $100.00, use the following command sequence:
SELECT *
FROM
PRODUCT
WHERE
P_PRICE BETWEEN 50.00 AND 100.00
If your DBMS does not BETWEEN, you can use:
SELECT*
FROM
PRODUCT
WHERE
P_PRICE > 50.00
AND
P_PRICE <100.00
Standard SQL allows the use of IS NULL to check for an empty or null attribute
value. A null entry can be found using the command sequence:
SELECT
P_CODE, P_DESCRIPTION
FROM
PRODUCT
WHERE
P_MIN IS NULL
SQL allows you to use the percent sign (%) and underscore (_) wildcard
characters to make matches when the entire string is not know:

% means any and all following characters are eligible. For example
'J%' includes Johnson, Jones, Jernigan, July, and J-231Q
'Jo%' includes Johnson and Jones

_means any one character may be substituted for the underline. For
example,
'_23-456-6789' includes 123-456-6789, 223-456-6789, and 323-456-6789
'_23-_56-678_' includes 123-156-6781, 123-256-6782, and 823-956-6788
'_o_es' includes Jones, Cones, Cokes, totes, and roles
Wildcard characters may be used in combinations. For example, the wildcard
search based on the string '_l%' can yield the string Al, Alton, Elgin, Blakeston,
blank, bloated, and eligible.
The conditional LIKE must be used to conjunction with wildcard characters. For
example the following query would find all the VENDOR rows for contacts whose
last names begin with Smith:
SELECT
V_NAME, V_CONTACT, V_AREACODE, V_PHONE
FROM
VENDOR
WHERE
V_CONTACT LIKE 'Smith%';
In the example below, would this query return any values?
SELECT
V_NAME, V_CONTACT, V_AREACODE, V_PHONE
FROM
VENDOR
WHERE
V_CONTACT LIKE 'SMITH%';
EXIST can be used whenever there is a requirement to execute a command
based on attribute values that are not null.
SELECT*
FROM
PRODUCT
WHERE
V_CODE EXIST;
Ordering A List
The ORDER BY clause is especially useful if the listing sequence is important to
you. The syntax:
ORDER BY <attribute>
This command will produce a list in ascending order. To produce a list in
descending order, use

ORDER BY <attribute> DESC


For example, if wanted the contents of the CUSTOMER table listed by Customer
Last Name (CUS_LNAME) in ascending order, use:
SELECT
CUS_CODE, CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM
CUSTOMER
WHERE
ORDER BY CUS_LNAME
The following results are returned:

Das könnte Ihnen auch gefallen