The WHERE clause is an optional clause that can be used with the DELETE, SELECT, and UPDATE statements to specify a selection criteria. The syntax for the DELETE, SELECT, and UPDATE statements are very similar.
SELECT * FROM MusicArtists;
MusicianID FirstName LastName Instrument Style
1 Elvis Presley Guitar Rock and Roll
2 B. B. King Guitar Blues
3 Charlie Daniels Fiddle Country
4 Johnny Cash Guitar Country
5 Henry Erickson Violin Classical
6 Jonny Lang Guitar Blues
7 Jerry Jamison Flute Classical
8 Bobby Lee Fiddle Classical
(8 row(s) affected)
The WHERE clause is optional. This simple query will return all rows from the table 'MusicArtists'.
SELECT * FROM MusicArtists WHERE Style='Country';
MusicianID FirstName LastName Instrument Style
3 Charlie Daniels Fiddle Country
4 Johnny Cash Guitar Country
(2 row(s) affected)
The addition of a WHERE to the previous example allows you to efficiently narrow the values that will be selected. In this example, we narrow the criteria to only 'Country'.
SELECT * FROM MusicArtists
WHERE Style='Country' AND Instrument='Guitar';
MusicianID FirstName LastName Instrument Style
4 Johnny Cash Guitar Country
(1 row(s) affected)
By using the AND logical operator we can further narrow the criteria to 'Country' artists who play the 'Guitar'.
SELECT * FROM MusicArtists
WHERE Style='Country' OR Style='Blues';
MusicianID FirstName LastName Instrument Style
2 B. B. King Guitar Blues
3 Charlie Daniels Fiddle Country
4 Johnny Cash Guitar Country
6 Jonny Lang Guitar Blues
(4 row(s) affected)
This example demonstrates how to widen a search to include multiple music styles using the OR logical operator.
SELECT * FROM MusicArtists
WHERE Style='Country' OR Style='Blues' OR Style='Rock and Roll'
AND Instrument='Guitar' OR Instrument='Fiddle';
MusicianID FirstName LastName Instrument Style
1 Elvis Presley Guitar Rock and Roll
2 B. B. King Guitar Blues
3 Charlie Daniels Fiddle Country
4 Johnny Cash Guitar Country
6 Jonny Lang Guitar Blues
(5 row(s) affected)
This example uses multiple criteria for the WHERE clause using the AND and OR operators. Within a WHERE clause, you can link up to a total of forty (40) such expressions using logical operators.