Relational Algebra and SQL
Relational Algebra
Selection(): selects a subset of rows from relation
Projection(): Retains only wanted colums from relation
Cross-product(): Allows us to combine two relations
Set-difference(): Tuples in one relation, but not in the other
Union(): Tuples in one relation and/or in the other
Projection
Retains only attributes that are in the projection list
Also elimates the duplicate
Selection
Operations can be combined
Union and Set-difference
These operations take two input relations, which must be union-compatible.
Same number of fields
Corresponding fields have the same type
Compound Operator: Intersection
Intersection retains rows that appear in both sets
Cross Product & Join
Cross product combines two relations:
Each row of one input is merged with each row from another input
Output is a new relation with all attributes of both inputs
The renaming operator:
And Here is cross join:
Joins are compound operators involving cross product, selection and sometimes projection.
Most common type: natural join , a cross product that matches rows where attributes that appear in both relations have equal values( and we omit duplicate attributes). And there’s a projection in the final output which means duplicate attributes will be eliminated.
Condition Join(or theta-join) is a cross product with a condition.
Equi-Join is a special case of condition join where condition only contains equalities.
Not same as natural join.
Notes:
Write Department Sale Item instead of Department Item Sale. The Department and Item do not share the same attributes.
Find the employees whose salary is less than half that of their managers.
SQL
Creating Table
Inset Data
MySQL doesn’t discard duplicates. To remove them use DISTINCT in front of the projection list
LIKE clause:
% represents zero, one or multiple charactres.
_ represents a single character.
All of the aggregate functions except for COUNT(*)
ignore null values and return null if all values are null. COUNT(*)
counts the number of records.
Limit and Offset
Limit number limits the output size
Offset number skips ‘number’ records
Join tables together
The natural join automatically eliminates duplicate columns, while the inner join does not.
Outer join: include records that don’t match the join from the other table
Set Operations
UNION
shows all rows returned from teh queries(or tables)
INTESECT
shows rows that are common in the queries(or the tables)
UNION/INTESECT ALL
if you want duplicate rows in the results you need to use ALL
keyword.
Union compatible are essentially two tables or expressions that have the same number of columns and corresponding columns are of same type.
Sub-Query Comparison Operators
IN / NOT IN
Used to test whether the attribute is IN/ NOT IN the subquery list
ANY
True if any value returned meets the condition
ALL
True if all values returned meet the condition
EXISTS
True if the subquery returns one or more records
JOIN
is a bit faster than nesting queries.
Check the records in the Buyer table one by one.
ALL
must satisfy all inner conditions
More on INSERT
INSERT
Inserting records from a table
Multiple record inserts:
The UPDATE Statement
Changes existing data in tables
Order of statement is important
Specifying a WHERE
clause is important
CASE
Views
More DDL Commands
ALTER
Allows us to add or remove attributes (columns) from a relation (table)
RENAME
TRUNCATE
Same as DELETE * FROM table
Faster but cannot ROLL BACK
a TRUNCATE
command
DROP
Porentially dangerous
Kills a relation - removes the data, removes the relation
DROP TABLE TableName
Last updated