0% found this document useful (0 votes)
202 views14 pages

SQL - 7

This document provides examples and explanations of logical operators, set operators, and other SQL operators that are used to combine conditions in a WHERE clause or join the results of multiple queries. Logical operators like AND, OR, and NOT are demonstrated with examples of how they control whether multiple conditions must be true or if only one needs to be true. Set operators like UNION, INTERSECT, and MINUS are shown combining the results of different queries. Additional operators like IN, BETWEEN are also explained.

Uploaded by

vinitajenny
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views14 pages

SQL - 7

This document provides examples and explanations of logical operators, set operators, and other SQL operators that are used to combine conditions in a WHERE clause or join the results of multiple queries. Logical operators like AND, OR, and NOT are demonstrated with examples of how they control whether multiple conditions must be true or if only one needs to be true. Set operators like UNION, INTERSECT, and MINUS are shown combining the results of different queries. Additional operators like IN, BETWEEN are also explained.

Uploaded by

vinitajenny
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

Logical Operators

Logical separate two or more conditions in the WHERE clause of an SQL


statement.Vacation time is always a hot topic around the workplace. Say you designed a
table called VACATION for the accounting department:

INPUT:

SQL> SELECT * FROM VACATION;

OUTPUT:

LASTNAME EMPLOYEENUM YEARS LEAVETAKEN


-------------- ----------- --------- ---------
ABLE 101 2 4
BAKER 104 5 23
BLEDSOE 107 8 45
BOLIVAR 233 4 80
BOLD 210 15 100
COSTALES 211 10 78

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:

SQL> SELECT LASTNAME,


2 YEARS * 12 - LEAVETAKEN REMAINING
3 FROM VACATION
4 WHERE LASTNAME LIKE 'B%'
5 AND
6 YEARS * 12 - LEAVETAKEN > 50;
LASTNAME REMAINING

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:

SQL> SELECT LASTNAME


2 FROM VACATION
3 WHERE YEARS <= 5
4 AND
5 LEAVETAKEN > 20 ;

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:

SQL> SELECT LASTNAME WORKAHOLICS


2 FROM VACATION
3 WHERE YEARS >= 5
4 AND
5 ((YEARS *12)-LEAVETAKEN)/(YEARS * 12) < 0.50;

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:

SQL> SELECT LASTNAME WORKAHOLICS


2 FROM VACATION
3 WHERE YEARS >= 5
4 OR
5 ((YEARS *12)-LEAVETAKEN)/(YEARS * 12) >= 0.50;
OUTPUT:

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:

LASTNAME EMPLOYEENUM YEARS LEAVETAKEN

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:

SQL> SELECT * FROM PRICE;

ITEM WHOLESALE

TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
CHEESE .89
APPLES .23
ORANGES

7 rows selected.

To find the non- NULL items, type this:

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

UNION and UNION ALL


UNION returns the results of two queries minus the duplicate rows. The following two
tables represent the rosters of teams:

INPUT/ OUTPUT:

SQL> SELECT * FROM FOOTBALL;

NAME

ABLE
BRAVO
CHARLIE
DECON
EXITOR
FUBAR
GOOBER

7 rows selected.

INPUT/ OUTPUT:

SQL> SELECT * FROM SOFTBALL;

NAME
--------------------
ABLE
BAKER
CHARLIE
DEAN
EXITOR
FALCONER
GOOBER

7 rows selected.
How many different people play on one team or another?

INPUT/OUTPUT:

SQL> SELECT NAME FROM SOFTBALL


2 UNION
3 SELECT NAME FROM FOOTBALL;

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:

SQL> SELECT NAME FROM SOFTBALL


2 UNION ALL
3 SELECT NAME FROM FOOTBALL;
NAME

ABLE
BAKER
CHARLIE
DEAN
EXITOR
FALCONER
GOOBER
ABLE
BRAVO
CHARLIE
DECON
EXITOR
FUBAR
GOOBER

14 rows selected.

ANALYSIS:

The combined list--courtesy of the UNION ALL statement--has 14 names. UNION


ALL works just like UNION except it does not eliminate duplicates. Now show me a
list of players who are on both teams. You can't do that with UNION --you need to
learn INTERSECT .

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:

SQL> SELECT * FROM FOOTBALL


2 INTERSECT
3 SELECT * FROM SOFTBALL;
OUTPUT:

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:

SQL> SELECT * FROM FOOTBALL


2 MINUS
3 SELECT * FROM SOFTBALL;

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:

SQL> SELECT * FROM SOFTBALL


2 MINUS
3 SELECT * FROM FOOTBALL;

OUTPUT:

NAME

BAKER
DEAN
FALCONER

Miscellaneous Operators: IN and BETWEEN


The two operators IN and BETWEEN provide a shorthand for functions you already
know how to do. If you wanted to find friends in Colorado, California, and Louisiana,
you could type the following:

INPUT:

SQL> SELECT *
2 FROM FRIENDS
3 WHERE STATE= 'CA'
4 OR
5 STATE ='CO'
6 OR
7 STATE = 'LA';

OUTPUT:

LASTNAME FIRSTNAME AREACODE PHONE ST ZIP

MERRICK BUD 300 555-6666 CO 80212


MAST JD 381 555-6767 LA 23456
PERKINS ALTON 911 555-3116 CA 95633
Or you could type this:

INPUT/OUTPUT:

SQL> SELECT *
2 FROM FRIENDS
3 WHERE STATE IN('CA','CO','LA');

LASTNAME FIRSTNAME AREACODE PHONE ST ZIP

MERRICK BUD 300 555-6666 CO 80212


MAST JD 381 555-6767 LA 23456
PERKINS ALTON 911 555-3116 CA 95633

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);

LASTNAME FIRSTNAME AREACODE PHONE ST ZIP

BUNDY AL 100 555-1111 IL 22333


MAST JD 381 555-6767 LA 23456
BOSS SIR 204 555-2345 CT 95633

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

Or using BETWEEN , you would write this:

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.

NOTE: If a WHOLESALE value of 0.25 existed in the PRICE table, that


record would have been retrieved also. Parameters used in the BETWEEN
operator are inclusive parametersinclusive.
Quiz
Use the FRIENDS table to answer the following questions.

LASTNAME FIRSTNAME AREACODE PHONE ST ZIP

BUNDY AL 100 555-1111 IL 22333


MEZA AL 200 555-2222 UK
MERRIC BUD 300 555-6666 CO 80212
MAST JD 381 555-6767 LA 23456
BULHER FERRIS 345 555-3223 IL 23332
PERKINS ALTON 911 555-3116 CA 95633
BOSS SIR 204 555-2345 CT 95633

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 .

3. Given two tables ( PART1 and PART2 ) containing columns named


PARTNO , how
would you find out which part numbers are in both tables? Write the query.

4. What shorthand could you use instead of WHERE a >= 10 AND a <=30 ?

5. What will this query return?

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

MERRICK, BUD 300-555-6666


MAST, JD 381-555-6767
BULHER, FERRIS 345-555-3223

You might also like