+ All Categories
Home > Documents > SQL and Conditions Speaker notes will provide additional information!

SQL and Conditions Speaker notes will provide additional information!

Date post: 04-Jan-2016
Category:
Upload: quentin-oliver
View: 215 times
Download: 1 times
Share this document with a friend
25
SQL and Conditions Speaker notes will provide additional information!
Transcript
Page 1: SQL and Conditions Speaker notes will provide additional information!

SQL and Conditions

Speaker notes will provide additional information!

Page 2: SQL and Conditions Speaker notes will provide additional information!

SQL QueriesSQL Queries

A query retrieves specified information from a database. In SQL, the WHERE clause is used to specify the conditions of the query.

For this slide, I will query the payroll table called first_pay that was created in the create presentation.

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

SQL> SELECT * 2 FROM first_pay 3 WHERE jobcode = 'CI';

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10005555 Richard Jones CI 30-OCT-92 50000 2000

The WHERE clause specifies the condition of the query. In this case we want all rows/records where the jobcode = ‘CI’.

NOTE: CI is enclosed in single quotes because it is a string literal.

Page 3: SQL and Conditions Speaker notes will provide additional information!

SQL queriesSQL queriesPAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------4444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

This query has the criteria of bonus = 2000. This means, the developer only wants to see records where this criteria is true.

Note: Because 2000 is a numeric literal, it is not enclosed in quotes.

Page 4: SQL and Conditions Speaker notes will provide additional information!

SQL queriesSQL queriesPAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL> SELECT * 2 FROM first_pay 3 WHERE salary >= 40000;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

All rows/records that meet the criteria of salary greater than or equal to 40000 are shown. This means the record for Susan Ash is not included because her salary is 25000.

Note also that the query could have been done with salary >= 40000.00 since the salary field can contain two decimal places.

Page 5: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL queriesSQL queries

SQL> SELECT pay_id, name, jobcode, salary 2 FROM first_pay 3 WHERE jobcode <= 'CM';

PAY_ NAME JO SALARY---- -------------------- -- ---------1111 Linda Costa CI 450003333 Susan Ash AP 250004444 Stephen York CM 420005555 Richard Jones CI 50000

In this query, only the columns/fields specified in the SELECT are shown.

The condition is to test jobcode which was described as CHAR(2) against the string literal CM. This requires that CM is enclosed in single quotes. The query will show all records where the code is less than CM. This includes AP and CI but note that rows/records with jobcode IN are not displayed.

Page 6: SQL and Conditions Speaker notes will provide additional information!

SQL> SELECT * 2 FROM donor 3 WHERE yrgoal IS NULL;

IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT----- --------------- --------------- ---------- -- ----- --------- --------- ------------22222 Carl Hersey 24 Benefit St Providence RI 02045 03-JAN-98 Susan Jones

IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT----- --------------- --------------- ---------- -- ----- --------- --------- ------------11111 Stephen Daniels 123 Elm St Seekonk MA 02345 03-JUL-98 500 John Smith12121 Jennifer Ames 24 Benefit St Providence RI 02045 24-MAY-97 400 Susan Jones22222 Carl Hersey 24 Benefit St Providence RI 02045 03-JAN-98 Susan Jones23456 Susan Ash 21 Main St Fall River MA 02720 04-MAR-92 100 Amy Costa33333 Nancy Taylor 26 Oak St Fall River MA 02720 04-MAR-92 50 John Adams34567 Robert Brooks 36 Pine St Fall River MA 02720 04-APR-98 50 Amy Costa

SQL queriesSQL queriesThis example looks at a previous discussed table called donor which contains a null value in the third row/record under the column/field YRGOAL.

The IS NULL test will find all rows/records where yrgoal is NULL.

Page 7: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL queriesSQL queries

SQL> SELECT pay_id, name, jobcode, salary 2 FROM first_pay 3 WHERE jobcode > 'CI' AND salary < 45000;

PAY_ NAME JO SALARY---- -------------------- -- ---------2222 John Davidson IN 400004444 Stephen York CM 42000

This query has two criteria in an AND relationship.

Records that meet the criteria jobcode > ‘CI’ have the jobcode shown above in green.

Records that meet the criteria salary < 45000 are shown above in blue.

Only records that meet both criteria are shown below. The last record with the jobcode of IN does not meet the salary criteria so it is not shown below. The third record with the salary of 25000 does not meet the jobcode criteria so it is not shown below. The second and fourth records meet both criteria and therefore are displayed.

Page 8: SQL and Conditions Speaker notes will provide additional information!

AND relationshipAND relationship

JOBCODE > CI

SALARY < 45000

Display the row/record

Logic for: IF JOBCODE > ‘CI’ AND SALARY < 45000 then display the record

Y

YN

N

Page 9: SQL and Conditions Speaker notes will provide additional information!

SQL queries - AND

SQL queries - AND

SQL> SELECT * 2 FROM first_pay 3 WHERE startdate >= '01-JAN-96' AND startdate <= '31-DEC-99';

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10004444 Stephen York CM 03-JUL-97 42000 2000

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

This shows an AND query where both tests are on the same column/field. This query looks for all start dates that are greater than or equal to the first day of 96 AND less than or equal to the last date in 99.

Page 10: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL queriesSQL queries

SQL> SELECT pay_id, name, jobcode, salary 2 FROM first_pay 3 WHERE jobcode > 'CI' OR salary < 45000;

PAY_ NAME JO SALARY---- -------------------- -- ---------2222 John Davidson IN 400003333 Susan Ash AP 250004444 Stephen York CM 420006666 Joanne Brown IN 48000

This query has two criteria in an OR relationship.

Records that meet the criteria jobcode > ‘CI’ have the jobcode shown above in green.

Records that meet the criteria salary < 45000 are shown above in blue.

Records that meet either criteria are displayed below. Record 1 and record 5 do not meet either criteria so they do not display. Record 2 and record 4 meet both criteria so they display. Record 3 meets the salary criteria so it displays. Record 6 meets the jobcode criteria so it displays.

Page 11: SQL and Conditions Speaker notes will provide additional information!

OR relationshipOR relationship

JOBCODE > CI

Logic for: IF JOBCODE > ‘CI’ OR SALARY < 45000 then display the record.

NOTE: If the answer to either question is true the logic goes to the box that displays the row/record.

YN

SALARY < 45000

Display the row/record

YNDisplay the row/record

Page 12: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

This shows an OR query where both tests are on the same column/field. This query looks for all start dates that are greater than or equal to the first day of 96 which will find dates in the 19 hundreds greater than that date OR less than or equal to the specified date in 00 which will find all dates up to the first day of 2000. As you can see, it finds the correct dates from the table.

SQL> SELECT * 2 FROM first_pay 3 WHERE startdate >= '01-JAN-96' OR startdate <= '31-DEC-00';

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 2000

SQL queries - OR

Page 13: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL queries - AND OR

SQL queries - AND OR

The RULE is that AND is resolved before OR. That means that the conditions around the AND are grouped together. In this case, that means that the condition after the OR stands alone.

This code says: bonus = 2000 AND jobcode = ‘IN’ OR just jobcode = ‘CI’. The results are consistent with this interpretation.

NOTE: the second record with bonus = 1500 and jobcode = ‘IN’ did not list while the first record with bonus = 1000 and jobcode = ‘CI’ did list. This is because the second record was controlled by the two conditions with the AND while the first record was controlled by only the one condition after the OR.

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000 AND jobcode = 'IN' OR jobcode = 'CI';

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Page 14: SQL and Conditions Speaker notes will provide additional information!

AND, OR relationship

AND, OR relationship

Bonus = 2000

YN

Jobcode = CI

Display the row/record

YN

Display the row/record

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000 AND jobcode = 'IN' OR jobcode = 'CI';

YJobcode = IN

N

Page 15: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL queries - AND OR

SQL queries - AND OR

The RULE is that AND is resolved before OR. That means that the conditions around the AND are grouped together. If the developer wants to change the order of resolution, parenthesis can be used. Information inside parenthesis is resolved first.

With the parenthesis, the code reads bonus = 2000 AND either jobcode = ‘IN’ OR jobcode = ‘CI’. This means that bonus has to be 2000 in all cases AND the jobcode has to be either IN or CI. Because of the parenthesis, the OR is resolved first and thereby grouped. When combined with the AND we have the requirement that bonus = 2000 AND either of the OR conditions.

The requirement of bonus = 2000 eliminates records 1, 2 and 3. The requirement that jobcode = IN or CI eliminates record 4. Only record 5 and 6 are shown. The results are consistent with this code.

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000 AND (jobcode = 'IN' OR jobcode = 'CI');

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------5555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Page 16: SQL and Conditions Speaker notes will provide additional information!

AND, OR relationship

AND, OR relationship

Bonus = 2000

YN

Display the row/record

YJobcode = IN

N

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000 AND (jobcode = 'IN' OR jobcode = 'CI');

Jobcode = CI

Display the row/record

YN

Page 17: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000 AND (salary >= 50000 OR jobcode = 'IN');

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------5555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

AND, OR relationship

AND, OR relationship

In this example, bonus must be = to 2000 and either salary must be greater than or equal to 50000 or jobcode must be equal to IN. The OR in parenthesis got resolved and combined with the AND to make bonus = 2000 AND either salary >=50000 OR jobcode=‘IN’.

Page 18: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL> SELECT * 2 FROM first_pay 3 WHERE (bonus = 2000 OR bonus = 1000) AND (jobcode = 'CI' OR jobcode = 'CM');

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 2000

AND, OR relationship

AND, OR relationship

In this example the information within the two sets of parenthesis is resolved first and then the results are combined with the AND. Bonus has to be 2000 or 1000 AND jobcode has to be CI or CM. Only results that have the correct bonus combined with the correct jobcode will display.

Records 2 and 3 are eliminated because they do not have one of the correct bonus values. Record 6 is eliminated because it does not have one of the correct jobcode values.

Page 19: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

AND, OR relationships

AND, OR relationships

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000 AND jobcode = 'CI' OR bonus = 500 AND jobcode = 'AP';

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------3333 Susan Ash AP 05-FEB-00 25000 5005555 Richard Jones CI 30-OCT-92 50000 2000

In this example I want bonus to be 2000 and jobcode ‘CI’ OR I want bonus to be 500 and jobcode ‘AP’. Because ANDs get resolved before ORs, no parenthesis are required. The bonus =2000 and jobcode = CI are grouped when the first AND is resolved and the bonus = 500 and jobcode = AP are grouped when the second AND is resolved. The OR then asks if the first group is true or the second group is true. If either group is true the results get displayed.

Page 20: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL queries - NOT

SQL queries - NOT

SQL> SELECT * 2 FROM first_pay 3 WHERE NOT bonus = 2000 AND salary > 40000;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 1000

This is looking for NOT bonus = 2000 and it is also looking for salary > 45000.

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = 2000 AND NOT salary > 45000;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------4444 Stephen York CM 03-JUL-97 42000 2000

This is looking for bonus = 2000 and a salary that is NOT greater than 45000.

Page 21: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

SQL queriesSQL queries

SQL> SELECT * 2 FROM first_pay 3 WHERE NOT (bonus =2000 AND salary > 40000);

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 500

This is looking for NOT the combination of bonus = 2000 and salary > 40000. Three records meet this criteria.

SQL> SELECT * 2 FROM first_pay 3 WHERE NOT (bonus = 2000 AND salary > 45000);

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 2000

The 2 records that do not display have a bonus = 2000 AND a salary > 45000. The records that display do NOT meet that criteria.

Page 22: SQL and Conditions Speaker notes will provide additional information!

SQL queriesSQL queries

Summary: The rules for working with NOT, AND, OR

•NOT is resolved first

•AND is resolved next

•OR is resolved last

•Parenthesis can be used to change the order because things within parenthesis are resolved before things that are not in parenthesis.

Page 23: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

betweenbetween

SQL> SELECT * 2 FROM first_pay 3 WHERE salary BETWEEN 40000 AND 50000;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

The BETWEEN operator allows the developer to ask for records between two points, therefore asking for a range. NOTE: The between is inclusive meaning it includes the two end points that are specified as meeting the criteria.

Page 24: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

inin

SQL> SELECT * 2 FROM first_pay 3 WHERE startdate IN ('03-JUL-97', '05-FEB-00', '30-OCT-92');

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------3333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 2000

SQL> SELECT * 2 FROM first_pay 3 WHERE bonus IN (1000, 2000);

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Two examples of the IN clause. In both cases only records that match the specification in the IN are displayed.

Page 25: SQL and Conditions Speaker notes will provide additional information!

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

Table being used is first_pay.

likelike

SQL> SELECT * 2 FROM first_pay 3 WHERE name LIKE 'Jo%';

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------2222 John Davidson IN 25-SEP-92 40000 15006666 Joanne Brown IN 18-AUG-94 48000 2000

In this example, I am looking for names starting with Jo. I do not care what the rest of the letters are, so I use the %. % means any number of letters can follow.

SQL> SELECT * 2 FROM first_pay 3 WHERE startdate LIKE '___J%';

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10004444 Stephen York CM 03-JUL-97 42000 2000

In this case I put the underscore 3 times followed by the J and the %. This indicates that specifically I am skipping 3 characters, then I want a J and then I don’t care how many characters follow.


Recommended