The Data Management Center

SQL WHERE


Google
 
Web infogoal.com
HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX


SQL Book Picks

SQL Cookbook
SQL Cookbook by Anthony Molinaro


SQL Visual Quick Start
SQL: Visual QuickStart Guide (2nd Edition) by Chris Fehily

SQL Bible
SQL Bible by Alex Kriegel, Boris M. Trukhnov

SQL Complete Reference
SQL: The Complete Reference, Second Edition by James R Groff, Paul N. Weinberg

SQL for Smarties
Joe Celko's SQL for Smarties: Advanced SQL Programming Third Edition by Joe Celko

Murach SQL Server
Murach's SQL for SQL Server by Bryan Syverson

Previous | Next

SQL > SQL Basics > SQL WHERE Clause


What is the SQL WHERE clause?

The SQL WHERE clause is that part of SQL statements that specifies which data is to be accessed. It establishes conditions that control the results of a SQL statements.

Why Use the SQL WHERE clause?

Anytime you want to access certain rows within a table use the SQL WHERE clause. You could use it to:

  • Show only certain rows (called filtering) with a SQL SELECT Statement
  • Update row(s) with a specific condition
  • Delete row(s) with a specific condition

How To Use the SQL WHERE Clause

The SQL WHERE clause is used as follows.

SQL WHERE Clause Syntax

 
<main-statement>
WHERE <conditions>
 

Each condition tests column(s) using comparison operator(s). The following basic comparison operators are supported:

Operator Description
=
Equal
<>
Not Equal
>
Greater Than
<
Less Than
>=
Greater Than Or Equal
<=
Less Than Or Equal

The comparison may involve literal value(s) that are constants like:

  • 10
  • 'Minnesota'
  • -5006.3

Alphanumeric literals are enclosed in single quotes ('XXX').

SQL WHERE Clause Example 1

The product table contains four rows, including one row with a product_status_code with a value of Inactive. We will use the SQL WHERE clause to filter for Active rows:

product_nbrproduct_nameproduct_status_code
1001 SQL Tool 1.0 Inactive
2001 SQL Tool 2.0 Light Active
2002 SQL Tool 2.0 Professional Active
2003 SQL Tool 2.0 Enterprise Active

 
SELECT product_nbr, product_name, product_status_code
FROM product
WHERE product_status_code = 'Active'
 

Results from the execution of the SQL SELECT statement with the WHERE clause are as follows:

product_nbrproduct_nameproduct_status_code
2001 SQL Tool 2.0 Light Active
2002 SQL Tool 2.0 Professional Active
2003 SQL Tool 2.0 Enterprise Active

SQL WHERE Clause Example 2

The customer table contains five rows, including two rows that have credit_balance_amt greater than credit_limit_amt. We will use the SQL WHERE clause to filter for Active rows:

customer_nbrcustomer_namecredit_limit_amtcredit_balance_amt
200-8889 Sandy Shores 7000.00 2100.00
301-7772 Pat Portabello 1000.00 1020.00
305-9999 Douglas Donovan 1000.00 999.00
400-1234 Edward Engle 2000.00 2000.00
500-1234 Fran Farckle 2000.00 2001.00

 
SELECT customer_nbr, customer_name, credit_limit_amt, credit_balance_amt
FROM customer
WHERE credit_balance_amt > credit_limit_amt
 

Results from the execution of the SQL SELECT statement with the WHERE clause are as follows:

customer_nbrcustomer_namecredit_limit_amtcredit_balance_amt
301-7772 Pat Portabello 1000.00 1020.00
500-1234 Fran Farckle 2000.00 2001.00

infogoal.com HOME
SQL OVERVIEW

SQL BASICS
SQL SELECT
SQL WHERE
SQL INSERT
SQL UPDATE
SQL DELETE

SQL ADMINISTRATION
SQL CREATE DATABASE
SQL DROP DATABASE
SQL CREATE TABLE
SQL ALTER TABLE
SQL DROP TABLE
SQL CREATE INDEX
SQL DROP INDEX
SQL ADD FOREIGN KEY
SQL DROP FOREIGN KEY
SQL CREATE VIEW
SQL DROP VIEW

SQL ADVANCED
SQL CONCAT
SQL SUBSTRING
SQL TRIM
SQL AND & OR
SQL IN
SQL BETWEEN
SQL LIKE
SQL DISTINCT
SQL GROUP BY
SQL AGGREGATE
SQL HAVING
SQL ORDER BY
SQL JOIN
SQL OUTER JOIN

SQL SYNTAX

Data Model Resource Book Vol 1
Data Model Resource Book Vol 2
HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX
Copyright© 1999-2006, First Place Software, Inc.