0% found this document useful (0 votes)
12 views47 pages

LectureFour - Relational Database Queries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views47 pages

LectureFour - Relational Database Queries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

SPATIAL DATABASES

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.

Note: Most of the SQL database programs also have their


own proprietary extensions in addition to the SQL standard
3. QUERYING A RELATIONAL DATABASE(3)
There are two basic SQL data sublanguages:
1. SQL Data Manipulation Language (DML)
• SQL is a syntax for executing queries
• also includes a syntax to update, insert, and delete records
• These query and update commands together form the Data
Manipulation Language (DML)
• SELECT - extracts data from a database table
• UPDATE - updates data in a database table
• DELETE - deletes data from a database table
• INSERT INTO - inserts new data into a database table
3. QUERYING A RELATIONAL DATABASE(4)

2. SQL Data Definition Language (DDL)


• The Data Definition Language (DDL) permits database tables to be
created or deleted
• also define indexes (keys),specify links between tables, and impose
constraints between database tables.
• The most important DDL statements in SQL are:
– CREATE TABLE - creates a new database table
– ALTER TABLE - alters(changes) a database table
– DROP TABLE - deletes a database table
– CREATE INDEX - creates an index (search key)
– DROP INDEX - deletes an index
3. QUERYING A RELATIONAL DATABASE(5)

The format is:


SELECT attributes
FROM table
WHERE condition
• In which attributes is the list of attribute names and expressions
for which values are to be retrieved by the query
• table is a name of the relation, the so called input relation that
we want to query
• condition is a conditional (boolean) selection condition that
identifies which tuples are to be retrieved by the query
3. QUERYING A RELATIONAL DATABASE(6)

The SQL SELECT Statement


• 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 column_name(s)
FROM table_name
3. QUERYING A RELATIONAL DATABASE(7)

Select All Columns


• To select all columns from the “Ownership" table, use
a * symbol instead of column names, like this:
Syntax
SELECT * FROM Ownership

3. QUERYING A RELATIONAL DATABASE(8)

Query
SELECT * FROM Ownership
Result

3. QUERYING A RELATIONAL DATABASE(9)

Semicolon after SQL Statements?


• 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.
• For MS Access the semicolon after each SQL
statement are optional, but some database
programs force you to use it.
3. QUERYING A RELATIONAL DATABASE(10)

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

DATABASE(12) 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
3. QUERYING A RELATIONAL DATABASE(13)

Using the WHERE Clause


• To select only the property owners living in
‘Mabvuku’, we add a WHERE clause to the
SELECT statement:
SELECT *
FROM Parcel
WHERE Location=‘Mabvuku‘

Query1
3. QUERYING A RELATIONAL DATABASE(14)

Using the WHERE Clause


SELECT *
FROM Parcel
WHERE Location=‘Mabvuku’

• 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
3. QUERYING A RELATIONAL

DATABASE(15)
3. QUERYING A RELATIONAL DATABASE(16)
• Example SQL for an attribute projection

SELECT FirstName, LastName


FROM Persons
Attributes to be included

The relation from which the selection is made from


3. QUERYING A RELATIONAL
DATABASE(17)
3. QUERYING A RELATIONAL DATABASE(18)
3. QUERYING A RELATIONAL DATABASE(19)
3. QUERYING A RELATIONAL DATABASE(19)
3. QUERYING A RELATIONAL DATABASE(19)
3. QUERYING A RELATIONAL

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,....)

3. QUERYING A RELATIONAL DATABASE(20)

The INSERT INTO Statement


• INSERT INTO Ownership(TaxID, Surname, BirthDate)
VALUES('5', 'Sydney', 12/12/2011);
Query6
3. QUERYING A RELATIONAL DATABASE(20)

The SELECT DISTINCT Statement


• The DISTINCT keyword is used to return only
distinct (different) values
• 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:
3. QUERYING A RELATIONAL DATABASE(20)

The SELECT DISTINCT Statement


Syntax

SELECT DISTINCT column_name(s)


FROM table_name
3. QUERYING A RELATIONAL
DATABASE(20) The SELECT DISTINCT

Statement

SELECT DISTINCT Location


FROM Parcel

Query8
3. QUERYING A RELATIONAL DATABASE(20)
The Update Statement
• The UPDATE statement is used to modify the data in
a table

Syntax

UPDATE table_nameSET column_name =


new_valueWHERE column_name = some_value
3. QUERYING A RELATIONAL

DATABASE(20) The Update Statement

UPDATE Parcel SETAreaSize= 500


WHERE PID= 33

UPDATE Parcel SET Location=


‘Marlborough', PID= 1000
WHEREAreaSize= 500
3. QUERYING A RELATIONAL

DATABASE(20) The SQL ORDER BY CLAUSE

The ORDER BY keyword is used to sort the result.


Sort the Rows

SELECT Location, PID FROM Parcel ORDER BY


PID
3. QUERYING A RELATIONAL

DATABASE(20) SQL AND & OR

• AND and OR join two or more conditions in a


WHERE clause

• TheAND operator displays a record or records if


ALL conditions listed are true. The OR operator
displays a record ifANY of the conditions listed
are true
3. QUERYING A RELATIONAL DATABASE(20)
SQL AND & OR
• AND and OR join two or more conditions in a
WHERE clause
• UseAND to display each person with the
surname equal to “Mazonde", and the TaxID
equal to 101

SELECT surname, TaxID


FROM Ownership
WHERE surname=‘Mazonde’ OR
TaxID = 50
3. QUERYING A RELATIONAL DATABASE(20)

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 surname, TaxID


FROM Ownership
WHERE surname IN (‘Mazonde’, ’Rumeu’)
3. QUERYING A RELATIONAL

DATABASE(20) SQL BETWEEN ... AND

The BETWEEN ... AND operator selects a range of


data between two values.
These values can be numbers, text, or dates.
Syntax
SELECT column_name
FROM table_nameWHERE column_nameBETWEEN
value1 AND value2
3. QUERYING A RELATIONAL

DATABASE(20) SQL BETWEEN ... AND

SELECT *
FROM Ownership
WHERE surname BETWEEN ‘Mazonde'AND
‘Sydney‘;
3. SUMMARY QUERYING A
RELATIONAL
DATABASE(21)

Common questions

Powered by AI

In SQL, the ORDER BY operator is used to sort the results of a query in either ascending or descending order based on one or more columns. The syntax is SELECT column_names FROM table_name ORDER BY column_name ASC|DESC. The AND operator is used to ensure that multiple conditions in a WHERE clause are all true for the records to be selected, meaning it filters results by applying a series of conditions that must all match. Conversely, the OR operator allows records to be selected if any one of the conditions applied is true, broadening the filter scope. These operators combined enable fine-tuning and complex dictation of the criteria for query results to match specific needs .

Relying on the Cartesian product in SQL can result in significant inefficiencies and performance issues, particularly with large datasets. The Cartesian product operates by concatenating every tuple from one table with every tuple from another, leading to exponential growth in the number of resulting tuples. This means that with large tables, such as a table P with 2500 tuples and table PP with 5000 tuples, their Cartesian product will produce 12,500,000 tuples. This scale can severely strain database resources, impacting query performance and system responsiveness. Consequently, it is crucial to be cautious when using Cartesian products, especially in queries involving multiple relations .

The WHERE clause in SQL is used to specify a selection criterion, allowing for the conditional selection of data from a table. It is an integral part of the SELECT statement, enabling the retrieval of specific data by applying logical conditions. Operators that can be used within a WHERE clause include equal (=), not equal (<>), greater than (>), less than (<), among others such as BETWEEN for specifying a range and LIKE for pattern searching. For instance, to select property owners living in 'Mabvuku', one would use: SELECT * FROM Parcel WHERE Location='Mabvuku' .

The SQL LIKE condition is used for pattern matching in a column. It allows for the retrieval of rows that match a specified pattern, thereby facilitating searches for textual data where exact matches are not feasible. Wildcards such as the asterisk (*) can be used to represent missing characters or strings within the pattern. For example, to return records with a surname starting with 'R', the SQL statement would be: SELECT * FROM Ownership WHERE surname LIKE 'R*'. This allows the query to match any surname beginning with the letter 'R' .

The two basic SQL data sublanguages are the SQL Data Manipulation Language (DML) and the SQL Data Definition Language (DDL). DML is used for executing queries and includes syntax for updating, inserting, and deleting records. It consists of commands like SELECT to extract data, UPDATE to modify data, DELETE to remove data, and INSERT INTO to add new data into database tables. The DDL permits the creation or deletion of database tables, defining indexes (keys), specifying links between tables, and imposing constraints. Some of its main statements are CREATE TABLE for creating tables, ALTER TABLE for changing tables, DROP TABLE for deleting tables, CREATE INDEX for creating an index, and DROP INDEX for deleting an index .

In SQL, single quotes are used to enclose text values as part of standard practice across most database systems. This is because SQL interprets text within single quotes as string literals. Although many databases also accept double quotes, the primary SQL standard promotes single quotes for text, ensuring consistency and avoiding syntax errors that may arise from database-specific configurations. For instance, the query SELECT * FROM Parcel WHERE Location='Mabvuku' uses single quotes around the text value 'Mabvuku' .

The SQL BETWEEN ... AND operator is used to select a range of data between two values inclusively. This operator is beneficial for filtering results according to specified numerical, textual, or date ranges, thereby simplifying queries that require results within specific intervals. The syntax is SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2. It is versatile and applicable to numbers, text, and date values, facilitating targeted data retrieval in queries such as filtering records within a date range or selecting numeric values within a certain threshold .

The SQL SELECT DISTINCT statement is used to return only distinct (different) values from the specified columns of a database table. This statement ensures that duplicate records are not included in the result set of a query. It is particularly useful when the goal is to list unique values or when data redundancy may lead to incorrect analyses. For instance, to select distinct locations from the Parcel table, the following query can be used: SELECT DISTINCT Location FROM Parcel .

The SQL INSERT INTO statement is used to add new rows into a database table. It supports two main formats for inserting data. The first format is for inserting values directly into all columns, using the syntax: INSERT INTO table_name VALUES (value1, value2, ...). The second format allows specifying specific columns for insertion, using the syntax: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...). This flexibility enables the insertion of partial data when only certain fields need updating .

SQL manages multi-statement execution by requiring a separation between consecutive SQL statements, which is typically done using a semicolon. The semicolon acts as a statement terminator, enabling multiple statements to be sent to the server in a single request. While some database systems, like MS Access, treat the semicolon as optional, others mandate its use to ensure clarity and correctness in SQL execution order. As a best practice, it is advised to use the semicolon to avoid any ambiguity and potential errors in environments that require explicit statement separation .

You might also like