WHERE

The WHERE keyword enables you to narrow down queries by returning only those where a certain expression is true. Returns only the rows where the column exactly matches the string 30 suosikkia, using the equality operator =.

Using the WHERE keyword:

SELECT author,title FROM classics WHERE author=”Markku Aro”;
SELECT author,title FROM classics WHERE isbn=”5051011157622″;

Given our current table, the two commands in display the same results.
But we could easily add more cd:s Markku Aro, in which case the first line would display all the titles he wrote and the second line would continue (because we know the ISBN is unique) to display 30 suosikkia. In other words, searches using a unique key are more predictable, and you’ll see further evidence later of the value of unique and primary keys.

You can also do pattern matching for your searches using the LIKE qualifier, which allows searches on parts of strings. This qualifier should be used with a % character before or after some text. When placed before a keyword, % means anything before. After a keyword, it means anything after 🙂

Using the LIKE qualifier:
SELECT author,title FROM classics WHERE author LIKE “Markku%”;
SELECT author,title FROM classics WHERE title LIKE “%suosikki”;
SELECT author,title FROM classics WHERE title LIKE “%30%”;

Leave a Reply

Your email address will not be published. Required fields are marked *