Tài liệu limiting selected rows pptx

40 299 0
Tài liệu limiting selected rows pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Limiting Selected Rows 2 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder2Ć2 Limiting Selected Rows 2Ć3 Objectives While retrieving data from the database, you may need to restrict the rows of data that are displayed or specify the order in which the rows are displayed. This lesson explains the commands you will use to perform these actions. At the end of this lesson you should be able to D Sort row output using the ORDER BY clause. D Enter search criteria using the WHERE clause. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder2Ć4 If the ORDER BY clause is not used, the sort order is undefined, and the Oracle7 Server may not fetch rows in the same order for the same query twice. Use the ORDER BY clause to display the rows in a specific order. Limiting Selected Rows 2Ć5 Ordering Rows with the ORDER BY Clause The order of rows returned in a query result is undefined. The ORDER BY clause may be used to sort the rows. If used, you must place the ORDER BY clause last. You can specify an expression or use position to sort. Syntax SELECT expr FROM table [ORDER BY {column,expr} [ASC|DESC]]; where: ORDER BY specifies the order in which the retrieved rows are displayed. ASC orders the rows in ascending order. This is the default order. DESC orders the rows in descending order. Example Query the employee table for employee last name, department number, and the hire date for all employees. Sort the results by employee last name. SQL> SELECT last_name, dept_id, start_date 2 FROM s_emp 3 ORDER BY last_name; LAST_NAME DEPT_ID START_DAT ------------ ------- --------- Biri 43 07-APR-90 Catchpole 44 09-FEB-92 Chang 44 30-NOV-90 Dancs 45 17-MAR-91 Dumas 35 09-OCT-91 Giljum 32 18-JAN-92 Havel 45 27-FEB-91 . Introduction to Oracle: SQL and PL/SQL Using Procedure Builder2Ć6 Limiting Selected Rows 2Ć7 Ordering Rows with the ORDER BY Clause continued Default Ordering of Data The default sort order is ascending: D Numeric values are displayed with the lowest values first, for example 1-999. D Date values are displayed with the earliest value first, for example 01-JAN-92 before 01-JAN-95. D Character values are displayed in alphabetical order, for example A first and Z last. D In Oracle7, null values are displayed last for ascending sequences and first for descending sequences. Reversing the Default Order To reverse the order in which rows are displayed, the command word DESC is specified after the column name in the ORDER BY clause. Example Query the employee table for employee last name, department number, and the hire date for all employees. Sort the results by the most recently hired employee. SQL> SELECT last_name, dept_id, start_date 2 FROM s_emp 3 ORDER BY start_date DESC; LAST_NAME DEPT_ID START_DAT ------------ ------- --------- . Urguhart 41 18-JAN-91 Chang 44 30-NOV-90 Patel 34 17-OCT-90 Menchu 42 14-MAY-90 . 25 rows selected. Ordering with Column Aliases You can use a column alias in the ORDER BY clause. This feature was made available in Oracle7 release 7.0.16. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder2Ć8 Limiting Selected Rows 2Ć9 Ordering Rows with the ORDER BY Clause continued Ordering by Position Another method for sorting query results is to sort by position. This is especially useful when sorting by a long expression. Rather than typing the expression again, you can specify its position in the SELECT list. SQL> SELECT last_name, salary*12 2 FROM s_emp 3 ORDER BY 2; Ordering by Many Columns You can sort query results by more than one column. The sort limit is the number of columns in the table. In the ORDER BY clause, specify the columns, and separate the column names using commas. If you want to reverse the order of a column, specify DESC after its name or position. You can order by columns that are not in the SELECT list. Example Display the last name, department number, and salary of all employees. Order the result by the department number, then in descending order by salary. SQL> SELECT last name, dept_id, salary 2 FROM s_emp 3 ORDER BY dept_id, salary DESC; LAST_NAME DEPT_ID SALARY ------------ ------- ---------- Quick-To-See 10 1450 Nagayama 31 1400 Magee 31 1400 Giljum 32 1490 Sedeghi 33 1515 Nguyen 34 1525 Patel 34 795 . Introduction to Oracle: SQL and PL/SQL Using Procedure Builder2Ć10 [...]... selected columns a different heading FROM table specifies the table containing the columns WHERE restricts the query to rows that meet a condition condition is composed of column names, expressions, constants, and comparison operators ORDER BY specifies the order in which the retrieved rows are displayed ASC orders rows in ascending order DESC orders the rows in descending order Limiting Selected Rows. .. no rows selected SQL> SELECT 2 FROM 3 WHERE id, name, credit_rating s_customer sales_rep_id = ’’; no rows selected SQL> SELECT 2 FROM 3 WHERE id, name, credit_rating s_customer sales_rep_id IS NULL; ID NAME CREDIT_RA - 207 Sweet Rock Sports GOOD Example Display all employee last names, titles, and commission percentages who make a commission SQL> SELECT 2 FROM 3 WHERE Limiting Selected. . .Limiting Selected Rows with the WHERE Clause You can restrict the rows returned from the query by using the WHERE clause A WHERE clause contains a condition that must be met, and directly follows the FROM clause Syntax SELECT FROM [WHERE [ORDER BY expr table condition(s)] expr]; where: WHERE restricts the query to rows that meet a condition condition is composed... (dept_id = 44 dept_id = 42); LAST_NAME SALARY DEPT_ID -Menchu 1250 42 Catchpole 1300 44 Nozaki 1200 42 Limiting Selected Rows 2Ć31 2Ć32 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary In this lesson, you have learned about sorting rows and restricting the rows returned by the SELECT statement You have also learned how to implement comparison operators Syntax SELECT FROM... last_name, title s_emp last_name = ’MAGEE’; no rows selected All character strings are case sensitive Therefore, change the last name to be initial capitals in order to acquire a match SQL> SELECT 2 FROM 3 WHERE first_name, last_name, title s_emp last_name = ’Magee’; FIRST_NAME LAST_NAME TITLE - Colin Magee Sales Representative Limiting Selected Rows 2Ć13 2Ć14 Introduction to Oracle:... clause and the ORDER BY clause Practice Contents D Selecting data and changing the order of rows displayed Using the WHERE clause to restrict rows, with a combination of logical and SQL operators D Using column aliases D PaperĆBased Questions For questions 1–3, circle either True or False Limiting Selected Rows 2Ć35 2Ć36 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder ... OR last_name, salary, dept_id, title s_emp dept_id = 41 title = ’Stock Clerk’; Note: OR is a less restrictive clause Consequently, more rows may be returned For more information, see Oracle7 Server SQL Reference, Release 7.3, “Logical Operators.” Limiting Selected Rows 2Ć27 Order Evaluated Operator 1 2 AND 3 2Ć28 All comparison operators OR Introduction to Oracle: SQL and PL/SQL Using Procedure Builder... Note that an error is not raised, the result is simply always FALSE Limiting Selected Rows 2Ć17 SQL> SELECT 2 FROM 3 WHERE 4 2Ć18 first_name, last_name, start_date s_emp start_date BETWEEN ’09-may-91’ AND ’17-jun-91’; Introduction to Oracle: SQL and PL/SQL Using Procedure Builder SQL Operators The BETWEEN Operator You can display rows based on a range of values using the BETWEEN operator The range... in the list, they must be enclosed in single quotation marks (‘ ’) Limiting Selected Rows 2Ć19 SQL> SELECT 2 FROM 3 WHERE 2Ć20 last_name s_emp last_name LIKE ’M%’; Introduction to Oracle: SQL and PL/SQL Using Procedure Builder SQL Operators continued The LIKE Operator You may not always know the exact value to search for You can select rows that match a character pattern by using the LIKE operator The... result is TRUE OR If either component condition returns TRUE, then the result is TRUE NOT Returns the opposite condition Limiting Selected Rows 2Ć15 2Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Negating Expressions You may find that it is easier to find the rows that do not meet a condition, rather than those that do Use the comparison and SQL operators with a negative expression . Using Procedure Builder2Ć10 Limiting Selected Rows 2Ć11 Limiting Selected Rows with the WHERE Clause You can restrict the rows returned from the query. Limiting Selected Rows 2 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder2Ć2 Limiting Selected Rows 2Ć3 Objectives

Ngày đăng: 10/12/2013, 17:15

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan