Vous êtes sur la page 1sur 16

Selection with condition

Selection with condition (where


clause)

SELECT [DISTINCT] {*| column [alias], ...}


FROM table
[WHERE condition(s)];
Selection with condition (where
clause)

SELECT company_id, order_id, description,price


FROM demo_order_item_tab
WHERE price > 2000;

CO ORDER_ID PRODUCT_ID DESCRIPTION PRICE

-- --------- ---------- ------------------------------ ---------

01 10004 409 Processor Pentium 166MHz 2300

01 10007 409 Processor Pentium 166MHz 2300


String and Dates
Strings and dates can also be used in the where clause.

They are enclosed in single quotation marks.

String values are case sensitive and dates are format


sensitive.

The default date format is DD-MON-YY.

SELECT ORDER_ID,
CUSTOMER_ID,
ORDER_DATE
FROM DEMO_ORDER_TAB
WHERE DELIVERY_TYPE = ‘Mail’;
Logical operators

Testing against a single values


= equality
> greater than
>= greater than or equal to
< less than
<= less than or equal to
!= inequality
<> inequality
Logical operators

Testing against a list of values


IN selects every row that fits criteria
NOT IN selects every row that does not fit the
criteria
BETWEEN selects every row whose value in
certain field is between two criteria
NOT BETWEEN selects every row whose value in
certain field is either below one
criteria and above another criteria
BETWEEN Operator
SELECT company_id, order_id, description,price
FROM demo_order_item_tab
WHERE price BETWEEN 2000 AND 2500;

CO ORDER_ID PRODUCT_ID DESCRIPTION PRICE

-- --------- ---------- ------------------------------ ---------

01 10004 409 Processor Pentium 166MHz 2300

01 10007 409 Processor Pentium 166MHz 2300


IN operator

SELECT CUSTOMER_ID,
NAME
FROM DEMO_CUSTOMER_TAB
WHERE NAME IN (‘IBM Trading’, ‘KADU Electronics’);
Pattern matching
NULL

SELECT CUSTOMER_ID,
ADDRESS
FROM DEMO_CUSTOMER_TAB
WHERE BENEFITS IS NULL;

SELECT CUSTOMER_ID,
ADDRESS
FROM DEMO_CUSTOMER_TAB
WHERE VALID IS NOT NULL;
Combination of logic

SELECT company_id, order_id, row_no, description


FROM demo_order_item_tab
WHERE quantity =>500
AND price < 1000;
Rules of Precedence

Order Evaluated Operator


1 All comparison
operators
2 NOT
3 AND
4 OR

Override rules of precedence by using parentheses.


ORDER BY Clause
 Sort rows with the ORDER BY clause
ASC: ascending order, default
DESC: descending order
 The ORDER BY clause comes last in the SELECT statement.

SELECT order_id, order_date, delivery_date


FROM demo_order_tab
order by delivery_date;

It is also possible to sort by multiple columns


Datatypes
NUMBER LONG
 For fixed and floating point  Variable length character
numbers strings up to 65000
characters
CHAR(size)
 Fixed length of characters RAW(size)
with a maximum of 255  A datatype for raw binary data

DATE LONGRAW
 For valid dates from 1  Same as RAW datatype
January to 31 December except with a size up to
65000 binary digits
VARCHAR2(size)
 Variable length character
string with a maximum of
2000 characters

Vous aimerez peut-être aussi