SQL WHERE Clause

How do I use the SQL WHERE clause?

A SQL WHERE clause filters for rows that meet certain criteria.
It is a way to limit the rows to the ones you're interested in.

WHERE is followed by a condition that returns either true or false.
The WHERE clause is used with SELECT, UPDATE, and DELETE.

The SQL WHERE syntax

Here is a SELECT statement with a WHERE clause:

SELECT column-names
  FROM table-name
 WHERE condition

And here is an UPDATE with a WHERE clause:

UPDATE table-name
   SET column-name = value
 WHERE condition

Finally, a DELETE statement with a WHERE clause:

DELETE table-name
 WHERE condition

CUSTOMER
Id
FirstName
LastName
City
Country
Phone

SQL WHERE Clause Examples

Problem: List all customers in Sweden
SELECT Id, FirstName, LastName, City, Country, Phone
  FROM Customer
 WHERE Country = 'Sweden'
Result:  2 records
Id FirstName LastName City Country Phone
5 Christina Berglund Luleå Sweden 0921-12 34 65
24 Maria Larsson Bräcke Sweden 0695-34 67 21

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
Problem: Update the city to Sydney for supplier Pavlova, Ltd.
UPDATE Supplier
   SET City = 'Sydney'
 WHERE Name = 'Pavlova, Ltd.'
Result:  1 record updated.

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
Problem: Delete all products with unit price higher than $50.
DELETE FROM Product
 WHERE UnitPrice > 50
Result:  7 records deleted.

Note: Referential integrity may prevent this deletion.
A better approach may be to discontinue the product, that is, set the column IsDiscontinued to true.



Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas