Date post: | 17-Dec-2015 |
Category: |
Documents |
Upload: | leslie-blankenship |
View: | 217 times |
Download: | 1 times |
Objectives
After completing this lesson, you should be able to do the following:
Limit the rows retrieved by a querySort the rows retrieved by a query
Limiting Rows Using a Selection
"…retrieve allemployees
in department 10"
Employeeemployee_nbr name job ... dept_nbr
7839 King President 10 7698 Blake Manager 30 7782 Clark Manager 10 7566 Jones Manager 20 ...
Employeeemployee_nbr name job ... dept_nbr
7839 King President 107782 Clark Manager 10
7934 Miller Manager 10 ...
Limiting Rows Selected
Restrict the rows returned by using the WHERE clause.
The WHERE clause follows the FROM clause.
SELECT [DISTINCT] {*| column [alias], ...}FROM table[WHERE condition(s)];
Using the WHERE Clause
MySQL>SELECT name, job, dept_nbr 2 FROM employee 3 WHERE job=‘Clerk’;
name job dept_nbr--------- --------- ---------James Clerk 30Smith Clerk 20Adams Clerk 20Miller Clerk 10
Character Strings and Dates
Character strings and date values are enclosed in single quotation marks.Character values are case sensitive and date values are format sensitive.The default date format is YYYY-MM-DD.
MySQL>SELECT name, job, dept_nbr 2 FROM employee 3 WHERE name = ;‘James’
Comparison Operators
Operator Meaning= Equal to> Greater than
>= Greater than or equal to< Less than
<= Less than or equal to<> Not equal to!= Not equal to
Using the Comparison Operators
MySQL>SELECT name, salary, commission 2 FROM employee 3 WHERE salary <= commission;
name salary commission---------- --------- ----------Martin 1250 1400
Other Comparison Operators
Operator MeaningBETWEEN…AND…
Between two values (inclusive)
IN (list) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
Using the BETWEEN Operator
Use the BETWEEN operator to display rows based on a range of values.
name salary---------- ---------Martin 1250Turner 1500Ward 1250Adams 1100Miller 1300
MySQL>SELECT name, salary 2 FROM employee 3 WHERE salary BETWEEN 1000 AND 1500;
Lowerlimit
Higherlimit
Using the IN Operator
Use the IN operator to test for values in a list.
MySQL>SELECT employee_nbr, name, salary, manager 2 FROM employee 3 WHERE manager IN (7902, 7566, 7788);
employee_nbr name salary manager------------ ---------- -------- --------- 7902 FORD 3000 7566 7369 SMITH 800 7902 7788 SCOTT 3000 7566 7876 ADAMS 1100 7788
Using the LIKE Operator
MySQL>SELECT name 2 FROM employee 3 WHERE name LIKE 'S%';
Use the LIKE operator to perform wildcard searches of valid search string values.Search conditions can contain either literal characters or numbers.
% denotes zero or many characters._ denotes one character.Searches are case sensitive.
Using the LIKE Operator
You can combine pattern-matching characters.You can use the ESCAPE identifier to search for "%" or "_".
MySQL>SELECT name 2 FROM employee 3 WHERE name LIKE ‘_a%’;
name---------- MartinJames Ward
Using the IS NULL Operator
Test for null values with the IS NULL operator.
MySQL>SELECT name, manager 2 FROM employee 3 WHERE manager IS NULL;
name manager---------- ---------King
Logical Operators
Operator Meaning
ANDReturns TRUE if both component conditions are TRUE
ORReturns TRUE if either component conditions is TRUE
NOTReturns TRUE if the following condition is FALSE
Using the AND Operator
MySQL>SELECT employee_nbr, name, job, salary 2 FROM employee 3 WHERE salary >= 1100 4 AND job = 'Clerk';
employee_nbr name job salary------------ ------- --------- --------- 7876 Adams Clerk 1100 7934 Miller Clerk 1300
AND requires both conditions to be TRUE.
Using the OR Operator
MySQL>SELECT employee_nbr, name, job, salary 2 FROM employee 3 WHERE salary >= 1100 4 OR job = ‘Clerk’;
employee_nbr name job salary------------ ------- --------- --------- 7839 King President 5000 7698 Blake Manager 2850 7782 Clark Manager 2450 7566 Jones Manager 2975 7654 Martin Salesman 1250 ... 7900 James Clerk 950 ...14 rows selected.
OR requires either conditions to be TRUE.
Using the NOT Operator
MySQL>SELECT name, job 2 FROM employee 3 WHERE job NOT IN ('Clerk','Manager',’Analyst');
name job---------- ---------King PresidentMartin SalesmanAllen SalesmanTurner SalesmanWard Salesman
Rules of Precedence
Override rules of precedence by using parentheses.
Order Evaluated Operator
1 All comparison operators
2 NOT
3 AND
4 OR
Rules of Precedence
name job Salary---------- --------- ---------King President 5000Martin Salesman 1250Allen Salesman 1600Turner Salesman 1500Ward Salesman 1250
MySQL>SELECT name, job, salary 2 FROM employee 3 WHERE job = ‘Salesman’ 4 OR job = ‘President’ 5 AND salary > 1500;
Rules of Precedence
name job salary---------- --------- ---------King President 5000Allen Salesman 1600
MySQL>SELECT name, job, salary 2 FROM employee 3 WHERE (job = ‘Salesman’ 4 OR job = ‘President’) 5 AND salary > 1500;
Use parentheses to force priority.
ORDER BY ClauseSort rows with the ORDER BY clause
ASC: ascending order, defaultDESC: descending order
The ORDER BY clause comes last in the SELECT statement.
MySQL>SELECT name, job, dept_nbr, hire_date 2 FROM employee 3 ORDER BY hire_date;
name job dept_nbr hire_date---------- --------- --------- ---------Smith Clerk 20 1980-12-17Allen Salesman 30 1981-02-20...14 rows selected.
Sorting in Descending OrderMySQL>SELECT name, job, dept_nbr, hire_date 2 FROM employee 3 ORDER BY hire_date DESC;
name job dept_nbr hire_date---------- --------- --------- ---------Adams Clerk 20 1983-01-12Scott Analyst 20 1982-12-09Miller Clerk 10 1982-01-23James Clerk 30 1981-12-03Ford Analyst 20 1981-12-03King President 10 1981-11-17Martin Salesman 30 1981-09-28...14 rows selected.
Sorting by Column Alias
MySQL>SELECT employee_nbr, name, salary*12 annual_salary 2 FROM employee 3 ORDER BY annual_salary;
employee_nbr name annual_salary------------ --------- ------------- 7369 SMITH 9600 7900 JAMES 11400 7876 ADAMS 13200 7654 MARTIN 15000 7521 WARD 15000 7934 Miller 15600 7844 TURNER 18000...14 rows selected.
Sorting by Multiple ColumnsThe order of ORDER BY list is the order of sort.
MySQL>SELECT name, dept_nbr, salary 2 FROM employee 3 ORDER BY dept_nbr, salary DESC;
name dept_nbr Salary---------- --------- ---------King 10 5000Clark 10 2450Miller 10 1300Ford 20 3000...14 rows selected.
You can sort by a column that is not in the SELECT list.