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
"…retrieve all employees in department 10" Employee employee_nbr name job... dept_nbr 7839 King President Blake Manager Clark Manager Jones Manager Employee employee_nbr name job... dept_nbr 7839 King President Clark Manager Miller Manager 10...
Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)];
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
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’
OperatorMeaning =Equal to >Greater than >=Greater than or equal to <Less than <=Less than or equal to <>Not equal to !=Not equal to
MySQL>SELECT name, salary, commission 2 FROM employee 3 WHERE salary <= commission; name salary commission Martin
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
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
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 FORD SMITH SCOTT ADAMS
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.
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
Test for null values with the IS NULL operator. MySQL>SELECT name, manager 2 FROM employee 3 WHERE manager IS NULL; name manager King
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
MySQL>SELECT employee_nbr, name, job, salary 2 FROM employee 3 WHERE salary >= AND job = 'Clerk'; employee_nbr name job salary Adams Clerk Miller Clerk 1300 AND requires both conditions to be TRUE.
MySQL>SELECT employee_nbr, name, job, salary 2 FROM employee 3 WHERE salary >= OR job = ‘Clerk’; employee_nbr name job salary King President Blake Manager Clark Manager Jones Manager Martin Salesman James Clerk rows selected. OR requires either conditions to be TRUE.
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
Override rules of precedence by using parentheses. Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR
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;
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.
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 Allen Salesman rows selected.
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 Scott Analyst Miller Clerk James Clerk Ford Analyst King President Martin Salesman rows selected.
MySQL>SELECT employee_nbr, name, salary*12 annual_salary 2 FROM employee 3 ORDER BY annual_salary; employee_nbr name annual_salary SMITH JAMES ADAMS MARTIN WARD Miller TURNER rows selected.
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 Clark Miller Ford rows selected. You can sort by a column that is not in the SELECT list.
SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];