SQL Joins-Anran Xing and Michelle Tin
SQL Joins-Anran Xing and Michelle Tin
An interface which helps you communicate with your system’s database using queries.
Basic SQL Queries
Students
Name
Mary
Jack
Ben
Ex: student_name_race_sex_state_by_term_level …
Select *
Select A.*, B.*
Select A.name, B.*
Select A.*, B.grade
Left Join Example
Left Join Multiple Tables
Courses
Grades
Colleges Levels
select distinct A.*, B.id as St_id, B.grade, college_name as school, level_decode as level
from courses A
left join grades as B on A.id=B.id and A.course=B.course
left join colleges on college=college_code
left join levels on level=level_code
where term=‘Fall 2019’
Left Join Examples
Apply conditions with ON vs WHERE statements
• In SQL, the 'WHERE' and 'ON' clauses are both conditional statements
• the ‘WHERE’ clause is used in select statements for specifying the conditions
• the 'ON’ clause is used in joins
• it verifies or checks if the records are matched in the target and source
tables before the tables are joined
Left Join Examples
Filter in the ON clause
Courses
Grades
Courses
Grades
Except function
select distinct term, id, course Term Id Course
Fall 2019 01 Math 101
from courses
Fall 2019 02 Math 101
except
Fall 2019 03 CS 511
select distinct term, id, course
from grades
Questions?
• Anran Xing, Institutional Research Analyst
[email protected]