Base de Datos Cap. 2

19
II/ Ifll/e start our discussion of SQL with the most basic operation" retrieving informa- tion. Often, a database of interest already exists, complete with data, and we want to query that data. As we've already noted, the best way to learn SQL is by doing it. You should experiment with the example database as much as possible. Try the statements from the book. Then make up some of your own. Scripts to create and populate the exam- ple database are available from the book Web site. Chapter 9 covers creating a database using SQL. If desired, you can skip to that chapter and return here after your database is created. In SQL the workhorse for data retrieval is the SELECT statement: J Section 3.2, p. 48 Section2.1, p. 271 SELECT I[DISTINCT I ALL]II <select list>l and Section 5.1 p. 911 ,r'FROM <table Iist> ' [WHERE < row condi t ion> ]~---..... ~ Section 2.1, p. 27 Section 4.5, p. 801 ~[GROUP BY <group Iist>] ~"'~Section 2.2, p. 30 ~[[HAVING <group condition>] Section 4.6, p. 82 ORDER BY <sort 1 ist>]~ ISection 3.5, p. 61 SELECT may look complex, but don't be intimidated. You can do amazing things with the simplest of queries. 2.1 Simple SELECT At its simplest, you only need to tell SELECT two things: 1) the data attributes you want and 2) the table where it should get it. Let's start with a simple query. Suppose you wanted 27

description

chapter 2: Retrieval Basic SELECTion

Transcript of Base de Datos Cap. 2

Page 1: Base de Datos Cap. 2

I I / Ifll/e start our discussion of SQL with the most basic operation" retrieving informa-

tion. Often, a database of interest already exists, complete with data, and we want to query that data. As we've already noted, the best way to learn SQL is by doing it. You should experiment with the example database as much as possible. Try the s ta tements from the book. Then make up some of your own. Scripts to create and populate the exam- ple database are available from the book Web site. Chapter 9 covers creating a database using SQL. If desired, you can skip to that chapter and return here after your database is created.

In SQL the workhorse for data retrieval is the SELECT statement:

J Section 3.2, p. 48

Section2.1, p. 271 SELECT I[DISTINCT I ALL]II <select list>l and Section 5.1 p. 911 ,r'FROM <table Iist>

' [WHERE < row condi t ion> ]~---.....~ Section 2.1, p. 27 Section 4.5, p. 801 ~[GROUP BY <group Iist>] ~ " ' ~ S e c t i o n 2.2, p. 30

~[[HAVING <group condition>] Section 4.6, p. 82 ORDER BY <sor t 1 i s t > ] ~ ISection 3.5, p. 61

SELECT may look complex, but don't be intimidated. You can do amazing things with the simplest of queries.

2.1 Simple SELECT

At its simplest, you only need to tell SELECT two things: 1) the data attr ibutes you want and 2) the table where it should get it. Let's start with a simple query. Suppose you wanted

2 7

Page 2: Base de Datos Cap. 2

2 8 Chapter 2: Retrieval: Basic SELECTion m

to find the names of all individual i tems on your menu. In the Restaurant Database, this informat ion is conta ined in the n a m e column of the i t ems table.

SELECT name FROM items:

This SQL s ta tement fetches the values from a single column, n a m e , from all of the rows in the i t ems table and displays the results.

To retrieve multiple columns, specify a comma-del imi ted list of column names.

I[ej:ta.,-a,~,4 Find the name, item ID, and price of all items

SELECT name, itemid, price FROM items;

The order in which the a t t r ibutes are specified in the column name list de te rmines the order of a t t r ibutes in the result. The presen ta t ion of a NULL value in a result is DBMS specific.

What if we want all of the columns in a table? Typing them all is both painful and error-prone. Fortunately, SQL provides * as a shor thand for all columns.

Page 3: Base de Datos Cap. 2

�9 2.1 Simple SELECT 29

SELECT * FROM items"

SQL has two types of comments . The first type begins with two minus signs ('-') and ends with the end of the line. The second type begins w i t h / * , ends with */, and can span multiple lines.

/ * This is a m u l t i l i n e comment * /

SELECT i temid - - This comment goes to the end of the l i n e FROM i tems;

Before we go on, there are several impor tan t things to note about SQL s ta tements :

White space is not s ign i f icant - -For readabil i ty we break our s t a tements into separate lines, one for each part of the SQL statement; however, SOL would be jus t as happy if we put the entire s ta tement on the same line. We can also use mult iple spaces, tabs, or o ther white space characters to separate e lements in the query.

SQI. s t a t e m e n t s a r e n o t c a s e sens i t ive-- -For readabil i ty we use uppercase for SOL key- words and use lowercase for our table and column names; however, SQL is most ly case insensit ive for keywords and t ab le /co lumn names. Even if we create our table as vendors, we can refer to it in SQL as vEnDOrS. Whereas SQL s ta tements and t ab le /co lumn names are case insensitive, character data may be sensit ive to case, such as when it is s tored and compared. We will point out when character compar i son is case sensitive.

Semicolons are op t i ona I - -SQL statements may be te rmina ted by a semicolon. This is

useful for separat ing multiple SOL s ta tements . Most DBMSs do not require a semi- colon for the final (or, in the case of a single s ta tement , only) s ta tement . We include semicolons in our examples for completeness .

SELECT genera tes a new table---Logically, the execution of a SELECT s ta tement gener- ates a new table, called the result table, with its own columns and rows. Although your DBMS may not actually create the table, it is helpful to think about queries this way.

Page 4: Base de Datos Cap. 2

3 0 Chapter 2" Retrieval" Basic SELECTion s

2.2 Selecting Rows

Using basic SELECT/FROM, we can re t r ieve da ta f rom any table and even spec i fy c o l u m n s . Tha t ' s nice, but wha t if we wan t to l imit the r o w s in the resul t? SQL uses the WHERE c lause to spec i fy the cond i t i on tha t a pa r t i cu la r row m u s t sa t i s fy to be in the q u e r y resul t . SQL p r o v i d e s m a n y d i f f e ren t o p e r a t o r s for c o n d i t i o n c o n s t r u c t i o n . Table 2.1 p r o v i d e s an ove rv i ew of these ope ra to r s . We d i s cus s each of t he se o p e r a t o r s in detai l in the fo l lowing.

Operator Evaluates Usage

>, <

>---, < - -

< >

[ NOT] BETWEEN IS [NOT] NULL [ NOT] LIKE [ NOT] IN NOT

Equal Greater/less than Greater/less than or equal to Not equal Between two values (inclusive) Value is NULL Equal strings using wildcards (e.g., '%', '_') Equal to any element in a list Negates a condition

Table 2.1: SQL Operators

r e p f n a m e = 'Bob' uni tpr ice > 10

i n v e n t o r y <= 5000 v e n d o r i d < > 3

vendorid BETWEEN 'A' AND 'M' referredby IS NULL replname LIKE '%ith' name IN ('Soda', 'Water') NOT itemid IN ('GDNSD', 'CHKSD')

2.3 Standard Comparison Operators

For s ta r te r s , SQL p r o v i d e s the s t a n d a r d c o m p a r i s o n opera tors" = (equal), <> (not equal) , > (grea te r than), < (less than), >= (grea ter t han or equal) , and <= (less than or equal) . S u p p o s e we wan t to c rea te a va lue m e n u of all i t ems cos t ing $0.99 or less.

Find the names of items that cost $0.99 or less

SELECT name FROM i tems WHERE price <= 0.99"

How does Query 2.4 work? To c o n s t r u c t the resu l t set, SQL beg ins by f e t ch ing the i t e m s

table specif ied in the FROM clause. Next, SQL goes t h r o u g h each row, eva lua t i ng the

Page 5: Base de Datos Cap. 2

.. 2.4 String Comparisons 31

WHERE condit ion. SQL el iminates any rows where price <= 0.99 does not evaluate to true. Finally, SQL e l iminates any co lumns not in the SELECT list. Note that the co lumn list does not necessar i ly need to contain the at tr ibute(s) used in the WHERE clause.

In Query 2.4, we use a numer ic literal, 0.99, for the price compar ison . The specific form of a literal depends on the needed data type. We discuss literal format for the var ious data types in Chapter 1. Let's look at a few examples . Literal s t r ings in SQL mus t be enclosed in single quotes ('). Let's verify that our Cheese ingredient is in the correct food group.

EeJ~[a~-Jl,aL-~ Find the ID and food group of ingredients named Cheese

SELECT ingred ient id , foodgroup FROM ingredients WHERE name - 'Cheese';

Dates use a literal expressed as 'YYYY-MM-DD'.

Eej,[a~-~,~m Find the food items added after 1999

SELECT * FROM i tems WHERE dateadded > ' 1 9 9 9 - 1 2 - 3 1 ' ;

2.4 String Comparisons

How does SQL compare values to de te rmine what is greater than, less than, or equal to? For mos t data types, it is jus t what we expect. For character data, compar i son is compl ica ted by issues such as collating sequences , case sensi t ivi ty, and padding. Unfortunately, the resul t of s tr ing compar i son is usual ly DBMS specific. Most DBMSs allow admin i s t r a to r s to configure each of these.

Page 6: Base de Datos Cap. 2

~ 2 Chapter 2: Retrieval: Basic SELECTion []

Collating sequence dicta tes how the sys tem orders charac ters to allow charac ter com- parisons. '3' is obviously less than '4', but what about 'S' and 'a'? ASCII (American Standard Code for Informat ion Interchange) is a c o m m o n l y used collating sequence , where '5' happens to be less than 'a'.

Case sensitivity de te rmines whe the r character compar i sons d is t inguish be tween upper- case and lowercase. If compar i sons are case sensit ive, then the collating sequence de te rmines which case is smaller. Case sensi t iv i ty can have a significant impac t on query results . To avoid confusion, many SOL users will conver t all s t r ings to uppercase or lowercase before compar ison .

Padding de te rmines how str ings of different lengths are handled when compared . If padding is enabled, then the shor ter s tr ing is padded with the padding charac te r (usually space) so that both s tr ings are the same length.

To compare two strings, SQL evaluates the s t r ings character by character , s tar t ing at the head of each string. If it finds two cor respond ing charac ters (e.g., the third charac te r of both strings) that are not equal, the str ing with the lowest character in the col lat ing sequence is less than the other string. If SQL runs out of co r respond ing charac ters before finding two characters that do not match, then the s t r ings are equal if they are the same length: otherwise, the shor ter s tr ing is less than the longer string. For example, cons ider Query 2.7.

I[.J,[a~,-J,~,- Find all items with a name less than or equal to 'garden'

SELECT name FROM items WHERE name <- 'garden';

Our resul t is from a da tabase that is case insensi t ive. As expected, Garden Salad is not in the resul ts because garden is shor ter and the compar i son is case insensit ive.

Recall that if a literal s tr ing contains the quote character ('), we quote it.

E.j,~[ai-a,~:! Find the name of the representative for Don's Dairy

SELECT repfname, replname FROM vendors WHERE companyname- 'Don"s Dairy';

Page 7: Base de Datos Cap. 2

�9 2.5 Matching String Patterns with LIKE 3 3

2.5 Matching String Patterns with LIKE

What if we canno t r e m e m b e r if a par t icular cus tomer ' s name is Bill, Phill, or Will? SQL pro- vides wi ldcard-based , pa t t e rn -ma tch ing capabi l i t ies with the LIKE opera tor . SQL wi ldcard charac ters include the following:

Wildcard Description

matches any substring containing 0 or more characters matches any single character

Use LIKE as you would the = opera tor , except the c o m p a r i s o n s t r ing may conta in wi ldcard characters .

[ej~[a~-NJ~a Find the list of vendor representative first names that begin with 'S'

SELECT repfname FROM vendors WHERE repfname LIKE 'S%';

repfname Sherman

Sam

[2 row(s)]

Case sensi t iv i ty and padd ing charac ters are d e t e r m i n e d by the DBMS configurat ion. Without any wi ldcard characters , LIKE works jus t like =.

Let's t ry a few examples using LIKE. Suppose you have the fol lowing list of charac te r strings: Ball, Beggy, Bill, Billy, ill, Meg, Phill, Philly, Peg, Peggy. Following are some example LIKE express ions along with what they match:

LIKE Matches strings Matches

%il l ending in 'ill' Bill, ill, Phill Peg% starting with 'Peg' Peg, Peggy B%y starting with B and ending with y Beggy, Billy _ill starting with any single character and ending in 'ill' Bill B_l l starting with B and ending with 'll' with any single character in between Ball, Bill %__i11_ starting with 2 or more characters and ending with 'ill' followed by any Philly

single character

Other wi ldcard charac ters may be s u p p o r t e d by your par t icu lar DBMS, so check your documen ta t ion . In fact, some DBMSs allow regular express ions . The SQL s t anda rd

Page 8: Base de Datos Cap. 2

34 Chapter 2: Retrieval: Basic SELECTion []

inc ludes the ope ra to r SIMILAR TO, which works s imilar ly to lIKE except it t akes r egu la r express ions . Few DBMSs i m p l e m e n t SIMILAR TO.

What abou t us ing LIKE to find s t r ings con ta in ing wi ldcard cha rac t e r s? For example , what if we wan ted to find all s t r ings con ta in ing an u n d e r s c o r e charac te r? We could t ry LIKE '%_%', but this finds all s t r ings con ta in ing 1 or more cha rac t e r s because SQL t rea t s '_' as a wi ldcard character . We need some way to turn a wi ldcard into a regular charac te r . To do this, SQL allows us to speci fy an escape charac ter . Any cha rac t e r tha t i m m e d i a t e l y fol lows an escape cha rac t e r is i n t e rp re t ed as a regular charac te r , even if it is n o r m a l l y a wi ldcard or even the escape charac ter . We define '#' as the escape cha rac t e r in Query 2.10.

Find all vendor names containing an '_'

SELECT companyname FROM vendors WHERE companyname LIKE '%#_%' ESCAPE '# ' '

Because '#' is des igna ted as the escape charac ter , '_' is i n t e rp re t ed literally, not as a wi ldcard. What if you want to search for the escape charac te r? Escape it (e.g., '##').

2.6 Getting What We Haven't Got with NOT

To fetch the rows that eva lua te to false, prefix the condi t ion with NOT.

Find the name of all of the food items other than salads

SELECT name FROM items WHERE NOT name LIKE '%Salad'.

You can use NOT with any of the c o m p a r i s o n ope ra to r s f rom Table 2.1.

Page 9: Base de Datos Cap. 2

m 2.7 Combining Conditions with AND and OR 3

2.7 Combining Conditions with AND and OR

Sometimes the criteria for including or excluding a row is more complicated than a simple condit ion can express. Fortunately, we can combine two or more condi t ions using AND and OR. Two condi t ions jo ined by AND must both be true for the entire condi t ion to evaluate to true.

SELECT ingredientid, name FROM ingredients WHERE foodgroup- 'F ru i t ' AND inventory > 100.

For two condi t ions jo ined by OR, the entire condi t ion is true if ei ther or both are true.

SELECT name, unitprice, unit FROM ingredients WHERE unitprice > 0.40 OR unit - 'glass'-

Note that Soda is true for both condit ions, whereas the Chicken and Water are only true for one.

You can combine as many ANDs and ORs together as needed, but beware. SQL may not in terpret the WHERE clause as you intended. Why? SQL doesn ' t strictly evaluate an express ion from left to right. Some opera tors have a higher precedence than others. Oper- ators with a higher precedence get evaluated before opera tors with a lower precedence, no mat ter where they are in the condit ion.

In SQL, the precedence order from highest to lowest is as follows:

Precedence Operator

Highest

Lowest

Compar ison Operators NOT AND OR

Page 10: Base de Datos Cap. 2

~ 6 Chapter 2: Retrieval: Basic SELECTion �9

The evaluat ion of a condi t ion begins with the individual compar i sons . Next, condi t ions prefixed with a NOT are negated. The ANDs are then applied from left to right. Finally, the ORs are applied from left to right. Consider the incorrect solut ion in Query 2.14.

INCORRECT! Find the food items that have a name beginning with either F or S that cost less than $3.50

SELECT name, price FROM i tems WHERE name LIKE 'F%' OR name LIKE 'S%' AND price < 3.50;

How did the Fruit Plate get into our result? Because AND has a higher precedence, name LIKE 'S%' AND p r i c e > 3.50 is evaluated first. For the Fruit Plate, this re turns false; however, name L I KE 'F%' re turns true, making the condi t ion true for the row. This query really finds the food items with a name beginning with F plus the food i tems with a price less than $3.50 that begin with S.

How do we write the correct query? Use parentheses .

CORRECT! Find the food items that have a name beginning with either F or S that cost less than $3.50

SELECT name, price FROM items WHERE (name LIKE 'F%' OR name LIKE 'S%') AND price < 3.50;

In Query 2.15, the condit ion inside the parenthes is has the highest priori ty so it is exe- cuted first, jus t as we wanted. Inside parentheses , normal precedence rules apply. It is always best to use parentheses with a WHERE clause containing more than two condi t ions to ensure the in terpre ta t ion of the query is correct.

We can nest parentheses to get any execution order desired. The condi t ion inside the innermos t parenthes is always has the highest priority. Once a parenthet ical express ion

Page 11: Base de Datos Cap. 2

�9 2.8 Selecting a Range of Values with BETWEEN 3 7

has b e e n eva lua ted , the no rma l ru les of p r e c e d e n c e apply . Tha t m e a n s ( ( n a m e L I KE ' F%'

OR name LIKE 'S%') AND p r i c e < 3 . 5 0 ) wou ld execu te j u s t like Query 2.15.

2.8 Selecting a Range of Values with BETWEEN

The BETWEEN o p e r a t o r al lows us to spec i fy a range of va lues ( inclusive) for m a t c h i n g as in Query 2.16.

Find the food items costing between $2.50 and $3.50

SELECT * FROM items WHERE price BETWEEN 2.50 AND 3.50;

To get the va lues tha t are not wi th in a specif ied range, use NOT BETWEEN.

Find the food items costing less than $2.50 or more than $3.50

SELECT * FROM items WHERE price NOT BETWEEN 2.50 AND 3.50;

The resu l t inc ludes any food i t ems cos t ing less than $2.50 or g r ea t e r than $3.50. Note tha t WHERE x BETWEEN y AND z is equ iva l en t to WHERE x > = y AND • <= z so it is real ly j u s t a n o t h e r way to a n s w e r the same query . Usually, t he re are m a n y ways to a n s w e r the s ame q u e r y in SQL. In m o s t cases , the choice of syn tax shou ld not change the p e r f o r m a n c e of the query; however , in a few cases , one va r ian t can be m u c h more eff ic ient than ano the r . This will d e p e n d on y o u r d a t a b a s e and y o u r DBMS.

Page 12: Base de Datos Cap. 2

38 Chapter 2: Retrieval: Basic SELECTion u

2.9 Selecting a Set of Values Using IN

The IN o p e r a t o r d e t e r m i n e s if a va lue is c o n t a i n e d in a l ist of va lues .

I I[eJ,~[a~-w, l l :~ . Find the vendor representat ives with last names of Corn or Sherbert

SELECT vendorid, repfname, replname FROM vendors WHERE replname IN ('Corn', 'Sherbert');

The list of v a l u e s m u s t be c o m m a - d e l i m i t e d a n d e n c l o s e d w i th in p a r e n t h e s e s . To e v a l u a t e WHERE x IN ( y l . . . . . yn ) , SQL e v a l u a t e s x = yi for each yi in the list. If a t l eas t o n e x = yi e v a l u a t e s to t rue , the c o n d i t i o n e v a l u a t e s to t rue .

As y o u p r o b a b l y a l r e a d y s u s p e c t , y o u can use NOT IN to find all of the v a l u e s no t c o n t a i n e d in a list.

[0 j : [a~-ar , Pml~a Find the ingredient ID, name, and unit of items not sold in pieces or strips

SELECT ingredientid, name, unit FROM ingredients WHERE unit NOT IN ('piece'. ' s t r i p ' ) ;

2 . !0 IS NULL: Exploring the Unknown

How do we f ind all of the v e n d o r s w h o w e r e no t r e f e r r e d by a n y o t h e r v e n d o r ? We n e e d all of the rows f r o m vendors with a referredby va lue of NULL, b u t beware--NULL

Page 13: Base de Datos Cap. 2

s 2.10 IS NULL: Exploring the Unknown ~ 9

is a s t range animal . SQL in te rp re t s NULL as unknown. If we c o m p a r e s o m e t h i n g tha t is u n k n o w n to any value, even unknown, the resu l t is unknown . This makes sense if you th ink abou t it. Cons ider a new v e n d o r r ep re sen t a t i ve with first name Bob and an u n k n o w n last name. If s o m e b o d y asked you if Bob's last name was Smith, how could you mos t accura te ly answer? That ' s r ightm"I don ' t know." The da t abase equ iva len t is unknown . Our s y s t e m of eva lua t ion now has th ree values: t rue, false, and unknown . It is not su rp r i s ing tha t the s y s t e m for eva lua t ing cond i t ions with three poss ib le resu l t s is called three-valued logic.

How does SQL handle u n k n o w n ? SQL only r epor t s the rows for which the WHERE condi t ion eva lua tes to true. Rows eva lua t ing to false or u n k n o w n are not inc luded in the answer . Compare Quer ies 2.20 and 2.21.

INCORRECT! Find all vendors not referred by anyone

SELECT * FROM vendors WHERE referredby - NULL;

I vendorid ] cornpanyname I repfname ! replnarne I referredby I [0 row(s)]

i i

This query r e tu rns no rows because each eva lua t ion of referredby = NULL r e tu rns unknown, even for the rows where re ferredby is NULL. So how do we tes t for NULL? We use IS NULL.

CORRECT! Find all vendors not referred by anyone

SELECT * FROM vendors WHERE referredby IS NULL,

As you might expect , we can find the rows with non-NULL values us ing the IS NOT NULL.

The impl ica t ions of NULL c o m p a r i s o n can be subtle. The rule to r e m e m b e r is tha t compar ing NULL to an a t t r ibu te (e.g., referredby), a literal (e.g., 'A', 3, etc.), or even to NULL itself a lways r e tu rns unknown. This m e a n s that NULL = NULL eva lua tes to unknown. Of course, NULL IS NULL always eva lua tes to true. Cons ider the Quer ies 2.22 and 2.23.

Page 14: Base de Datos Cap. 2

40 Chapter 2: Retrieval: Basic SELECTion m

[ej,~[~,-m,.~.~.~ Find all items with price greater than $0.99

SELECT * FROM items WHERE price > 0.99:

[ej,.ta~-a,,~,,gg Find all items with price less than or equal to $0.99

SELECT * FROM items WHERE price <- 0.99;

Where 's the Mil lennium Salad? Its price is NULL, so c o m p a r i n g it to $0.99 a lways r e tu rns unknown, which SQL exc ludes f rom both resul ts . In effect, the NULL price for Mil lennium Salad is not less than, g rea te r than, or equal to $0.99. To list this sa lad 's row (and any o ther rows with a price of NULL), we need Query 2.24.

[ejw.[~a~-,w,~,,,l~,. Find all items with no price

SELECT * FROM items WHERE price IS NULL,

The incomparab i l i t y of NULL has o the r impl ica t ions . For example , ne i the r LIKE '%' nor NOT LIKE '%' ma tch NULL. NULL is ne i the r BETWEEN nor NOT BETWEEN any two values , inc luding NULL itself.

Page 15: Base de Datos Cap. 2

n 2.11 ANDs, ORs, NOTs with NULLs: Three-Valued Logic 4 |

2.1 I ANDs, ORs, NOTs with NULLs: Three-Valued Logic

What happens when we AND, OR, or NOT a value of unknown? SQL evaluates condi t ions using three-valued logic. Table 2.2 shows the logic tables for AND, OR, and NOT.

AND t r u e f a l s e u n k n o w n

t rue true false unknown fa lse false false false u n k n o w n unknown false unknown

OR t r u e f a l s e u n k n o w n

t r u e true true true f a l s e true false unknown u n k n o w n true unknown unknown

NOT

t r u e false f a l s e true u n k n o w n unknown

Table 2.2: Three-valued logic tables.

Let's try our three-valued logic tables on an example.

Find the ingredient ID, food group, and inventory for fruits or ingredients with inventory not less than or equal to 200

SELECT ingredientid, foodgroup, inventory FROM ingredients WHERE foodgroup - 'Fru i t ' OR NOT inventory <- 200;

Page 16: Base de Datos Cap. 2

42 Chapter 2: Retrieval: Basic SELECTion i

The resu l t s for rows with n o n - N U L L va lues for b o t h f o o d g r o u p and i n v e n t o r y are no t su rpr i s ing . Let's c o n s i d e r two o the r rows in the i n g r e d i e n t s table: the ones iden t i f i ed by WTRML and SCTDR. First, let 's eva lua te the c o n d i t i o n for the WTRML row f rom i n g r e d i e n t s :

f o o d g r o u p = 'Frui t ' - - t r u e / O R = t r u e

N O T i n v e n t o r y <= 2 0 0 = N O T u n k n o w n - u n k n o w n /

The first cond i t ion , f o o d g r o u p = ' F r u i t ' , e v a l u a t e s t o t r u e . i n v e n t o r y <= 200 e v a l u a t e s to u n k n o w n , and the NOT of u n k n o w n is i tsel f u n k n o w n . For this row, SQL e v a l u a t e s t rue OR u n k n o w n , which is t rue so the WTRML row is inc luded .

Now c o n s i d e r the SCTDR row.

f o o d g r o u p = 'Frui t ' = -- u n k n o w n I OR = u n k n o w n

N O T i n v e n t o r y <= 2 0 0 - N O T t r u e = f a l s e /

f o o d g r o u p = ' F r u i t ' eva lua t e s to u n k n o w n , and NOT i n v e n t o r y <= 200 e v a l u a t e s to false. Because false OR u n k n o w n is u n k n o w n , the SCTDR row is not i nc luded in the r e su l t s of Query 2.25.

2.12 Three-Valued Logic and the IN Operator

Let's c o n s i d e r the impl i ca t ions of t h r e e - v a l u e d logic for the IN ope ra to r . As we 've a l r eady s ta ted , to eva lua te x IN (y l . . . . . yn) , SQL eva lua t e s x = yi for each yi in the list. If at least one x = yi eva lua t e s to t rue, the c o n d i t i o n eva lua t e s to t rue. If all x = yi eva lua t e to false or the IN list is emp ty , the cond i t i on eva lua t e s to false. If ne i t he r of t he se cases holds , t hen the cond i t i on r e t u r n s u n k n o w n .

The cond i t i on x NOT IN (y l . . . . . yn) is e q u iv a l e n t to NOT x IN ( y l . . . . . yn) , l eading to the non in tu i t i ve resul t of Query 2.26.

~ej,~r==~-~w.~,~[~ INCORRECT! Find all of the vendors whose vendor ID is neither BADID or NULl i i i i

SELECT * FROM vendors WHERE vendorid NOT IN ( 'BADID' , NULL);

I vendorid I companyname I repfname I replname ! referredby I [0 row(s)] I II I I II

Why is the resu l t e m p t y ? C o m p a r i n g each v e n d o r i d with BADID r e t u r n s false and with N U L L r e t u r n s unknown ; c o n s e q u e n t l y , IN r e t u r n s u n k n o w n for each row. App ly ing NOT still r e t u r n s u n k n o w n . Because the c o n d i t i o n eva lua t e s to u n k n o w n for all rows, the resu l t

Page 17: Base de Datos Cap. 2

" 2.14 Wrap Up 43

is empty. Clearly, the solut ion is to avoid NULL with the IN operator , but as we will see in later chapters, IN can be used in s i tuat ions where we do not have that much control.

2.13 How WHERE Determines What's In and Out

What de termines which rows are in or out? SQL evaluates the WHERE condi t ion for each row, and a row is only included if the condi t ion evaluates to true for that part icular row. Consider Query 2.2 7.

I I I[ej , [a~-a,~,al ,J Find all items

SELECT * FROM items WHERE 1 - 1:

For each row, 1 = 1 is true, so this query re turns all rows in the items table. If we change the WHERE condi t ion to 1 = 2, our query result will contain no rows because 1 = 2 is always false. Although this part icular example is trivial, it is impor tan t to unders t and how SQL thinks when we consider more complex queries later.

As this example shows, it is perfectly legal in SQL for the condi t ion in the WHERE clause to have nothing to do with the values in the row. SQL does require that any column referenced in the SELECT and WHERE clauses must be an a t t r ibute in a table in the FROM clause; otherwise, SQL returns an error.

2.14 Wrap Up

In this chapter, we've learned how to extract data from a single table. We specify the a t t r ibutes we want in the SELECT list. The rows in the result are de te rmined by the WHERE condit ion. SQL evaluates the WHERE condi t ion for each row, including only those rows in the result where the condit ion evaluates to true. SQL provides a powerful set of compar i son opera tors for use in the WHERE clause to de termine which rows are in and out. We can join individual condit ionals together using AND and OR, and we can negate using NOT. Some at t r ibutes may have a value of NULL, which SQL interprets as unknown. To allow for unknown values, SQL evaluates a condit ional using three-valued logic.

Page 18: Base de Datos Cap. 2

4 4 Chapter 2: Retrieval: Basic SELECTion m

Review Questions

1. The result of a SELECT s ta tement is a new

2. SQL uses -valued logic. The possible values of the sys tem are , , and

3. Does5 BETWEEN 3 AND 5 re turn true or false?

4. NOT unknown AND NOT false OR unknown evaluates to

5. Circle the strings that L I KE ' _ _ s g l ' matches: [ baseball, football, soccer, basket- ball, cricket ]

6. NULL > NULL evaluates to

7. NULL > NULL AND true OR false evaluates to

8. NULL > NULL OR true AND false evaluates to

9. What string matching pat tern matches all s tr ings beginning with 'B' and ending with '11' with exactly one character in between (e.g., 'Bill', 'Ball') ? Same quest ion with one or m o r e characters in be tween

10. Are string compar i sons case sensitive for your database?

11. The easiest way to select all columns in a table is to use

12. Does an at t r ibute in the SELECT clause have to appear in the WHERE clause?

13. Does an at t r ibute in the WHERE clause have to appear in the SELECT clause?

14. Which opera tors are equivalent to NOT >, NOT <=, and NOT <>?

15. In the Restaurant Database, how many rows does the following query return?

SELECT * FROM vendors;

16. Will this query execute?

SELECTnameFROMi ngredi ents,

17. Will this query execute?

SELECT name FROM ingredients

Practice

For these exercises, use the Employees Database presen ted at the end of Chapter 1. For each quest ion, give the single SQL s ta tement to answer it. Your query mus t work for any set of data in the Employees Database, not jus t the set of data we provide.

Page 19: Base de Datos Cap. 2

,, Practice 45

1. List the first and last names of all employees .

2. List all a t t r ibu tes of the projects with revenue grea ter than $40,000.

3. List the d e p a r t m e n t codes of the projects with revenue be tween $100,000 and $150,000.

4. List the project IDs for the projects that s ta r ted on or before July 1, 2004.

5. List the names of the d e p a r t m e n t s that are top level (i.e., not a subdepar tment ) .

6. List the ID and descr ip t ions of the projects under the d e p a r t m e n t s with code ACCNT, CNSLT, or HDWRE.

7. List all of the informat ion about employees with last names that have exact ly 8 characters and end in 'ware'.

8. List the ID and last name of all employees who work for d e p a r t m e n t ACTNG and make less than $30,000.

9. List the "magical" projects that have not s ta r ted ( indicated by a start date in the future or NULL)bu t are genera t ing revenue.

10. List the IDs of the project e i ther from the ACTNG d e p a r t m e n t or that are ongoing (i.e., NULL end date). Exclude any projects that have a revenue of $50,000 or less.

11. Consider the table T with co lumns CI VARCHAR(10), C2 INTEGER and C3 INTEGER and the query

SELECT * FROM T WHERE CI LIKE '%ar_' OR NOT C2 BETWEEN 3 AND 7 AND C3 < 5;

Fill in the miss ing resul ts for the example rows.

i

In r e s u l t ? CI C2 C3 Row V a l u e

No 4 9 false No

Share NULL 5

Car 2 NULL

Sarah 8 1

NULL true Yes