-
Notifications
You must be signed in to change notification settings - Fork 0
/
more_joins.clj
203 lines (175 loc) · 5.88 KB
/
more_joins.clj
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
(ns seaquell.zoo.more-joins
(:refer-clojure
:exclude [alter distinct drop group-by into set update partition-by when])
(:require [seaquell.core :refer :all]))
;; The following queries are solutions (as of 4/20/2013) to the
;; tutorial at http://sqlzoo.net/wiki/More_JOIN_operations
;; Each query is named for the question it answers.
;;
;; To try them out, open a repl and issue the commands below.
;; Submitting the resulting SQL into the tutorial should produce
;; correct answers.
(comment
(use 'seaquell.core)
(use 'seaquell.zoo.more-joins)
(println (select$ q1)))
(def q1 (select [:id :title] (from :movie) (where {:yr 1962})))
(def q2 (select :yr (from :movie) (where {:title "Citizen Kane"})))
(def q3
(select [:id :title :yr]
(from :movie)
(where [:like :title "Star Trek%"])
(order-by :yr)))
#_(def q4
(select :title
(from :movie)
(where [:in :id [vals 11768 11955 21191]])))
(def q4 (select :id (from :actor) (where {:name "Glenn Close"})))
(def q5 (select :id (from :movie) (where {:title "Casablanca"})))
(def q6
(select :name
(from :casting :actor)
(where {:movieid (select :id (from :movie)
(where {:title "Casablanca"}))
:actorid :actor.id})))
(def q6-b
(select :name
(from :casting (join :actor :on {:actorid :actor.id}))
(where {:movieid (select :id (from :movie)
(where {:title "Casablanca"}))})))
(def q7
(select :name
(from :movie :casting :actor)
(where {:title "Alien"
:movieid :movie.id
:actorid :actor.id})))
(def q7-b
(select :name
(from :movie
(join :casting :on {:movieid :movie.id})
(join :actor :on {:actorid :actor.id}))
(where {:title "Alien"})))
(def q8
(select :title
(from :movie :casting :actor)
(where '{name "Harrison Ford"
movieid movie.id
actorid actor.id})))
(def q8-b
(select :title
(from :movie
(join :casting :on {:movieid :movie.id})
(join :actor :on {:actorid :actor.id}))
(where '{name "Harrison Ford"})))
(def q9
(select :title
(from :movie :casting :actor)
(where {:name "Harrison Ford"
:movieid :movie.id
:actorid :actor.id
:ord [not= 1]})))
(def q9-b
(select :title
(from :movie
(join :casting :on {:movieid :movie.id})
(join :actor :on {:actorid :actor.id}))
(where {:name "Harrison Ford", :ord [not= 1]})))
(def q10
(select [:title :name]
(from :movie :casting :actor)
(where {:yr 1962
:movieid :movie.id
:actorid :actor.id
:ord 1})))
(def q10-b
(select [:title :name]
(from :movie
(join :casting :on {:movieid :movie.id})
(join :actor :on {:actorid :actor.id}))
(where {:yr 1962, :ord 1})))
(def q11
(select
[:yr [count :title]]
(from :movie
(join :casting :on {:movie.id :movieid})
(join :actor :on {:actorid :actor.id}))
(where {:name "Rock Hudson"})
(group-by :yr)
(having [> [count :title] 2])))
(def q12
(select
[:title :name]
(from :movie :casting :actor)
(where
`(and (= movieid movie.id)
(= actorid actor.id)
(= ord 1)
(in movieid ~(select :movieid
(from :casting :actor)
(where {:actorid :actor.id
:name "Julie Andrews"})))))))
(def q12-b
(select
[:title :name]
(from :movie
(join :casting :on {:movieid :movie.id})
(join :actor :on {:actorid :actor.id}))
(where
`(and (= ord 1)
(in movieid ~(select :movieid
(from :casting
(join :actor :on {:actorid :actor.id}))
(where {:name "Julie Andrews"})))))))
(def q12-c
(select
[:title :name]
(from (select-distinct
[:title :movie.id]
(from :movie
(join :casting :on {:movieid :movie.id})
(join :actor :on {:actorid :actor.id}))
(where {:name "Julie Andrews"})) (as :ja)
(join :casting :on {:ja.id :movieid})
(join :actor :on {:actorid :actor.id}))
(where {:ord 1})))
(def q13
(select :name
(from :casting
(join :actor :on {:actorid :actor.id}))
(where {:ord 1})
(group-by :name)
(having [>= [count :movieid] 15])))
(def q14
(select [:title [count :actorid]]
(from :casting :movie)
(where {:yr 1978, :movieid :movie.id})
(group-by :title)
(order-by (desc 2) :title)))
(def q14-b
(select [:title [count :actorid]]
(from :casting
(join :movie :on {:movieid :movie.id}))
(where {:yr 1978})
(group-by :title)
(order-by (desc 2) :title)))
(def q15
(select-distinct
:d.name
(from :actor :as :d
(join :casting :as :a :on {:a.actorid :d.id})
(join :casting :as :b :on {:a.movieid :b.movieid})
(join :actor :as :c :on {:b.actorid :c.id
:c.name "Art Garfunkel" }))
(where '(not= d.id c.id))))
(def q15-b
(select
:name
(from (select-distinct
:movie.id
(from :movie
(join :casting :on {:movieid :movie.id})
(join :actor :on {:actorid :actor.id}))
(where {:name "Art Garfunkel"})) (as :ag)
(join :casting :on {:ag.id :movieid})
(join :actor :on {:actorid :actor.id}))
(where [not= :name "Art Garfunkel"])))