DavidSzczesniak
1/17/2018 - 3:01 PM

WHERE clauses

This tool allows you to choose which rows are returned from a SELECT statement.

\!h Simplest form - exactly match a value

Example 1: details about the artist "New Order"
SELECT * FROM artist WHERE artist_name = "New Order";

Example 2: find name of the artist with an artist_id of 4
SELECT artist_name FROM artist WHERE artist_id = 4;

\!h Retrieving values in a range

Example 1: find names of all artists with an artist_id less than 5
SELECT artist_name FROM artist WHERE artist_id < 5;

--The most common operators for numbers are =, >, <, <=, =>, != and <>(not equal to).

Example 2: find all albums that don't have an album_id of 2
SELECT album_name FROM album WHERE album_id <> 2;

--The same can be done for strings:

Example: list all artists whose name appears earlier alphabetically than (is less than) 'M'
SELECT artist_name FROM artist WHERE artist_name < 'M';

Finding matches that start with a prefix, contain a string, or end in a suffix
\!h (Done with the LIKE operator)

Example 1: find all album names beginning with the word "Retro":
SELECT album_name FROM album WHERE album_name LIKE "Retro%";

Example 2: all tracks that begin with a three letter word that starts with 'R':
SELECT * FROM track WHERE track_name LIKE "R__ %";
will match a words like "Red" or "Run - followed by a space character, then any string