SQL Statements
SQL Statements
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
Keep in Mind That...
LastName
Hansen
FirstName
Ola
Address
Timoteivn 10
City
Sandnes
2
3
Svendson
Pettersen
Tove
Kari
Borgvn 23
Storgt 20
Sandnes
Stavanger
Now we want to insert a new row in the "Persons" table. We use the following SQL statement:
INSERT INTO Persons
VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')
The "Persons" table will now look like this:
P_Id
1
2
3
4
LastName
Hansen
Svendson
Pettersen
Nilsen
FirstName
Ola
Tove
Kari
Johan
Address
Timoteivn 10
Borgvn 23
Storgt 20
Bakken 2
City
Sandnes
Sandnes
Stavanger
Stavanger
LastName
Hansen
Svendson
Pettersen
Nilsen
Tjessem
FirstName
Ola
Tove
Kari
Johan
Jakob
Address
Timoteivn 10
Borgvn 23
Storgt 20
Bakken 2
City
Sandnes
Sandnes
Stavanger
Stavanger
FirstName
Ola
Tove
Kari
Johan
Jakob
Address
Timoteivn 10
Borgvn 23
Storgt 20
Bakken 2
Nissestien 67
City
Sandnes
Sandnes
Stavanger
Stavanger
Sandnes
LastName
Hansen
Svendson
Pettersen
Nilsen
Tjessem
Now we want to delete the person "Tjessem, Jakob" in the "Persons" table. We use the
following SQL statement:
DELETE FROM Persons
LastName
Hansen
Svendson
Pettersen
Nilsen
FirstName
Ola
Tove
Kari
Johan
Address
Timoteivn 10
Borgvn 23
Storgt 20
Bakken 2
City
Sandnes
Sandnes
Stavanger
Stavanger
LastNamez
Hansen
Svendson
Pettersen
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
4
Nilsen
Johan
Bakken 2
Stavanger
5
Tjessem
Jakob
Nissestien 67
Sandnes
SQL UPDATE Warning
Be careful when updating records. If we had omitted the WHERE clause in the example above,
like this:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
The "Persons" table would have looked like this:
P_Id
1
2
3
4
5
LastName
Hansen
Svendson
Pettersen
Nilsen
Tjessem
FirstName
Ola
Tove
Kari
Johan
Jakob
Address
Nissestien 67
Nissestien 67
Nissestien 67
Nissestien 67
Nissestien 67
City
Sandnes
Sandnes
Sandnes
Sandnes
Sandnes
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
LastName
FirstName
Address
City
1
2
3
Hansen
Svendson
Pettersen
Ola
Tove
Kari
Timoteivn 10
Borgvn 23
Storgt 20
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
WHERE Clause 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 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
Description
Equal
Not equal
Greater than
Less than
Greater than or equal
Less than or equal
Between an inclusive range
Search for a pattern
If you know the exact value you want to return for at least one of the columns
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
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
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
FirstName
Address
City
1
4
3
2
Hansen
Nilsen
Pettersen
Svendson
Ola
Tom
Kari
Tove
Timoteivn 10
Vingvn 23
Storgt 20
Borgvn 23
Sandnes
Stavanger
Stavanger
Sandnes
Description
A substitute for zero or more characters
A substitute for exactly one character
Any single character in charlist
Any single character not in charlist
[!charlist]
SQL Wildcard Examples
We have the following "Persons" table:
P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger
Using the % Wildcard
Now we want to select the persons living in a city that starts with "sa" from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE 'sa%'
The result-set will look like this:
P_Id
1
LastName
Hansen
FirstName
Ola
Address
Timoteivn 10
City
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
Next, we want to select the persons living in a city that contains the pattern "nes" from the
"Persons" table. We use the following SELECT statement:
SELECT * FROM Persons
WHERE City LIKE '%nes%'
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
Activity I
Create a database name exercise1
1. Create a table name Employee with idno integer primary key, emp_name
varchar(200) , position varchar(300) , salary double, yrs_service integer.
2. Add the following records in the employee table.
Idno
111
222
333
444
555
666
777
888
999
110
Emp_name
Juan
Maria
Pedro
Jose
Marcos
Jeffrey
Efren
Joan
Grace
Hans
Position
Clerk1
Secretary
Manager
Clerk
Supervisor
Clerk1
Accountant
Secretary
Clerk
Manager
Yrs_service
Salary
2
6000
2
8000
5
10000
1
5000
8
12000
3
7000
3
9000
4
9000
1
5500
6
11000
Pro_no
Pro_name
101
Pencil
10
103
Eraser
5
101
Pencil
104
Ballpen
103
Eraser
104
Ballpen
105
Notebook
4
102
Stapler
5
102
Stapler
6
Quatity
8
3
6
3