-
Notifications
You must be signed in to change notification settings - Fork 0
/
Udemy_beginner.sql
329 lines (265 loc) · 5.78 KB
/
Udemy_beginner.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
SELECT * FROM table
WHERE ###conditions
AND
OR
COUNT () ###function
DISTINCT () ##return rows with unique values
ORDER BY DESC/ASC ##order rows by coumns
LIMIT
BETWEEN AND
IN ##list of conditions
LIKE % _ ##pattern matching
ILIKE
AVG()
MIN()
MAX()
SUM()
GROUP BY
HAVING
###assessment Test1
SELECT customer_id,SUM(amount) FROM payment
WHERE staff_id =2
GROUP BY customer_id
HAVING SUM(amount) >=110;
SELECT COUNT(*) FROM film
WHERE title LIKE 'J%';
SELECT first_name,last_name FROM customer
WHERE first_name LIKE 'E%' AND address_id <500
ORDER BY customer_id DESC
LIMIT 1;
####JOIN
AS
INNER JOIN ...ON
FULL OUTER JOIN
WHERE .. IS NUL
LEFT OUTER JOIN
RIGHT OUTER JOIN
UNION
###timestamps and extract
SHOW ALL
SHOW TIMEZONE
SELECT NOW()
SELECT TIMEOFDAY()
SELECT CURRENT_DATE
SELECT CURRENT_TIME
EXTRACT()
AGE()
TO_CHAR()
###mathematical functions and operators
###string functions anf operators
###subquery
SELF-JOIN
###assessment Test2
SELECT * FROM cd.facilities;
SELECT name,membercost FROM cd.facilities;
SELECT name,membercost FROM cd.facilities
WHERE membercost != 0;
SELECT facid,name,membercost,monthlymaintenance FROM cd.facilities
WHERE membercost !=0 AND membercost < monthlymaintenance/50;
SELECT name FROM cd.facilities
WHERE name LIKE '%Tennis%';
SELECT * FROM cd.facilities
WHERE facid IN (1,5);
SELECT members.memid,surname,firstname,joindate FROM cd.members
WHERE joindate>='2012-09-01' AND memid !=0;
SELECT DISTINCT(surname) FROM cd.members
ORDER BY surname
LIMIT 10;
#solution1
SELECT joindate FROM cd.members
ORDER BY joindate DESC
LIMIT 1;
#solution2
SELECT MAX(joindate)
FROM cd.members
SELECT COUNT(*) FROM cd.facilities
WHERE guestcost>=10;
SELECT facid,SUM(slots) FROM cd.bookings
WHERE EXTRACT(MONTH FROM starttime)=9
GROUP BY facid
ORDER BY SUM(slots);
SELECT facid,SUM(slots) FROM cd.bookings
GROUP BY facid
HAVING SUM(slots)>1000
ORDER BY SUM(facid);
SELECT starttime,name FROM cd.bookings
INNER JOIN cd.facilities
ON cd.bookings.facid=cd.facilities.facid
WHERE TO_CHAR(starttime,'YYYY-MM-DD')='2012-09-21'
AND name LIKE 'Tennis%'
ORDER BY starttime;
SELECT starttime FROM cd.bookings
INNER JOIN cd.members
ON cd.bookings.memid=cd.members.memid
WHERE firstname='David' AND surname='Farrell';
###creat tables
CREATE TABLE account(
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL,
email VARCHAR(250) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
)
CREATE TABLE job(
job_id SERIAL PRIMARY KEY,
job_name VARCHAR(200) UNIQUE NOT NULL
)
CREATE TABLE account_job(
user_id INTEGER REFERENCES account(user_id),
job_id INTEGER REFERENCES job(job_id),
hire_date TIMESTAMP
)
##INSERT VALUES
INSERT INTO account(username,password,email,created_on)
VALUES
('Jose','password','[email protected]',CURRENT_TIMESTAMP)
###UPDATE table
UPDATE account_job
SET hire_date=account.created_on
FROM account
WHERE account_job.user_id=account.user_id
##ALTER
##RENAME TABLE
ALTER TABLE information
RENAME TO new_info
##RENAME COLUMN
ALTER TABLE new_info
RENAME COLUMN person TO people
###drop
ALTER TABLE new_info
DROP COLUMN IF EXISTS people CASCADE
###check
CREAT TABLE example(
ex_id SERIAL PRIMARY KEY,
age SMALLINT CHECK(age>21),
parent_age SMALLINT CHECK(parent_age>age)
);
CREATE TABLE employees(
emp_id SERIAL PRIMARY KEY,
first_name VARCHAR(50)NOT NULL,
last_name VARCHAR(50) NOT NULL,
birthdate DATE CHECK(birthdate>'1900-01-01'),
hire_date DATE CHECK(hire_date>birthdate),
salary INTEGER CHECK (salary>0)
)
####assessment test3
CREATE TABLE student(
student_id SERIAL PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
homeroom_number INTEGER,
phone VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(250) UNIQUE,
grad_year INTEGER
)
INSERT INTO student(
first_name,
last_name,
phone,
email,
grad_year,
homeroom_number
)
VALUES(
'Mark',
'Watney',
'777-555-1234',
NULL,
2035,
5
)
CREATE TABLE teachers(
teacher_id SERIAL PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
homeroom_number INTEGER,
phone VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(250) UNIQUE
)
INSERT INTO teachers(
first_name,
last_name,
phone,
email,
homeroom_number
)
VALUES(
'Jonas',
'Salk',
'777-555-4321',
5
)
###General CASE
SELECT customer_id,
CASE
WHEN customer_id<=100 THEN 'Premium'
WHEN customer_id BETWEEN 100 AND 200 THEN 'Plus'
ELSE 'Normal'
END AS customer_class
FROM customer
##CASE expression
SELECT customer_id,
CASE customer_id
WHEN 2 THEN 'Winner'
WHEN 5 THEN 'second Place'
ELSE 'Normal'
END AS raffle_results
FROM customer
###
SELECT
SUM(CASE rental_rate
WHEN 0.99 THEN 1
ELSE 0
END ) AS number_of_bargains
FROM film
###
SELECT
SUM(CASE rental_rate
WHEN 0.99 THEN 1
ELSE 0
END) AS bargains,
SUM(CASE rental_rate
WHEN 2.99 THEN 1
ELSE 0
END) AS regular
FROM film
##coalesce: chnang NULL into 0
SELECT item,(price-COALESCE(discount,0))
AS final FROM table
EG:SELECT CHAR_LENGTH(CAST(customer_id AS VARCHAR)) FROM rental
###CAST FUNCTION
SELECT CAST(date AS TIMESTAMP) FROM table
###CAST operator
SELECT '5'::INTEGER
###NULLIF
CREATE TABLE depts(
first_name VARCHAR(50),
department VARCHAR(50)
)
INSERT INTO depts(
first_name,
department
)
VALUES
('Vinton','A'),
('Lauren','A'),
('Claire','B');
DELETE FROM depts
WHERE department='B'
SELECT(
SUM(CASE WHEN department = 'A' THEN 1 ELSE 0 END)/
NULLIF(SUM(CASE WHEN department='B' THEN 1 ELSE 0 END),0)
) AS dept_ratio
FROM depts
###VIEW
CREATE OR REPLACE VIEW customer_info AS
SELECT first_name,last_name,address,district FROM customer
INNER JOIN address
ON customer.address_id = address.address_id
SELECT * FROM customer_info
ALTER VIEW customer_info RENAME TO cust_info
DROP VIEW IF EXISTS cust_info
###postgresql with python
https://wiki.postgresql.org/wiki/Psycopg2_Tutorial