SQL Lab Manual
SQL Lab Manual
Page 1
Fundamentals of Database Lab Manual
INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob')
The "Persons" table will now look like this:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Johan Bakken 2 Stavanger
5 Tjessem Jakob
Below is an example of a table called "Persons":
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 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.
Keep in Mind That...
SQL is not case sensitive
Some database systems require a semicolon at the end of each SQL statement.
Semicolon is the standard way to separate each SQL statement in database systems that allow more
than one SQL statement to be executed in the same call to the server.
We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL
statement, but some database programs force you to use it.
SQL DML and DDL
SQL can be divided into two parts:
The Data Manipulation Language (DML) and
The Data Definition Language (DDL).
The queries and update commands that form DML include:
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys),
specifies links between tables, and imposes constraints between tables.
The most important DDL statements in SQL are:
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
Page 2
Fundamentals of Database Lab Manual
Page 3
Fundamentals of Database Lab Manual
Page 4
Fundamentals of Database Lab Manual
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
Note: In some versions of SQL the <> operator may be written as !=
The AND & OR Operators
The AND operator displays a record if both the first condition and the second condition are true.
The OR operator displays a record if either the first condition or the second condition is true.
AND Operator Example
The "Persons" table:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 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_ I d LastName FirstName Address City
2 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_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
Combining AND & OR
You can also combine AND and OR (use parenthesis to form complex expressions).
Now we want to select only the persons with the last name equal to "Svendson" AND the first name
equal to "Tove" OR to "Ola":
Page 5
Fundamentals of Database Lab Manual
Page 6
Fundamentals of Database Lab Manual
Page 7
Fundamentals of Database Lab Manual
Page 8
Fundamentals of Database Lab Manual
Now we want to select only the two first records in the table above.
We use the following SELECT statement:
SELECT TOP 2 * FROM Persons
The result-set will look like this:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
SQL TOP PERCENT Example
The "Persons" table:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Tom Vingvn 23 Stavanger
Now we want to select only 50% of the records in the table above.
We use the following SELECT statement:
SELECT TOP 50 PERCENT * FROM Persons
The result-set will look like this:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.
SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern
LIKE Operator Example
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 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_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Next, we want to select the persons living in a city that ends with an "s" from the "Persons" table.
Page 9
Fundamentals of Database Lab Manual
Page 10
Fundamentals of Database Lab Manual
Page 11
Fundamentals of Database Lab Manual
SQL IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)FROM table_name WHERE column_name IN (value1,value2,...)
IN Operator Example
The "Persons" table:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 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_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
SQL BETWEEN Operator
The BETWEEN operator is used in a WHERE clause to select a range of data between two values.
The BETWEEN Operator
The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or
dates.
SQL BETWEEN Syntax
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2
BETWEEN Operator Example
The "Persons" table:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 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_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
Note: The BETWEEN operator is treated differently in different databases!
In some databases, persons with the LastName of "Hansen" or "Pettersen" will not be listed, because
the BETWEEN operator only selects fields that are between and excluding the test values.
In other databases, persons with the LastName of "Hansen" or "Pettersen" will be listed, because the
BETWEEN operator selects fields that are between and including the test values.
Page 12
Fundamentals of Database Lab Manual
And in other databases, persons with the LastName of "Hansen" will be listed, but "Pettersen" will not
be listed (like the example above), because the BETWEEN operator selects fields between the test
values, including the first test value and excluding the last test value.
Therefore: Check how your database treats the BETWEEN operator.
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_ I d LastName FirstName Address City
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
SQL Alias
You can give a table or a column another name by using an alias. This can be a good thing to do if you
have very long or complex table names or column names.
An alias name could be anything, but usually it is short.
SQL Alias Syntax for Tables
SELECT column_name(s) FROM table_name AS alias_name
SQL Alias Syntax for Columns
SELECT column_name AS alias_name FROM table_name
Alias Example
Assume we have a table called "Persons" and another table called "Product_Orders". We will give the
table aliases of "p" and "po" respectively.
Now we want to list all the orders that "Ola Hansen" is responsible for.
We use the following SELECT statement:
SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Orders AS po WHERE
p.LastName='Hansen' AND p.FirstName='Ola'
The same SELECT statement without aliases:
SELECT Product_Orders.OrderID, Persons.LastName, Persons.FirstName FROM Persons, Product_Orders
WHERE Persons.LastName='Hansen' AND Persons.FirstName='Ola'
As you'll see from the two SELECT statements above; aliases can make queries easier both to write and
to read.
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a
relationship between certain columns in these tables.
Tables in a database are often related to each other with keys.
A primary key is a column (or a combination of columns) with a unique value for each row. Each primary
key value must be unique within the table. The purpose is to bind data together, across tables, without
repeating all of the data in every table.
Look at the "Persons" table:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two rows can
have the same P_Id. The P_Id distinguishes two persons even if they have the same name.
Page 13
Fundamentals of Database Lab Manual
Page 14
Fundamentals of Database Lab Manual
Page 15
Fundamentals of Database Lab Manual
The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if there are no
matches in the left table (table_name1).
SQL RIGHT JOIN Syntax
SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
SQL RIGHT JOIN Example
The "Persons" table:
P_ I d LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
The "Orders" table:
O_Id OrderNo P_ I d
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
Now we want to list all the orders with containing persons - if any, from the tables above.
We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON
Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName
The result-set will look like this:
LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
34764
The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches
in the left table (Persons).
SQL FULL JOIN Keyword
The FULL JOIN keyword return rows when there is a match in one of the tables.
SQL FULL JOIN Syntax
SELECT column_name(s) FROM table_name1 FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
SQL FULL JOIN Example on the "Persons" table is:
Page 16
Fundamentals of Database Lab Manual
Page 17