Relational Algebra and Joins
Relational Algebra
Relational Algebra
Projection
π·col1, col2, β¦, coln(R)
Projection
π·Name, DoB(MUSICIAN)
MusID | Name | HomePhone | DoB |
1224 | Tom Morello | 7577777777 | 30/05/1964 |
6719 | Zack de la Rocha | 8044444444 | 12/01/1970 |
2743 | Brad Wilk | 9199999999 | 05/09/1968 |
Name | DoB |
Tom Morello | 30/05/1964 |
Zack de la Rocha | 12/01/1970 |
Brad Wilk | 05/09/1968 |
Selection
πpredicate(R)
Selection
πDoB < 01/01/1970 (MUSICIAN)
MusID | Name | HomePhone | DoB |
1224 | Tom Morello | 7577777777 | 30/05/1964 |
6719 | Zack de la Rocha | 8044444444 | 12/01/1970 |
2743 | Brad Wilk | 9199999999 | 05/09/1968 |
MusID | Name | HomePhone | DoB |
1224 | Tom Morello | 7577777777 | 30/05/1964 |
2743 | Brad Wilk | 9199999999 | 05/09/1968 |
Combining relations
SQL
Joins
SELECT statement
SELECT column(s)
FROM table(s)
WHERE condition(s)
GROUP BY column(s)
ORDER BY column(s)
LIMIT number
Joins
SELECT *
FROM country
JOIN city ON code = countrycode
ORDER BY code;
JOIN syntax
SELECT city.Name, country.Name, Region
FROM country, city
WHERE code = countrycode;οΏ½
SELECT *
FROM title
JOIN aka USING (tid)
WHERE title = 'Moana'
AND type = 'movie';
JOIN syntax
Cross product
SELECT city.Name, country.Name, Region
FROM country, city
order by countrycode;
COUNTRY and CITY joined with no join condition
Joining more than 2 tables
SELECT city.name, country.name, countrylanguage.language
FROM country
JOIN city ON code = city.countrycode
JOIN countrylanguage ON code = countrylanguage.countrycode
WHERE country.name LIKE 'C%';
Joining more than 2 tables
SELECT city.name, country.name, countrylanguage.language
FROM country
JOIN city ON code = city.countrycode
JOIN countrylanguage ON code = countrylanguage.countrycode
WHERE country.name LIKE 'C%';
Aliasing using AS
SELECT cy.name as 'City Name', cn.name, cl.language
FROM country as cn
JOIN city as cy ON cn.code = cy.countrycode
JOIN countrylanguage cl ON cn.code = cl.countrycode
WHERE cn.name LIKE 'C%';