SQL WHERE

The SQL WHERE clause is used to filter records by adding it to an SQL statement – SQL Select, update, delete, insert statement.

SQL WHERE Syntax

SELECT *
FROM TableName
WHERE ColumnName operator Value

SQL WHERE Examples

Table: Employees

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt Finance 62000
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000
304 Tyoysww Osk Development 49000

The following are examples of different operators:

= (Equal)

SELECT *
FROM Employees
WHERE FirstName = 'Jssme'

<> (Not Equal)

SELECT *
FROM Employees
WHERE FirstName <> 'Jssme'

> (Greater than)

SELECT *
FROM Employees
WHERE Salary > 55000

< (Less than)

SELECT *
FROM Employees
WHERE Salary < 55000

>= (Greater than or equal)

SELECT *
FROM Employees
WHERE Salary >= 55000

<= (Less than or equal)

SELECT *
FROM Employees
WHERE Salary <= 55000

BETWEEN (Between a range)

SELECT *
FROM Employees
WHERE Salary BETWEEN 55000 AND 75000

LIKE (seach for a pattern)

SELECT *
FROM Employees
WHERE FirstName LIKE 'Jss%'

IN (In a value-set)

SELECT *
FROM Employees
WHERE EmployeeId IN (203, 204, 302)

We can also combine the operators together:

SELECT *
FROM Employees
WHERE (FirstName LIKE 'J%'
       AND Salary > 55000
      )
      OR EmployeeId = 304

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>