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 Employees 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 employees with the first name of John.
Listing all employees named John:
Type the following:
SELECT * FROM Employees WHERE GivenName = 'John'
EmployeeID |
ManagerID |
Surname |
GivenName |
DepartmentID |
... |
---|---|---|---|---|---|
318 |
1576 |
Crow |
John |
400 |
... |
862 |
501 |
Sheffield |
John |
100 |
... |
1483 |
1293 |
Letiecq |
John |
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 GivenName, Surname, BirthDate FROM Employees WHERE GivenName = 'John' ORDER BY BirthDate
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.