Where - High-Flying C# Coding Tips


SQL Tips

Add a Unique Constraint to a Column

CURSOR

DELETE

Dirty Reads and Phantom Reads

INSERT

Recursive SQL

ROW_NUMBER() - Automatic Paging

SELECT, FROM

SQL Default Date Format

UPDATE

WHERE

Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

WHERE

Here we look at the Where condition in SQL.

The Where condition allows us to specify a certain set of data within a database table. You can use Where with the Select, Update and Delete commands.

For instance, in the first example, we first perform a Select command and gather the entire database table contents, and then we perform a Select command with a Where clause which only returns products of type Postal.


-- Get all the data in the table
SELECT * FROM Products

-- Get only products of type Postal
SELECT * FROM Products WHERE ProductType = 'Postal'


We can have multiple conditions with Where, for instance if you want to find products of type Postal and where the price is greater than 5.00, the next code example shows how to do this.


-- Get only Postal type products and products with
-- a price greater than 5.00
SELECT * FROM Products WHERE ProductType = 'Postal'
AND Price > '5.00'


Where can also be used with the Update and Delete commands, as seen here.


-- Delete only cars of colour red
DELETE FROM Cars WHERE Colour = 'Red'

-- Update the product price for bananas
UPDATE Fruit SET Price = '5.99' WHERE ProductName = 'Bananas'