SQL WHERE Statement


WHERE

Use the WHERE clause to return only the records that satisfy a specified condition

Syntax


SELECT column1, column2, ...
FROM table_name
WHERE condition
SELECT salesperson#, amount_paid
FROM ar_invoices
WHERE amount_paid > 0
salesperson# amount_paid
11.00

Sample Data

These are the tables we will use as an example:

SELECT * 
FROM ar_invoices
invoice# customer# salesperson# invoice_date invoice_amount amount_paid current_month_interest_charges total_interest_charges
1112019-06-17 10:43:53.727100.001.000.000.00
2222019-05-28 10:43:53.72750.000.005.0010.00
3322019-06-17 10:43:53.72770.000.0010.0020.00
4212019-07-07 10:43:53.72780.000.000.000.00
SELECT * 
FROM ar_salespeople
salesperson# salesperson_name
1bob
2mary
3tim

WHERE Operators

Operator Description
= Equals
> Greater Than
< Lesser Than
>= Greater Than or Equal
<= Lesser Than or Equal
<> or != Not Equal
BETWEEN Between a Certain Range (inclusive)
LIKE Search for a Pattern
IN To Specify Multiple Possible Values for a Column

BETWEEN

Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2
SELECT salesperson#, invoice_amount
FROM ar_invoices
WHERE invoice_amount BETWEEN 60 AND 90
salesperson# invoice_amount
270.00
180.00

LIKE

There are 2 wildcards used with the LIKE operator:

  • % - Represents none, one, or multiple characters
  • _ - Represents a single character

Syntax


SELECT column1, column2, ...
FROM table_name
WHERE columnn LIKE pattern
SELECT salesperson#, total_interest_charges
FROM ar_invoices
WHERE total_interest_charges LIKE '_0.00'
salesperson# total_interest_charges
210.00
220.00

IN

The IN operator is a shorthand for multiple OR conditions

Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...)
SELECT salesperson#, amount_paid
FROM ar_invoices
WHERE salesperson# IN (1)
salesperson# amount_paid
11.00
10.00
SELECT salesperson#, amount_paid
FROM ar_invoices
WHERE salesperson# NOT IN (1)
salesperson# amount_paid
20.00
20.00

~or~
Combine the IN operator with a SELECT statement


SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT)
SELECT DISTINCT salesperson#, salesperson_name
FROM ar_salespeople
WHERE salesperson# IN (SELECT salesperson# FROM ar_invoices)
salesperson# salesperson_name
1bob
2mary

The WHERE clause can be combined with the AND, OR, and NOT operators

Operator Description
AND Displays a record if all the conditions seperated by AND are TRUE
OR Displays a record if any of the conditions seperated by OR is TRUE
NOT Displays a record if the condition(s) is NOT TRUE

For Example:

AND

Syntax


SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...
SELECT salesperson#, invoice_amount
FROM ar_invoices
WHERE salesperson# = 1 AND invoice_amount > 90
salesperson# invoice_amount
1100.00

OR

Syntax


SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...
SELECT customer#, invoice_amount
FROM ar_invoices
WHERE customer# = 1 OR customer# = 2
customer# invoice_amount
1100.00
250.00
280.00

NOT

Syntax


SELECT column1, column2, ...
FROM table_name
WHERE NOT condition
SELECT customer#, invoice_amount
FROM ar_invoices
WHERE NOT customer# = 2
customer# invoice_amount
1100.00
370.00