# Relational Algebra Relational algebra consists of a set of relational operators Each operator has one or more relations as input and creates a new relation.

## Presentation on theme: "Relational Algebra Relational algebra consists of a set of relational operators Each operator has one or more relations as input and creates a new relation."— Presentation transcript:

Relational Algebra Relational algebra consists of a set of relational operators Each operator has one or more relations as input and creates a new relation Three basic operations are: SELECTION PROJECTION JOIN Fourth operation is DIVISION

Relational Algebra (continued) u DDL (Data Definition Language) –CREATE TABLE… –ALTER TABLE… –DROP TABLE… u DML (Data Manipulation Language) –SELECT… –INSERT INTO… –DELETE…

SELECTION Selects certain tuples (rows) and forms a new relation PROJECTION Selects specified attributes (columns) and forms a new relation JOIN Combines two or more relations into a single relation joining tuples with matching attribute values Relational Algebra (continued)

Structured Query Language The basic form of a query is: SELECT some data (names of columns) FROM some place (names of tables) WHERE some conditions exist (if any) More precisely: SELECT FROM WHERE

Examples:(see handout) SELECT StoreId FROM STORES SELECT StoreId, Phone FROM STORES SELECT * FROM STORES SELECT StoreId FROM STORES WHERE LocatedInCity = ‘BOSTON’ SELECT ItemId FROM ITEMS WHERE Size NEQ ‘SMALL’ SELECT ItemId FROM ITEMS WHERE Size = ‘SMALL’ AND Weight > 10 Structured Query Language (continued)

More Examples: SELECT StoreId FROM HOLD WHERE ItemId = ‘I4’ ORDER BY StoreId ASCENDING SELECT DISTINCT ItemId FROM HOLD WHERE QtyHeld > 250 Nested Mappings in SQL SELECT ItemId FROM HOLD WHERE StoreId IN (SELECT StoreId FROM STORES WHERE LocatedInCity = ‘BOSTON’) Structured Query Language (continued)

Expressing Relational Joins SELECT ItemId FROM HOLD, STORES WHERE HOLD.StoreId = STORES.StoreId AND STORES.LocatedInCity = ‘BOSTON’ Retrievals Using EXISTS SELECT ItemId FROM HOLD WHERE EXISTS (SELECT * FROM STORES WHERE StoreId = HOLD.StoreId AND LocatedInCity = 'BOSTON') Structured Query Language (continued)

Retrieval Using ANY SELECT ItemId FROM HOLD WHERE StoreId = IN (SELECT StoreId FROM STORES WHERE LocatedInCity = ‘BOSTON’) SELECT OrderNo FROM ITEMS-ORDERED WHERE ItemId = ‘I2’ AND QtyOrdered < ANY (SELECT QtyHeld FROM HOLD WHERE ItemId = ‘I2’) “Which orders for I2 have a quantity we can fill?” Structured Query Language (continued)

Set Exclusion SELECT ItemId FROM HOLD WHERE StoreId NOT IN (SELECT StoreId FROM STORES WHERE LocatedInCity = ‘BOSTON’) “Select items not held in the Boston Stores.” Compound Conditions SELECT ItemId FROM HOLD WHERE StoreId IN (SELECT StoreId FROM STORES WHERE LocatedInCity = ‘BOSTON’) AND QtyHeld >20 Structured Query Language (continued)

More Examples: max(), min(), count(), sum(), avg(), etc… Functions SELECT AVG(QtyHeld) FROM HOLD WHERE StoreId = ‘ST-A’ Grouping and Partitioning SELECT StoreId FROM HOLD GROUP BY StoreId HAVING SUM(QtyHeld) > 500 Set Comparison CONTAINS SELECT StoreId FROM HOLD GROUP BY StoreId HAVING SET (ItemId) CONTAINS (SELECT ItemId FROM ITEMS WHERE Size = ‘SMALL’) “Which stores contain all small items?” Structured Query Language (continued)

Relational system designed for nonprogrammers About the same selective power as SQL Graphical interface Provides the user with an example output in form of a table headings and structure Returns the rows that satisfy the values in any of specified columns Query- By-Example

Download ppt "Relational Algebra Relational algebra consists of a set of relational operators Each operator has one or more relations as input and creates a new relation."

Similar presentations