codesnout.com

SQL WHERE

Google Ads

SQL WHERE


SQL Sample Code - SQL WHERE clause


SELECT column_name(s)
FROM table_name
WHERE column_name operator value;

See the SQL samples section below for an explanation of the WHERE clause.

Notes:- Use single quotes around text. Most database vendors will also allow double quotes too.

Numeric values should not use quotations.


What does the SQL WHERE clause do?

The SQL WHERE clause searches for a condition specified in the WHERE part of the statement, and retrieves only those records which satisfy that condition.

A variety of criteria and operators can be used in the WHERE clause.


Sample 1 – SQL WHERE

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town
1 SQL Sample 1 Sample St Hamburg
2 SQL Code Land 2 Code Rd Hamburg
3 Sample Code World 66 SQL St Curry
4 SQL Reference Ltd 34 Reference St Pisa

In this SQL sample we only want to retrieve all the Columns from the tblCompany table whose company is based in the town of Hamburg.

Use the SELECT statement below:

SELECT * FROM tblCompany WHERE Town = ‘Hamburg’;

Use the SELECT statement below:

The table of results will look like this:

Company_ID CompanyName Address Town
1 SQL Sample 1 Sample St Hamburg
2 SQL Code Land 2 Code Rd Hamburg

Quotations and Numeric Values.

Use single quotes around text. Most database vendors will also allow double quotes too.

Numeric values should not use quotations.

Sample 2 – SQL WHERE

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this SQL sample we only want to retrieve all the Columns from the tblCompany table whose Sales are over £10000.

Use the SELECT statement below:

SELECT * FROM tblCompany WHERE Sales >10000;

The table of results will look like this:

Company_ID CompanyName Address Town Sales
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

WHERE Operators that can be used.

Operator Description
= Equal to
<> Not Equal To
> Greater Than
< Less Than
>= Greater than or equal To
<= Less than or equal to
LIKE SQL LIKE allows you to perform pattern matching in the criteria rather an exact match. Like can be used in conjunction with %
BETWEEN Between an inclusive range
IN Can retrieve results based upon multiple known criteria.

 


Related SQL Sample Code:


About Us | Privacy Policy | Contact Us | ©2010 Thunderousity Information Management Solutions | www.autosnout.com - Car Performance Statistics Website Utilising SQL | SQL Blog |

SQL Home | SQL Intro | SQL SELECT Statement | DISTINCT | WHERE | SQL Wildcards | AND OR | IN | BETWEEN | LIKE | ORDER BY | GROUP BY | TOP | ALIAS | DELETE | INSERT | UPDATE | CREATE DATABASE | CREATE TABLE | ALTER | INNER JOIN | FULL JOIN | LEFT JOIN | RIGHT JOIN | UNION | CONSTRAINTS | NOT NULL| Aggregate Functions | SUM | AVG | COUNT | MAX | MIN | FIRST | LAST | GROUP BY | HAVING | LIMIT | Microsoft Access Data Types | MySQL Data Types | SQL Server Data Types | ASCII HTML Reference Sheet |