Structure Query Language
Structure Query Language
SQL SELECT Statement
The SELECT statement is used to select data from a database.
SELECT Syntax
SELECT column1, column2, ...
FROM table_name;
Here, column1, column2, ... are the field names of the table you want to select data from. If you want
to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name;
DEMO TABLE: (note: use this table as reference for the examples to be given below)
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
SELECT CustomerName, City FROM Customers;
SELECT * Example
The following SQL statement selects all the columns from the "Customers" table:
SELECT * FROM Customers;
SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only
want to list the different (distinct) values.
SELECT DISTINCT Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
The following SQL statement selects only the DISTINCT values from the "Country" column in
the "Customers" table:
SELECT DISTINCT Country FROM Customers;
The WHERE clause is used to extract only those records that fulfill a specified condition.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT * FROM Customers
WHERE Country='Mexico';
SELECT * FROM Customers
WHERE CustomerID=1;
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written
as !=
The AND and OR operators are used to filter records based on more than one condition:
The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
AND Example
The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city is "Berlin":
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
OR Example
The following SQL statement selects all fields from "Customers" where city is "Berlin" OR
"München":
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
The following SQL statement selects all fields from "Customers" where country is "Germany"
OR "Spain":
SELECT * FROM Customers
WHERE Country='Germany' OR Country='Spain';
NOT Example
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany":
SELECT * FROM Customers
WHERE NOT Country='Germany';
The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city must be "Berlin" OR "München" (use parenthesis to form complex expressions):
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany" and NOT "USA":
SELECT * FROM Customers
WHERE NOT (Country='Germany' AND Country='USA');
There are two wildcards often used in conjunction with the LIKE operator:
Note: The percent sign and the underscore can also be used in combinations!
LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Here are some examples showing different LIKE operators with '*' and '?' wildcards:
WHERE CustomerName LIKE Finds any values that start with "a"
'a*'
WHERE CustomerName LIKE Finds any values that end with "a"
'*a'
WHERE CustomerName LIKE Finds any values that have "or" in any position
'*or*'
WHERE CustomerName LIKE Finds any values that have "r" in the second position
'?r*'
WHERE CustomerName LIKE Finds any values that start with "a" and are at least 3
'a??*' characters in length
WHERE ContactName LIKE Finds any values that start with "a" and ends with "o"
'a*o'
SELECT * FROM Customers
WHERE CustomerName LIKE 'a*';
The following SQL statement selects all customers with a CustomerName ending with "a":
SELECT * FROM Customers
WHERE CustomerName LIKE '*a';
The following SQL statement selects all customers with a CustomerName that have "or" in
any position:
SELECT * FROM Customers
WHERE CustomerName LIKE '*or*';
The following SQL statement selects all customers with a CustomerName that have "r" in the
second position:
SELECT * FROM Customers
WHERE CustomerName LIKE '?r*';
Wildcard characters are used with the SQL LIKE operator. The LIKE operator is used in a
WHERE clause to search for a specified pattern in a column.
* Represents zero or more bl* finds bl, black, blue, and blob
characters
[] Represents any single character h[oa]t finds hot and hat, but not hit
within the brackets
! Represents any character not in h[!oa]t finds hit, but not hot and hat
the brackets
# Represents any single numeric 2#5 finds 205, 215, 225, 235, 245, 255,
character 265, 275, 285, and 295
SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Demo Database
Below is a selection from the "Products" table in the Northwind sample database:
BETWEEN Example
The following SQL statement selects all products with a price BETWEEN 10 and 20:
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
Demo Table
Below is a selection from the "Orders" table in the Northwind sample database:
The following SQL statement selects all orders with an OrderDate BETWEEN '01-July-1996'
and '31-July-1996':
SELECT * FROM Orders
WHERE OrderDate BETWEEN #01/07/1996# AND #31/07/1996#;
OR:
SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';