-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLZOO_MySQL notes.sql
187 lines (147 loc) · 4.96 KB
/
SQLZOO_MySQL notes.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
---POSITION() in MySQL = CHARINDEX() in T-SQL
SELECT POSITION('A' IN '15426Afdsfd')
---ALTER TABLE
--add column
ALTER TABLE testTable
ADD customer varchar(25)
--drop column
ALTER TABLE testTable
DROP COLUMN customer
--ALTER COLUMN in T-SQL
ALTER TABLE testTable
ALTER COLUMN customer nvarchar(max)
--ALTER COLUMN in MySQL
ALTER TABLE testTable
MODIFY COLUMN customer nvarchar(max)
---CREATE TABLE in MySQL when multiple columns are set to be PRIMARY KEY
CREATE TABLE track (album char(10) NOT NULL, dsk int NOT NULL, posn int NOT NULL, song varchar(255), PRIMARY KEY(album, dsk, posn))
----identity column in different SQLs
--1) T-SQL
CREATE TABLE t_sql_test(id int IDENTITY PRIMARY KEY, name varchar(10), city varchar(10))
INSERT INTO t_sql_test
VALUES
('Andrew', 'Paris'),
('Gordon', 'LA')
--2) SQLITE
CREATE TABLE sql_lite_test(id INTEGER PRIMARY KEY, name varchar(10), city varchar(10))
INSERT INTO sql_lite_test
VALUES
(NULL, 'Andrew', 'Paris'),
(NULL, 'Gordon', 'LA')
--3) PostgreSQL
CREATE TABLE postgre_sql_test(id SERIAL PRIMARY KEY, name varchar(10), city varchar(10))
INSERT INTO postgre_sql_test(name, city)
VALUES
('Andrew', 'Paris'),
('Gordon', 'LA')
--4) MySQL
CREATE TABLE mysql_test(id int AUTO_INCREMENT PRIMARY KEY, name varchar(10), city varchar(10))
INSERT INTO mysql_test(name, city)
VALUES
('Andrew', 'Paris'),
('Gordon', 'LA')
----Rename Column in MySQL
CREATE TABLE a(x int)
INSERT INTO a
VALUES
(2)
ALTER TABLE a
CHANGE x y int
SELECT * FROM a
----String contains a quote '
INSERT INTO t_q
VALUE
('O''Brian')
----Unpivot in T-SQL vs MySQL
--1) Unpivot in T-SQL
CREATE TABLE #TestTable1(ProductCategoryID int, Black decimal(10, 2), Blue decimal(10, 2), Grey decimal(10, 2), Multi decimal(10, 2), None decimal(10, 2), Red decimal(10, 2), Silver decimal(10, 2), Silver_Black decimal(10, 2), White decimal(10, 2), Yellow decimal(10, 2))
INSERT INTO #TestTable1
SELECT *
FROM
(SELECT ProductCategoryID, ISNULL(Color, 'None') AS Color, ListPrice
FROM SalesLT.Product) AS BP
PIVOT
(AVG(ListPrice) FOR Color IN ([Black], [Blue], [Grey], [Multi], [None], [Red], [Silver], [Silver/Black], [White], [Yellow])) AS AP
SELECT * FROM #TestTable1
SELECT ProductCategoryID, Color, AvgListPrice
FROM
(SELECT ProductCategoryID, Black, Blue, Grey, Multi, None, Red, Silver, Silver_Black, White, Yellow
FROM #TestTable1) AS BUP
UNPIVOT
(AvgListPrice FOR Color IN (Black, Blue, Grey, Multi, None, Red, Silver, Silver_Black, White, Yellow)) AS AUP
--2) Unpivot in MySQL
CREATE TABLE normal(Line varchar(1), Col int, Val int, PRIMARY KEY(Line, Col));
INSERT INTO normal
SELECT Line, 'F1', F1 FROM unnormal
UNION
SELECT Line, 'F2', F2 FROM unnormal
UNION
SELECT Line, 'F3', F3 FROM unnormal
UNION
SELECT Line, 'F4', F4 FROM unnormal;
SELECT * FROM normal ORDER BY Line
----Pivot in T-SQL vs MySQL
--1) Pivot in T-SQL
CREATE TABLE #TestTable2(ProductCategoryID int, Black decimal(10, 2), Blue decimal(10, 2), Grey decimal(10, 2), Multi decimal(10, 2), None decimal(10, 2), Red decimal(10, 2), Silver decimal(10, 2), Silver_Black decimal(10, 2), White decimal(10, 2), Yellow decimal(10, 2))
INSERT INTO #TestTable2
SELECT *
FROM
(SELECT ProductCategoryID, ISNULL(Color, 'None') AS Color, ListPrice
FROM SalesLT.Product) AS BP
PIVOT
(AVG(ListPrice) FOR Color IN ([Black], [Blue], [Grey], [Multi], [None], [Red], [Silver], [Silver/Black], [White], [Yellow])) AS AP
--2) Pivot in MySQL
SELECT name, MAX(IF(course='Java', grade, NULL)) AS Java, MAX(IF(course='Database', grade, NULL)) AS DB, MAX(IF(course='Algebra', grade, NULL)) AS Algebra
FROM exam AS ex
JOIN courseGrade AS cg
ON ex.name = cg.student
GROUP BY name
----DATEDIFF in MySQL vs T-SQL
--1) T-SQL
PRINT DATEDIFF(mm, GETDATE(), '2011/08/25')
--2) MySQL
SELECT DATEDIFF(NOW(), '2011/08/25')
----INTERVAL in MySQL
SELECT *
FROM totp
WHERE '1976-05-20' BETWEEN wk - INTERVAL 7 DAY AND wk
----DATENAME in T-SQL vs DAYNAME, MONTHNAME in MySQL
--1a) Day of Week name in T-SQL
PRINT DATENAME(dw, GETDATE())
--1b) Day of Week name in MySQL
SELECT DAYNAME(NOW())
--2a) Month Name in T-SQL
PRINT DATENAME(mm, GETDATE())
--2b) Month Name in MySQL
SELECT MONTHNAME(NOW())
----TOP in T-SQL vs LIMIT in MySQL
--1) TOP in T-SQL
SELECT TOP 3 *
FROM people
ORDER BY birthday ASC
--2) LIMIT in MySQL
SELECT *
FROM people
ORDER BY birthday ASC
LIMIT 3
----OFFSET...FETCH... in T-SQL vs LIMIT in MySQL
--1) OFFSET...FETCH... in T-SQL
SELECT *
FROM bbc
ORDER BY population DESC
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY
--2) LIMIT in MySQL
SELECT *
FROM bbc
ORDER BY population DESC
LIMIT 11, 10
----GROUP BY ROLLUP in T-SQL vs GROUP BY...WITH ROLLUP in MySQL
--1) GROUP BY ROLLUP in T-SQL
SELECT item, serialnumber, SUM(price)
FROM serial
GROUP BY ROLLUP(item, serialnumber)
--2) GROUP BY...WITH ROLLUP in MySQL
SELECT item, serialnumber, SUM(price)
FROM serial
GROUP BY item, serialnumber WITH ROLLUP