If you wanted to see the EMPLOYEEIDNO's of those making at or over $50,000,
use the following:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY >= 50000;
Notice that the >= (greater than or equal to) sign is used, as we wanted to
see those who made greater than $50,000, or equal to $50,000, listed together.
This displays:
EMPLOYEEIDNO
------------
010
105
152
215
244
The WHERE> description, SALARY >= 50000, is known as a condition
The same can be done for text columns:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager'>
This displays the ID Numbers of all Managers. Generally, with text columns,
stick to equal to or not equal to conditions, and make sure that any text that
appears in the statement is surrounded by single quotes (').
More Complex Conditions: Compound Conditions
The AND operator joins two or more conditions, and displays a row only
if that row's data satisfies ALL conditions listed (i.e. all conditions
hold true). For example, to display all staff making over $40,000, use:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY > 40000 AND POSITION = 'Staff';
The OR operator joins two or more conditions, but returns a row if
ANY of the conditions listed hold true. To see all those who make less than
$40,000 or have less than $10,000 in benefits, listed together, use the
following query:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY < 40000 OR BENEFITS < 10000;
AND & OR can be combined, for example:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager' AND SALARY > 60000 OR BENEFITS > 12000;
First, SQL finds the rows where the salary is greater than $60,000 or the
benefits is greater than $12,000, then taking this new list of rows, SQL then
sees if any of these rows satisfies the condition that the Position column if
equal to 'Manager'. Subsequently, SQL only displays this second new list of
rows, as the AND operator forces SQL to only display such rows satisfying the
Position column condition. Also note that the OR operation is done first.
To generalize this process, SQL performs the OR operation(s) to determine the
rows where the OR operation(s) hold true (remember: any one of the conditions is
true), then these results are used to compare with the AND conditions, and only
display those remaining rows where the conditions joined by the AND operator
hold true.
To perform AND's before OR's, like if you wanted to see a list of managers or
anyone making a large salary (>$50,000) and a large benefit package (>$10,000),
whether he or she is or is not a manager, use parentheses:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager' OR (SALARY > 50000 AND BENEFIT > 10000);
IN & BETWEEN
An easier method of using compound conditions uses IN or BETWEEN.
For example, if you wanted to list all managers and staff:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION IN ('Manager', 'Staff');
or to list those making greater than or equal to $30,000, but less than or
equal to $50,000, use:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY BETWEEN 30000 AND 50000;
To list everyone not in this range, try:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY NOT BETWEEN 30000 AND 50000;
Similarly, NOT IN lists all rows excluded from the IN list.
Using LIKE
Look at the EmployeeStatisticsTable, and say you wanted to see all people
whose last names started with "L"; try:
SELECT EMPLOYEEIDNO
FROM EMPLOYEEADDRESSTABLE
WHERE LASTNAME LIKE 'L%';
The percent sign (%) is used to represent any possible character (number,
letter, or punctuation) or set of characters that might appear after the "L". To
find those people with LastName's ending in "L", use '%L', or if you wanted the
"L" in the middle of the word, try '%L%'. The '%' can be used for any
characters, in that relative position to the given characters. NOT LIKE displays
rows not fitting the given description. Other possiblities of using LIKE, or any
of these discussed conditionals, are available, though it depends on what DBMS
you are using; as usual, consult a manual or your system manager or
administrator for the available features on your system, or just to make sure
that what you are trying to do is available and allowed. This disclaimer holds
for the features of SQL that will be discussed below. This section is just to
give you an idea of the possibilities of queries that can be written in SQL.
|