7.0 SQL Notes - Querying Databases
7.0 SQL Notes - Querying Databases
What is SQL
SQL stands for Structured Query Language
SQL allows you to access a database
SQL is an ANSI standard computer language
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert new records in a database
SQL can delete records from a database
SQL can update records in a database
SQL is easy to learn
Types of databases
1. SQL
2. Access
3. Oracle
4. Sybase,
5. SQL Server
6. DB2
7. Access
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.
The table above contains three records (one for each person) and four columns
(LastName, FirstName, Address, and City).
1
SQL Queries
With SQL, we can query a database and have a result set returned.
LastName
Hansen
Svendson
Pettersen
SQL (Structured Query Language) is a syntax for executing queries. But the SQL
language also includes a syntax to update, insert, and delete records.
These query and update commands together form the Data Manipulation Language
(DML) part of SQL:
The Data Definition Language (DDL) part of SQL permits database tables to be created
or deleted. We can also define indexes (keys), specify links between tables, and impose
constraints between database tables.
2
Data types
decimal(size,d) Hold numbers with fractions. The maximum number of digits are
numeric(size,d) specified in "size". The maximum number of digits to the right of the
decimal is specified in "d".
char(size) Holds a fixed length string (can contain letters, numbers, and special
characters). The fixed size is specified in parenthesis.
varchar(size) Holds a variable length string (can contain letters, numbers, and
special characters). The maximum size is specified in parenthesis.
Create a Database
To create a database:
Example
This example demonstrates how you can create a table named Person, with four columns.
The column names will be LastName, FirstName, Address, and Age:
This example demonstrates how you can specify a maximum length for
some columns:
The data type specifies what type of data the column can hold. The table below contains
the most common data types in SQL:
The INSERT INTO statement is used to insert new data into a table.
Syntax
You can also specify the columns for which you want to insert data:
4
INSERT INTO tablename (column1, column2...)
VALUES (value1, value2 ...)
The SELECT statement is used to select data from a table. The tabular result is stored in a
result table (called the result-set).
Syntax
SELECT columnname(s)
FROM tablename;
The result
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
6
Select All Columns
To select all columns from the "Persons" table, use a * symbol instead of column names,
like this:
Result
The result from a SQL query is stored in a result-set. Most database software systems
allow navigation of the result set with programming functions, like: Move-To-First-
Record, Get-Record-Content, Move-To-Next-Record, etc.
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.
Some SQL tutorials end each SQL statement with a semicolon. Is this necessary? 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.
The SELECT statement returns information from table columns. But what if we only
want to select distinct elements?
With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:
7
Syntax
To select ALL values from the column named Company we use a SELECT
statement like this:
Orders table
Company OrderNumber
Yu 3412
Safaricom 2312
Orange 4678
Safaricom 6798
Result
Company
Yu
Safaricom
Orange
Safaricom
To select only DIFFERENT values from the column named Company we use a
SELECT DISTINCT statement like this:
Result
Company
8
Yu
Safaricom
Orange
To conditionally select data from a table, a WHERE clause can be added to the SELECT
statement.
Syntax
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
To select only the persons living in the city Sandnes, we add a WHERE clause to
the SELECT statement:
Persons table
9
LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960
Result
Using Quotes
Note that we have used single quotes around the conditional values in the examples.
SQL uses single quotes around text values (most database systems will also accept
double quotes). Numeric values should not be enclosed in quotes.
This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove;
This is correct:
SELECT * FROM Persons WHERE Year>1965;
This is wrong:
SELECT * FROM Persons WHERE Year>'1965';
Syntax
10
A "%" sign can be used to define wildcards (missing letters in the pattern) both before
and after the pattern.
Using LIKE
The following SQL statement will return persons with first names that
start with an 'O':
The following SQL statement will return persons with first names that
end with an 'a':
The following SQL statement will return persons with first names that
contain the pattern 'la':
AND & OR
The AND operator displays a row if ALL conditions listed are true. The OR operator
displays a row if ANY of the conditions listed are true.
Original Table
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes
Example
Use AND to display each person with the first name equal to Tove, and
the last name equal to Svendson:
Result:
Example
Use OR to display each person with the first name equal to Tove, or
the last name equal to Svendson:
Result:
Example
You can also combine AND and OR (use parentheses to form complex
expressions):
Result
SQL IN
IN
The IN operator may be used if you know the exact value you want to
return for at least one of the columns.
12
SELECT columnname FROM tablename
WHERE columnname IN (value1,value2,..);
Example 1
Result
SQL BETWEEN
The BETWEEN ... AND operator selects a range of data between two
values. These values can be numbers, text, or dates.
Example 1
Result
Example 2
Result:
14
Syntax
UPDATE tablename
SET columnname = newvalue
WHERE columnname = somevalue;
Person:
Result:
We want to change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen';
Result
ALTER TABLE
Some database systems don't allow the dropping of a column in a database table (DROP
COLUMN columnname).
Person:
Example
Result:
Example
16
To drop the Address column in the Person table:
Result:
Syntax
Person
Delete a Row
Result
17
SQL sorting
Orders
Company OrderNumber
Orange 3412
Airtel 5678
Zain 6798
Safaricom 2312
Example
Result
Company OrderNumber
Orange 5678
Airtel 3412
Zain 6798
Safaricom 2312
Example
18
Result
Company OrderNumber
Orange 5678
Airtel 3412
Zain 2312
Safaricom 6798
Example
Result:
Company OrderNumber
Zain 6798
Zain 2312
Sega 3412
ABC Shop 5678
Example
Result:
Company OrderNumber
Zain 2312
Zain 6798
Sega 3412
ABC Shop 5678
Notice that there are two equal company names in the result above. The only time you
will see the second column in ASC order would be when there are duplicated values in
the first sort column, or a handful of nulls.
19
SQL Alias
With SQL, aliases can be used for column names and table names.
20
Family Name
Hansen Ola
Svendson Tove
Pettersen Kari
Table Employees:
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
SQL JOIN
Sometimes we have to select data from two or more tables to make our result complete.
We have to perform a join.
Tables in a database can be related to each other with keys. A primary key is a column
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.
21
In the Employees table below, the EmployeeID column is the primary key, meaning that
no two rows can have the same EmployeeID. The EmployeeID distinguishes two persons
even if they have the same name.
Employees
EmployeeID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Orders
Example
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Example
22
SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.EmployeeID=Orders.EmployeeID
AND Orders.Product='Printer';
Result
Name
Hansen, Ola
Using Joins
INNER JOIN
The INNER JOIN returns all rows from both tables where there is a match. If there are
rows in Employees that do not have matches in Orders, those rows will not be listed.
Syntax
Example
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
23
LEFT JOIN
The LEFT JOIN returns all the rows from the first table (Employees), even if there are no
matches in the second table (Orders). If there are rows in Employees that do not have
matches in Orders, those rows also will be listed.
Syntax
Example
Result
Name Product
Hansen, Ola Printer
Svendson, Tove
Svendson, Stephen Table
Svendson, Stephen Chair
Pettersen, Kari
RIGHT JOIN
The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no
matches in the first table (Employees). If there had been any rows in Orders that did not
have matches in Employees, those rows also would have been listed.
Syntax
Example
SELECT Employees.Name, Orders.Product
FROM Employees
RIGHT JOIN Orders
ON Employees.EmployeeID=Orders.EmployeeID;
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Example
SELECT Employees.Name
FROM Employees
INNER JOIN Orders
ON Employees.EmployeeID=Orders.EmployeeID
WHERE Orders.Product = 'Printer';
Result
Name
Hansen, Ola
UNION
25
The UNION command is used to select related information from two
tables, much like the JOIN command. However, when using the UNION
command all selected columns need to be of the same data type.
SQL Statement 1
UNION
SQL Statement 2;
Employees1
EmpID EmpName
1 John
2 Emilly
3 James
4 Mary
5 Johnson
Employees2
EmpID EmpName
1 Daniel
2 Donny
3 Kenny
4 Smith
5 Faith
Example
26
SELECT EmpName FROM Employees1
UNION
SELECT EmpName FROM Employees2;
This command cannot be used to list all employees in Norway and USA. In the example
above we have two employees with equal names, and only one of them is listed. The
UNION command only selects distinct values.
UNION ALL
SQL Statement 1
UNION ALL
SQL Statement 2;
Example
Create Index
Indices are created in an existing table to locate rows more quickly and efficiently. It is
possible to create an index on one or more columns of a table, and each index is given a
name. The users cannot see the indexes; they are just used to speed up queries.
Updating a table containing indexes takes more time than updating a table without, this is
because the indexes also need an update. So, it is a good idea to create indexes only on
columns that are often used for a search.
A Unique Index
Creates a unique index on a table. A unique index means that two rows cannot have the
same index value.
27
The columnname specifies the column you want indexed.
A Simple Index
Example
This example creates a simple index, named PersonIndex, on the LastName field of
the Person table:
If you want to index the values in a column in descending order, you can add the
reserved word DESC after the column name.
If you want to index more than one column you can list the column names within the
parentheses, separated by commas.
28
Drop Index
You can delete an existing index in a table with the DROP INDEX statement.
To delete a table (the table structure attributes, and indexes will also be
deleted):
To delete a database
What if we only want to get rid of the data inside a table, and not the
table itself? Use the TRUNCATE TABLE command (deletes only the data
inside the table):
SQL Functions
SQL aggregate functions return a single value, calculated from values in a column.
29
SQL Scalar functions
SQL scalar functions return a single value, based on the input value.
SQL AVG ( )
Syntax
SQL AVG ( )
Example
Sales
30
SELECT AVG(OrderPrice) FROM sales;
OrderAverage
950
Now we want to find the customers that have an OrderPrice value higher than the average
OrderPrice value.
Customer
Hansen
Nilsen
Jensen
The COUNT () function returns the number of rows that matches a specified
criteria.
31
SQL COUNT (DISTINCT columnname) Syntax
Note: COUNT (DISTINCT) works with ORACLE and Microsoft SQL Server, but not
with Microsoft Access.
The result of the SQL statement above will be 2, because the customer
Nilsen has made 2 Sales in total:
CustomerNilsen
2
32
The result-set will look like this:
NumberOfSales
6
Now we want to count the number of unique customers in the Sales table.
NumberOfCustomers
3
This is the number of unique customers (Hansen, Nilsen, and Jensen) in the Sales table.
The FIRST () function returns the first value of the selected column.
Sales
33
Now we want to find the first value of the OrderPrice column.
FirstOrderPrice
1000
The LAST () function returns the last value of the selected column.
34
We use the following SQL statement:
LastOrderPrice
100
The MAX () function returns the largest value of the selected column.
Sales
35
The result-set will look like this:
LargestOrderPrice
2000
The MIN() function returns the smallest value of the selected column.
Sales
SmallestOrderPrice
100
Sales
OrderTotal
5700
The GROUP BY statement is used in conjunction with the aggregate functions to group
the result-set by one or more columns.
Sales
Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
Result
Customer SUM(OrderPrice)
38
Hansen 5700
Nilsen 5700
Hansen 5700
Hansen 5700
Jensen 5700
Nilsen 5700
Explanation of why the above SELECT statement cannot be used: The SELECT
statement above has two columns specified (Customer and SUM (OrderPrice). The SUM
(OrderPrice) returns a single value (that is the total sum of the OrderPrice column), while
Customer returns 6 values (one value for each row in the Sales table). This will therefore
not give us the correct result. However, you have seen that the GROUP BY statement
solves this problem.
We can also use the GROUP BY statement on more than one column,
like this:
The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions.
Sales
39
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
Result
Customer SUM(OrderPrice)
Nilsen 1700
Result
Customer SUM(OrderPrice)
Hansen 2000
Jensen 2000
Now we want to select the content of the LastName and FirstName columns above, and
convert the LastName column to uppercase.
LastName FirstName
HANSEN Ola
SVENDSON Tove
PETTERSEN Kari
Persons
Result
LastName FirstName
Hansen Ola
Venison Tove
Petersen Kari
Parameter Description
columnname Required. The field to extract characters from
Start Required. Specifies the starting position (starts at 1)
Length Optional. The number of characters to return. If omitted, the MID()
function returns the rest of the text
Persons
Now we want to extract the first four characters of the "City" column above.
Result
SmallCity
Sand
Sand
Stav
The LEN () function returns the length of the value in a text field.
Persons
Now we want to select the length of the values in the Address column above.
43
We use the following SELECT statement:
LengthOfAddress
12
9
9
The ROUND () function is used to round a numeric field to the number of decimals
specified.
Parameter Description
columnname Required. The field to round.
decimals Required. Specifies the number of decimals to be returned.
Products
Result
44
ProductName UnitPrice
Bread 10
Margarine 33
Milk 16
The NOW () function returns the current system date and time.
Parameter Description
columnname Required. The field to be formatted.
Format Required. Specifies the format.
Now we want to display the products and prices per today's date (with today's date
displayed in the following format YYYY-MM-DD).
46
SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Each table should have a primary key, and each table can have only ONE primary key.
The following SQL creates a PRIMARY KEY on the PId column when the Persons table
is created:
MySQL:
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns.
To create a PRIMARY KEY constraint on the "P_Id" column when the table is already
created, use the following SQL:
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
Note: If you use the ALTER TABLE statement to add a primary key, the primary key
column(s) must already have been declared to not contain NULL values (when the table
was first created).
MySQL:
48
SQL FOREIGN KEY Constraint
Let's illustrate the foreign key with an example. Look at the following two tables:
Sales
Note that the PId column in the "Sales" table points to the "P_Id" column in the "Persons"
table.
The PId column in the Persons table is the PRIMARY KEY in the Persons table.
The PId column in the Sales table is a FOREIGN KEY in the Sales table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
The FOREIGN KEY constraint also prevents that invalid data form being inserted into
the foreign key column, because it has to be one of the values contained in the table it
points to.
49
The following SQL creates a FOREIGN KEY on the PId column when the Sales table is
created:
MySQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
To create a FOREIGN KEY constraint on the "P_Id" column when the "Sales" table is
already created, use the following SQL:
MySQL:
51