Browse Source

done for class today

Jared Dunbar 1 year ago
parent
commit
ac7ceab443
Signed by: Jared Dunbar <jrddunbr@gmail.com> GPG Key ID: CF202CC859BAC692
1 changed files with 164 additions and 0 deletions
  1. 164
    0
      11-14-18.sql

+ 164
- 0
11-14-18.sql View File

@@ -0,0 +1,164 @@
1
+-- continuing on previous database
2
+
3
+-- some random thing to start class
4
+SELECT question, COUNT(*), AVG(score)
5
+FROM nss
6
+GROUP BY question
7
+ORDER BY AVG(score);
8
+
9
+-- "explanatory factors"
10
+
11
+-- runs that and avg score together
12
+SELECT question, COUNT(*), AVG(score)
13
+FROM nss;
14
+SELECT question, COUNT(*), AVG(score)
15
+FROM nss
16
+GROUP BY question
17
+ORDER BY AVG(score);
18
+
19
+
20
+-- close but not exact. this is the "real average"
21
+SELECT question, COUNT(*), AVG(score), SUM(score * response) / SUM(response)
22
+FROM nss
23
+GROUP BY question
24
+ORDER BY AVG(score);
25
+
26
+-- some stuff for institution
27
+SELECT institution, COUNT(*), AVG(score), SUM(score * response) / SUM(response) as real_avg
28
+FROM nss
29
+GROUP BY institution;
30
+
31
+-- some stuff by level
32
+SELECT level, COUNT(*), AVG(score), SUM(score * response) / SUM(response) as real_avg
33
+FROM nss
34
+GROUP BY level
35
+ORDER BY AVG(score);
36
+
37
+-- lots more stuff
38
+SELECT institution, question, COUNT(*), AVG(score), SUM(score * response) / SUM(response) as real_avg
39
+FROM nss
40
+GROUP BY institution, question
41
+ORDER BY AVG(score) DESC;
42
+
43
+
44
+SELECT institution, subject, COUNT(*), AVG(score), SUM(score * response) / SUM(response) as real_avg
45
+FROM nss
46
+GROUP BY institution, subject
47
+ORDER BY AVG(score) DESC;
48
+
49
+
50
+-- q6: Show the percentage of students who A_STRONGLY_AGREE to question 22 for the subject '(8) Computer Science', and show the same figure for  '(H) Creative Arts and Design'
51
+SELECT subject, ROUND(SUM(A_STRONGLY_AGREE * response)/SUM(response),0) AS pct_STRONGLY_AGREE, ROUND(AVG(A_STRONGLY_AGREE),0) as non_weighted
52
+FROM nss
53
+WHERE question = 'Q22'
54
+AND subject IN ('(8) Computer Science', '(H) Creative Arts and Design')
55
+GROUP BY subject;
56
+
57
+
58
+-- q7: Show the average scores for question 'Q22' for each institution that includes 'Manchester' in the name
59
+SELECT institution, ROUND(SUM(score * response)/SUM(response)) as real_avg
60
+FROM nss
61
+WHERE question='Q22'
62
+AND (institution LIKE '%Manchester%')
63
+GROUP BY institution;
64
+
65
+
66
+-- q8: Show the institution, sample size, and number of computing students for instititions in Manchester for 'Q01'
67
+
68
+-- first part
69
+SELECT institution, SUM(sample)
70
+FROM nss
71
+WHERE question='Q01'
72
+AND (institution LIKE '%Manchester%')
73
+GROUP BY institution;
74
+
75
+-- now just CS
76
+SELECT institution, SUM(sample) AS comp
77
+FROM nss
78
+WHERE question='Q01'
79
+AND (institution LIKE '%Manchester%')
80
+AND subject = '(8) Computer Science'
81
+GROUP BY institution;
82
+
83
+-- alright, the answer wants both. Now what?
84
+-- fact: this is not possible with one query, must connect two queries
85
+
86
+
87
+-- diverges to talk about homework.
88
+
89
+-- q5.1a
90
+CREATE VIEW e51a AS
91
+SELECT vendorID, vendorname
92
+FROM vendor;
93
+
94
+-- These are from the book.
95
+-- Best to test and then save as view
96
+
97
+-- q5.1c
98
+CREATE VIEW e51c AS
99
+SELECT productid, productname, productprice
100
+FROM product
101
+WHERE productprice >= 100;
102
+
103
+-- q5.1 ?
104
+-- display pid and pname of the cheapest product
105
+-- find price of cheapest product
106
+-- find that product by price and show the information about it
107
+SELECT productid, productname
108
+FROM product
109
+WHERE productprice = (
110
+  SELECT MIN(productprice)
111
+  FROM product
112
+);
113
+
114
+-- q5.1m
115
+-- find the average price
116
+-- show products with price below average
117
+CREATE VIEW e51m AS
118
+SELECT productid, productname, productprice, vendorid
119
+FROM product
120
+WHERE productprice < (
121
+  SELECT AVG(productprice)
122
+  FROM product
123
+);
124
+
125
+-- q5.1n
126
+-- show the ID of the product that has been sold in the highest quantity
127
+SELECT productid, noofitems
128
+FROM soldvia
129
+WHERE noofitems = (
130
+  SELECT MAX(noofitems)
131
+  FROM soldvia
132
+);
133
+-- note - this only finds one thing
134
+
135
+-- find total of each product
136
+SELECT productid, SUM(noofitems)
137
+FROM soldvia
138
+GROUP BY productid;
139
+
140
+-- find maximum quantity
141
+SELECT MAX(noofitems)
142
+FROM soldvia;
143
+
144
+-- his example
145
+SELECT MAX(t.sumquant)
146
+FROM (
147
+  SELECT productid, SUM(noofitems) AS sumquant
148
+  FROM soldvia
149
+  GROUP BY productid
150
+) AS t;
151
+
152
+-- all in one query
153
+-- CREATE VIEW e51n AS -- aparently you can't create a subview here?
154
+SELECT productid, SUM(noofitems)
155
+FROM soldvia
156
+GROUP BY productid
157
+HAVING SUM(noofitems) = (
158
+  SELECT MAX(t.sumquant)
159
+  FROM (
160
+    SELECT productid, SUM(noofitems) AS sumquant
161
+    FROM soldvia
162
+    GROUP BY productid
163
+  ) AS t
164
+);

Loading…
Cancel
Save