diff --git a/index.html b/index.html index ef71ef2..d216d1f 100644 --- a/index.html +++ b/index.html @@ -2,4 +2,4 @@ in most popular RDBMSs and be as short as possible and refference to a reliable source for more info. You can set up your environment and import sample data in the Setup post. If you find any issues or want to add more examples, -feel free to open an issue or send a pull request in GitHub.

Introduction

Database and Table Management

Modifying Data

Query and Filtering Data

Latest Posts

\ No newline at end of file +feel free to open an issue or send a pull request in GitHub.

Introduction

Database and Table Management

Modifying Data

Query and Filtering Data

Latest Posts

\ No newline at end of file diff --git a/index.xml b/index.xml index 03b3977..d0cc033 100644 --- a/index.xml +++ b/index.xml @@ -1,4 +1,6 @@ -Learn SQL by example on SQL by examplehttps://peyman.blog/sql/Recent content in Learn SQL by example on SQL by exampleHugo -- gohugo.ioen-usslh.peyman@gmail.com (Peyman Salehi)slh.peyman@gmail.com (Peyman Salehi)Fri, 02 Feb 2024 15:09:44 +0330LIMIThttps://peyman.blog/sql/posts/limit/Fri, 02 Feb 2024 15:09:44 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/limit/When the number of rows in a select query are a lot, we can limit them by the LIMIT statement. +Learn SQL by example on SQL by examplehttps://peyman.blog/sql/Recent content in Learn SQL by example on SQL by exampleHugo -- gohugo.ioen-usslh.peyman@gmail.com (Peyman Salehi)slh.peyman@gmail.com (Peyman Salehi)Sat, 03 Feb 2024 15:49:57 +0330MIN, MAXhttps://peyman.blog/sql/posts/min-max/Sat, 03 Feb 2024 15:49:57 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/min-max/MIN returns the smallest value of a column. MAX returns the biggest value of a column. +-- return the lowest total_price from orders of user with id of 10 SELECT MIN(total_price) FROM orders WHERE user_id = 10; -- +------+ -- | min | -- |------| -- | 1132 | -- +------+ -- return the highest total_price from the orders of user with id of 10 SELECT MAX(total_price) FROM orders WHERE user_id = 10; -- +------+ -- | max | -- |------| -- | 1998 | -- +------+ PostgreSQL docsCommenthttps://peyman.blog/sql/posts/comment/Sat, 03 Feb 2024 15:11:15 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/comment/When you writing multiline SQL queries you can use -- to comment a single line and use /* */ to comment multiline texts. +-- Single line comment /* Multi line comment */LIMIThttps://peyman.blog/sql/posts/limit/Fri, 02 Feb 2024 15:09:44 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/limit/When the number of rows in a select query are a lot, we can limit them by the LIMIT statement. -- only return 3 item SELECT * FROM users LIMIT 3; -- +----+---------------------+------------+-----------+--------------------------------+----------------+--------+ -- | id | datetime_joined | first_name | last_name | email | city | active | -- |----+---------------------+------------+-----------+--------------------------------+----------------+--------| -- | 1 | 2023-08-18 21:44:13 | Carmen | Malone | rmcconnell@yahoo.com | East Jeanmouth | True | -- | 2 | 2023-07-27 04:31:11 | Stephanie | Wallace | hjennings@curry.AND, OR, NOThttps://peyman.blog/sql/posts/and-or-not/Fri, 02 Feb 2024 14:18:01 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/and-or-not/With the help of AND, OR, and NOT we can add more power to our query conditions. -- select all orders with product_id of 10 and delivered -- only select rows where both conditions are true SELECT * FROM orders WHERE product_id = '10' AND delivered = true; -- +-----+---------------------+--------------------------------------+------------+---------+----------+-------------+-----------+ -- | id | created_at | order_code | product_id | user_id | quantity | total_price | delivered | -- |-----+---------------------+--------------------------------------+------------+---------+----------+-------------+-----------| -- | 113 | 2023-07-08 21:09:44 | 12f82968-46fc-4118-9c58-fb568c38fa4e | 10 | 343 | 1 | 445 | True | -- | 121 | 2023-06-03 00:18:42 | 24d070f9-2e4b-4b41-8d76-c99167083405 | 10 | 239 | 8 | 3560 | True | -- | 487 | 2023-07-04 06:34:56 | 23da1388-e818-48af-ab5b-bc14951a22e0 | 10 | 41 | 5 | 2225 | True | -- | 774 | 2023-04-20 09:35:38 | 07bbf026-ef4f-4078-a919-48702af47cb0 | 10 | 67 | 3 | 1335 | True | -- +-----+---------------------+--------------------------------------+------------+---------+----------+-------------+-----------+ -- select all users where their first or last name is John -- select rows where only one of the conditions are true SELECT * FROM users WHERE first_name = 'John' OR last_name = 'John'; -- +-----+---------------------+------------+-----------+------------------------------+-----------------+--------+ -- | id | datetime_joined | first_name | last_name | email | city | active | -- |-----+---------------------+------------+-----------+------------------------------+-----------------+--------| -- | 14 | 2022-12-18 03:47:51 | John | Combs | katherinewiley@myers.GROUP BYhttps://peyman.blog/sql/posts/group-by/Fri, 12 Jan 2024 15:40:37 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/group-by/GROUP BY comblines the output into groups of rows that matches on one or multiple values. Also, GROUP BY usually used with aggregate functions like COUNT, MIN, MAX, SUM, and AVG. -- count number of products for each company SELECT COUNT(id), company FROM products GROUP BY company; -- +-------+---------------------------------+ -- | count | company | -- |-------+---------------------------------| -- | 1 | Edwards, Fox and Valentine | -- | 2 | Hale-Bryan | -- | 1 | Alexander Inc | -- | 1 | Stark and Sons | -- | 2 | Martin, Baker and Henderson | -- .ORDER BYhttps://peyman.blog/sql/posts/order-by/Wed, 03 Jan 2024 23:55:35 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/order-by/In a select query, after choosing the columns and adding some conditions we can choose the ordering of the output data based on a columns which can be an integer, datetime or some other kinds of sortable data types. diff --git a/posts/comment/index.html b/posts/comment/index.html new file mode 100644 index 0000000..30835fe --- /dev/null +++ b/posts/comment/index.html @@ -0,0 +1,7 @@ +Comment - SQL by example

When you writing multiline SQL queries you can use -- to comment a single line and +use /* */ to comment multiline texts.

-- Single line comment
+
+/* Multi
+line comment
+*/
+
\ No newline at end of file diff --git a/posts/index.html b/posts/index.html index ab6b383..4a5400b 100644 --- a/posts/index.html +++ b/posts/index.html @@ -1 +1 @@ -Blog - SQL by example \ No newline at end of file +Blog - SQL by example \ No newline at end of file diff --git a/posts/index.xml b/posts/index.xml index b6ef102..c5b4cd5 100644 --- a/posts/index.xml +++ b/posts/index.xml @@ -1,4 +1,6 @@ -Blog on SQL by examplehttps://peyman.blog/sql/posts/Recent content in Blog on SQL by exampleHugo -- gohugo.ioen-usslh.peyman@gmail.com (Peyman Salehi)slh.peyman@gmail.com (Peyman Salehi)Sun, 24 Feb 2019 00:00:00 +0000LIMIThttps://peyman.blog/sql/posts/limit/Fri, 02 Feb 2024 15:09:44 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/limit/When the number of rows in a select query are a lot, we can limit them by the LIMIT statement. +Blog on SQL by examplehttps://peyman.blog/sql/posts/Recent content in Blog on SQL by exampleHugo -- gohugo.ioen-usslh.peyman@gmail.com (Peyman Salehi)slh.peyman@gmail.com (Peyman Salehi)Sun, 24 Feb 2019 00:00:00 +0000MIN, MAXhttps://peyman.blog/sql/posts/min-max/Sat, 03 Feb 2024 15:49:57 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/min-max/MIN returns the smallest value of a column. MAX returns the biggest value of a column. +-- return the lowest total_price from orders of user with id of 10 SELECT MIN(total_price) FROM orders WHERE user_id = 10; -- +------+ -- | min | -- |------| -- | 1132 | -- +------+ -- return the highest total_price from the orders of user with id of 10 SELECT MAX(total_price) FROM orders WHERE user_id = 10; -- +------+ -- | max | -- |------| -- | 1998 | -- +------+ PostgreSQL docsCommenthttps://peyman.blog/sql/posts/comment/Sat, 03 Feb 2024 15:11:15 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/comment/When you writing multiline SQL queries you can use -- to comment a single line and use /* */ to comment multiline texts. +-- Single line comment /* Multi line comment */LIMIThttps://peyman.blog/sql/posts/limit/Fri, 02 Feb 2024 15:09:44 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/limit/When the number of rows in a select query are a lot, we can limit them by the LIMIT statement. -- only return 3 item SELECT * FROM users LIMIT 3; -- +----+---------------------+------------+-----------+--------------------------------+----------------+--------+ -- | id | datetime_joined | first_name | last_name | email | city | active | -- |----+---------------------+------------+-----------+--------------------------------+----------------+--------| -- | 1 | 2023-08-18 21:44:13 | Carmen | Malone | rmcconnell@yahoo.com | East Jeanmouth | True | -- | 2 | 2023-07-27 04:31:11 | Stephanie | Wallace | hjennings@curry.AND, OR, NOThttps://peyman.blog/sql/posts/and-or-not/Fri, 02 Feb 2024 14:18:01 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/and-or-not/With the help of AND, OR, and NOT we can add more power to our query conditions. -- select all orders with product_id of 10 and delivered -- only select rows where both conditions are true SELECT * FROM orders WHERE product_id = '10' AND delivered = true; -- +-----+---------------------+--------------------------------------+------------+---------+----------+-------------+-----------+ -- | id | created_at | order_code | product_id | user_id | quantity | total_price | delivered | -- |-----+---------------------+--------------------------------------+------------+---------+----------+-------------+-----------| -- | 113 | 2023-07-08 21:09:44 | 12f82968-46fc-4118-9c58-fb568c38fa4e | 10 | 343 | 1 | 445 | True | -- | 121 | 2023-06-03 00:18:42 | 24d070f9-2e4b-4b41-8d76-c99167083405 | 10 | 239 | 8 | 3560 | True | -- | 487 | 2023-07-04 06:34:56 | 23da1388-e818-48af-ab5b-bc14951a22e0 | 10 | 41 | 5 | 2225 | True | -- | 774 | 2023-04-20 09:35:38 | 07bbf026-ef4f-4078-a919-48702af47cb0 | 10 | 67 | 3 | 1335 | True | -- +-----+---------------------+--------------------------------------+------------+---------+----------+-------------+-----------+ -- select all users where their first or last name is John -- select rows where only one of the conditions are true SELECT * FROM users WHERE first_name = 'John' OR last_name = 'John'; -- +-----+---------------------+------------+-----------+------------------------------+-----------------+--------+ -- | id | datetime_joined | first_name | last_name | email | city | active | -- |-----+---------------------+------------+-----------+------------------------------+-----------------+--------| -- | 14 | 2022-12-18 03:47:51 | John | Combs | katherinewiley@myers.GROUP BYhttps://peyman.blog/sql/posts/group-by/Fri, 12 Jan 2024 15:40:37 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/group-by/GROUP BY comblines the output into groups of rows that matches on one or multiple values. Also, GROUP BY usually used with aggregate functions like COUNT, MIN, MAX, SUM, and AVG. -- count number of products for each company SELECT COUNT(id), company FROM products GROUP BY company; -- +-------+---------------------------------+ -- | count | company | -- |-------+---------------------------------| -- | 1 | Edwards, Fox and Valentine | -- | 2 | Hale-Bryan | -- | 1 | Alexander Inc | -- | 1 | Stark and Sons | -- | 2 | Martin, Baker and Henderson | -- .ORDER BYhttps://peyman.blog/sql/posts/order-by/Wed, 03 Jan 2024 23:55:35 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/order-by/In a select query, after choosing the columns and adding some conditions we can choose the ordering of the output data based on a columns which can be an integer, datetime or some other kinds of sortable data types. diff --git a/posts/min-max/index.html b/posts/min-max/index.html new file mode 100644 index 0000000..e499524 --- /dev/null +++ b/posts/min-max/index.html @@ -0,0 +1,17 @@ +MIN, MAX - SQL by example

MIN returns the smallest value of a column. +MAX returns the biggest value of a column.

-- return the lowest total_price from orders of user with id of 10
+SELECT MIN(total_price) FROM orders WHERE user_id = 10;
+-- +------+
+-- | min  |
+-- |------|
+-- | 1132 |
+-- +------+
+
+-- return the highest total_price from the orders of user with id of 10
+SELECT MAX(total_price) FROM orders WHERE user_id = 10;
+-- +------+
+-- | max  |
+-- |------|
+-- | 1998 |
+-- +------+
+

PostgreSQL docs

\ No newline at end of file diff --git a/posts/setup/index.html b/posts/setup/index.html index 2eb39e9..4bd055a 100644 --- a/posts/setup/index.html +++ b/posts/setup/index.html @@ -1,6 +1,6 @@ Setup - SQL by example

If you want to run the examples in the following posts, you can follow these instructions to setup your environment and import -our sample data to work with.