Week 6 Flashcards
Join
a relational operation that connects 2+ tables with a common domain
combined into a single table/view
Equi-join
join in which the joining condition is based on equality between values in the common columns
COMMON COLUMNS WILL APPEAR REDUNDANTLY IN RESLT TABLE
Inner Join
equi-join where one of the duplicate columns is eliminated in result table
Inner join example, when would you use this?
when you need info that is in both tables
SELECT ____
FROM table 1 INNER JOIN table 2 ON t1.ID=t2.ID
NOTE THAT THE ID THAT ID BEING CONNECTED NEEDS TO BE THE SAME ID!!
if it is just JOIN
inner join is implict
Outer Join (Left, Right, Full)
A join in which rows that do not have matching values in common columns are included in the result table
Multi table join
You have to look at how the table flows and then connect the fks to each other accordingly
CONNECTED VIA INNER JOINS
Self Join Example
you oin to yourself, but basically you gotta make diff aliases
SELF JOIN CODE
SELECT s.EmployeeID [Supervisor ID], s.EmployeeName [Supervisor Name], e.EmployeeID,
e.EmployeeName, e.EmployeeSupervisor
FROM Employee_T s INNER JOIN Employee_T e on e.EmployeeSupervisor=s.EmployeeID
WHERE e.EmployeeSupervisor = s.EmployeeI
Sub query
placing an inner select statement query inside an outer query
where can you put the sub query?
a. in the condition of the WHERE CLAUSE
b. as a table of the FROM clause
c. returning a field for the SELECT clause
d. within the HAVING Clause
2 types of subqueries
non correalted: executed once for the entire outer query
correlated: execture once for each row returned by the outer query
what is the logic of subquery?
how do you have all the employees having the same supervisor as laura come?
SELECT *
FROM Employee_T
WHERE SupervisorName = (
SELECT SupervisorName
FROM Employee_T
WHERE EmployeeName LIKE ‘Laura%’)
Basically the where is the something = to something else, and then you solve for something else in the sub query
How to do a subquery in the from clause?
First off, you can alias subqueries! calling them t3 and t4
Basically you will label each sub query a diff name
SELECT t3.____, t3._______
FROM
(SELECT ____
FROM inner join the tables
WHERE _____condition) t3,
(SELECT ____
FROM inner join the tables
WHERE _____condition) t4
WHERE t3.____=t4._______
non corelated subqueries
Do not depend on data from the outer query
Execute once for the entire outer query