Pull rows out, then filter them

INPUT · Slides

Narrowing down with WHERE

01 / 04

You want fewer rows

SELECT was handing back every row. But more often you want only the rows that match — "dishes at 600 or under", "just the noodles".

The job that if does in JavaScript is done here by WHERE.

02 / 04

How WHERE looks

Write SELECT ... FROM ... WHERE condition; — the condition goes on after FROM.

A text value is wrapped in ' (single quotes). That is the SQL rule, and it differs from JavaScript's ", so watch out.

SELECT * FROM menus WHERE category = 'noodles';

Result

only the two rows, tofu udon and soy ramen

03 / 04

Comparing numbers

For number conditions you have the comparison operators.

  • price < 600 … under 600
  • price <= 600 … 600 or under
  • price > 600 … over 600

Much like JavaScript, except that "equals" is a single =, not ===. That is the big difference.

SELECT name, price FROM menus WHERE price <= 600;

04 / 04

"Not equal" is <>

"Is not equal" is written <> (!= works too).

WHERE narrows the rows, SELECT narrows the columns. You can now choose both.

SELECT name FROM menus WHERE category <> 'noodles';