Last Sync: 2022-08-05 20:30:04
This commit is contained in:
parent
c679657cd8
commit
76c9c5ed65
6 changed files with 279 additions and 0 deletions
100
Databases/SQL/10_Joins.md
Normal file
100
Databases/SQL/10_Joins.md
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Programming_Languages
|
||||||
|
- Databases
|
||||||
|
- sql
|
||||||
|
---
|
||||||
|
|
||||||
|
# SQL: Joins
|
||||||
|
|
||||||
|
Once a relationship has been created using primary and foreign keys (as detailed in the previous section), you are able to combine and integrated data from the different tables. This is known as performing **joins.**
|
||||||
|
|
||||||
|
## Inner joins
|
||||||
|
|
||||||
|
We can demonstrate this with the following scenario:
|
||||||
|
|
||||||
|
>
|
||||||
|
> We want to create a list of the name of all computers that have been sold and when they were sold.
|
||||||
|
|
||||||
|
This will require us to use the `name` field from the `model` table and the `sale_date` field from the `sales` table.
|
||||||
|
|
||||||
|
Here's the SQL:
|
||||||
|
|
||||||
|
````sql
|
||||||
|
SELECT model.name, sales.sale_date
|
||||||
|
FROM model
|
||||||
|
INNER JOIN sales on model.model_id = sales.model_id;
|
||||||
|
````
|
||||||
|
|
||||||
|
* We use dot notation to distinguish the `table.field` for each table.
|
||||||
|
* We use `INNER JOIN` to join the `sales` table with the `model` table where `model_id` field in `model` is the same as the `model_id` field in `sales`
|
||||||
|
|
||||||
|
This returns:
|
||||||
|
|
||||||
|
````sql
|
||||||
|
name sale_date
|
||||||
|
-------------------- ----------
|
||||||
|
Raspberry Pi 2 Mo 4 2015-02-01
|
||||||
|
Raspberry Pi 3 Mo 4 2018-11-01
|
||||||
|
````
|
||||||
|
|
||||||
|
Note data will only be returned when there is a match between both fields stated in the `SELECT` clause. There must be corresponding data between `model.name` and `sale.sale_data` for a row to be returned. For example if there is a model that has not been sold, there will be a `mode.model_name` but no `sale_data` .
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Outer joins
|
||||||
|
|
||||||
|
In the example above, we used the `INNER JOIN` method. This enshrines the logic:
|
||||||
|
|
||||||
|
> return only rows where there is a matching row in both tables
|
||||||
|
|
||||||
|
Which in the applied context means:
|
||||||
|
|
||||||
|
* If there is a model that has never been sold, it won’t be returned
|
||||||
|
* If there is a sale without a model, it won’t be returned
|
||||||
|
|
||||||
|
But there are other types of join that satisfy other types of logic.
|
||||||
|
|
||||||
|
The logical state that obtains in the case of **inner joins**:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
The logical state that obtains in the case of **left outer joins**
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
The logical state that obtains in the case of **right outer joins**:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
The logical state that obtains in the case of **full outer joins**:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
This type of join is used when you want to discern when there is *not* a match between two fields across tables. For example: imagine that you wanted a list of computers that had never been sold. In this case, you would be interested in rows where there is a `model_id` without a corresponding `sales_id` .
|
||||||
|
|
||||||
|
In SQL this would be achieved with:
|
||||||
|
|
||||||
|
````sql
|
||||||
|
SELECT model.name, sales.sale_date
|
||||||
|
FROM model
|
||||||
|
LEFT JOIN sales on model.model_id = sales.model_id;
|
||||||
|
````
|
||||||
|
|
||||||
|
Note that this would return all the model names but where there isn't a sale data, `NULL` would be returned. This is an **important distinction :** the outer join method doesn't just return the rows with a `NULL` value for `sale_date` as we might expect. It returns all models along with those that have not been sold. This is because it is oriented to the "left" table, equivalent to the table in the SQL that we cited first with the `on` keyword.
|
||||||
|
|
||||||
|
>
|
||||||
|
> A left outer join returns all the records from the left (model) table and those that match in the right (sales) table. Where there are no matching records in the right (sales) table, a `NULL` value is returned.
|
||||||
|
|
||||||
|
A **right outer join**, often referred to as a right join, is the opposite of a left outer; it returns all the records from the right table and those that match in the left table. In our scenario this would be all the models that had a `sale_date` including models that didn't have a `sale_date` , i.e which returned `NULL`
|
||||||
|
|
||||||
|
Finally, a **full outer join** returns all the records from both tables, and where a record cannot be matched, a NULL value is returned. So this would mean there could be `NULL`s in both fields of the returned rows.
|
||||||
|
|
||||||
|
We can combine multiple types of join in the same SQL query:
|
||||||
|
|
||||||
|
````sql
|
||||||
|
SELECT model.name, sales.sale_date, manufacturer.url
|
||||||
|
FROM model
|
||||||
|
LEFT JOIN sales on model.model_id = sales.model_id
|
||||||
|
INNER JOIN manufacturer on model.manufacturer_id = manufacturer.manufacturer_id;
|
||||||
|
````
|
37
Databases/SQL/11_Aggregate_functions.md
Normal file
37
Databases/SQL/11_Aggregate_functions.md
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Programming_Languages
|
||||||
|
- Databases
|
||||||
|
- sql
|
||||||
|
---
|
||||||
|
|
||||||
|
# SQL Aggregate functions
|
||||||
|
|
||||||
|
## Count return with custom variable
|
||||||
|
|
||||||
|
````sql
|
||||||
|
SELECT COUNT(*) AS total_sales
|
||||||
|
FROM SALES
|
||||||
|
````
|
||||||
|
|
||||||
|
## Sum
|
||||||
|
|
||||||
|
````sql
|
||||||
|
SELECT SUM(price) as total_value
|
||||||
|
FROM sales
|
||||||
|
````
|
||||||
|
|
||||||
|
## Average
|
||||||
|
|
||||||
|
````sql
|
||||||
|
SELECT AVG(price) as average_income
|
||||||
|
FROM sales
|
||||||
|
````
|
||||||
|
|
||||||
|
## Applying aggregate function with sorting applied
|
||||||
|
|
||||||
|
````sql
|
||||||
|
SELECT COUNT(*) AS total_sales
|
||||||
|
FROM sales
|
||||||
|
GROUP BY employee_id
|
||||||
|
````
|
|
@ -0,0 +1,50 @@
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Programming_Languages
|
||||||
|
- Databases
|
||||||
|
- sql
|
||||||
|
---
|
||||||
|
|
||||||
|
# SQL: Language structure
|
||||||
|
|
||||||
|
Before we start using the syntax we need to understand the grammar:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Expressions differ from clauses and predicates in that they are not the mechanism for returning data (i.e. declaring a clause and a logical condition) they do something to the data, as part of the retrieval. This is a bit subtle:
|
||||||
|
|
||||||
|
* `SELECT name FROM model WHERE cores = "4"`
|
||||||
|
* This retrieves the models that have 4 cores
|
||||||
|
* `SELECT count(*) FROM model WHERE cores = "4" `
|
||||||
|
* This counts the number of models that are returned where the counting is a function over and above the retrieval itself.
|
||||||
|
|
||||||
|
### Examples from `computer_sales.db`
|
||||||
|
|
||||||
|
`sqlite> SELECT * from model WHERE cpu_speed=0.7` : return all models with a CPU speed equal to 0.7:
|
||||||
|
|
||||||
|
````
|
||||||
|
model_id manufacturer_id name cpu_speed ram cores wifi release_date
|
||||||
|
---------- --------------- ---------------------- ---------- ---------- ---------- ---------- ------------
|
||||||
|
1 1 Raspberry Pi 1 Model A 0.7 256.0 1 0 2013-02-01
|
||||||
|
2 1 Raspberry Pi 1 Model B 0.7 256.0 1 0 2012-04-01
|
||||||
|
3 1 Raspberry Pi 1 Model B 0.7 512.0 1 0 2012-10-01
|
||||||
|
4 1 Raspberry Pi 1 Model A 0.7 512.0 1 0 2014-11-01
|
||||||
|
5 1 Raspberry Pi 1 Model B 0.7 512.0 1 0 2014-07-01
|
||||||
|
````
|
||||||
|
|
||||||
|
````
|
||||||
|
count(*)
|
||||||
|
----------
|
||||||
|
5
|
||||||
|
````
|
||||||
|
> Any value that is not a number should be in single-quotes, never double quotes
|
||||||
|
|
||||||
|
## Main commands
|
||||||
|
|
||||||
|
There are obviously many SQL commands but most standard CRUD actions can be executed with a small number of commands:
|
||||||
|
|
||||||
|
* `SELECT`
|
||||||
|
* `UPDATE`
|
||||||
|
* `CREATE`
|
||||||
|
* `INSERT`
|
||||||
|
* `DELETE`
|
|
@ -0,0 +1,19 @@
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Programming_Languages
|
||||||
|
- Databases
|
||||||
|
- sql
|
||||||
|
---
|
||||||
|
|
||||||
|
# SQL: ALTER
|
||||||
|
|
||||||
|
|
||||||
|
We use the `ALTER` query to add, remove and otherwise change the structural properties of a table.
|
||||||
|
|
||||||
|
## Add an additional field to existing table
|
||||||
|
|
||||||
|
This adds a `price` field to the `sales` table. The `price` field accepts data of the type `real` . `real` is a slightly less precise (less memory) version of float
|
||||||
|
|
||||||
|
````sql
|
||||||
|
ALTER TABLE sales ADD price real;
|
||||||
|
````
|
28
Databases/SQL/8_CREATE.md
Normal file
28
Databases/SQL/8_CREATE.md
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Programming_Languages
|
||||||
|
- Databases
|
||||||
|
- sql
|
||||||
|
---
|
||||||
|
|
||||||
|
# SQL: CREATE
|
||||||
|
|
||||||
|
## Create a table (`CREATE`)
|
||||||
|
|
||||||
|
````sql
|
||||||
|
CREATE TABLE employee (
|
||||||
|
employee_id text PRIMARY KEY,
|
||||||
|
first_name text,
|
||||||
|
surname text,
|
||||||
|
address_number integer,
|
||||||
|
address_1 text,
|
||||||
|
address_2 text,
|
||||||
|
locality text,
|
||||||
|
region text,
|
||||||
|
postal_code text,
|
||||||
|
phone_number text,
|
||||||
|
days_per_week real
|
||||||
|
);
|
||||||
|
````
|
||||||
|
|
||||||
|
We specify the new table name first, then it's fields and their corresponding data types. We also set a primary key
|
45
Databases/SQL/9_Utilising_foreign_keys.md
Normal file
45
Databases/SQL/9_Utilising_foreign_keys.md
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Programming_Languages
|
||||||
|
- Databases
|
||||||
|
- sql
|
||||||
|
---
|
||||||
|
|
||||||
|
# SQL: Creating relationships between tables with `PRIMARY` and `FOREIGN` keys
|
||||||
|
|
||||||
|
We will demonstrate with an example. We already have the `sales` table. We want to create new table called `returns` that will sustain a one-to-one relationship with `sales`. We are going to use the `sale_id` as our foreign key in `returns`. This is the primary key in `sales`.
|
||||||
|
|
||||||
|
The `sales` table:
|
||||||
|
|
||||||
|
````
|
||||||
|
sale_id model_id sale_date employee_id price
|
||||||
|
---------- ---------- ---------- ----------- ----------
|
||||||
|
1 44 2020-07-27 tbishop 399.99
|
||||||
|
2 22 2021-02-07 tbishop 200.99
|
||||||
|
````
|
||||||
|
|
||||||
|
Creating the `returns` table and establishing relationship with `sales` using the `FOREIGN KEY` keyword:
|
||||||
|
|
||||||
|
````sql
|
||||||
|
CREATE TABLE returns (
|
||||||
|
return_id integer PRIMARY KEY,
|
||||||
|
sale_id integer NOT NULL,
|
||||||
|
date_returned text,
|
||||||
|
reason text,
|
||||||
|
FOREIGN KEY (sale_id) REFERENCES sales(sale_id)
|
||||||
|
);
|
||||||
|
````
|
||||||
|
|
||||||
|
Here's an example with more than one foreign key:
|
||||||
|
|
||||||
|
````sql
|
||||||
|
CREATE TABLE returns (
|
||||||
|
return_id integer PRIMARY KEY,
|
||||||
|
sale_id integer NOT NULL,
|
||||||
|
employee_id text NOT NULL,
|
||||||
|
date_returned text,
|
||||||
|
reason text,
|
||||||
|
FOREIGN KEY(sale_id) REFERENCES sales(sale_id),
|
||||||
|
FOREIGN KEY(employee_id) REFERENCES employee(employee_id)
|
||||||
|
);
|
||||||
|
````
|
Loading…
Add table
Reference in a new issue