LectureFour - Relational Database Queries
LectureFour - Relational Database Queries
AND DATABASE
DESIGN
DATABASEQUERIES 142
LECTURE OUTLINE
• Query Definition
• Introduction to SQL
• SQL Syntax
• SQL in Action
Image Source: GIS Geography
2
3. QUERYING A RELATIONAL
DATABASE
3. QUERYING A RELATIONAL DATABASE(1)
What is a Query?
• A query is a question you ask of your database
• You can:
– Display data from multiple tables
– Control which fields display
– Perform calculations on field values
– Save a query automatically
3. QUERYING A RELATIONAL DATABASE(2)
What is SQL?
• SQL stands for Structured Query Language
• SQL is an ANSI (American National Standards Institute) standard
computer language for accessing and manipulating database
systems.
• SQL statements are used to retrieve and update data in a database.
• SQL works with database programs like MS Access, DB2, Informix,
MS SQL Server, Oracle, Sybase, etc.
Query
SELECT * FROM Ownership
Result
WHERE Clause
• The WHERE clause is used to specify a selection
criterion
• i.e. conditionally select data from a table, a WHERE
clause is added to the SELECT statement.
3. QUERYING A RELATIONAL DATABASE(11)
Database
3. QUERYING A RELATIONAL
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
3. QUERYING A RELATIONAL DATABASE(13)
Query1
3. QUERYING A RELATIONAL DATABASE(14)
DATABASE(15)
3. QUERYING A RELATIONAL DATABASE(16)
• Example SQL for an attribute projection
DATABASE(19)
3. QUERYING A RELATIONAL
DATABASE(19)
3. QUERYING A RELATIONAL
DATABASE(19)
Cartesian product
• Cartesian product of P and PP, written P x PP the
relation obtained from the
concatenating(‘glueing’) any tuple of P with any
tuple of PP
• the resulting tuple has all the attributes of P and all
those of PP
• Dangerous operation: if P has 2500 tuples and PP has
5000 tuples P x PP will have 12 500 000 tuples •
Queries with four or more input relations are not
uncommon
• With N input relations must typically have N-1 join
3. QUERYING A RELATIONAL
DATABASE(20)
3. QUERYING A RELATIONAL DATABASE(20)
The LIKE condition
• The LIKE condition is used to specify a search for a
pattern in a column.
• A “*" sign can be used to define wildcards (missing
letters in the pattern) both before and after the
pattern.
Syntax
SELECT column
FROM table
WHERE column LIKE pattern
3. QUERYING A RELATIONAL
DATABASE(20)
3. QUERYING A RELATIONAL DATABASE(20)
• The LIKE condition
• The following SQL statement will return persons with surname thatstart with an ‘R':
• SELECT *
• FROM Ownership
• WHERE surname LIKE ‘R*’
Query3
3. QUERYING A RELATIONAL DATABASE(20)
• The INSERT INTO Statement
• The INSERT INTO statement is used to insert new rowsinto a table.
• Syntax
• INSERT INTOtable_name VALUES (value1, value2,....)
• You can also specify the columns for which you want to insert data: •
INSERT INTOtable_name (column1, column2,...)VALUES (value1, value2,....)
Statement
Query8
3. QUERYING A RELATIONAL DATABASE(20)
The Update Statement
• The UPDATE statement is used to modify the data in
a table
Syntax
SQL IN
• The IN operator may be used if you know the exact
value you want to return for at least one of the
columns
SELECT *
FROM Ownership
WHERE surname BETWEEN ‘Mazonde'AND
‘Sydney‘;
3. SUMMARY QUERYING A
RELATIONAL
DATABASE(21)