Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16
Restricting and Sorting Data
Objective After completing this lesson, you should be able to do :
• Limit the rows retrieved by a query
• Sort the rows retrieved by a query Limiting the Rows Selected • Use the WHERE clause SELECT * | { [DISTINCT] column/expression [alias],… } FROM table [ WHERE conditions(s) ]; • The WHERE clause follows the FROM clause. • The condition is composed of column names, expressions, constants and a comparison operator • Example : SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90; Character Strings and Dates • Are enclosed in single quotation marks • Character values are case sensitive • Date values are format sensitive • The default date format is DD-MON-RR • Example : SELECT last_name, job_id, department_id FROM employees WHERE last_name = ‘Whalen’ ; Comparison Operators Operator Meaning = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> , != , ^= Not Equal to They are used in the WHERE clause in the following Format. Syntax : ….. WHERE expr operator value Other Comparison Operators Operator Meaning BETWEEN..AND… Between two values (inclusive) IN(Set) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value • Use the LIKE condition to perform wildcard searches of valid search string values - % denotes zero or many characters - _ denotes one character (underscore) Examples • BETWEEN..AND… operator SELECT ename, sal FROM emp WHERE sal BETWEEN 2500 AND 3500; • IN operator SELECT empno, ename, sal FROM emp WHERE mgr IN (100, 101, 201); • LIKE operator SELECT ename FROM emp WHERE ename LIKE ‘S%’ ; • IS NULL operator SELECT ename, mgr FROM emp WHERE comm IS NULL; Logical Operator Operator Meaning AND Returns TRUE if both component conditions are true OR Returns TRUE if either component condition is true NOT Returns TRUE if the following condition is false
• Combines the result of two component conditions to produce a
single result • With these operators we can use several conditions in one WHERE clause. Using the AND Operators • AND requires both conditions to be true SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >=10000 AND job_id LIKE ‘%MAN%’ ;
AND TRUE FALSE NULL
TRUE TRUE FALSE NULL FALSE FALSE FALSE FALSE NULL NULL FALSE NULL Using the OR operator • OR requires either condition to be true SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary > 10000 OR job_id LIKE ‘%MAN%’ ;
OR TRUE FALSE NULL
TRUE TRUE TRUE TRUE FALSE TRUE FALSE NULL NULL TRUE NULL NULL Using the NOT Operator • To negate the result we use the NOT Operator SELECT last_name, job_id FROM employees WHERE job_id NOT IN ( ‘IT_PROG’, ‘ST_CLERK’ );
NOT TRUE FALSE NULL
FALSE TRUE NULL Rules of Precedence Order Evaluated Operator 1 Arithmetic operators 2 Concatenation operator 3 Comparison conditions 4 IS [NOT] NULL, LIKE, [NOT] IN 5 [NOT] BETWEEN 6 NOT logical condition 7 AND logical condition 8 OR logical condition Override rules of precedence by using parentheses. The same precedence operators work from left to right. ORDER BY Clause • Sort rows with the ORDER BY clause - ASC : Ascending order, default - DESC : Descending order • Syntax : SELECT expr,column(s) FROM table [WHERE condition(s)] [ORDER BY {column, expr} [ASC|DESC] ] ; • Default Ordering of Data Numeric values lowest values first 1 - 999 Date values earliest value first 1-jan-92 before 1- jan-95 Character values alphabetical order a first and z last Null values last Sorting Examples • SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date ; • SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date DESC ; • Sort order can be specified by position value also SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY 3 DESC ; Some more sorting methods • Sorting by Column Alias SELECT employee_id, last_name, salary*12 annsal FROM employees ORDER BY annsal ; • Sorting by Multiple Columns - we can use multiple columns in the ORDER BY clause - The order of ORDER BY list is the order of sort SELECT last_name, department_id, salary FROM employees ORDER BY department_id, salary DESC; Summary In this lesson, you should have learned how to : • Use the WHERE clause to restrict rows of output - use the comparison conditions - use the BETWEEN..AND.. , IN , LIKE , and NULL conditions - Apply the logical AND, OR, and NOT operators • Use the ORDER BY clause to sort rows of output