Nested queries and joins (practical) Flashcards

1
Q

What is a nested query?

A

Its another select statement (inner select) which result are used in the outer statement to help retrieve the right data.

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

What is a nested query / inner select also called?

A

A subquery

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

How does an inner join work?

A

compares each row of a table with all the rows of another table joining the row where the join condition is met.

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

What is an implicit inner join?

A

join statement where you dont write join:

SELECT *
FROM Employee, Department
WHERE Employee.ssn = Department.mgr_ssn;

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

IN MYSQL write a join statement that uses implicit join

A

USE EasterDB;
SELECT *
FROM DEPENDENT D, EMPLOYEE E
WHERE D.ESSN = E.SSN;

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

What a natural join?

A

A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables.

select DISTINCT Fname, Ssn as Essn, Pno
from EMPLOYEE
natural join WORKS_ON

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

Natural join requires the attributes to have the same name how can you make sure that happens

A

Use the alias functionality

SELECT SSN as ESSN FROM EMPLOYEE…

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

Natural join might return duplicates why is that?

A

Because it joins the rows together for each tuple with the condition. so if it can join two rows, they both have the attribute and it will join two times.

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

How can you prevent that the natural join output has duplicate rows?

A

With distinct

SELECT DISTINCT Fname, Lname
FROM EMPLOYEE
NATURAL JOIN DEPARTMENT

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

A row has information about an employee and the employee’s supervisor. How can you write a statement that shows a table with all the employees next to their supervisor in two columns?

A

Join the table on itself and take one instance of each

SELECT E.Lname, S.Lname
FROM EMPLOYEE AS E INNER JOIN EMPLOYEE AS S ON E.dno = S.dno WHERE E.ssn < S.ssn
ORDER BY E.ssn, S.ssn;

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

What is the difference between a left and right join?

A

They both keep the records of the first mentioned table and appends the rows that have matching attributes. They leave the rest of the attributes as null

the difference is which side the retained rows will appear, first (left) or last (right)

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

What does a full join do?

A

It retains all the rows from the tables and merges where the attributes match leaving null values on both sides.

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

What is the syntax for left join?

A

select * from EMPLOYEE

LEFT JOIN WORKS_ON on EMPLOYEE.Ssn = WORKS_ON.ESSN

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

What is the syntax for right join?

A

select * from EMPLOYEE

right JOIN WORKS_ON on EMPLOYEE.Ssn = WORKS_ON.ESSN

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

What is the syntax for full join?

A

SELECT * FROM Employee

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