Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14.

Similar presentations


Presentation on theme: "Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14."— Presentation transcript:

1 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

2 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-2 Agenda Assignment 5 CorrectedAssignment 5 Corrected –1 A, 1 B, 3 C’s and 1 D –Lots of missing data Assignment 6 PostedAssignment 6 Posted –Due March 22 Capstone Progress Reports Over DueCapstone Progress Reports Over Due –Missing 3 Some hands on work from chapter 2Some hands on work from chapter 2 –SQL Coding Exercises and Question on page 44 Today we will discusToday we will discus –Single Table Query Basics (Chap 3) –Adding Power to Queries (Chap 4) –Lots of Hands-on stuff We will be in the Oracle SQL text for the next 2+ weeksWe will be in the Oracle SQL text for the next 2+ weeks

3 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-3 Alternate Web-Based Admin (fixed) http://littleblack.umfk.maine.edu:NNNN/em Where NNNN is Buddy = 5501 Craig = 5502 Fred = 5503 G2 = 5504 Dawn = 5505 Rob = 5506 Ray = 5507 http://littleblack.umfk.maine.edu:1158/em/

4 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-4 CHAPTER 3: SINGLE TABLE QUERY BASICS

5 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-5 SIMPLE SELECT STATEMENTS The main element in an SQL query is the SELECT statement. The main element in an SQL query is the SELECT statement. A properly written SELECT statement will always produce a result in the form of one or more rows of output. A properly written SELECT statement will always produce a result in the form of one or more rows of output. The SELECT statement chooses (selects) rows from one or more tables according to specific criteria. The SELECT statement chooses (selects) rows from one or more tables according to specific criteria.

6 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-6 Example SELECT * FROM employee; EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_MIDDLE_NAME ------------ ------------------------ ------------------------- --------------------------- 999666666 Bordoloi Bijoy 999555555 Joyner Suzanne A 999444444 Zhu Waiman Z more rows and columns will be displayed This query selects rows from the “employee” table. This query selects rows from the “employee” table. The asterisk (*) tells Oracle to select (display) all columns contained in the table “employee.” The asterisk (*) tells Oracle to select (display) all columns contained in the table “employee.”

7 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-7 Example The following SELECT statement produces an identical output.The following SELECT statement produces an identical output. SELECT emp_ssn, emp_last_name, emp_first_name, emp_middle_name, emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth, emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth, emp_salary, emp_parking_space, emp_gender, emp_salary, emp_parking_space, emp_gender, emp_dpt_number, emp_superssn emp_dpt_number, emp_superssn FROM employee;

8 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-8 Oracle will process the query regardless of whether you type the entire query on one line, or indent.Oracle will process the query regardless of whether you type the entire query on one line, or indent. There are no rules about how many words can be put on a line or where to break a line.There are no rules about how many words can be put on a line or where to break a line. Although Oracle does not require indenting, indenting enhances readability.Although Oracle does not require indenting, indenting enhances readability. Indenting SQL Code

9 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-9 The following keywords are your signal to start a new line.The following keywords are your signal to start a new line. »SELECT »FROM »WHERE »GROUP BY »HAVING »ORDER BY Indenting SQL Code

10 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-10 Specify the column names to be displayed in the result set by typing the exact, complete column names.Specify the column names to be displayed in the result set by typing the exact, complete column names. Separate each column name with a comma (,).Separate each column name with a comma (,). Specify the name of the table after the FROM clause.Specify the name of the table after the FROM clause. Terminate the query with a semicolon (;).Terminate the query with a semicolon (;). SELECT emp_ssn, emp_last_name, emp_first_name FROM employee; Selecting Specific Columns

11 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-11 Using Column Commands IF the result out put is “too long” to viewIF the result out put is “too long” to view –SET WRAP OFF; Prevent text wrappingPrevent text wrapping –COLUMN FORMAT A15 15 characters wide15 characters wide –COLUMN FORMAT 99999.99 –COLUMN FORMAT $99,999.9

12 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-12 COLUMN emp_salary FORMAT 99999.99; select emp_salary from employee; COLUMN emp_salary FORMAT $99,999.99; select emp_salary from employee; EMP_SALARY ---------- 55000.00 43000.00 25000.00 30000.00 38000.00 25000.00 8 rows selected. EMP_SALARY ----------- $55,000.00 $43,000.00 $25,000.00 $30,000.00 $38,000.00 $25,000.00 8 rows selected.

13 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-13 There are syntactical rules that must be followed or Oracle gives an error message instead of the desired result table.There are syntactical rules that must be followed or Oracle gives an error message instead of the desired result table. The Oracle relational database management system communicates errors in SELECT statements by providing unique error numbers and accompanying error descriptions.The Oracle relational database management system communicates errors in SELECT statements by providing unique error numbers and accompanying error descriptions. Common Errors

14 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-14 Invalid Column Name In this SELECT statement, the employee social security number column name is spelled incorrectly.In this SELECT statement, the employee social security number column name is spelled incorrectly. SELECT emp_socsecno FROM employee; ERROR at line 1: ORA-00904: invalid column name

15 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-15 FROM Keyword Missing The next SELECT statement is missing the FROM clause so that no table name has been specified.The next SELECT statement is missing the FROM clause so that no table name has been specified. Without a table name, the database management system does not know which table to query.Without a table name, the database management system does not know which table to query. SELECT emp_ssn; ERROR at line 1: ORA-00923: FROM keyword not found where expected

16 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-16 THE DISTINCT CLAUSE Oracle provides a means for eliminating duplicate rows in a result table through use of the DISTINCT keyword.Oracle provides a means for eliminating duplicate rows in a result table through use of the DISTINCT keyword. SELECT emp_salary FROM employee; EMP_SALARY----------------- $55,000.00 $55,000.00 $43,000.00 $43,000.00 $25,000.00 $25,000.00 $30,000.00 $30,000.00 $38,000.00 $38,000.00 $25,000.00 $25,000.00 8 rows selected.

17 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-17DISTINCT The query is rewritten using the DISTINCT keyword to eliminate duplicate rows.The query is rewritten using the DISTINCT keyword to eliminate duplicate rows. SELECT DISTINCT emp_salary FROM employee; EMP_SALARY------------------- $25,000.00 $25,000.00 $30,000.00 $30,000.00 $38,000.00 $38,000.00 $43,000.00 $43,000.00 $55,000.00 $55,000.00

18 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-18 THE WHERE CLAUSE THE WHERE CLAUSE Specific rows can be selected by adding a WHERE clause to the SELECT query.Specific rows can be selected by adding a WHERE clause to the SELECT query. SELECT emp_ssn, emp_last_name, emp_first_name, emp_salary FROM employee WHERE emp_salary >= 35000; EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_SALARY -------------- -------------------------- --------------------------- ------------------- 999666666 Bordoloi Bijoy $55,000.00 999555555 Joyner Suzanne $43,000.00 999444444 Zhu Waiman $43,000.00 more rows will be displayed…

19 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-19 Comparison Operators Operator Meaning =equal to =equal to <less than <less than >greater than >greater than >=greater than or equal to >=greater than or equal to <=less than or equal to <=less than or equal to !=not equal to !=not equal to <>not equal to <>not equal to !>not greater than !>not greater than !<not less than !<not less than

20 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-20 Comparing Character Data Comparing Character Data Comparison operators are not limited to numeric data.Comparison operators are not limited to numeric data. They can also be used with columns containing character data.They can also be used with columns containing character data. If the value is a character string or date, you must surround the value (string of characters) with which a column is being compared with single quotation (' ') marks.If the value is a character string or date, you must surround the value (string of characters) with which a column is being compared with single quotation (' ') marks. SELECT emp_ssn, emp_last_name, emp_first_name FROM employee WHERE emp_gender = 'M';

21 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-21 Comparing Character Data Comparing Character Data SELECT emp_ssn, emp_last_name, emp_first_name FROM employee WHERE emp_gender = M; ERROR at line 3: ORA-00904: invalid column name Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a column name.Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a column name. There is no column named M in the table so an error was returned.There is no column named M in the table so an error was returned.

22 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-22 THE ORDER BY CLAUSE Output from a SELECT statement can be sorted by using the optional ORDER BY clause.Output from a SELECT statement can be sorted by using the optional ORDER BY clause. SELECT emp_last_name, emp_first_name FROM employee WHERE emp_last_name >= 'J' ORDER BY emp_last_name; EMP_LAST_NAME EMP_FIRST_NAME ------------------------- -------------------------- Joshi Dinesh Joyner Suzanne Markis Marcia more rows will be displayed …

23 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-23 ORDER BY With ASC and DESC To sort columns from high to low, or descending, an optional keyword DESC must be specified.To sort columns from high to low, or descending, an optional keyword DESC must be specified.  ASC - Ascending, low to high.  DESC - Descending, high to low. When ASC or DESC is used, it must be followed by the column name.When ASC or DESC is used, it must be followed by the column name.

24 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-24 ORDER BY With More Than One Column Sorting by multiple columns improves the look and usability of the information.Sorting by multiple columns improves the look and usability of the information. SELECT emp_dpt_number, emp_last_name, emp_first_name FROM employee ORDER BY emp_dpt_number, emp_last_name; EMP_DPT_NUMBER EMP_LAST_NAME EMP_FIRST_NAME ---------------------------- -------------------------- --------------- 1 Bordoloi Bijoy 1 Bordoloi Bijoy 3 Amin Hyder 3 Amin Hyder 3 Joyner Suzanne 3 Joyner Suzanne 3 Markis Marcia 3 Markis Marcia 7 Bock Douglas 7 Bock Douglas 7 Joshi Dinesh 7 Joshi Dinesh 7 Prescott Sherri 7 Prescott Sherri 7 Zhu Waiman 7 Zhu Waiman

25 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-25 CHAPTER 4: ADDING POWER TO QUERIES

26 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-26 LOGICAL OPERATORS (AND, OR, AND NOT) AND: Joins two or more conditions, and returns results only when all of the conditions are true.AND: Joins two or more conditions, and returns results only when all of the conditions are true. OR: Joins two or more conditions, and returns results when any of the conditions are true.OR: Joins two or more conditions, and returns results when any of the conditions are true. NOT: Negates the expression that follows it.NOT: Negates the expression that follows it.

27 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-27 AND OPERATOR Last Name First Name Gender ---------- ------------- ------ Joyner Suzanne F Markis Marcia F Prescott Sherri F

28 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-28 OR OPERATOR SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_gender "Gender" FROM employee WHERE emp_gender = 'F' OR emp_last_name > 'M' ORDER BY emp_last_name; Last Name First Name Gender --------------- --------------- ------ Joyner Suzanne F Markis Marcia F Prescott Sherri F Zhu Waiman M

29 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-29 NOT OPERATOR SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_dpt_number "Dept" FROM employee WHERE NOT emp_dpt_number = 7 ORDER BY emp_last_name; Last Name First Name Dept --------------- --------------- ----- Amin Hyder 3 Bordoloi Bijoy 1 Joyner Suzanne 3 Markis Marcia 3

30 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-30 Combining OR and AND Operators When the AND operator is combined with OR, the Oracle server will evaluate the condition connected by the AND first before any conditions connected with OR.When the AND operator is combined with OR, the Oracle server will evaluate the condition connected by the AND first before any conditions connected with OR. Parenthesis must be used to force an order of operation.Parenthesis must be used to force an order of operation.

31 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-31 Combining OR and AND Operators SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_dpt_number "Dept", emp_gender "Gender" FROM employee WHERE emp_last_name > 'E' AND emp_gender = 'F‘ OR emp_dpt_number = 1 ORDER BY emp_last_name; Last Name First Name Dept Gender --------------- -------------- ----- ------ Bordoloi Bijoy 1 M Joyner Suzanne 3 F Markis Marcia 3 F Prescott Sherri 7 F

32 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-32 Combining OR and AND Operators SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_dpt_number "Dept", emp_gender "Gender" FROM employee WHERE emp_last_name > 'E' AND (emp_gender = 'F' OR emp_dpt_number = 1) (emp_gender = 'F' OR emp_dpt_number = 1) ORDER BY emp_last_name; Last Name First Name Dept Gender --------------- --------------- ----- ------ Joyner Suzanne 3 F Markis Marcia 3 F Prescott Sherri 7 F

33 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-33 LISTS (IN and NOT IN) There are two operators that are designed for testing to determine if data stored in a table column are either in or not in a list or set of values.There are two operators that are designed for testing to determine if data stored in a table column are either in or not in a list or set of values. These are the IN and NOT IN operators.These are the IN and NOT IN operators. These operators greatly simplify the task of writing queries that might otherwise require a large number of either OR logical operators or an unwieldy use of the NOT logical operator.These operators greatly simplify the task of writing queries that might otherwise require a large number of either OR logical operators or an unwieldy use of the NOT logical operator.

34 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-34 Using IN Operator SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" emp_salary "Salary" FROM employee WHERE emp_salary = 43000 OR emp_salary = 30000 OR emp_salary = 25000 ORDER BY emp_salary; Last Name First Name Salary --------------- --------------- ----------- Markis Marcia $25,000.00 Amin Hyder $25,000.00 Prescott Sherri $25,000.00 Bock Douglas $30,000.00 Joyner Suzanne $43,000.00 Zhu Waiman $43,000.00 6 rows selected.

35 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-35 Using IN Operator SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" FROM employee WHERE emp_salary IN (43000, 30000, 25000) ORDER BY emp_salary; Last Name First Name Salary --------------- --------------- ----------- Markis Marcia $25,000.00 Amin Hyder $25,000.00 Prescott Sherri $25,000.00 Bock Douglas $30,000.00 Joyner Suzanne $43,000.00 Zhu Waiman $43,000.00 6 rows selected.

36 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-36 Using IN Operator SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_city "City" emp_city "City" FROM employee WHERE emp_city IN ('Marina', 'Edwardsville', 'St. Louis') ORDER BY emp_city; Last Name First Name City --------------- --------------- ------------- Bordoloi Bijoy Edwardsville Prescott Sherri Edwardsville Joyner Suzanne Marina Amin Hyder Marina Zhu Waiman St. Louis Bock Douglas St. Louis

37 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-37 Using the NOT IN Operator NOT can precede IN to form negative.NOT can precede IN to form negative. To get the list of employees who did not earn the three salary figures listed above:To get the list of employees who did not earn the three salary figures listed above: SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" emp_salary "Salary" FROM employee WHERE emp_salary NOT IN (43000, 30000, 25000) ORDER BY emp_salary; Last Name First Name Salary --------------- --------------- ----------- Joshi Dinesh $38,000.00 Bordoloi Bijoy $55,000.00

38 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-38 RANGES (BETWEEN and NOT BETWEEN) Oracle provides two operators, BETWEEN and NOT BETWEEN that can simply the range of values in a query.Oracle provides two operators, BETWEEN and NOT BETWEEN that can simply the range of values in a query. This eliminates the need to use a more complex WHERE clause involving the use of the AND logical operator.This eliminates the need to use a more complex WHERE clause involving the use of the AND logical operator.

39 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-39 Using the BETWEEN Operator The following query uses the AND logical operator.The following query uses the AND logical operator. SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" emp_salary "Salary" FROM employee WHERE emp_salary >= 25000 AND emp_salary = 25000 AND emp_salary <= 40000 ORDER BY emp_salary; Last Name First Name Salary -------------- --------------- ----------- Markis Marcia $25,000.00 Amin Hyder $25,000.00 Prescott Sherri $25,000.00 Bock Douglas $30,000.00 Joshi Dinesh $38,000.00

40 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-40 Using the BETWEEN Operator The query can be rewritten using the BETWEEN operator.The query can be rewritten using the BETWEEN operator. SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" emp_salary "Salary" FROM employee WHERE emp_salary BETWEEN 25000 AND 40000 ORDER BY emp_salary; Last Name First Name Salary --------------- --------------- ----------- Markis Marcia $25,000.00 Amin Hyder $25,000.00 Prescott Sherri $25,000.00 Bock Douglas $30,000.00 Joshi Dinesh $38,000.00

41 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-41 Using the NOT BETWEEN Operator SELECT emp_last_name "Last Name", emp_salary "Salary" FROM employee WHERE emp_salary NOT BETWEEN 28000 AND 50000 ORDER BY emp_salary; Last Name Salary --------------- ----------- Markis $25,000.00 Amin $25,000.00 Prescott $25,000.00 Bordoloi $55,000.00

42 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-42 LIKE and NOT LIKE The LIKE and NOT LIKE operators can be used to search for data rows containing incomplete or partial character strings within a data column.The LIKE and NOT LIKE operators can be used to search for data rows containing incomplete or partial character strings within a data column. The next query searches the employee table for employee names that begin with the characters 'Bo'.The next query searches the employee table for employee names that begin with the characters 'Bo'. The search is case-sensitive meaning that 'Bo' is not equivalent to 'BO'.The search is case-sensitive meaning that 'Bo' is not equivalent to 'BO'.

43 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-43 LIKE and NOT LIKE SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_last_name LIKE 'Bo%'; Last Name First Name ------------- ------------- Bordoloi Bijoy Bock Douglas

44 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-44 LIKE and NOT LIKE Wild cardMeaning % (percent) any string of zero or more characters _ (underscore)any single character [ ] (brackets)any single character within a specified range such as 'a' to 'd', inclusive [a-d] or a set of characters such as [aeiouy] [^] (not brackets)any single character not in the specified range or set. (e.g., [^a-f] )

45 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-45 MORE EXAMPLES LIKE '%inger' will search for every name that ends with 'inger' (Ringer, Stringer).LIKE '%inger' will search for every name that ends with 'inger' (Ringer, Stringer). LIKE '%en%' will search for every name that has the letters 'en' in the name (Bennet, Green, McBadden).LIKE '%en%' will search for every name that has the letters 'en' in the name (Bennet, Green, McBadden). LIKE '_heryl' will search for every six-letter name ending with 'heryl' (Cheryl). Notice how this is different than '%heryl' which would return names that are six characters or more.LIKE '_heryl' will search for every six-letter name ending with 'heryl' (Cheryl). Notice how this is different than '%heryl' which would return names that are six characters or more.

46 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-46 MORE EXAMPLES LIKE '[CK]ars[eo]n' will search for every six-letter name that begins with a 'C' or 'K' and has the letter 'e' or 'o' between 'ars' and 'n' (e.g., 'Carsen,' 'Karsen,' 'Carson,' and 'Karson'.LIKE '[CK]ars[eo]n' will search for every six-letter name that begins with a 'C' or 'K' and has the letter 'e' or 'o' between 'ars' and 'n' (e.g., 'Carsen,' 'Karsen,' 'Carson,' and 'Karson'. LIKE '[M-Z]inger' will search for all the names ending with 'inger' that begin with any single letter 'M' thru 'Z' (Ringer).LIKE '[M-Z]inger' will search for all the names ending with 'inger' that begin with any single letter 'M' thru 'Z' (Ringer). LIKE 'M[^c]%' will search for all the names that begin with 'M' not having 'c' as the second letter.LIKE 'M[^c]%' will search for all the names that begin with 'M' not having 'c' as the second letter.

47 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-47 MORE EXAMPLES The SELECT statement shown below generates a result table that includes all DISTINCT rows where the employee social security number in the assignment table ends with the numbers 555.The SELECT statement shown below generates a result table that includes all DISTINCT rows where the employee social security number in the assignment table ends with the numbers 555. SELECT DISTINCT work_emp_ssn "Emp SSN" FROM assignment WHERE work_emp_ssn LIKE '%555'; Emp SSN ------------999555555

48 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-48 MORE EXAMPLES ExpressionResult LIKE ‘5%’ Returns any row where the column data value is '5' followed by any string of zero or more characters. LIKE ‘5[%]’ Returns any row where the data value is 5%. LIKE ‘_n’ Returns any row where the column data value is a two-character value ending in the letter 'n', e.g., an, in, on, etc. LIKE ‘[ _ ]n’ Returns any row where the column data value is _n. LIKE ‘[ ] ]’ Returns any row where the column data value is ].

49 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-49 UNKNOWN VALUES (IS NULL and IS NOT NULL) NULL value is not synonymous with "zero" (numerical values) or "blank" (character values).NULL value is not synonymous with "zero" (numerical values) or "blank" (character values). NULL values allow users to distinguish between a deliberate entry of zero/blank and a non-entry of data.NULL values allow users to distinguish between a deliberate entry of zero/blank and a non-entry of data. SELECT * FROM assignment WHERE work_hours IS NULL; WORK_EMP_ WORK_PRO_NUMBER WORK_HOURS --------- --------------- ---------- 999444444 1 999666666 20

50 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-50 MORE EXAMPLES SELECT * FROM assignment WHERE work_hours = 0; no rows selected The query did not return a result table because none of the rows in the assignment table have a zero value for work_hours. Thus, you can see that zero (0) is a value, not an "unknown value."The query did not return a result table because none of the rows in the assignment table have a zero value for work_hours. Thus, you can see that zero (0) is a value, not an "unknown value."

51 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-51 MORE EXAMPLES The NOT NULL condition can also be tested.The NOT NULL condition can also be tested. SELECT * FROM assignment WHERE work_hours IS NOT NULL; 15 rows selected. The result table contains all the rows where work_hours column contains a value.The result table contains all the rows where work_hours column contains a value.

52 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-52 EXPRESSIONS IN SELECT CLAUSES An expression is formed by combining a column name or constant with an arithmetic operator.An expression is formed by combining a column name or constant with an arithmetic operator. The arithmetic operators used in SQL areThe arithmetic operators used in SQL are SYMBOLOPERATIONORDER *Multiplication1 /Division1 %Modulo1 +Addition2 -Subtraction2

53 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-53 MORE EXAMPLES SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary/12 "Monthly Salary" emp_salary/12 "Monthly Salary" FROM employee WHERE emp_salary/12 > 3500 ORDER BY emp_last_name; Last Name First Name Monthly Salary --------------- --------------- -------------- Bordoloi Bijoy $4,583.33 Joyner Suzanne $3,583.33 Zhu Waiman $3,583.33 Zhu Waiman $3,583.33

54 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-54 EXPRESSIONS When expression is used in select with a column name it has no effect on the table’s underlying values.When expression is used in select with a column name it has no effect on the table’s underlying values. The data contained in each column must be defined as int, smallint, tinyint, float, money, and smallmoney.The data contained in each column must be defined as int, smallint, tinyint, float, money, and smallmoney. The result of an arithmetic operation on NULL is NULL.The result of an arithmetic operation on NULL is NULL.


Download ppt "Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14."

Similar presentations


Ads by Google