32
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007

Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

Chapter 11.1 and 11.2Data Manipulation: Relational Algebra and SQL

Brian CobarrubiaIntroduction to Database Management SystemsOctober 4, 2007

Agenda 11.1 – Relational Algebra

Unary Operators

Binary Operators

11.2 – Structured Query Language (SQL) SQL Queries on a Single Table

SQL Queries on Binary Operators

Subqueries

Relational Algebra Unary Operators

Select Operator is used to select a horizontal subset of the tuples that satisfy a selection condition from a relation

σ<selection condition> (R) Project Operator is used to select a vertical subset of certain

attributes from a relation

Π<attribute list> (R)

Relational Algebra Binary Operators

Cartesian Product Operator Set Theoretic Operators

Union Intersection Difference

Join Operators Natural Join Operator Equijoin Operator Theta Join Operator Outer Join Operator

The Divide Operator

Relational Algebra Binary Operators

Cartesian Product Operator is used to combine tuples from any two relations in a combinatorial fashion

Relational Algebra Syntax:

RELATION_R X RELATION_S

Relational Algebra Binary Operators

Set Theoretic Operators are used to combine the tuples from two relations, which are then applied to two sets, the two relations must be union compatible

Two relations (R and S) are said to be Union Compatible if they have:

The same degree (i.e. Same number of attributes) Each pair of corresponding attributes in R and S share the same domain

The operators are Union, Intersection, and Difference Union is denoted by R U S, it is an operator that includes all tuples that

belong to either R or S or to both R and S. Duplicates are eliminated Intersection is denoted by R ∩ S, it is an operator that includes all the tuples that

are only in R and S Difference is denoted by R – S, it is an operator that includes all tuples that are in R

but not in S

Relational Algebra Join Operators

Join Operators specify how to relate tables in the query, it is used to combine related tuples from two relations into single tuples

In order to join the two relations R and S, they must be join compatible, which means that the join condition must involve attributes from R and S which share the same domain

Relational Algebra Join Operators

Natural Join Operator produces all the different combinations of the tuples from the two relations R and S that satisfy a join condition

R * <join condition> S

Relational Algebra Join Operators

Equijoin Operator is the most common join that produces all the combinations of tuples from R and S that satisfy a join condition with only equality comparisons, there will be duplication of the joining attributes

R [X]<join condition> S note that [X] also means

Let R be STAFF table and S be DEPT:

The resulting equijoin operation would produce:

Relational Algebra Join Operators

Theta Join Operator produces the combinations of tuples from R and S that satisfy a join condition which does not have to involve equality comparisons (if it did then they are called an Equijoin)

R [X]<join condition> S

Let R be the CAR relation and S be the BOAT relation if a customer wants to buy both a car and boat but doesn’t want to spend more money on a boat then a car we would use a theta join operator to give a new relation with all the possible options

CarPrice ≥ BoatPrice

Relational Algebra Join Operators

Outer Join Operators are Inner Join operations that only look at matching tuples from one relation to the other, attributes of tuples with no matching tuples are set to NULL

Left Outer Join is denoted by ]X|, and keeps every tuple in the left or first relation (R)

Right Outer Join is denoted by |X[, and keeps every tuple in the right or second relation (S)

Full Outer Join is denoted by ]X[, and keeps all the tuples in both the left and right relations when no matching tuples are found

Relational Algebra Join Operators

Outer Join examples take two relations EMPLOYEE and DEPT

Left Outer Join Right Outer Join Full Outer Join

Relational Algebra Divide Operator

Divide Operator returns every tuple from R that match all tuples in S; R and S must be division compatible

R ÷ S Let R be COMPLETED and S be DBPROJECT we can get the

result as

Structured Query Language (SQL) Overview SQL Queries based on a Single Table

Sample SQL Queries Handling null values

SQL Queries based on Binary Operators Subqueries

Structured Query Language (SQL) Overview

When using SQL with Relational Algebra rows are tuples, columns are attributes, and tables are relations

One of the core components of using SQL is the SELECT statement, the basic form of the SQL SELECT statement is called a select-from-where block that contains three clauses:

SELECT <column list> Takes the table and returns the columns listed as arguments

FROM <table list> Produces a table from the argument given

WHERE <condition> We can specify from this clause to search in the rows for specific value

types using all the comparison operators (i.e. > ≥ = ≤ <) and different conditional operations

Structured Query Language (SQL) Overview

Other useful clauses for a SELECT statement include:

GROUP BY group_by_expression Forms groups of rows with the same value

HAVING group_condition Filters the groups subject to some condition

ORDER BY column name(s) Specifies the order of the output

After your SELECT statement has been written a semi-colon ‘;’ is needed at the end to close up the statement

Structured Query Language (SQL) SQL Queries Based on a Single Table

We can use an asterisk (*) to denote that all columns from a table is to be selected

We can also use the logical operators AND OR and NOT with the WHERE clause to form more precise clauses

When writing a query the hierarchy order in any expression follows

The comparison operators (> ≥ = ≠ ≤ <)= is equals <> is not equals

>= is greater than or equal to <= is less than or equal to

> Is greater than < is less than

NOT operator AND operator OR operator

Structured Query Language (SQL) SQL Queries Based on a Single Table

Example 1

Suppose we want to know courses that are 3 hours a week

SELECT *

FROM COURSE

WHERE COURSE.CO_HOURS = 3;

Structured Query Language (SQL) SQL Queries Based on a Single Table

Example 2

Suppose we want to know all professors who make more than $6000 a month and the professors who work in a certain department (known as code 3)

SELECT *

FROM PROFESSOR

WHERE PROFESSOR.SALARY/12 > 6000

AND PROFESSOR.PR_DPT_DCODE = 3;

Structured Query Language (SQL) SQL Queries Based on a Single Table

We can also simulate a projection operation by using SELECT We can get all the colleges of a university from the co_college

column with this command

SELECT COURSE.CO_COLLEGE

FROM COURSE;

Structured Query Language (SQL) SQL Queries Based on a Single Table

Other operations to use and play with BETWEEN

Used to search for rows in a specific range of values LIKE

Used to search for a pattern in strings IN and NOT IN

IN is evaluated in the context of being “equal to any member of” a set of values

TRUNC Truncates a decimal value

ROUND Rounds a decimal value

DISTINCT Gets rid of duplicate rows

Structured Query Language (SQL) SQL Queries Based on a Single Table

Aggregate functions take a set of values as input and returns one value as output

Common Aggregate functions are:

COUNT(x) Counts the number of non-null values in a set of values

SUM(x) Sums all numbers in a set of values

AVG(x) Computes the average of a set of numbers in a set of values

MAX(x) Computes the maximum of a set of numbers in a set of values

MIN(x) Computes the minimum of a set of numbers in a set of values

Structured Query Language (SQL) SQL Queries Based on a Single Table

Example 3

Suppose we want to know the number of salaries, the total salary, average salary, the maximum, and the minimum salary from all the professors

SELECT COUNT(PR_SALARY), SUM(PR_SALARY), AVG(PR_SALARY), MAX(PR_SALARY), MIN(PR_SALARY)

FROM PROFESSOR;

Structured Query Language (SQL) SQL Queries Based on a Single Table

Handling null values A data field without a value in it contains a null value, they occurs when a

value is unknown and where a value is not meaningful If you try to query a column or row with a null value then it will include

those spaces even though there isn’t a value associated to it Must use IS NULL and IS NOT NULL for comparison operators with

null values This example will display that the query returned 9 rows

SELECT TEXTBOOK.TX_PUBLISHER

FROM TEXTBOOK;

Structured Query Language (SQL) SQL Queries Based on Binary Operators

To perform Binary Operations on tables we use the follow keywords combined with JOIN in the FROM clause

SELECT *

FROM R CROSS JOIN S;

INNER JOIN is used in the FROM clause when the Cartesian Product operation is accompanied by a Selection operation

SELECT R.some_column(s)

FROM R INNER JOIN S

ON some_condition;

ON clause is used to specify the join conditions

Structured Query Language (SQL) SQL Queries Based on Binary Operators

To perform Set Theoretic Operators in SQL we use the following:

SELECT *

FROM R

some_set_operator

SELECT *

FROM S;

Where some_set_operator can be:

UNION

INTERSECT

MINUS

Structured Query Language (SQL) SQL Queries Based on Binary Operators

To perform joins in SQL we use the JOIN statement and compare columns that match, we can also use the predefined joins for example in Natural Joins we can use either:

SELECT * FROM R NATURAL JOIN S

SELECT * FROM R

JOIN S USING (COLUMN_1, COLUMN_2, ...);

SELECT some_R_column

FROM SECTION JOIN S ON

some_R_column = some_S column,

some_R_column = some_S column,

...;

Structured Query Language (SQL) SQL Queries Based on Binary Operators

Example 4

Take a Natural Join from two tables SECTION and TAKES with this input

SELECT *

FROM SECTION NATURAL JOIN TAKES;

Structured Query Language (SQL) SQL Queries Based on Binary Operators

There is a FULL OUTER JOIN statement with the syntax

SELECT *

FROM GRAD_STUDENT FULL OUTER JOIN TAKES

ON GRAD_STUDENT.GS_ST_SID = TAKES.TK_ST_SID;

Structured Query Language (SQL) Subqueries

A complete SELECT statement that is embedded within another SELECT statement is a subquery

Subqueries are used in: The SELECT list of a SELECT statement In the FORM clause In the WHERE clause In the ORDER BY clause

We can use the comparison operators in combination with the ALL or ANY operator to treat the subquery as a set of values rather than individual values

Structured Query Language (SQL) Subqueries

Example 4

Displays the name and salaries of those professors who earn more than all professors in department number 3

SELECT PROFESSOR.PR_NAME, PROFESSOR.PR_SALARY

FROM PROFESSOR

WHERE PROFESSOR.PR_SALARY > ALL

(SELECT PROFESSOR.PR_SALARY

FROM PROFESSOR

WHERE PROFESSOR.PR_DPT_DCODE = 3);

The End