-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_class_join_exercises_26OCT22.sql
148 lines (112 loc) · 4.6 KB
/
in_class_join_exercises_26OCT22.sql
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* INDICES, JOINS, PKIS AND OTHER KEYS */
use farmers_market;
DESCRIBE customer_purchases;
SELECT * FROM customer_purchases LIMIT 13;
SELECT DISTINCT customer_id FROM customer_purchases;
USE customer;
describe customer;
SELECT DISTINCT customer_id FROM customer;
SELECT * FROM customer;
SELECT * FROM customer_purchases LIMIT 10;
USE product;
describe product;
SELECT * FROM product limit 10;
USE product_category;
describe pro duct_category;
SELECT * FROM product_category;
-- joins and joins and joins --
USE join_example_db;
DESCRIBE join_example_db;
-- what's in here ?
SHOW tables;
-- what's in these tables, specifically ?
SELECT * FROM roles;
-- fields present : id, name
SELECT * FROM users;
-- fields present : id, name, email, role_id
-- our first join : the inner join (default type of join, unless otherwise specified to SQL).
-- Select everything from the Users table
SELECT * FROM users
-- join the Roles table in
JOIN roles
-- specify how the match between Users and Roles looks :
ON users.role_id = roles.id;
-- Specific fields in the context of a join ?
-- Tell SQL what to grab, with dot notation (ie, db.users)
-- Format of field calls : table.field (ie, column name)
SELECT email, roles.name /* alternately, SELECT users.email, roles.name (but, in this case, 'email', was a unique column/field name) */
FROM users
JOIN roles
ON users.role_id = roles.id;
-- if we alias tables, we can reference them even in the first line : faketablename.field, faketablename2.field2
SELECT pizza.email, hamsandwich.name
-- note the alias of the table here
FROM users AS pizza
-- and here
JOIN roles AS hamsandwich
-- note that the aaliased table names are ebign used for the key pairing.
ON pizza.role_id = hamsandwich.id;
-- alternately...
SELECT pizza.email, hamsandwich.name
FROM users pizza -- the alias must directly follow the table name in order to drop the 'as'.
JOIN roles hamsandwich
ON pizza.role_id = hamsandwich.id;
SELECT * FROM roles
JOIN users USING(id); /* alternately, ON users.id = roles.id --> 'using' indicates that the two table names are names being addressed. 'id' and 'id' on each table match up because both integers, but we, as operators, are the ones who understand that they are associated as ids, and not simply as matching integers. The cell match (ie, integers, strings, etc) must be exact for the 'using' to work. */
-- LEFT AND RIGHT ?
-- --> TAKE EVERYTHING.
SELECT *
-- from users (to start)
FROM users
-- join roles
LEFT JOIN roles
ON users.role_id = roles.id;
-- Everything from Users is present, but flipped / reversed ;
-- two rows of info that have Users info, but nothing from Roles.
-- The cells from Roles are filled with null values.
SELECT *
-- from Roles
FROM roles
-- join Users
LEFT JOIN users
ON users.role_id = roles.id;
-- as a right join ? Same info as first (Left Join) table, but info is slightly flipped.
SELECT *
-- from Roles
FROM roles
-- join Users
RIGHT JOIN users
ON users.role_id = roles.id;
/* -- Inner join : only instances where we have a match on both sides inside of our key pairing.
-- consequence : we can loose information from both tables if there is not a match on either side.
*/
/*
L or R joins :
-- Left : give all info present in L table, and any matches from the R table.
-- Consequence : maintain every row present in the L table, but may not see everything from the R table. Furthermore, we have info in the L table with no match, we will see 'null' filled into these cells instead of data.
*/
USE world;
-- tables in here ?
SHOW tables;
-- city (fields present : id, name, countrycode, district, population), country, countrylanguage
SELECT * FROM city LIMIT 7 OFFSET 599;
SELECT * FROM country LIMIT 7;
/* code (looks like country.Code is a foreign key match with city.CountryCode), name, continent, region, surfacearea, indepuyear, population, lifeexpectancy, gnp, ... */
SELECT * FROM countrylanguage LIMIT 7;
/* countrylanguage.countrycode is match with country.code and city.countrycode */
-- let's link these together using an innter join. Take every field.
SELECT * FROM city
JOIN countrylanguage USING(CountryCode)
JOIN country ON country.Code = countrylanguage.CountryCode;
SELECT * FROM city
LEFT JOIN countrylanguage USING(CountryCode)
LEFT JOIN country ON country.Code = countrylanguage.CountryCode;
-- ALTNERNATE SYNTAX
SELECT * FROM city
JOIN countrylanguage ON city.CountryCode = countrylanguage.CountryCode
JOIN country ON country.Code = countrylanguage.CountryCode;
--
SELECT * FROM city
JOIN countrylanguage ON city.CountryCode = countrylanguage.CountryCode
JOIN country ON country.Code = countrylanguage.CountryCode
WHERE language = 'Turkish';