Sei sulla pagina 1di 1

Chapter 3: Selecting 119

[ NOT ] IN
"(" <basic_expression> ")"
| <expression>
[ NOT ] IN
<subquery> -- single-column result set
Here is an example using IN with a list to show all the employees in the
ASADEMO database with a first name of John, Paul, George, or Ringo:
SELECT emp_id,
emp_fname,
emp_lname
FROM employee
WHERE emp_fname IN ( 'John', 'Paul', 'George', 'Ringo' )
ORDER BY emp_id;
Heres the final result set; three Johns and a Paul but no George or Ringo:
emp_id emp_fname emp_lname
====== ========= =========
318 John Crow
862 John Sheffield
1021 Paul Sterling
1483 John Letiecq
Here is another example using subqueries in two IN predicates to find all the
employees whose first name is the same as someone elses last name, or vice
versa:
SELECT emp_id,
emp_fname,
emp_lname
FROM employee
WHERE emp_fname IN ( SELECT emp_lname
FROM employee )
OR emp_lname IN ( SELECT emp_fname
FROM employee )
ORDER BY emp_id;
The final result set contains two rows, showing that Scott is the only name
that appears as both first and last name in the employee table:
emp_id emp_fname emp_lname
====== ========= =========
501 David Scott
1576 Scott Evans

3.12.4 BETWEEN Predicates


The fourth kind of predicate tests one value to see if it falls in a range between
two other values; the test is inclusive, meaning that it returns TRUE if the value
being tested matches either of the end points.
<between_predicate> ::= <expression>
[ NOT ] BETWEEN
<expression> AND <expression>
The BETWEEN predicate is especially useful for date ranges. Here is an exam-
ple that returns all sales_order rows where the order_date falls in the range
2000-01-03 to 2000-01-07:
SELECT id,
cust_id,
order_date

Potrebbero piacerti anche