WHERE clauses

Simple SELECTs are great if you want to explore your database, or if you really do want to see every row. But what if you only want to find red Skodas, or Andy Jones?

We only have four rows, so it isn't a problem, but what if we had 4 million rows? You need a way to restrict the rows you SELECT.

This is what WHERE clauses are for. They come after your SELECT and specify extra information about the rows you want back.

Here's an example:

SELECT  name, contactNumber FROM owners
WHERE   name = 'Andy Jones'

Our SELECT specifies which columns we want, and our WHERE clause specifies which rows we're interested in, i.e. any rows where the name is Andy Jones.

The WHERE clause has three parts:

  1. A left-hand value e.g. name.
  2. An operator e.g. =. We'll cover more of these in the next lesson.
  3. A right-hand value e.g. 'Andy Jones'.

These values can be all sorts of things, but for now our left-hand values will all be columns, and our right-hand values will be numbers or strings of letters.

Strings of letters need single quote marks around them e.g. 'Andy Jones'. Numbers don't, e.g.

SELECT  name, contactNumber FROM owners
WHERE   id = 4

As you'll notice, the column in your WHERE clause doesn't have to appear in your SELECT.

Task

What your results should look like

Query

Result

There was an error in your SQL query: {{error}}

{{executedQuery}}

Correct answer. Bonza!

Congratulations! You've completed all of the tasks in this lesson.

Next lesson: More operators for WHERE clauses

Wrong answer. Have another a go.

Hint: {{hint}}

Next lesson: More operators for WHERE clauses

Once you think you've nailed this lesson, feel free to go to the next lesson.

Next lesson

Questions? Suggestions?

We'd love to hear any feedback you have for us. Just send an email to hello@learnsomesql.com.