Week 7 - SQL Flashcards
What is inner join?
It matches the PK with the FK if a matching tuple exists. Ie. FK is not NULL.
What is equijoin?
It is a type of inner join that matches common values using the equality operator ‘=’
EQUIJOIN R1.PK = R2.PK
Show the employees who are working in the department ‘Research’.
SELECT Fname, Lname, Address
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE Dname=’Research’;
What is the OUTER JOIN operator?
returns all rows from both the participating tables regardless of whether there the FK matches the PK, ie. returns tuples where the FK is NULL.
What is the LEFT OUTER JOIN operator?
Every tuple (row) in the relation (table1) for the LHS must appear in the result & if no matching tuple exists in the RHS (table 2), add NULL values.
What is the RIGHT OUTER JOIN operator?
Every tuple (row) in the relation (table2) for the RHS must appear in the result & if no matching tuple exists in the LHS (table2), add NULL values.
Query 1: Show the last name of an employee and the last name of their supervisor, if there exists!
SELECT E.Lname, S.Lname
FROM (EMPLOYEE AS E LEFT OUTER JOIN EMPLOYEE AS S ON E.SUPER_SSN = S.SSN)
What are we expecting from the below query? What knowledge do we extract?
SELECT E.Fname, E.Minit, E.Lname, D.Dname
FROM EMPLOYEE AS E LEFT OUTER JOIN DEPARTMENT AS D ON E.SSN = D.MGR_SSN
Dname of employees that are managers & NULL for employees who aren’t managers.
What aggregate function would you use to calculate how many employees are working in Dept 5?
COUNT( * )
Which aggregate function would you use to sum up all salaries of employees in Dept 5.
SUM ( X )
What aggregate function would you use to find out the youngest employee in department 5?
MAX( X ) / MIN ( X )
What aggregate function would you use to find the average salary of employees in Dept. 5.
AVG( X )
What aggregation function would you use to find the correlation between Age and Salary of employees in Dept. 5
CORR(X, Y)
Show the maximum, minimum and average salary of those
employees who work in Dept. 5
SELECT MAX (Salary) AS Highest_Sal,
MIN (Salary) AS Lowest_Sal,
AVG (Salary) AS Average_Sal
FROM EMPLOYEE
WHERE DNO = 5;
Which aggregate function does NOT discared NULL values?
COUNT(*)