-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathfirst_task.sql
More file actions
178 lines (157 loc) · 5.9 KB
/
first_task.sql
File metadata and controls
178 lines (157 loc) · 5.9 KB
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
/*
First problem
Queries for MySQL DB
*/
/* create a new database */
CREATE DATABASE b12 CHARACTER SET utf8 COLLATE utf8_general_ci;
/* use database b12 */
USE b12;
/* create a table 'users' */
CREATE TABLE users (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`source` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `users_emails_idx` (`email` ASC)
) ENGINE=InnoDB;
/* create a table 'businesses' */
CREATE TABLE businesses (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`id_users` INT UNSIGNED NOT NULL,
`name` VARCHAR(255) NOT NULL,
`industry` VARCHAR(255) NOT NULL,
`monthly_plan` VARCHAR(255),
PRIMARY KEY (`id`),
INDEX `fk_businesses_1_idx` (`id_users` ASC),
CONSTRAINT `fk_businesses_1`
FOREIGN KEY (`id_users`)
REFERENCES `users` (`id`)
) ENGINE=InnoDB;
/* add rows into the table 'users' */
INSERT INTO users
(id, email, name, source)
VALUES
(NULL, 'john@gmail.com', 'john', 'facebook'),
(NULL, 'dan@gmail.com', 'Dan', 'blog'),
(NULL, 'bill@gmail.com', 'Bill', 'instagram');
/* add rows into a table 'businesses' */
INSERT INTO businesses
(id, id_users, name, industry, monthly_plan)
VALUES
(NULL, 1, 'Coca-Cola', 'Drinks', 'starter'),
(NULL, 2, 'Liverpool FC', 'Sport', 'professional'),
(NULL, 3, 'Solar City', 'Energy', NULL);
/* calculate paid conversion rate */
SELECT source AS marketing_channel,
CONCAT((COUNT(*)*100/(SELECT count(*) FROM `users`)) , " %") AS paid_conversion_rate
FROM users
JOIN businesses
WHERE users.id = businesses.id_users
AND businesses.monthly_plan IS NOT NULL
GROUP BY source;
/* create a user without any related business object */
INSERT INTO users
(id, email, name, source)
VALUES
(NULL, 'ann@gmail.com', 'Ann', 'facebook');
/* correct query for calculating paid conversion rate for new conditions */
SELECT source AS marketing_channel, CONCAT((COUNT(*)*100/(SELECT COUNT(*) FROM `users` JOIN businesses WHERE users.id = businesses.id_users)) , " %")
AS paid_conversion_rate FROM users
JOIN businesses
WHERE users.id = businesses.id_users
AND businesses.monthly_plan IS NOT NULL
GROUP BY source;
/*
To handle these requirements we need to create many-to-many relationship
between users and businesses tables. To do this we must create
an intermediate table users_businesses that mainly stores the primary keys
of each relationship. There will be 3 fields in users_businesses table: id
for relationship object, id_users for user id and
id_businesses for business id. Also we need to set id_users and
id_businesses as foreign keys to link users and businesses tables together.
As for conversion analysis, if we use every relationship between two tables
we will count business subscription for every user associated with business
object. Instead of 1 real business subscription we will get, for example,
2 subscriptions, it will give us wrong paid conversion rate. It would be
more correct to calculate the paid conversion rate not for each user but
for each business instead. It will give us more reliable and precise data.
*/
/* Queries for SQLITE3 DB */
/* create a table 'users' */
CREATE TABLE users (
`id` INTEGER NOT NULL,
`email` TEXT NOT NULL,
`name` TEXT NOT NULL,
`source` TEXT NOT NULL,
PRIMARY KEY (`id`)
);
/* create an index for email field in table 'users' */
CREATE UNIQUE INDEX `users_emails_idx`
ON `users` (`email` ASC);
/* create a table 'businesses' */
CREATE TABLE businesses (
`id` INTEGER NOT NULL,
`id_users` INTEGER NOT NULL,
`name` TEXT NOT NULL,
`industry` TEXT NOT NULL,
`monthly_plan` TEXT,
PRIMARY KEY (`id`),
CONSTRAINT `fk_businesses_1`
FOREIGN KEY (`id_users`)
REFERENCES `users` (`id`)
);
/* create an index for id_users field in table 'businesses' */
CREATE INDEX `fk_businesses_1_idx`
ON `businesses` (`id_users` ASC);
/* add rows in tables 'users' */
INSERT INTO users
(id, email, name, source)
VALUES
(NULL, 'john@gmail.com', 'john', 'facebook'),
(NULL, 'dan@gmail.com', 'Dan', 'blog'),
(NULL, 'bill@gmail.com', 'Bill', 'instagram');
/* add rows in tables 'businesses' */
INSERT INTO businesses
(id, id_users, name, industry, monthly_plan)
VALUES
(NULL, 1, 'Coca-Cola', 'Drinks', 'starter'),
(NULL, 2, 'Liverpool FC', 'Sport', 'professional'),
(NULL, 3, 'Solar City', 'Energy', NULL);
/* calculate paid conversion rate */
SELECT source AS marketing_channel,
(COUNT(*)*100/(SELECT count(*) FROM `users`)) AS paid_conversion_rate
FROM users
JOIN businesses
WHERE users.id = businesses.id_users
AND businesses.monthly_plan IS NOT NULL
GROUP BY source;
/* create a new user without any related business */
INSERT INTO users
(id, email, name, source)
VALUES
(NULL, 'ann@gmail.com', 'Ann', 'facebook');
/* correct query for calculating paid conversion rate for new conditions */
SELECT source AS marketing_channel,
(COUNT(*)*100/(SELECT COUNT(*) FROM `users` JOIN businesses
WHERE users.id = businesses.id_users))
AS paid_conversion_rate FROM users
JOIN businesses
WHERE users.id = businesses.id_users
AND businesses.monthly_plan IS NOT NULL
GROUP BY source;
/*
To handle these requirements we need to create many-to-many relationship
between users and businesses tables. To do this we must create
an intermediate table users_businesses that mainly stores the primary keys
of each relationship. There will be 3 fields in users_businesses table: id
for relationship object, id_users for user id and
id_businesses for business id. Also we need to set id_users and
id_businesses as foreign keys to link users and businesses tables together.
As for conversion analysis, if we use every relationship between two tables
we will count business subscription for every user associated with business
object. Instead of 1 real business subscription we will get, for example,
2 subscriptions, it will give us wrong paid conversion rate. It would be
more correct to calculate the paid conversion rate not for each user but
for each business instead. It will give us more reliable and precise data.
*/