Presentation is loading. Please wait.

Presentation is loading. Please wait.

Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.

Similar presentations


Presentation on theme: "Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using."— Presentation transcript:

1 Limiting Selected Rows

2 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using the WHERE clause. Enter search criteria using the WHERE clause.

3 2-3 The ORDER BY Clause Sort rows with the ORDER BY clause. ASC – ascending order, default. ASC – ascending order, default. DESC – descending order. DESC – descending order. ORDER BY clause is last in SELECT command. ORDER BY clause is last in SELECT command. SELECT last_name, dept_id, start_date FROM s_emp ORDER BY last_name; SELECT last_name, dept_id, start_date FROM s_emp ORDER BY last_name;

4 2-4 The ORDER BY Clause The default sort order is ascending. The default sort order is ascending. The sort order can be reversed by using DESC. The sort order can be reversed by using DESC. You can sort by expressions or aliases. You can sort by expressions or aliases. SELECT last_name EMPLOYEE, start_date FROM s_emp ORDER BY EMPLOYEE DESC; SELECT last_name EMPLOYEE, start_date FROM s_emp ORDER BY EMPLOYEE DESC; Null values are displayed Null values are displayed –Last for ascending sequences. –First for descending sequences.

5 2-5 Sorting by Multiple Columns SELECT last_name, dept_id, salary FROM s_emp ORDER BY dept_id, salary DESC; SELECT last_name, dept_id, salary FROM s_emp ORDER BY dept_id, salary DESC; The order of ORDER BY list is order of sort. The order of ORDER BY list is order of sort. You can sort by a column that is not in the SELECT list. You can sort by a column that is not in the SELECT list.

6 2-6 Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. The WHERE clause follows the FROM clause. Conditions consist of the following: Conditions consist of the following: – Column name, expression, constant – Comparison operator SELECT last_name, dept_id, salary FROM s_emp WHERE dept_id = 42; SELECT last_name, dept_id, salary FROM s_emp WHERE dept_id = 42;

7 2-7 Character Strings and Dates Character strings and dates are enclosed within single quotation marks. Character strings and dates are enclosed within single quotation marks. Number values are not enclosed within quotation marks. Number values are not enclosed within quotation marks. Character values are case-sensitive. Character values are case-sensitive. The default date format is 'DD-MON-YY'. The default date format is 'DD-MON-YY'. SELECT first_name, last_name, title FROM s_emp WHERE last_name = 'Magee'; SELECT first_name, last_name, title FROM s_emp WHERE last_name = 'Magee';

8 2-8 Comparison and Logical Operators Logical comparison operators Logical comparison operators = > >= >= < <= SQL comparison operators SQL comparison operators – BETWEEN... AND... – IN(list) – LIKE – IS NULL Logical operators Logical operators – AND – OR – NOT

9 2-9 Negating Expressions Sometimes it is easier to exclude rows you know you do not want. Logical Operators Logical Operators != <> ^= SQL Operators SQL Operators – NOT BETWEEN – NOT IN – NOT LIKE – IS NOT NULL

10 2-10 BETWEEN and IN SQL Operators Use the BETWEEN operator to test for values between, and inclusive of, a range of values. Use the BETWEEN operator to test for values between, and inclusive of, a range of values. SELECTid, name, region_id FROM s_dept WHEREregion_id IN (1,3); SELECTid, name, region_id FROM s_dept WHEREregion_id IN (1,3); SELECT first_name, last_name, start_date FROM s_emp WHERE start_date BETWEEN '09-may-91' AND '17-jun-91'; AND '17-jun-91'; SELECT first_name, last_name, start_date FROM s_emp WHERE start_date BETWEEN '09-may-91' AND '17-jun-91'; AND '17-jun-91'; Use IN to test for values in a list. Use IN to test for values in a list.

11 2-11 LIKE SQL Operator You can use the LIKE operator to perform wildcard searches of valid search string values. You can use the LIKE operator to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers. Search conditions can contain either literal characters or numbers. – "%" denotes none or many characters. – "_" denotes one character. SELECTlast_name FROM s_emp WHERElast_name LIKE 'M%'; SELECTlast_name FROM s_emp WHERElast_name LIKE 'M%';

12 2-12 LIKE SQL Operator The LIKE operator can be used as a shortcut for some BETWEEN comparisons. The LIKE operator can be used as a shortcut for some BETWEEN comparisons. You can combine pattern matching characters. You can combine pattern matching characters. You can use the ESCAPE identifier to search for "%" or "_". You can use the ESCAPE identifier to search for "%" or "_". SELECTlast_name FROM s_emp WHERElast_name LIKE '_a%'; SELECTlast_name FROM s_emp WHERElast_name LIKE '_a%'; SELECTlast_name, start_date FROM s_emp WHEREstart_date LIKE '%91'; SELECTlast_name, start_date FROM s_emp WHEREstart_date LIKE '%91';

13 2-13 IS NULL SQL Operator Test for null values with the IS NULL operator. Test for null values with the IS NULL operator. Do not use the = operator. Do not use the = operator. SELECT id, name, credit_rating FROM s_customer WHERE sales_rep_id IS NULL; SELECT id, name, credit_rating FROM s_customer WHERE sales_rep_id IS NULL;

14 2-14 Multiple Conditions Use complex criteria. Use complex criteria. Combine conditions with AND or OR operators. Combine conditions with AND or OR operators. AND requires both conditions to be TRUE. AND requires both conditions to be TRUE. OR requires either condition to be TRUE. OR requires either condition to be TRUE. SELECT last_name, salary, dept_id, title FROM s_emp WHEREdept_id = 41 ANDtitle = 'Stock Clerk'; SELECT last_name, salary, dept_id, title FROM s_emp WHEREdept_id = 41 ANDtitle = 'Stock Clerk'; SELECT last_name, salary, dept_id, title FROM s_emp WHERE dept_id = 41 ORtitle = 'Stock Clerk'; ORtitle = 'Stock Clerk'; SELECT last_name, salary, dept_id, title FROM s_emp WHERE dept_id = 41 ORtitle = 'Stock Clerk'; ORtitle = 'Stock Clerk';

15 2-15 Rules of Precedence Order EvaluatedOperator 1All comparison operators. 2AND 3OR Override rules of precedence by using parentheses.

16 2-16 Display information for those employees in department 44 who earn 1000 or more, and any employees in department 42. Display information for those employees in department 44 who earn 1000 or more, and any employees in department 42. SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND dept_id = 44 OR dept_id = 42; OR dept_id = 42; SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND dept_id = 44 OR dept_id = 42; OR dept_id = 42; Rules of Precedence: Examples SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND (dept_id = 44 OR dept_id = 42); SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND (dept_id = 44 OR dept_id = 42); Display information for those employees in department 44 or 42 who earn 1000 or more.Display information for those employees in department 44 or 42 who earn 1000 or more.


Download ppt "Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using."

Similar presentations


Ads by Google