The WHERE and LIKE Clauses | SQL Tutorials

Home >> The WHERE and LIKE Clauses

The WHERE and LIKE Clauses

PDF

The WHERE Clause

The WHERE clause us used to specify a selection criterion. To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.

Syntax

SELECT column FROM table WHERE column operator value

With the WHERE clause, the following operators can be used:

Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern

Using the WHERE Clause

To select only the contacts with the last name "Marsat" from the MeshDigital CRM system, we add a WHERE clause to the SELECT statement:

For example:

SELECT first_name, last_name FROM sales_contacts WHERE last_name='Marsat'

When to use Quotes

Note that we have used single quotes around the conditional values in the examples.

SQL uses single quotes around text values. Numeric values should not be enclosed in quotes.

For text values:

SELECT first_name FROM sales_contacts WHERE last_name='Marsat'

For numeric values:

SELECT first_name FROM sales_contacts WHERE contactid=12

The LIKE Condition

The LIKE condition is used to specify a search for a pattern in a column.

Syntax

SELECT column FROM table WHERE column LIKE pattern

A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.

Using LIKE

The following SQL statement will return contacts with first names that start with a 'D':

SELECT * FROM sales_contacts WHERE first_name LIKE 'D%'

The following statement will return contacts with first names that end with a 'c':

SELECT * FROM sales_contacts WHERE first_name LIKE '%c'

The following statement will return contacts with first names that contain the patter 'ini':

SELECT * FROM sales_contacts WHERE first_name LIKE '%ini%'


Partners: Hearing Aid Talk Forum for Hearing Aids | One Direction Forum | Adult Toys

© Pixel Kicks, 5 Marlborough Road, Royton, Oldham, OL2 6AU.