Download presentation
Presentation is loading. Please wait.
Published byLeslie Blankenship Modified over 9 years ago
2
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query
3
"…retrieve all employees in department 10" Employee employee_nbr name job... dept_nbr 7839 King President 10 7698 Blake Manager 30 7782 Clark Manager 10 7566 Jones Manager 20... Employee employee_nbr name job... dept_nbr 7839 King President 10 7782 Clark Manager 10 7934 Miller Manager 10...
4
Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)];
5
MySQL>SELECT name, job, dept_nbr 2 FROM employee 3 WHERE job=‘Clerk’; name job dept_nbr --------- --------- --------- James Clerk 30 Smith Clerk 20 Adams Clerk 20 Miller Clerk 10
6
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>SELECTname, job, dept_nbr 2 FROM employee 3 WHEREname = ; MySQL>SELECTname, job, dept_nbr 2 FROM employee 3 WHEREname = ; ‘James’
7
OperatorMeaning =Equal to >Greater than >=Greater than or equal to <Less than <=Less than or equal to <>Not equal to !=Not equal to
8
MySQL>SELECT name, salary, commission 2 FROM employee 3 WHERE salary <= commission; name salary commission ---------- --------- ---------- Martin 1250 1400
9
OperatorMeaning BETWEEN …AND… Between two values (inclusive) IN (list)Match any of a list of values LIKEMatch a character pattern IS NULLIs a null value
10
Use the BETWEEN operator to display rows based on a range of values. name salary ---------- --------- Martin 1250 Turner 1500 Ward 1250 Adams 1100 Miller 1300 MySQL>SELECTname, salary 2 FROM employee 3 WHEREsalary BETWEEN 1000 AND 1500; Lower limit Higher limit
11
Use the IN operator to test for values in a list. MySQL>SELECTemployee_nbr, name, salary, manager 2 FROM employee 3 WHEREmanager 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
12
MySQL>SELECTname 2 FROM employee 3 WHEREname 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.
13
You can combine pattern-matching characters. You can use the ESCAPE identifier to search for "%" or "_". MySQL>SELECTname 2 FROMemployee 3 WHEREname LIKE ‘_a%’; name ---------- Martin James Ward
14
Test for null values with the IS NULL operator. MySQL>SELECT name, manager 2 FROM employee 3 WHERE manager IS NULL; name manager ---------- --------- King
15
OperatorMeaning AND Returns TRUE if both component conditions are TRUE OR Returns TRUE if either component conditions is TRUE NOT Returns TRUE if the following condition is FALSE
16
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.
17
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.
18
MySQL>SELECT name, job 2 FROM employee 3 WHERE job NOT IN ('Clerk','Manager',’Analyst'); name job ---------- --------- King President Martin Salesman Allen Salesman Turner Salesman Ward Salesman
19
Override rules of precedence by using parentheses. Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR
20
name job Salary ---------- --------- --------- King President 5000 Martin Salesman 1250 Allen Salesman 1600 Turner Salesman 1500 Ward Salesman 1250 name job Salary ---------- --------- --------- King President 5000 Martin Salesman 1250 Allen Salesman 1600 Turner Salesman 1500 Ward Salesman 1250 MySQL>SELECT name, job, salary 2 FROM employee 3 WHERE job = ‘Salesman’ 4 OR job = ‘President’ 5 AND salary > 1500;
21
name job salary ---------- --------- --------- King President 5000 Allen Salesman 1600 name job salary ---------- --------- --------- King President 5000 Allen 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.
22
Sort rows with the ORDER BY clause ASC: ascending order, default DESC: 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-17 Allen Salesman 30 1981-02-20... 14 rows selected.
23
MySQL>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-12 Scott Analyst 20 1982-12-09 Miller Clerk 10 1982-01-23 James Clerk 30 1981-12-03 Ford Analyst 20 1981-12-03 King President 10 1981-11-17 Martin Salesman 30 1981-09-28... 14 rows selected.
24
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.
25
The 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 5000 Clark 10 2450 Miller 10 1300 Ford 20 3000... 14 rows selected. You can sort by a column that is not in the SELECT list.
26
SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.