DB Basics, Cross, Natural, Inner, Outer, Theta Join
In this blog-post I'll try to go from formal notions in Relational Algebra to the practical SQL using the same queries as in https://it-tuff.blogspot.com/2019/07/relational-algebra-db-basics-select.html.
Prerequisites for practical learning:
- install mysq or mariadb server
- RA Relation is table in SQL and tables are in database:
- CREATE DATABASE Test;
- USE Test;
- SHOW DATABASES;
- RA key is PRIMARY KEY in SQL, RA Atribute is column in SQL and RA Tuple is row in SQL. To fill table we first must create it's schema:
- Data types:
- VARCHAR - used for storing alphabetic or mixed alpha-numeric data
- INTEGER - storing whole numbers from ~ -2billions to ~+2billions
- DECIMAL - storing whole and non-whole numbers, you must specify length of number and also length of the fractional part - DECIMAL(10,4) - number length is 10 digits with 4 digits after decimal-point
- after showing data type you must show probable maximal length of that data
- CREATE TABLE College (cName VARCHAR(255), PRIMARY KEY (cName) , state VARCHAR(10), enrollment INTEGER);
- SHOW TABLES;
- CREATE TABLE Student (sID INTEGER, PRIMARY KEY(sID), sName VARCHAR(255), GPA DECIMAL(4,2), sizeHS INTEGER); # HS = High School
- SHOW TABLES;
- CREATE TABLE Apply (sID INTEGER, PRIMARY KEY(sID), cName VARCHAR(255), major VARCHAR(255), decision VARCHAR(20));
- SHOW TABLES;
- Now fill tables with test data:
- INSERT INTO College (cName, state, enrollment) VALUES ("Amridge", "AL", 749), ("Berkeley", "CA", 42159), ("Stanford", "CA", 43797), ("Wyoming", "WY", 2024), ("Harcum", "PA", 1425);
- INSERT INTO Student (sID, sName, GPA, sizeHS) VALUES (1001, "Nita Millwood", 3.2, 900), (1002, "Vincenzo Lyons", 3.8, 750), (1003, "Zachery Lefebvre", 2.9, 1500), (1004, "Wilbert Chan", 3.6, 1620), (1005, "Mirna Hamann", 3.9, 1000), (1006, "Delta Shutt", 2.5, 1300), (1007, "Ryan Lacefield", 3.1, 1460);
- INSERT INTO Apply (sID, cName, major, decision) VALUES (1001, "Amridge", "BA", "accept"), (1002, "Berkeley", "CS", "accept"), (1003, "Houston", "CE" ,"reject"), (1004, "Berkeley", "CS", "reject"), (1005, "Stanford", "CS", "accept");
Practicing SQL:
- In SQL RA Select and Project are combined into one operator SELECT:
- right after select we write Projection part (* means all columns/attributes)
- after Projection part we write FROM and then write table/relation name
- after table name we write WHERE with needed column/attribute parameters - this is condition of the Selection
- RA ^ (logical and) is AND in SQL
- students with GPA>3.7 :
- Select * FROM Student WHERE GPA > 3.7;
- Application for Stanford for CS major
- SELECT * FROM Apply WHERE cName="Stanford" AND major="CS"
- ID and name of students with GPA>3.7:
- SELECT sID,sName FROM Student WHERE GPA > 3.7
- In SQL RA Cross-Product is CROSS JOIN in MySQL CROSS JOIN and INNER JOIN are the same, in Oracle you can't specify ON clause for CROSS JOIN (only WHERE is allowed) and Oracle INNER JOIN allows ON clause. Also theta join is join using only WHERE condition and not using ON or USING:
- Names and GPA's of students with sizeHS>1000 who applied to CS and were rejected:
- To deeply understand this we'll compose this query step by step:
- First we'll find all students:
- SELECT * FROM Student ;
- Now we need to find applications of all students (cross-product):
- SELECT * FROM Student CROSS JOIN Apply ;
- Previous query must be filtered by the condition Student.sID=Apply.sID:
- SELECT * FROM Student CROSS JOIN Apply WHERE Student.sID=Apply.sID ;
- Add sizeHS > 1000 condition:
- SELECT* FROM Student CROSS JOIN Apply WHERE Student.sID=Apply.sID AND sizeHS>1000;
- Add two other conditions - major="CS" and decision="reject":
- SELECT * FROM Student CROSS JOIN Apply WHERE Student.sID=Apply.sID AND sizeHS>1000 AND major="CS" AND decision="Reject" ;
- Now make projection to select only sName and GPA:
- SELECT sName, GPA FROM Student CROSS JOIN Apply WHERE Student.sID=Apply.sID AND sizeHS>1000 AND major="CS" AND decision="Reject" ;
- RA Union in SQL is UNION - this operator is used to make composition of the results of two (or more) select statements:
- List of college and student names:
- SELECT cName FROM College
- UNION
- SELECT sName FROM Student;
- RA Rename operator is AS in SQL:
- List of college and student names under the name Names:
- SELECT cName AS Names FROM College
- UNION
- SELECT sName FROM Student;
- for disambiguation in self-joins (when relation/table is joined with itself):
- pairs of colleges in same state (we name 1st call of College table C1, and the second - C2):
- Only renaming tables:
- SELECT *
- FROM College AS C1
- CROSS JOIN College AS C2
- WHERE
- C1.state=C2.state AND
- C1.cName != C2.cName;
- Renaming tables and columns:
- SELECT C1.cName AS C1, C2.cName AS C2, C1.State
- FROM College AS C1
- CROSS JOIN College AS C2
- WHERE C1.state=C2.state AND
- C1.cName != C2.cName;
- Natural join operator performs cross-product operator and then enforces equality on all of the attributes with the same name (as in above cross-join example: Student.sID=Apply.sID) also natural join eliminates one copy of duplicate attributes:
- Names and GPA's of students with sizeHS>1000 who applied to CS and were rejected:
- SELECT sName, GPA
- FROM Student
- NATURAL JOIN Apply
- WHERE sizeHS>100 AND
- major="CS" AND
- decision="reject";
- The same with column and table renaming:
- SELECT St.sName, St.GPA
- FROM Student AS St
- NATURAL JOIN Apply AS Ap
- WHERE St.sizeHS>1000 AND
- Ap.major="CS" AND
- Ap.decision="reject";
- Names and GPA's of students with HS>1000 who applied to CS and were rejected to colleges with the enrollment greater than 20000:
- Using table rename and two select statements:
- SELECT S.sName, S.GPA
- FROM Student AS S
- NATURAL JOIN
- (SELECT *
- FROM Apply AS A
- NATURAL JOIN College AS C
- WHERE C.enrollment>20000 AND
- A.major="CS" AND
- A.decision="reject") AS A
- WHERE S.sizeHS>1000;
- Using several natural joins in one Select:
- SELECT sName, GPA
- FROM Student
- NATURAL JOIN Apply
- NATURAL JOIN College
- WHERE sizeHS>1000 AND
- major="CS" AND
- decision="reject";
- RA Difference operator can be simulated with LEFT JOIN in MySQL (left join adds found rows from the right side to the left side, if right side is empty then NULL values are used), here you must show which columns are used for selection:
- IDs of students who didn't apply anywhere:
- We can use ON Student.sID = Apply.sID:
- SELECT Student.sID
- FROM Student
- LEFT JOIN Apply
- ON Student.sID=Apply.sID
- WHERE Apply.sID IS NULL;
- Also "ON Student.sID=Apply.sID" = USING(sID) - when both columns have the same name:
- SELECT Student.sID
- FROM Student
- LEFT JOIN Apply
- USING(sID)
- WHERE Apply.sID IS NULL;
- MySQL RIGHT JOIN works similar to LEFT JOIN, the difference is that RIGHT JOIN uses right relation as the main, and LEFT JOIN uses left relation as the main one.
- FULL JOIN is INNER JOIN + RIGHT JOIN + LEFT JOIN
- Intersection operator can be simulated in MySQL using join and DISTINCT (show only unique values):
- Names that are both college name and student name:
- SELECT DISTINCT(sName)
- FROM Student
- INNER JOIN College
- ON sName=cName;
- Inner and Outer joins:
- Inner join show only data which is in both left and right relations (using ON or USING)
- Outer joins use on relation as the main and completes this relation with the data from the other one and all empty data filled with NULLs (LEFT and RIGHT joins are: LEFT OUTER JOIN and RIGHT OUTER JOIN)
No comments:
Post a Comment