SQL DML and DDL
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).
The query and update commands form the DML part of SQL:
- 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 define indexes (keys), specify links between tables, and impose 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
- CREATE INDEX - creates an index (search key)
- DROP INDEX - deletes an index
Operators Allowed in the WHERE Clause
With the WHERE clause, the following operators can be used:
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 | If you know the exact value you want to return for at least one of the columns |
Note: In some versions of SQL the <> operator may be written as !=
The AND & OR Operators
The AND & OR operators are used to filter records based on more than one condition.
The AND operator displays a record if both the first condition and the second condition is 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_Id | 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_Id | 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_Id | 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":
We use the following SELECT statement:
SELECT * FROM Persons WHERE LastName='Svendson' AND (FirstName='Tove' OR FirstName='Ola') |
The result-set will look like this:
P_Id | LastName | FirstName | Address | City |
2 | Svendson | Tove | Borgvn 23 | Sandnes |
SQL Joins
SQL joins are used to query data from two or more tables,
based on a relationship between certain columns in these tables.
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_Id |
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.
Next, we have the "Orders" table:
O_Id |
OrderNo |
P_Id |
---|
1 |
77895 |
3 |
2 |
44678 |
3 |
3 |
22456 |
1 |
4 |
24562 |
1 |
5 |
34764 |
15 |
Note that the "O_Id" column is the primary key in the "Orders" table
and that the "P_Id" column refers to the
persons in the "Persons" table without using their names.
Notice that the relationship between the two tables above is the "P_Id"
column.
Different SQL JOINs
Before we continue with examples, we will list the types of JOIN you can use,
and the differences between them.
- JOIN: Return rows when there is at least one match in both
tables
- LEFT JOIN: Return all rows from the left table, even if
there are no matches in the right table
- RIGHT JOIN: Return all rows from the right table, even
if there are no matches in the left table
- FULL JOIN: Return rows when there is a match in one of
the tables
SQL INNER JOIN Example
The "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 |
The "Orders" table:
O_Id |
OrderNo |
P_Id |
---|
1 |
77895 |
3 |
2 |
44678 |
3 |
3 |
22456 |
1 |
4 |
24562 |
1 |
5 |
34764 |
15 |
Now we want to list all the persons with any orders.
We use
the following SELECT
statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER 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 |
The INNER JOIN keyword return rows when there is at least one match in both
tables. If there are rows in
"Persons" that do not have matches in "Orders", those rows will
NOT be
listed.