Examples in Each Chapter: Example
Examples in Each Chapter: Example
Examples in Each Chapter: Example
With our online SQL editor, you can edit the SQL statements, and click
on a button to view the result.
Example
SELECT * FROM Customers;
Try it Yourself »
Example
SELECT CustomerName, City FROM Customers;
Try it Yourself »
The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct
(different) values.
Example
SELECT DISTINCT Country FROM Customers;
Example
SELECT COUNT(DISTINCT Country) FROM Customers;
Try it Yourself »
The WHERE clause is used to extract only those records that fulfill a
specified condition.
Example
SELECT * FROM Customers
WHERE Country='Mexico';
Operator Description
= Equal
The AND and OR operators are used to filter records based on more than
one condition:
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';
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';
NOT Example
The following SQL statement selects all fields from "Customers" where
country is NOT "Germany":
Example
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):
Example
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":
Example
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
Try it Yourself »
ORDER BY Example
The following SQL statement selects all customers from the "Customers"
table, sorted by the "Country" column:
Example
SELECT * FROM Customers
ORDER BY Country;
Example
SELECT * FROM Customers
ORDER BY Country DESC;
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;
Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
The first way specifies both the column names and the values to be
inserted:
Example
INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
Try it Yourself »
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');
Try it Yourself »
SQL NULL Values
What is a NULL Value?
A field with a NULL value is a field with no value.
Note: A NULL value is different from a zero value or a field that contains
spaces. A field with a NULL value is one that has been left blank during
record creation!
The following SQL lists all customers with a NULL value in the "Address"
field:
Example
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
The following SQL lists all customers with a value in the "Address" field:
Example
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
UPDATE Table
The following SQL statement updates the first customer (CustomerID =
1) with a new contact person and a new city.
Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The following SQL statement will update the contactname to "Juan" for
all records where country is "Mexico":
Example
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
Example
UPDATE Customers
SET ContactName='Juan';