Académique Documents
Professionnel Documents
Culture Documents
Database Tables
A database most often contains one or more tables. Each table is identified by a name
(e.g. "Customers" or "Orders"). Tables contain records (rows) with data.
Below is an example of a table called "Persons":
P_Id
1
LastName
Hansen
FirstName
Ola
Address
Timoteivn 10
City
Sandnes
2
3
Svendson
Pettersen
Tove
Kari
Borgvn 23
Storgt 20
Sandnes
Stavanger
The table above contains three records (one for each person) and five columns (P_Id,
LastName, FirstName, Address, and City).
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
The following SQL statement will select all the records in the "Persons" table:
SELECT * FROM Persons
In this tutorial we will teach you all about the different SQL statements.
The DDL part of SQL permits database tables to be created or deleted. It also define
indexes (keys), specify links between tables, and impose constraints between tables. The
most important DDL statements in SQL are:
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
Now we want to select the content of the columns named "LastName" and "FirstName"
from the table above.
We use the following SELECT statement:
SELECT LastName,FirstName FROM Persons
The result-set will look like this:
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
Tip: The asterisk (*) is a quick way of selecting all columns!
The result-set will look like this:
P_Id
1
2
3
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
FROM table_name
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
Now we want to select only the distinct values from the column named "City" from the
table above.
We use the following SELECT statement:
SELECT DISTINCT City FROM Persons
The result-set will look like this:
City
Sandnes
Stavanger
The WHERE clause is used to filter records.
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
Now we want to select only the persons living in the city "Sandnes" from the table above.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City='Sandnes'
The result-set will look like this:
P_Id
1
2
LastName
Hansen
Svendson
FirstName
Ola
Tove
Address
Timoteivn 10
Borgvn 23
City
Sandnes
Sandnes
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
Now we want to select only the persons with the first name equal to "Tove" AND the last
name equal to "Svendson":
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'
The result-set will look like this:
P_Id
2
LastName
Svendson
FirstName
Tove
Address
Borgvn 23
City
Sandnes
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first
name equal to "Ola":
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Tove'
OR FirstName='Ola'
The result-set will look like this:
P_Id
1
2
LastName
Hansen
Svendson
FirstName
Ola
Tove
Address
Timoteivn 10
Borgvn 23
City
Sandnes
Sandnes
LastName
Svendson
FirstName
Tove
Address
Borgvn 23
City
Sandnes
ORDER BY Example
The "Persons" table:
P_Id
1
2
3
4
LastName
Hansen
Svendson
Pettersen
Nilsen
FirstName
Ola
Tove
Kari
Tom
Address
Timoteivn 10
Borgvn 23
Storgt 20
Vingvn 23
City
Sandnes
Sandnes
Stavanger
Stavanger
Now we want to select all the persons from the table above, however, we want to sort the
persons by their last name.
We use the following SELECT statement:
SELECT * FROM Persons
ORDER BY LastName
The result-set will look like this:
P_Id
1
4
3
2
LastName
Hansen
Nilsen
Pettersen
Svendson
FirstName
Ola
Tom
Kari
Tove
Address
Timoteivn 10
Vingvn 23
Storgt 20
Borgvn 23
City
Sandnes
Stavanger
Stavanger
Sandnes
LastName
Svendson
Pettersen
Nilsen
Hansen
FirstName
Tove
Kari
Tom
Ola
Address
Borgvn 23
Storgt 20
Vingvn 23
Timoteivn 10
City
Sandnes
Stavanger
Stavanger
Sandnes
Primer:
ContactName
Maria Anders
Christina Berglund
Francisco Chang
Roland Mendel
Diego Roel
Address
Obere Str. 57
Berguvsvgen 8
Sierras de Granada
9993
Kirchgasse 6
C/ Moralzarzal, 86
City
Berlin
Lule
Mxico D.F.
Graz
Madrid
10
Eduardo Saavedra
Helen Bennett
Kniglich Essen
Laughing Bacchus Wine
Cellars
Magazzini Alimentari
Riuniti
North/South
Philip Cramer
Yoshi Tannamuri
Paris spcialits
Marie Bertrand
Rattlesnake Canyon
Grocery
Simons bistro
The Big Cheese
Paula Wilson
Vaffeljernet
Wolski Zajazd
Palle Ibsen
Zbyszek
Piestrzeniewicz
Giovanni Rovelli
Simon Crowther
Jytte Petersen
Liz Nixon
Bergamo
Vinbltet 34
89 Jefferson Way Suite
2
Smagslget 45
ul. Filtrowa 68
Kbenhavn
Portland
London
Paris
Albuquerque
rhus
Warszawa
To preserve space, the table above is a subset of the Customers table used in the example
below.
Try it Yourself
To see how SQL works, you can copy the SQL statements below and paste them into the
textarea, or you can make your own SQL statements.
SELECT * FROM customers
SELECT CompanyName, ContactName FROM customers
SELECT * FROM customers WHERE companyname LIKE 'a%'
SELECT CompanyName, ContactName
FROM customers
WHERE CompanyName > 'a'
When using SQL on text data, "alfred" is greater than "a" (like in a dictionary).
SELECT CompanyName, ContactName
11
FROM customers
WHERE CompanyName > 'g'
AND ContactName > 'g'
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
Now we want to select the persons living in a city that starts with "s" from the table
above.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE 's%'
The "%" sign can be used to define wildcards (missing letters in the pattern) both before
and after the pattern.
The result-set will look like this:
P_Id
1
2
3
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
12
Next, we want to select the persons living in a city that ends with an "s" from the
"Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE '%s'
The result-set will look like this:
P_Id
1
2
LastName
Hansen
Svendson
FirstName
Ola
Tove
Address
Timoteivn 10
Borgvn 23
City
Sandnes
Sandnes
Next, we want to select the persons living in a city that contains the pattern "tav" from the
"Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE '%tav%'
The result-set will look like this:
P_Id
3
LastName
Pettersen
FirstName
Kari
Address
Storgt 20
City
Stavanger
It is also possible to select the persons living in a city that NOT contains the pattern "tav"
from the "Persons" table, by using the NOT keyword.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City NOT LIKE '%tav%'
The result-set will look like this:
P_Id
1
2
LastName
Hansen
Svendson
FirstName
Ola
Tove
Address
Timoteivn 10
Borgvn 23
City
Sandnes
Sandnes
The IN Operator
13
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
IN Operator Example
The "Persons" table:
P_Id
1
2
3
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
Now we want to select the persons with a last name equal to "Hansen" or "Pettersen"
from the table above.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
The result-set will look like this:
P_Id
1
3
LastName
Hansen
Pettersen
FirstName
Ola
Kari
Address
Timoteivn 10
Storgt 20
City
Sandnes
Stavanger
14
LastName
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
Now we want to select the persons with a last name alphabetically between "Hansen" and
"Pettersen" from the table above.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE LastName
BETWEEN 'Hansen' AND 'Pettersen'
The result-set will look like this:
P_Id
1
LastName
Hansen
FirstName
Ola
Address
Timoteivn 10
City
Sandnes
15
Example 2
To display the persons outside the range in the previous example, use NOT BETWEEN:
SELECT * FROM Persons
WHERE LastName
NOT BETWEEN 'Hansen' AND 'Pettersen'
The result-set will look like this:
P_Id
2
3
LastName
Svendson
Pettersen
FirstName
Tove
Kari
Address
Borgvn 23
Storgt 20
City
Sandnes
Stavanger
SQL Functions
16
Tip: The aggregate functions and the scalar functions will be explained in details in the
next chapters.
he AVG() Function
The AVG() function returns the average value of a numeric column.
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
17
Now we want to find the customers that have an OrderPrice value higher then the average
OrderPrice value.
We use the following SQL statement:
SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
The result-set will look like this:
Customer
Hansen
Nilsen
Jensen
The COUNT() function returns the number of rows that matches a specified criteria.
18
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
Now we want to count the number of unique customers in the "Orders" table.
We use the following SQL statement:
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
The result-set will look like this:
NumberOfCustomers
3
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders"
table.
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
FirstOrderPrice
1000
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
OrderDate
2008/11/12
2008/10/23
OrderPrice
1000
1600
Customer
Hansen
Nilsen
22
3
4
5
6
2008/09/02
2008/09/03
2008/08/30
2008/10/04
700
300
2000
100
Hansen
Hansen
Jensen
Nilsen
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
23
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
The result-set will look like this:
Customer
SUM(OrderPrice)
24
Hansen
Nilsen
Jensen
2000
1700
2000
SUM(OrderPrice)
5700
5700
5700
5700
5700
5700
25
OrderDate
2008/11/12
2008/10/23
2008/09/02
2008/09/03
2008/08/30
2008/10/04
OrderPrice
1000
1600
700
300
2000
100
Customer
Hansen
Nilsen
Hansen
Hansen
Jensen
Nilsen
Now we want to find if any of the customers have a total order of less than 2000.
We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
The result-set will look like this:
Customer
Nilsen
SUM(OrderPrice)
1700
Now we want to find if the customers "Hansen" or "Jensen" have a total order of more
than 1500.
We add an ordinary WHERE clause to the SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
26
SUM(OrderPrice)
2000
2000
27