In this section you will learn procedures for comparing dates, using compound search conditions in the WHERE clause, pattern matching, and search condition shortcuts.
Sometimes you will not want to see information on all the employees in the employee table. Adding a WHERE clause to the SELECT statement allows only some rows to be selected from a table.
For example, suppose you would like to look at the employees with first name John.
List all employees named John:
Type the following:
SELECT * FROM employee WHERE emp_fname = 'John'
emp_id |
manager_id |
emp_fname |
emp_lname |
dept_id |
---|---|---|---|---|
318 |
1576 |
John |
Crow |
400 |
862 |
501 |
John |
Sheffield |
100 |
1483 |
1293 |
John |
Letiecq |
300 |
The apostrophes (single quotes) around the name 'John' are required. They indicate that John is a character string. Quotation marks (double quotes) have a different meaning. Quotation marks can be used to make otherwise invalid strings valid for column names and other identifiers.
The sample database is not case sensitive, so you would get the same results whether you searched for ' 'JOHN', 'john', or 'John'.
Again, you can combine what you have learned:
SELECT emp_fname, emp_lname, birth_date FROM employee WHERE emp_fname = 'John' ORDER BY birth_date
How you order clauses is important. The FROM clause comes first, followed by the WHERE clause, and then the ORDER BY clause. If you type the clauses in a different order, you will get a syntax error.
You do not need to split the statement into several lines. You can enter the statement into the SQL Statements window in any format. If you use more than the number of lines that fit on the screen, the text scrolls in the SQL Statements window.