-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLesson12.sql
More file actions
67 lines (53 loc) · 1.84 KB
/
Lesson12.sql
File metadata and controls
67 lines (53 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
### JOIN
/*
The JOIN introduces the second table from which you would like to pull
data, and the ON tells you how you would like to merge the tables in the
FROM and JOIN statements together.
*/
SELECT orders.*
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;
/*
Above, we are only pulling data from the orders table since in the
SELECT statement we only reference columns from the orders table.
*/
/*
Additional Information - If we wanted to only pull individual elements
from either the orders or accounts table, we can do this by using the
exact same information in the FROM and ON statements. However, in your
SELECT statement, you will need to know how to specify tables and
columns in the SELECT statement:
1. The table name is always before the period.
2.The column you want from that table is always after the period.
For example, if we want to pull only the account name and the dates
in which that account placed an order, but none of the other columns,
we can do this with the following query:
*/
SELECT accounts.name, orders.occurred_at
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;
/*
This query only pulls two columns, not all the information in these two
tables. Alternatively, the below query pulls all the columns from both
the accounts and orders table.
*/
SELECT *
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;
/*
1. Try pulling all the data from the accounts table, and all the data from
the orders table.
2. Try pulling standard_qty, gloss_qty, and poster_qty from the orders
table, and the website and the primary_poc from the accounts table.
*/
SELECT orders.*, accounts.*
FROM accounts
JOIN orders
ON accounts.id = orders.account_id;
SELECT orders.standard_qty, orders.gloss_qty, orders.poster_qty, accounts.website, accounts.primary_poc
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;