Week 7 - SQL Flashcards

1
Q

What is inner join?

A

It matches the PK with the FK if a matching tuple exists. Ie. FK is not NULL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is equijoin?

A

It is a type of inner join that matches common values using the equality operator ‘=’
EQUIJOIN R1.PK = R2.PK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Show the employees who are working in the department ‘Research’.

A

SELECT Fname, Lname, Address
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE Dname=’Research’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the OUTER JOIN operator?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the LEFT OUTER JOIN operator?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the RIGHT OUTER JOIN operator?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Query 1: Show the last name of an employee and the last name of their supervisor, if there exists!

A

SELECT E.Lname, S.Lname

FROM (EMPLOYEE AS E LEFT OUTER JOIN EMPLOYEE AS S ON E.SUPER_SSN = S.SSN)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

Dname of employees that are managers & NULL for employees who aren’t managers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What aggregate function would you use to calculate how many employees are working in Dept 5?

A

COUNT( * )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which aggregate function would you use to sum up all salaries of employees in Dept 5.

A

SUM ( X )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What aggregate function would you use to find out the youngest employee in department 5?

A

MAX( X ) / MIN ( X )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What aggregate function would you use to find the average salary of employees in Dept. 5.

A

AVG( X )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What aggregation function would you use to find the correlation between Age and Salary of employees in Dept. 5

A

CORR(X, Y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Show the maximum, minimum and average salary of those

employees who work in Dept. 5

A

SELECT MAX (Salary) AS Highest_Sal,
MIN (Salary) AS Lowest_Sal,
AVG (Salary) AS Average_Sal
FROM EMPLOYEE
WHERE DNO = 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which aggregate function does NOT discared NULL values?

A

COUNT(*)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can we define our own function?

A

Yes eg, calculate the GPA as a weighted some of grades.

17
Q

How do we use the GROUP BY function to Show the number of employees per department & average salary per department?

A

SELECT DNO, COUNT (*), AVG (Salary)
FROM EMPLOYEE
GROUP BY DNO;

Step 1: Partition EMPLOYEE into separate groups w.r.t. department.
Step 2: For each group, calculate its cardinality (num of tuples per group) and taking the average salary per group.

Actual Analytics Query is: Which is the most populated department?

18
Q

What is the lecturer trying to say about GROUP BY?

A

Grouping tuples together having the same value, reminds you of the frequency of value being evaluated.