SQL
SQL
SQL
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written
BETWEEN Between a certain range
BETWEEN
LIKE
IN
The AND and OR operators are used to filter records based on more than one
condition:
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;
Demo Database
The table below shows the complete "Customers" table from the Northwind
sample database:
CustomerID CustomerName ContactName Address City Po
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12
2 Ana Trujillo Emparedados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05
y helados
18 Du monde entier Janine Labrune 67, rue des Cinquante Otages Nantes 44
36 Hungry Coyote Import Yoshi Latimer City Center Plaza 516 Main St. Elgin 97
Store
45 Let's Stop N Shop Jaime Yorres 87 Polk St. Suite 5 San Francisco 94
54 Océano Atlántico Ltda. Yvonne Moncada Ing. Gustavo Moncada 8585 Buenos Aires 10
Piso 20-A
58 Pericles Comidas clásicas Guillermo Calle Dr. Jorge Cash 321 México D.F. 05
Fernández
62 Queen Cozinha Lúcia Carvalho Alameda dos Canàrios, 891 São Paulo 05
64 Rancho grande Sergio Gutiérrez Av. del Libertador 900 Buenos Aires 10
75 Split Rail Beer & Ale Art P.O. Box 555 Lander 82
Braunschweiger
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98
AND Example
The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city is "Berlin":
Example
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
Try it Yourself »
OR Example
The following SQL statement selects all fields from "Customers" where city is
"Berlin" OR "München":
Example
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
Try it Yourself »
The following SQL statement selects all fields from "Customers" where country is
"Germany" OR "Spain":
Example
SELECT * FROM Customers
WHERE Country='Germany' OR Country='Spain';
Try it Yourself »
NOT Example
The following SQL statement selects all fields from "Customers" where country is
NOT "Germany":
Example
SELECT * FROM Customers
WHERE NOT Country='Germany';
Try it Yourself »
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):
Example
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');
Try it Yourself »
The following SQL statement selects all fields from "Customers" where country is
NOT "Germany" and NOT "USA":
Example
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Ex
The following SQL statement will insert a new record, but only insert data in the
"CustomerName", "City", and "Country" columns (CustomerID will be
updated automatically):
Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of
the values is in the same order as the columns in the table. Here, the INSERT
INTO syntax would be as follows:
It is not possible to test for NULL values with comparison operators, such as =,
<, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Note: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which record(s) that should
be updated. If you omit the WHERE clause, all records in the table will be
updated!
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
SELECT MIN(column_name)
FROM table_name
WHERE condition;
The SQL COUNT(), AVG() and SUM()
Functions
The COUNT() function returns the number of rows that matches a specified
criterion.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT COUNT(ProductID)
FROM Products;
SELECT AVG(Price)
FROM Products;
SELECT SUM(Quantity)
FROM OrderDetails;
There are two wildcards often used in conjunction with the LIKE operator:
Tip: You can also combine any number of conditions using AND or OR operators.
Here are some examples showing different LIKE operators with '%' and '_'
wildcards:
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2
length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3
length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
The following SQL statement selects all customers with a CustomerName that
have "r" in the second position:
Example
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
The following SQL statement selects all customers with a CustomerName that
does NOT start with "a":
Try it Yourself »
The following SQL statement selects all customers with a City starting with "a",
"b", or "c":
Example
SELECT * FROM Customers
WHERE City LIKE '[a-c]%';
Example
SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
The 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.
Example
SELECT * FROM Products
WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di
Giovanni'
ORDER BY ProductName;
Example
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either
left or right table
The EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppl
ierID = Suppliers.supplierID AND Price < 20);
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
SELECT ProductName
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
The SELECT INTO statement copies data from one table into a new table.