diff --git a/index.html b/index.html index 3bd909a..ef71ef2 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 4dbde3b..03b3977 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, 12 Jan 2024 15:40:37 +0330GROUP 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. +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. +-- 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. By adding ORDER BY we can sort the output. -- list orders and sort them by total_price -- this will list them from lowest to highest SELECT id, order_code, quantity, total_price FROM orders ORDER BY total_price; -- +------+--------------------------------------+----------+-------------+ -- | id | order_code | quantity | total_price | -- |------+--------------------------------------+----------+-------------| -- | 917 | 27fdbd2b-1fa0-4633-ac2f-1f007fd9d6e2 | 1 | 200 | -- | 266 | 95b02f2a-bedd-4ad5-b767-3854cb886ef9 | 1 | 201 | -- | 871 | e232f447-4bae-4c45-b4d2-87663217df0a | 1 | 206 | -- | 89 | b2c93d87-0169-4152-986e-ee3fa20ff50c | 1 | 210 | -- | 200 | a7a612cd-6f1d-4844-b19c-d588530863fb | 1 | 211 | -- .DELETEhttps://peyman.blog/sql/posts/delete/Mon, 01 Jan 2024 20:40:45 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/delete/With DELETE we can delete one or multiple rows from a table. diff --git a/posts/and-or-not/index.html b/posts/and-or-not/index.html new file mode 100644 index 0000000..8c924ec --- /dev/null +++ b/posts/and-or-not/index.html @@ -0,0 +1,49 @@ +AND, OR, NOT - SQL by example

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.com     | Port Anthony    | True   |
+-- | 51  | 2022-07-05 12:04:27 | John       | Fritz     | darren27@gmail.com           | Masonborough    | True   |
+-- | 285 | 2021-11-17 01:40:06 | John       | Sanders   | ygordon@yahoo.com            | Erinfurt        | True   |
+-- ...
+
+-- select all products without the ones that their company
+-- name is 'Allen-Jones'
+SELECT
+  created_at,
+  product_code,
+  name,
+  company,
+  price
+FROM products
+WHERE NOT company = 'Allen-Jones';
+-- +---------------------+--------------+---------------+---------------------------------+-------+
+-- | created_at          | product_code | name          | company                         | price |
+-- |---------------------+--------------+---------------+---------------------------------+-------|
+-- | 2023-09-01 23:59:14 | 30248960     | teal paint    | Burke, Payne and Oliver         | 427   |
+-- | 2022-09-09 15:49:58 | 12996179     | olive paint   | Alexander Inc                   | 377   |
+-- | 2022-01-07 00:53:59 | 84201010     | olive paint   | Gray, Carson and Oneill         | 402   |
+-- ...
+

PostgreSQL docs

\ No newline at end of file diff --git a/posts/index.html b/posts/index.html index 9063453..ab6b383 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 67e5dbc..b6ef102 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 +0000GROUP 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. +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. +-- 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. By adding ORDER BY we can sort the output. -- list orders and sort them by total_price -- this will list them from lowest to highest SELECT id, order_code, quantity, total_price FROM orders ORDER BY total_price; -- +------+--------------------------------------+----------+-------------+ -- | id | order_code | quantity | total_price | -- |------+--------------------------------------+----------+-------------| -- | 917 | 27fdbd2b-1fa0-4633-ac2f-1f007fd9d6e2 | 1 | 200 | -- | 266 | 95b02f2a-bedd-4ad5-b767-3854cb886ef9 | 1 | 201 | -- | 871 | e232f447-4bae-4c45-b4d2-87663217df0a | 1 | 206 | -- | 89 | b2c93d87-0169-4152-986e-ee3fa20ff50c | 1 | 210 | -- | 200 | a7a612cd-6f1d-4844-b19c-d588530863fb | 1 | 211 | -- .DELETEhttps://peyman.blog/sql/posts/delete/Mon, 01 Jan 2024 20:40:45 +0330slh.peyman@gmail.com (Peyman Salehi)https://peyman.blog/sql/posts/delete/With DELETE we can delete one or multiple rows from a table. diff --git a/posts/limit/index.html b/posts/limit/index.html new file mode 100644 index 0000000..ff71834 --- /dev/null +++ b/posts/limit/index.html @@ -0,0 +1,11 @@ +LIMIT - SQL by example

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.com            | West Misty     | True   |
+-- | 3  | 2023-01-30 08:00:52 | Brandon    | Jenkins   | lrichardson@meadows-hodges.biz | Pinedamouth    | True   |
+-- +----+---------------------+------------+-----------+--------------------------------+----------------+--------+
+

PostgreSQL docs

\ No newline at end of file diff --git a/posts/setup/index.html b/posts/setup/index.html index baa927f..2eb39e9 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.