SQL JOIN Flashcards

1
Q

How to make CROSS JOIN connection between 2 tables?

A

SELECT * FROM Customers CROSS JOIN Categories;

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

How to make CROSS JOIN connection and show only 2 columns from each table?

A

SELECT cust.CustomerName, categor.CategoryName FROM Customers AS cust CROSS JOIN Categories AS categor;

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

How to make INNER JOIN connection?

A

SELECT * FROM Products INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID;

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

How to make NATURAL JOIN connection?

A
# we use natural join if we have the same fields in two tables
SELECT * FROM Products NATURAL JOIN Categories;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to make LEFT OUTER JOIN?

A

SELECT * FROM Customers
LEFT OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;

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

How to make RIGHT OUTER JOIN?

A

SELECT * FROM Orders
RIGHT OUTER JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID;

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

How to combine result of 2 selects into one? ( sum of 2 tables)

A
# all cities from the first table + all cities from another table
# values will be UNIQ
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to combine result of 2 selects into one with duplication? ( sum of 2 tables)

A
# all cities from the first table + all cities from another table
# values won't be UNIQ
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to return data from the first request which are also included in another request?

A

SELECT City FROM Customers
INTERSECT
SELECT City FROM Suppliers;

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

How to return data from the first request but without the data from the second request?

A

SELECT City FROM Customers
EXCEPT
SELECT City FROM Suppliers;

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

How to create nested join in rails?

A

Building ( has_many :floors)
Floor ( has many :cabinets)
Cabinet ( belongs_to floor)

to get access from cabinet to building

Cabinet.joins(:floors => buildings)

you can nest more than one join and do it in 2 directions

AvailableCurrency.joins(scheme: {companies: :employees}).where(‘employees.id = 1’)

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

How to return data from joined table?

A

> > cities = RefCity.joins(:country).select(‘ref_cities.id, ref_cities.name’)
cities.first.id # city id
cities.first.name # city name

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