html-flip-book-react 0.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/flip-book.d.ts +14 -0
  2. package/dist/flip-book.d.ts.map +1 -0
  3. package/dist/flip-book.js +1410 -0
  4. package/dist/flip-book.js.map +1 -0
  5. package/example/.vscode/settings.json +3 -0
  6. package/example/README.md +47 -0
  7. package/example/assets/pages_data/en/assets/cover.jpg +0 -0
  8. package/example/assets/pages_data/en/assets/sql-command.png +0 -0
  9. package/example/assets/pages_data/en/content/000-introduction.md +85 -0
  10. package/example/assets/pages_data/en/content/001-databases.md +39 -0
  11. package/example/assets/pages_data/en/content/002-install-mysql.md +162 -0
  12. package/example/assets/pages_data/en/content/003-creating-tables.md +304 -0
  13. package/example/assets/pages_data/en/content/004-basic-syntax.md +145 -0
  14. package/example/assets/pages_data/en/content/005-select.md +359 -0
  15. package/example/assets/pages_data/en/content/006-where.md +225 -0
  16. package/example/assets/pages_data/en/content/007-order-and-group-by.md +142 -0
  17. package/example/assets/pages_data/en/content/008-insert.md +86 -0
  18. package/example/assets/pages_data/en/content/009-update.md +92 -0
  19. package/example/assets/pages_data/en/content/010-delete.md +28 -0
  20. package/example/assets/pages_data/en/content/011-join.md +297 -0
  21. package/example/assets/pages_data/en/content/012-sql-command-categories.md +121 -0
  22. package/example/assets/pages_data/en/content/013-sub-queries.md +112 -0
  23. package/example/assets/pages_data/en/content/014-unions.md +124 -0
  24. package/example/assets/pages_data/en/content/015-Keys-in-a-Relational Database.md +51 -0
  25. package/example/assets/pages_data/en/content/016-Logical-operator-keywords.md +17 -0
  26. package/example/assets/pages_data/en/content/017-having-clause_aggregate-functions.md +184 -0
  27. package/example/assets/pages_data/en/content/018-essential-mysql-functions.md +190 -0
  28. package/example/assets/pages_data/en/content/019-triggers-in-sql.md +133 -0
  29. package/example/assets/pages_data/en/content/020-TCL-commands.md +154 -0
  30. package/example/assets/pages_data/en/content/021-DCL-commands.md +126 -0
  31. package/example/assets/pages_data/en/content/100-mysqldump.md +109 -0
  32. package/example/assets/pages_data/en/content/101-learn-materialize.md +267 -0
  33. package/example/assets/pages_data/en/content/999-conclusion.md +24 -0
  34. package/example/assets/pages_data/he/4.txt +2 -0
  35. package/example/assets/pages_data/he/5.txt +4 -0
  36. package/example/assets/pages_data/he/6.txt +4 -0
  37. package/example/index.html +21 -0
  38. package/example/package-lock.json +5324 -0
  39. package/example/package.json +39 -0
  40. package/example/src/App.css +52 -0
  41. package/example/src/App.tsx +25 -0
  42. package/example/src/EnBook.tsx +55 -0
  43. package/example/src/HeBook.tsx +44 -0
  44. package/example/src/index.tsx +12 -0
  45. package/example/vite-env.d.ts +1 -0
  46. package/example/vite.config.js +84 -0
  47. package/package.json +45 -0
  48. package/src/FlipBook.tsx +45 -0
  49. package/vite.config.js +66 -0
@@ -0,0 +1,142 @@
1
+ # Sorting with ORDER and GROUP BY
2
+
3
+ In the last chapter, you've learned how to use the `SELECT` statement with the `WHERE` clause and filter the result set based on some conditions.
4
+
5
+ More often than not, you would want to order the results in a specific way based on a particular column. For example, you might want to order the users alphabetically, based on their username.
6
+
7
+ In this chapter, you will learn how to use the `ORDER BY` and `GROUP BY` clauses.
8
+
9
+ ## ORDER BY
10
+
11
+ The main thing that you need to keep in mind when using `ORDER BY` is to specify the column or columns you want to order by. In case that you want to specify multiple columns to order by, you need to separate each column with a comma.
12
+
13
+ If we were to run the following statement without providing an `ORDER BY` clause:
14
+
15
+ ```sql
16
+ SELECT id, username FROM users;
17
+ ```
18
+
19
+ We will get the following output:
20
+
21
+ ```
22
+ +----+----------+
23
+ | id | username |
24
+ +----+----------+
25
+ | 2 | bobby |
26
+ | 3 | devdojo |
27
+ | 4 | tony |
28
+ | 5 | bobby |
29
+ | 6 | devdojo |
30
+ | 7 | tony |
31
+ +----+----------+
32
+ ```
33
+
34
+ As you can see, the result set is sorted by the primary key, which in our case is the `id` of each user. If we wanted to sort the output by `username`, we would run the following query:
35
+
36
+ ```sql
37
+ SELECT id, username FROM users ORDER BY username;
38
+ ```
39
+
40
+ > Note: The `ORDER BY` statement is followed by the column's name that we want to order by.
41
+
42
+ The output, in this case, will be:
43
+
44
+ ```
45
+ +----+----------+
46
+ | id | username |
47
+ +----+----------+
48
+ | 2 | bobby |
49
+ | 5 | bobby |
50
+ | 3 | devdojo |
51
+ | 6 | devdojo |
52
+ | 4 | tony |
53
+ | 7 | tony |
54
+ +----+----------+
55
+ ```
56
+
57
+ > Note: You can use `ORDER BY` with and without specifying a `WHERE` clause. If you've used a `WHERE` clause, you need to put the `ORDER BY` clause after the `WHERE` clause.
58
+
59
+ The default sorting is ascending and is specified with the `ASC` keyword, and you don't need to add it explicitly, but if you want to sort by descending order, you need to use the `DESC` keyword.
60
+
61
+ If we use the query above and add `DESC` at the end as follows:
62
+
63
+
64
+ ```sql
65
+ SELECT id, username FROM users ORDER BY username DESC;
66
+ ```
67
+
68
+ We will see the following output:
69
+
70
+ ```
71
+ +----+----------+
72
+ | id | username |
73
+ +----+----------+
74
+ | 4 | tony |
75
+ | 7 | tony |
76
+ | 3 | devdojo |
77
+ | 6 | devdojo |
78
+ | 2 | bobby |
79
+ | 5 | bobby |
80
+ +----+----------+
81
+ ```
82
+
83
+ As you can see, we've got the same list of users sorted alphabetically but in reverse order.
84
+
85
+ ## GROUP BY
86
+
87
+ The `GROUP BY` statement allows you to use a function like `COUNT`, `MIN`, `MAX` etc., with multiple columns.
88
+
89
+ For example, let's say that we wanted to get all of the counts of all users grouped by username.
90
+
91
+ In our case, we have two users with username `bobby`, two users with username `tony`, and two users with username `devdojo`. This represented in an SQL statement would look like this:
92
+
93
+ ```sql
94
+ SELECT COUNT(username), username FROM users GROUP BY username;
95
+ ```
96
+
97
+ The output, in this case, would be:
98
+
99
+ ```
100
+ +-----------------+----------+
101
+ | COUNT(username) | username |
102
+ +-----------------+----------+
103
+ | 2 | bobby |
104
+ | 2 | devdojo |
105
+ | 2 | tony |
106
+ +-----------------+----------+
107
+ ```
108
+
109
+ The `GROUP BY` statement grouped the identical usernames. Then it ran a `COUNT` on each of `bobby`, `tony` and `devdojo`.
110
+
111
+ The main thing to remember here is that the `GROUP BY` should be added after the `FROM` clause and after the `WHERE` clause.
112
+
113
+ ## HAVING Clause
114
+
115
+ The `HAVING` clause allows you to filter out the results on the groups formed by the `GROUP BY` clause.
116
+
117
+ For example, let's say that we wanted to get all usernames that are duplicates, i.e., all the usernames present in more than one table record.
118
+
119
+ In our case, we have two users with username `bobby`, two users with username `tony`, and two users with username `devdojo`. This represented in an SQL statement would look like this:
120
+
121
+ ```sql
122
+ SELECT COUNT(username), username
123
+ FROM users
124
+ GROUP BY username
125
+ HAVING COUNT(username) > 1;
126
+ ```
127
+
128
+ The output, in this case, would be:
129
+
130
+ ```
131
+ +-----------------+----------+
132
+ | COUNT(username) | username |
133
+ +-----------------+----------+
134
+ | 2 | bobby |
135
+ | 2 | devdojo |
136
+ | 2 | tony |
137
+ +-----------------+----------+
138
+ ```
139
+
140
+ The `GROUP BY` clause grouped the identical usernames, calculated their counts and filtered out the groups using the `HAVING` clause.
141
+
142
+ > **NOTE** :- _The WHERE clause places conditions on the selected columns, whereas the HAVING clause places conditions on groups created by the GROUP BY clause._
@@ -0,0 +1,86 @@
1
+ # INSERT
2
+
3
+ To add data to your database, you would use the `INSERT` statement. You can insert data into one table at a time only.
4
+
5
+ The syntax is the following:
6
+
7
+ ```sql
8
+ INSERT INTO table_name
9
+ (column_name_1,column_name_2,column_name_n)
10
+ VALUES
11
+ ('value_1', 'value_2', 'value_3');
12
+ ```
13
+
14
+ You would start with the `INSERT INTO` statement, followed by the table that you want to insert the data into. Then you would specify the list of the columns that you want to insert the data into. Finally, with the `VALUES` statement, you specify the data that you want to insert.
15
+
16
+ > The important part is that you need to keep the order of the values based on the order of the columns that you've specified.
17
+
18
+ In the above example the `value_1` would go into `column_name_1`, the `value_2` would go into `column_name_2` and the `value_3` would go into `column_name_x`.
19
+
20
+ Let's use the table that we created in the last chapter and insert 1 user into our `users` table:
21
+
22
+ ```sql
23
+ INSERT INTO users
24
+ (username, email, active)
25
+ VALUES
26
+ ('greisi', 'g@devdojo.com', true);
27
+ ```
28
+
29
+ Rundown of the insert statement:
30
+
31
+ * `INSERT INTO users`: First, we specify the `INSERT INTO` keywords which tells MySQL that we want to insert data into the `users` table.
32
+ * `users (username, email, active)`: Then, we specify the table name `users` and the columns that we want to insert data into.
33
+ * `VALUES`: Then, we specify the values that we want to insert in.
34
+
35
+ ## Inserting multiple records
36
+
37
+ We've briefly covered this in one of the previous chapters, but in some cases, you might want to add multiple records in a specific table.
38
+
39
+ Let's say that we wanted to create 5 new users, rather than running 5 different queries like this:
40
+
41
+ ```sql
42
+ INSERT INTO users (username, email, active) VALUES ('user1', 'user1@devdojo.com', true);
43
+ INSERT INTO users (username, email, active) VALUES ('user1', 'user2@devdojo.com', true);
44
+ INSERT INTO users (username, email, active) VALUES ('user1', 'user3@devdojo.com', true);
45
+ INSERT INTO users (username, email, active) VALUES ('user1', 'user4@devdojo.com', true);
46
+ INSERT INTO users (username, email, active) VALUES ('user1', 'user5@devdojo.com', true);
47
+ ```
48
+
49
+ What you could do is to combine this into one `INSERT` statement by providing a list of the values that you want to insert as follows:
50
+
51
+ ```sql
52
+ INSERT INTO users
53
+ (username, email, active)
54
+ VALUES
55
+ ('user1', 'user1@devdojo.com', true),
56
+ ('user2', 'user2@devdojo.com', true),
57
+ ('user3', 'user3@devdojo.com', true),
58
+ ('user4', 'user4@devdojo.com', true),
59
+ ('user5', 'user5@devdojo.com', true);
60
+ ```
61
+
62
+ That way, you will add 5 new entries in your `users` table with a single `INSERT` statement. This is going to be much more efficient.
63
+
64
+ ## Inserting multiple records using another table
65
+
66
+ In the previous section, we have discussed how we can insert multiple records using a single INSERT query.
67
+ But sometimes there are cases where we need to insert multiple records which are residing in some other table.
68
+
69
+ In this section, we are going to learn how we can insert multiple records at once using a single INSERT query.
70
+
71
+ Consider a table, say `prospect_users`, which stores the information of the people who want to become the users of our service, but they are not yet actual users.
72
+
73
+ In order to add them to our user database, we have to insert there entries into our `users` table.
74
+ We can achieve the same by writing an `INSERT` query with multiple `VALUES` listed in them (as discussed in previous section).
75
+
76
+ But there is an easier way where we achieve the same by querying the `prospect_users` table.
77
+
78
+
79
+ ```sql
80
+ INSERT INTO users (username, email, active)
81
+ SELECT username, email, active
82
+ FROM prospect_users
83
+ WHERE active=true;
84
+ ```
85
+
86
+ Using the above statement, an entry for each active prospect users will be made in our `users` table.
@@ -0,0 +1,92 @@
1
+ # UPDATE
2
+
3
+ As the name suggests, whenever you have to update some data in your database, you would use the `UPDATE` statement.
4
+
5
+ You can use the `UPDATE` statement to update multiple columns in a single table.
6
+
7
+ The syntax would look like this:
8
+
9
+ ```sql
10
+ UPDATE users SET username='bobbyiliev' WHERE id=1;
11
+ ```
12
+
13
+ Rundown of the statement:
14
+
15
+ * `UPDATE users`: First, we specify the `UPDATE` keyword followed by the table that we want to update.
16
+ * `username='bobbyiliev'`: Then we specify the columns that we want to update and the new value that we want to set.
17
+ * `WHERE id=1`: Finally, by using the `WHERE` clause, we specify which user should be updated. In our case, it is the user with ID 1.
18
+
19
+ The most important thing that you need to keep in mind is that if you don't specify a `WHERE` clause, all of the entries inside the `users` table would be updated, and all users would have the `username` set to `bobbyiliev`.
20
+
21
+ > Important: You need to be careful when you use the `UPDATE` statement without a `WHERE` clause as every single row will be updated.
22
+
23
+ If you have been following along all of the user entries in our `users` table, it currently have no data in the `about` column:
24
+
25
+ ```
26
+ +----+----------+-------+
27
+ | id | username | about |
28
+ +----+----------+-------+
29
+ | 2 | bobby | NULL |
30
+ | 3 | devdojo | NULL |
31
+ | 4 | tony | NULL |
32
+ | 5 | bobby | NULL |
33
+ | 6 | devdojo | NULL |
34
+ | 7 | tony | NULL |
35
+ +----+----------+-------+
36
+ ```
37
+
38
+ Let's go ahead and update this for all users and set the column value to `404 bio not found`, For example:
39
+
40
+ ```sql
41
+ UPDATE users SET about='404 bio not found';
42
+ ```
43
+
44
+ The output would let you know how many rows have been affected by the query:
45
+
46
+ ```
47
+ Query OK, 6 rows affected (0.02 sec)
48
+ Rows matched: 6 Changed: 6 Warnings: 0
49
+ ```
50
+
51
+ Now, if you were to run a select for all `users`, you would get the following result:
52
+
53
+ ```
54
+ +----+----------+-------------------+
55
+ | id | username | about |
56
+ +----+----------+-------------------+
57
+ | 2 | bobby | 404 bio not found |
58
+ | 3 | devdojo | 404 bio not found |
59
+ | 4 | tony | 404 bio not found |
60
+ | 5 | bobby | 404 bio not found |
61
+ | 6 | devdojo | 404 bio not found |
62
+ | 7 | tony | 404 bio not found |
63
+ +----+----------+-------------------+
64
+ ```
65
+
66
+ Let's now say that we wanted to update the `about` column for the user with an id of 2. In this case, we need to specify a `WHERE` clause followed by the ID of the user that we want to update as follows:
67
+
68
+ ```sql
69
+ UPDATE users SET about='Hello World :)' WHERE id=2;
70
+ ```
71
+
72
+ The output here should indicate that only 1 row was updated:
73
+
74
+ ```
75
+ Query OK, 1 row affected (0.01 sec)
76
+ Rows matched: 1 Changed: 1 Warnings: 0
77
+ ```
78
+
79
+ Now, if you again run the `SELECT id, username, about FROM users` query, you would see that the user with `id` of 2 now has an updated `about` column data:
80
+
81
+ ```
82
+ +----+----------+-------------------+
83
+ | id | username | about |
84
+ +----+----------+-------------------+
85
+ | 2 | bobby | Hello World :) |
86
+ | 3 | devdojo | 404 bio not found |
87
+ | 4 | tony | 404 bio not found |
88
+ | 5 | bobby | 404 bio not found |
89
+ | 6 | devdojo | 404 bio not found |
90
+ | 7 | tony | 404 bio not found |
91
+ +----+----------+-------------------+
92
+ ```
@@ -0,0 +1,28 @@
1
+ # DELETE
2
+
3
+ As the name suggests, the `DELETE` statement would remove data from your database.
4
+
5
+ The syntax is as follows:
6
+
7
+ ```sql
8
+ DELETE FROM users WHERE id=5;
9
+ ```
10
+
11
+ The output should indicate that 1 row was affected:
12
+
13
+ ```
14
+ Query OK, 1 row affected (0.01 sec)
15
+ ```
16
+
17
+ > Important: Just like the `UPDATE` statement, if you don't specify a `WHERE` clause, all of the entries from the table will be affected, meaning that all of your users will be deleted. So, it is critical to always add a `WHERE` clause when executing a `DELETE` statement.
18
+
19
+ ```sql
20
+ DELETE FROM users;
21
+ ```
22
+
23
+ The output should indicate (where x is the number of tuples in the table):
24
+ ```
25
+ Query OK, x row(s) affected (0.047 sec)
26
+ ```
27
+
28
+ Similar to the Linux `rm` command, when you use the `DELETE` statement, the data would be gone permanently, and the only way to recover your data would be by restoring a backup.
@@ -0,0 +1,297 @@
1
+ # JOIN
2
+
3
+ The `JOIN` clause allows you to combine the data from 2 or more tables into one result set.
4
+
5
+ As we will be selecting from multiple columns, we need to include the list of the columns we want to choose data from after the `FROM` clause is separated by a comma.
6
+
7
+ In this chapter, we will go over the following `JOIN` types:
8
+
9
+ * `CROSS` Join
10
+ * `INNER` Join
11
+ * `LEFT` Join
12
+ * `RIGHT` Join
13
+
14
+ Before we get started, let's create a new database and two tables that we are going to work with:
15
+
16
+ * We are going to call the database `demo_joins`:
17
+
18
+ ```sql
19
+ CREATE DATABASE demo_joins;
20
+ ```
21
+
22
+ * Then, switch to the new database:
23
+
24
+ ```sql
25
+ USE demo_joins;
26
+ ```
27
+
28
+ * Then, the first table will be called `users`, and it will only have two columns: `id` and `username`:
29
+
30
+ ```sql
31
+ CREATE TABLE users
32
+ (
33
+ id INT PRIMARY KEY AUTO_INCREMENT,
34
+ username VARCHAR(255) NOT NULL
35
+ );
36
+ ```
37
+
38
+ * Then, let's create a second table called `posts`, and to keep things simple, we will have three two columns: `id`, `user_id` and `title`:
39
+
40
+ ```sql
41
+ CREATE TABLE posts
42
+ (
43
+ id INT PRIMARY KEY AUTO_INCREMENT,
44
+ user_id INT,
45
+ title VARCHAR(255) NOT NULL
46
+ );
47
+ ```
48
+
49
+ > The `user_id` column would be used to reference the user's ID that the post belongs to. It is going to be a one to many relations, e.g. one user could have many posts:
50
+
51
+ ![One to many relation](https://imgur.com/ipIjCaL.png)
52
+
53
+ * Now, let's add some data into the two tables first by creating a few users:
54
+
55
+ ```sql
56
+ INSERT INTO users
57
+ ( username )
58
+ VALUES
59
+ ('bobby'),
60
+ ('devdojo'),
61
+ ('tony'),
62
+ ('greisi');
63
+ ```
64
+
65
+ * And finally add some posts:
66
+
67
+ ```sql
68
+ INSERT INTO posts
69
+ ( user_id, title )
70
+ VALUES
71
+ ('1', 'Hello World!'),
72
+ ('2', 'Getting started with SQL'),
73
+ ('3', 'SQL is awesome'),
74
+ ('2', 'MySQL is up!'),
75
+ ('1', 'SQL - structured query language');
76
+ ```
77
+
78
+ Now that we've got our tables and demo data ready, let's go ahead and learn how to use joins.
79
+
80
+ ## CROSS JOIN
81
+
82
+ The `CROSS` join allows you to put the result of two tables next to each other without specifying any `WHERE` conditions. This makes the `CROSS` join the simplest one, but it is also not of much use in a real-life scenario.
83
+
84
+ So if we were to select all of the users and all of the posts side by side, we would use the following query:
85
+
86
+ ```sql
87
+ SELECT * FROM users CROSS JOIN posts;
88
+ ```
89
+
90
+ The output will be all of your users and all of the posts side by side:
91
+
92
+ ```
93
+ +----+----------+----+--------+-----------------+
94
+ | id | username | id |user_id | title |
95
+ +----+----------+----+--------+-----------------+
96
+ | 4 | greisi | 1 | 1 | Hello World! |
97
+ | 3 | tony | 1 | 1 | Hello World! |
98
+ | 2 | devdojo | 1 | 1 | Hello World! |
99
+ | 1 | bobby | 1 | 1 | Hello World! |
100
+ | 4 | greisi | 2 | 2 | Getting started |
101
+ | 3 | tony | 2 | 2 | Getting started |
102
+ | 2 | devdojo | 2 | 2 | Getting started |
103
+ | 1 | bobby | 2 | 2 | Getting started |
104
+ | 4 | greisi | 3 | 3 | SQL is awesome |
105
+ | 3 | tony | 3 | 3 | SQL is awesome |
106
+ | 2 | devdojo | 3 | 3 | SQL is awesome |
107
+ | 1 | bobby | 3 | 3 | SQL is awesome |
108
+ | 4 | greisi | 4 | 2 | MySQL is up! |
109
+ | 3 | tony | 4 | 2 | MySQL is up! |
110
+ | 2 | devdojo | 4 | 2 | MySQL is up! |
111
+ | 1 | bobby | 4 | 2 | MySQL is up! |
112
+ | 4 | greisi | 5 | 1 | SQL |
113
+ | 3 | tony | 5 | 1 | SQL |
114
+ | 2 | devdojo | 5 | 1 | SQL |
115
+ | 1 | bobby | 5 | 1 | SQL |
116
+ +----+----------+----+--------+-----------------+
117
+ ```
118
+
119
+ As mentioned above, you will highly unlikely run a `CROSS` join for two whole tables in a real-life scenario. If the tables have tens of thousands of rows, an unqualified CROSS JOIN can take minutes to complete.
120
+
121
+ You would most likely use one of the following with a specific condition.
122
+
123
+ In MySQL, CROSS JOIN and INNER JOIN are equivalent to JOIN.
124
+
125
+ ## INNER JOIN
126
+
127
+ The `INNER` join is used to join two tables. However, unlike the `CROSS` join, by convention, it is based on a condition. By using an `INNER` join, you can match the first table to the second one.
128
+
129
+ As we have a one-to-many relationship, a best practice would be to use a primary key for the posts `id` column and a foreign key for the `user_id`; that way, we can 'link' or relate the users table to the posts table. However, this is beyond the scope of this SQL basics eBook, though I might extend it in the future and add more chapters.
130
+
131
+ As an example and to make things a bit clearer, let's say that you wanted to get all of your users and the posts associated with each user. The query that we would use will look like this:
132
+
133
+ ```sql
134
+ SELECT *
135
+ FROM users
136
+ INNER JOIN posts
137
+ ON users.id = posts.user_id;
138
+ ```
139
+
140
+ Rundown of the query:
141
+
142
+ * `SELECT * FROM users`: This is a standard select we've covered many times in the previous chapters.
143
+ * `INNER JOIN posts`: Then, we specify the second table and which table we want to join the result set.
144
+ * `ON users.id = posts.user_id`: Finally, we specify how we want the data in these two tables to be merged. The `user.id` is the `id` column of the `user` table, which is also the primary ID, and `posts.user_id` is the foreign key in the email address table referring to the ID column in the users table.
145
+
146
+ The output will be the following, associating each user with their post based on the `user_id` column:
147
+
148
+ ```
149
+ +----+----------+----+---------+-----------------+
150
+ | id | username | id | user_id | title |
151
+ +----+----------+----+---------+-----------------+
152
+ | 1 | bobby | 1 | 1 | Hello World! |
153
+ | 2 | devdojo | 2 | 2 | Getting started |
154
+ | 3 | tony | 3 | 3 | SQL is awesome |
155
+ | 2 | devdojo | 4 | 2 | MySQL is up! |
156
+ | 1 | bobby | 5 | 1 | SQL |
157
+ +----+----------+----+---------+-----------------+
158
+ ```
159
+ Note that the INNER JOIN could (in MySQL) equivalently be written merely as JOIN, but that can vary for other SQL dialects:
160
+
161
+ ```sql
162
+ SELECT *
163
+ FROM users
164
+ JOIN posts
165
+ ON users.id = posts.user_id;
166
+ ```
167
+
168
+ The main things that you need to keep in mind here are the `INNER JOIN` and `ON` clauses.
169
+
170
+ With the inner join, the `NULL` values are discarded. For example, if you have a user who does not have a post associated with it, the user with NULL posts will not be displayed when running the above `INNER` join query.
171
+
172
+ To get the null values as well, you would need to use an outer join.
173
+
174
+ ### Types of INNER JOIN
175
+
176
+ 1. **Theta Join ( θ )** :- Theta join combines rows from different tables provided they satisfy the theta condition.
177
+ The join condition is denoted by the symbol `θ`. \
178
+ Here the comparison operators `(≤, ≥, ˂, ˃, =, ̚ )` come into picture. \
179
+ **Notation** :- R<sub>1</sub> ⋈<sub>θ</sub> R<sub>2</sub>. \
180
+ \
181
+ For example, suppose we want to buy a mobile and a laptop, based on our budget we have thought of buying both such that mobile price should be less than that of laptop. \
182
+ \
183
+ `SELECT mobile.model, laptop.model
184
+ FROM mobile, laptop
185
+ WHERE mobile.price < laptop.price;`
186
+
187
+ 2. **Equijoin** :- When Theta join uses only equality (=) comparison operator, it is said to be equijoin. \
188
+ For example, suppose we want to buy a mobile and a laptop, based on our budget we have thought of buying both of the same prices. \
189
+ \
190
+ `SELECT mobile.model, laptop.model
191
+ FROM mobile, laptop
192
+ WHERE mobile.price = laptop.price;`
193
+
194
+ 3. **Natural Join ( ⋈ )** :- Natural join does not use any comparison operator. It does not concatenate the way a Cartesian product does. \
195
+ We can perform a Natural Join only if at least one standard column exists between two tables. In addition, the column must have the same name and domain. \
196
+ \
197
+ `SELECT * FROM mobile NATURAL JOIN laptop;`
198
+
199
+
200
+ ## LEFT JOIN
201
+
202
+ Using the `LEFT OUTER` join, you would get all rows from the first table that you've specified, and if there are no associated records within the second table, you will get a `NULL` value.
203
+
204
+ In our case, we have a user called `graisi`, which is not associated with a specific post. As you can see from the output from the previous query, the `graisi` user was not present there. To show that user, even though it does not have an associated post with it, you could use a `LEFT OUTER` join:
205
+
206
+ ```sql
207
+ SELECT *
208
+ FROM users
209
+ LEFT JOIN posts
210
+ ON users.id = posts.user_id;
211
+ ```
212
+
213
+ The output will look like this:
214
+
215
+ ```
216
+ +----+----------+------+---------+-----------------+
217
+ | id | username | id | user_id | title |
218
+ +----+----------+------+---------+-----------------+
219
+ | 1 | bobby | 1 | 1 | Hello World! |
220
+ | 2 | devdojo | 2 | 2 | Getting started |
221
+ | 3 | tony | 3 | 3 | SQL is awesome |
222
+ | 2 | devdojo | 4 | 2 | MySQL is up! |
223
+ | 1 | bobby | 5 | 1 | SQL |
224
+ | 4 | greisi | NULL | NULL | NULL |
225
+ +----+----------+------+---------+-----------------+
226
+ ```
227
+
228
+ ## RIGHT JOIN
229
+
230
+ The `RIGHT OUTER` join is the exact opposite of the `LEFT OUTER` join. It will display all of the rows from the second table and give you a `NULL` value in case that it does not match with an entry from the first table.
231
+
232
+ Let's create a post that does not have a matching user id:
233
+
234
+ ```sql
235
+ INSERT INTO posts
236
+ ( user_id, title )
237
+ VALUES
238
+ ('123', 'No user post!');
239
+ ```
240
+
241
+ We specify `123` as the user ID, but we don't have such a user in our `users` table.
242
+
243
+ Now, if you were to run the `LEFT` outer join, you would not see the post as it has a null value for the corresponding `users` table.
244
+
245
+ But if you were to run a `RIGHT` outer join, you would see the post but not the `greisi` user as it does not have any posts:
246
+
247
+ ```sql
248
+ SELECT *
249
+ FROM users
250
+ RIGHT JOIN posts
251
+ ON users.id = posts.user_id;
252
+ ```
253
+
254
+ Output:
255
+
256
+ ```
257
+ +------+----------+----+---------+-----------------+
258
+ | id | username | id | user_id | title |
259
+ +------+----------+----+---------+-----------------+
260
+ | 1 | bobby | 1 | 1 | Hello World! |
261
+ | 2 | devdojo | 2 | 2 | Getting started |
262
+ | 3 | tony | 3 | 3 | SQL is awesome |
263
+ | 2 | devdojo | 4 | 2 | MySQL is up! |
264
+ | 1 | bobby | 5 | 1 | SQL |
265
+ | NULL | NULL | 6 | 123 | No user post! |
266
+ +------+----------+----+---------+-----------------+
267
+ ```
268
+
269
+ Joins can also be limited with WHERE conditions. For instance, in the preceding example, if we wanted to join the tables and then restrict to only username `bobby`.
270
+
271
+
272
+ ```sql
273
+ SELECT *
274
+ FROM users
275
+ RIGHT JOIN posts
276
+ ON users.id = posts.user_id
277
+ WHERE username = 'bobby';
278
+ ```
279
+
280
+ Output:
281
+
282
+ ```
283
+ +------+----------+----+---------+-----------------+
284
+ | id | username | id | user_id | title |
285
+ +------+----------+----+---------+-----------------+
286
+ | 1 | bobby | 1 | 1 | Hello World! |
287
+ | 1 | bobby | 5 | 1 | SQL |
288
+ +------+----------+----+---------+-----------------+
289
+ ```
290
+
291
+ ## Conclusion
292
+
293
+ Joins are fundamental to using SQL with data. The whole concept of joins might be very confusing initially but would make a lot of sense once you get used to it.
294
+
295
+ The best way to wrap your head around it is to write some queries, play around with each type of `JOIN`, and see how the result set changes.
296
+
297
+ For more information, you could take a look at the official documentation [here](https://dev.mysql.com/doc/refman/8.0/en/join.html).