SQL - 7
SQL - 7
INPUT:
OUTPUT:
6 rows selected.
Suppose your company gives each employee 12 days of leave each year. Using what
you have learned and a logical operator, find all the employees whose names start with
B and who have more than 50 days of leave coming.
INPUT/OUTPUT:
BLEDSOE 51
BOLD 80
ANALYSIS:
This query is the most complicated you have done so far. The SELECT clause (lines 1
and 2) uses arithmetic operators to determine how many days of leave each employee
has remaining. The normal precedence computes YEARS * 12 - LEAVETAKEN . (A
clearer approach would be to write (YEARS * 12) - LEAVETAKEN .)
LIKE is used in line 4 with the wildcard % to find all the B names. Line 6 uses the
> to find all occurrences greater than 50 .
The new element is on line 5. You used the logical operator AND to ensure that you
found records that met the criteria in lines 4 and 6.
AND
AND means that the expressions on both sides must be true to return TRUE . If either
expression is false, AND returns FALSE . For example, to find out which employees
have been with the company for 5 years or less and have taken more than 20 days
leave, try this:
INPUT:
OUTPUT:
LASTNAME
--------
BAKER
BOLIVAR
If you want to know which employees have been with the company for 5 years or
more and have taken less than 50 percent of their leave, you could write:
INPUT/OUTPUT:
WORKAHOLICS
---------------
BAKER
BLEDSOE
Check these people for burnout. Also check out how we used the AND to combine
these two conditions.
OR
You can also use OR to sum up a series of conditions. If any of the comparisons is
true, OR returns TRUE . To illustrate the difference, conditionsrun the last query with
OR instead of with AND :
INPUT:
WORKAHOLICS
---------------
ABLE
BAKER
BLEDSOE
BOLD
COSTALES
ANALYSIS:
The original names are still in the list, but you have three new entries (who would
probably resent being called workaholics). These three new names made the list
because they satisfied one of the conditions.OR requires that only one of the conditions
be true in order for data to be returned.
NOT
NOT means just that. If the condition it applies to evaluates to TRUE , NOT make it
FALSE . If the condition after the NOT is FALSE , it becomes TRUE . For
example, the following SELECT returns the only two names not beginning with B in
the table:
INPUT:
SQL> SELECT *
2 FROM VACATION
3 WHERE LASTNAME NOT LIKE 'B%';
OUTPUT:
ABLE 101 2 4
COSTALES 211 10 78
NOT can also be used with the operator IS when applied to NULL . Recall the
PRICES table where we put a NULL value in the WHOLESALE column opposite
the item ORANGES .
INPUT/OUTPUT:
ITEM WHOLESALE
TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
CHEESE .89
APPLES .23
ORANGES
7 rows selected.
INPUT/OUTPUT:
SQL> SELECT *
2 FROM PRICE
3 WHERE WHOLESALE IS NOT NULL;
ITEM WHOLESALE
TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
CHEESE .89
APPLES .23
6 rows selected.
Set Operators
INPUT/ OUTPUT:
NAME
ABLE
BRAVO
CHARLIE
DECON
EXITOR
FUBAR
GOOBER
7 rows selected.
INPUT/ OUTPUT:
NAME
--------------------
ABLE
BAKER
CHARLIE
DEAN
EXITOR
FALCONER
GOOBER
7 rows selected.
How many different people play on one team or another?
INPUT/OUTPUT:
NAME
ABLE
BAKER
BRAVO
CHARLIE
DEAN
DECON
EXITOR
FALCONER
FUBAR
GOOBER
10 rows selected.
UNION returns 10 distinct names from the two lists. How many names are on both
lists (including duplicates)?
INPUT/OUTPUT:
ABLE
BAKER
CHARLIE
DEAN
EXITOR
FALCONER
GOOBER
ABLE
BRAVO
CHARLIE
DECON
EXITOR
FUBAR
GOOBER
14 rows selected.
ANALYSIS:
INTERSECT
INTERSECT returns only the rows found by both queries. The next SELECT
statement shows the list of players who play on both teams:
INPUT:
NAME
ABLE
CHARLIE
EXITOR
GOOBER
ANALYSIS:
In this example INTERSECT finds the short list of players who are on both teams by
combining the results of the two SELECT statements.
MINUS (Difference)
Minus returns the rows from the first query that were not present in the second. For
example:
INPUT:
OUTPUT:
NAME
BRAVO
DECON
FUBAR
ANALYSIS:
The preceding query shows the three football players who are not on the softball
team. If you reverse the order, you get the three softball players who aren't on the
football team:
INPUT:
OUTPUT:
NAME
BAKER
DEAN
FALCONER
INPUT:
SQL> SELECT *
2 FROM FRIENDS
3 WHERE STATE= 'CA'
4 OR
5 STATE ='CO'
6 OR
7 STATE = 'LA';
OUTPUT:
INPUT/OUTPUT:
SQL> SELECT *
2 FROM FRIENDS
3 WHERE STATE IN('CA','CO','LA');
ANALYSIS:
The second example is shorter and more readable than the first. You never know when
you might have to go back and work on something you wrote months ago. IN also
works with numbers. Consider the following, where the column AREACODE is a
number:
INPUT/OUTPUT:
SQL> SELECT *
2 FROM FRIENDS
3 WHERE AREACODE IN(100,381,204);
If you needed a range of things from the PRICE table, you could write the following:
INPUT/OUTPUT:
SQL> SELECT *
2 FROM PRICE
3 WHERE WHOLESALE > 0.25
4 AND
5 WHOLESALE < 0.75;
ITEM WHOLESALE
TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
INPUT/OUTPUT:
SQL> SELECT *
2 FROM PRICE
3 WHERE WHOLESALE BETWEEN 0.25 AND 0.75;
ITEM WHOLESALE
TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
Again, the second example is a cleaner, more readable solution than the first.
1. Write a query that returns everyone in the database whose last name begins
with M .
2. Write a query that returns everyone who lives in Illinois with a first name of
AL .
4. What shorthand could you use instead of WHERE a >= 10 AND a <=30 ?
SELECT FIRSTNAME
FROM FRIENDS
WHERE FIRSTNAME = 'AL'
AND LASTNAME = 'BULHER';
Exercises
1. Using the FRIENDS table, write a query that returns the following:
NAME ST
AL FROM IL
INPUT:
SQL> SELECT (FIRSTNAME || 'FROM') NAME, STATE
2 FROM FRIENDS
3 WHERE STATE = 'IL'
4 AND
5 LASTNAME = 'BUNDY';
OUTPUT:
NAME ST
AL FROM IL
2. Using the FRIENDS table, write a query that returns the following:
NAME PHONE