Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Adding Power to Queries

Similar presentations


Presentation on theme: "Chapter 4: Adding Power to Queries"— Presentation transcript:

1 Chapter 4: Adding Power to Queries
SQL for SQL Server Bijoy Bordoloi and Douglas Bock Chapter 4: Adding Power to Queries Prentice Hall © 2004

2 OBJECTIVES Use logical operators (AND, OR, NOT) to write complex query conditions. Use the IN and BETWEEN operators. Use the LIKE operator for character matching. Use the IS NULL operator when querying for unknown values. Use expressions in WHERE clauses. Prentice Hall © 2004

3 LOGICAL OPERATORS (AND, OR, AND NOT)
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. NOT: Negates the expression that follows it. Prentice Hall © 2004

4 AND OPERATOR SELECT emp_last_name, emp_first_name, emp_gender "Gender"
FROM employee WHERE emp_gender = ‘F’ AND emp_last_name >= 'E' ORDER BY emp_last_name; emp_last_name emp_first_name Gender Joyner Suzanne F Markis Marcia F Prescott Sherri F Prentice Hall © 2004

5 AND OPERATOR SELECT CAST(emp_last_name AS Char (12)) "Last Name",
CAST(emp_first_name AS CHAR(2)) "First Name", CAST(emp_date_of_birth AS CHAR(12))"Birth Day", emp_gender "Gender", ‘$’ + CONVERT (CHAR (10), emp_salary, 1) "Salary" FROM employee WHERE emp_last_name > ‘E’AND emp_date_of_birth > ‘20-Jun-71’AND emp_gender = ‘M’ AND emp_salary > 20000 ORDER BY emp_last_name; Last Name First Name Birth Day Gender Salary Joshi Di Sep M $ 38,000.00 Zhu Wa Dec M $ 43,000.00 Prentice Hall © 2004

6 OR OPERATOR SELECT emp_last_name, emp_first_name, emp_gender
FROM employee WHERE emp_gender = 'F' OR emp_last_name >= 'M' ORDER BY emp_last_name; emp_last_name emp_first_name emp_gender Joyner Suzanne F Markis Marcia F Prescott Sherri F Zhu Waiman M Prentice Hall © 2004

7 NOT OPERATOR SELECT emp_last_name, emp_first_name, emp_dpt_number
FROM employee WHERE NOT emp_dpt_number = 7 ORDER BY emp_last_name;  emp_last_name emp_first_name emp_dpt_number Amin Hyder Bordoloi Bijoy Joyner Suzanne Markis Marcia Prentice Hall © 2004

8 Combining OR and AND Operators
When the AND operator is combined with OR, the SQL Server will evaluate the condition connected by the AND first before any conditions connected with OR. Parenthese must be used to force an order of operation. Prentice Hall © 2004

9 Combining OR and AND Operators Contd.
Q? Display a list of employees with a last name that begins with or is greater than the letter ‘E’, and who are either female or works in department number #1. Prentice Hall © 2004

10 Combining OR and AND Operators Contd.
SELECT CAST(emp_last_name AS CHAR(12)) "Last Name", CAST(emp_first_name AS CHAR(12)) "First Name", emp_gender, emp_dpt_number 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 emp_gender emp_dpt_number Bordoloi Bijoy M Joyner Suzanne F Markis Marcia F Prescott Sherri F Is this answer correct? Prentice Hall © 2004

11 Combining OR and AND Operators
SELECT CAST(emp_last_name AS CHAR(12)) "Last Name", CAST(emp_first_name AS CHAR(12)) "First Name", emp_gender, emp_dpt_number 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 emp_gender emp_dpt_number Joyner Suzanne F Markis Marcia F Prescott Sherri F Prentice Hall © 2004

12 LISTS (IN AND NOT IN) There are two operators that are designed for testing to determine if data stored in a table column is either in or not in a list or set of values. 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. Prentice Hall © 2004

13 Using IN Operator SELECT CAST(emp_last_name AS Char (15)) "Last Name",
CAST(emp_first_name AS CHAR(15)) "First Name", CAST(emp_salary AS DECIMAL(10, 2)) "Salary" FROM employee WHERE emp_salary = OR emp_salary = OR emp_salary = 25000 ORDER BY emp_salary; Last Name First Name Salary Amin Hyder Markis Marcia Prescott Sherri Bock Douglas Zhu Waiman Joyner Suzanne Prentice Hall © 2004

14 Using IN Operator Contd.
SELECT CAST(emp_last_name AS Char (15)) "Last Name", CAST(emp_first_name AS CHAR(15)) "First Name", CAST(emp_salary AS DECIMAL(10, 2)) "Salary" FROM employee WHERE emp_salary IN (43000, 30000, 25000) ORDER BY emp_salary; Last Name First Name Salary Amin Hyder Markis Marcia Prescott Sherri Bock Douglas Zhu Waiman Joyner Suzanne Prentice Hall © 2004

15 Using IN Operator Contd.
SELECT CAST(emp_last_name AS Char (15)) "Last Name", CAST(emp_first_name AS CHAR(15)) "First Name", 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 Amin Hyder Marina Joyner Suzanne Marina Bock Douglas St. Louis Zhu Waiman St. Louis Prentice Hall © 2004

16 Using the NOT IN Operator
NOT can precede IN to form negative. To get the list of employees who did not earn the three salary figures listed earlier SELECT CAST(emp_last_name AS Char (15)) "Last Name", CAST(emp_first_name AS CHAR(15)) "First Name", CAST(emp_salary AS DECIMAL(10, 2)) "Salary" FROM employee WHERE emp_salary NOT IN (43000, 30000, 25000) ORDER BY emp_salary; Last Name First Name Salary Joshi Dinesh Bordoloi Bijoy Prentice Hall © 2004

17 RANGES (BETWEEN AND NOT BETWEEN)
SQL provides two operators, BETWEEN and NOT BETWEEN that can simplify 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. Prentice Hall © 2004

18 Using the BETWEEN Operator
The following query uses the AND logical operator. SELECT CAST(emp_last_name AS Char (15)) "Last Name", CAST(emp_first_name AS CHAR(15)) "First Name’’ ‘$’ + Convert(char(10),emp_salary, 1) "Salary" FROM employee WHERE emp_salary >= AND emp_salary <= 40000 ORDER BY emp_salary; Last Name First Name Salary Amin Hyder $ 25,000.00 Markis Marcia $ 25,000.00 Prescott Sherri $ 25,000.00 Bock Douglas $ 30,000.00 Joshi Dinesh $ 38,000.00 Prentice Hall © 2004

19 Using the BETWEEN Operator Contd.
The query can be rewritten using the BETWEEN operator. SELECT CAST(emp_last_name AS Char (15)) "Last Name", CAST(emp_first_name AS CHAR(15)) "First Name", ‘$’+ Convert(char(10),emp_salary, 1) "Salary" FROM employee WHERE emp_salary BETWEEN AND 40000 ORDER BY emp_salary; Last Name First Name Salary Amin Hyder $ 25,000.00 Markis Marcia $ 25,000.00 Prescott Sherri $ 25,000.00 Bock Douglas $ 30,000.00 Joshi Dinesh $ 38,000.00 Prentice Hall © 2004

20 Specifying More Than One Salary Range
SELECT CAST(emp_last_name AS Char (15)) "Last Name", ‘$’ + Convert(char(10),emp_salary, 1) "Salary" FROM employee WHERE emp_salary BETWEEN AND 30000 OR emp_salary BETWEEN AND 43000 ORDER BY emp_salary; Last Name Salary Amin $ 25,000.00 Markis $ 25,000.00 Prescott $ 25,000.00 Bock $ 30,000.00 Zhu $ 43,000.00 Joyner $ 43,000.00 Prentice Hall © 2004

21 Using the NOT BETWEEN Operator
SELECT CAST(emp_last_name AS Char (15)) "Last Name", ‘$’ + Convert(char(10),emp_salary, 1) "Salary" FROM employee WHERE emp_salary NOT BETWEEN AND 50000 ORDER BY emp_salary; Last Name Salary Amin $ 25,000.00 Markis $ 25,000.00 Prescott $ 25,000.00 Bordoloi $ 55,000.00 Prentice Hall © 2004

22 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 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’. Prentice Hall © 2004

23 LIKE AND NOT LIKE Wild card Meaning
% (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] ) Prentice Hall © 2004

24 MORE EXAMPLES 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 ‘_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. Prentice Hall © 2004

25 MORE EXAMPLES Contd. 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[^c]%’ will search for all the names that begin with ‘M’ not having ‘c’ as the second letter. Prentice Hall © 2004

26 MORE EXAMPLES Contd. 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 Prentice Hall © 2004

27 UNKNOWN VALUES (IS NULL and IS NOT NULL)
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. SELECT * FROM assignment WHERE work_hours IS NULL; work_emp_ssn work_pro_number work_hours NULL NULL Prentice Hall © 2004

28 MORE EXAMPLES Contd. SELECT * FROM assignment WHERE work_hours = 0; work_emp_ssn work_pro_number work_hours (0 row(s) affected) 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.” Prentice Hall © 2004

29 MORE EXAMPLES The NOT NULL condition can also be tested.
SELECT * FROM assignment WHERE work_hours IS NOT NULL; (15 row(s) affected) The result table contains all the rows where work_hours column contains a value. Prentice Hall © 2004

30 EXPRESSIONS IN SELECT CLAUSES
An expression is formed by combining a column name or constant with an arithmetic operator. The arithmetic operators used in SQL are SYMBOL OPERATION ORDER * Multiplication 1 / Division % Modulo + Addition 2 - Subtraction Prentice Hall © 2004

31 EXAMPLE Last Name First Name Monthly Salary
SELECT CAST(emp_last_name AS Char (15)) "Last Name", CAST(emp_first_name AS CHAR(15)) "First Name", 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 Joyner Suzanne Zhu Waiman Prentice Hall © 2004

32 EXPRESSIONS The result of an arithmetic operation on NULL is NULL.
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 numeric data. The result of an arithmetic operation on NULL is NULL. Prentice Hall © 2004

33 Examples of Operations on Nulls
SELECT work_emp_ssn "SSN", work_pro_number "Project", work_hours/40 "Avg Hours/Week" FROM assignment WHERE work_pro_number = 1 ORDER BY work_emp_ssn; SSN Project Avg Hours/Week NULL Prentice Hall © 2004

34 Examples of Operations on Nulls Contd. Table: Contract_Employee
emp_id, emp_job emp_salary emp_bonus 10 BIG BOSS 100000 NULL 20 LITTLE BOSS 50000 30 WARRIOR 10000 2000 40 3000 Prentice Hall © 2004

35 Examples of Operations on Nulls Contd.
SELECT emp_id, emp_job, emp_salary+emp_bonus "Total Comp" FROM contract_employee; emp_id emp_job Total Comp BIG BOSS NULL LITTLE BOSS NULL WORKER WORKER Prentice Hall © 2004

36 SUMMARY Logical operators (AND and OR) in the WHERE clause add power to your queries. When the AND operator is combined with OR, AND takes precedence over OR. Always use parenthese to clarify the order of operation. IN AND NOT IN operators are used to compare a column against several values. The BETWEEN operators specifies an inclusive range of values. Use the LIKE operator for incomplete search of character string. Use the IS NULL operator when querying for unknown values. The result of an arithmetic operation on NULL is NULL. Prentice Hall © 2004


Download ppt "Chapter 4: Adding Power to Queries"

Similar presentations


Ads by Google