diff --git a/Databases/SQL/2_SELECT.md b/Databases/SQL/2_SELECT.md new file mode 100644 index 0000000..9311fa6 --- /dev/null +++ b/Databases/SQL/2_SELECT.md @@ -0,0 +1,82 @@ +--- +tags: + - Programming_Languages + - Databases + - sql +--- + +# SQL: The SELECT query + +## Print/retrieve/write an entire table, unfiltered + +````sql +SELECT * FROM [table_name] + +SELECT * FROM model +```` + +## Retrieve all data from a specific field + +````sql +SELECT [field_name] FROM [table_name] + +SELECT name FROM manufacturer +```` + +## Retrieve data and order it + +This example orders alphabetically: + +````sql +SELECT [field_name] FROM [table_name] ORDER BY [property] +SELECT name FROM model ORDER BY name +```` + > When `ORDER BY` is used the default method for strings is alphabetical and for integers it is ascending order. + +Here's a more complex real-life request: + +````sql +SELECT name, cores, ram FROM model ORDER BY ram, name +```` + +It gives us: + +```` +name cores ram +---------------- ---------- ---------- +Commodore VIC-20 1 0.005 +Commodore 64 1 0.064 +Amiga 500 1 0.5 +Apple Lisa 1 1.0 +Raspberry Pi 1 M 1 256.0 +Raspberry Pi 1 M 1 256.0 +Raspberry Pi 1 M 1 512.0 +Raspberry Pi 1 M 1 512.0 +Raspberry Pi 1 M 1 512.0 +Raspberry Pi Zer 1 512.0 +```` + +But we can obviously specify our own ordering method: + +````sql +SELECT name, cores, release_date FROM model ORDER BY cores DESC, name; +```` + +Returns: + +```` +name cores release_date +----------------- ---------- ------------ +Apple MacBook Pro 6 2019-05-21 +Apple iMac 4 2019-03-19 +Raspberry Pi 2 Mo 4 2015-02-01 +Raspberry Pi 3 Mo 4 2018-11-01 +Raspberry Pi 3 Mo 4 2016-02-01 +Raspberry Pi 3 Mo 4 2018-03-14 +Raspberry Pi 4 Mo 4 2019-06-24 +Raspberry Pi 4 Mo 4 2019-06-24 +Raspberry Pi 4 Mo 4 2019-06-24 +```` + + > + > `ORDER BY` always comes last, after the selection and any filtering clauses but *before* a `WHERE` clause diff --git a/Databases/SQL/3_INSERT.md b/Databases/SQL/3_INSERT.md new file mode 100644 index 0000000..e69de29 diff --git a/Databases/SQL/4_WHERE.md b/Databases/SQL/4_WHERE.md new file mode 100644 index 0000000..e69de29 diff --git a/Databases/SQL/5_UPDATE.md b/Databases/SQL/5_UPDATE.md new file mode 100644 index 0000000..4c8a1b0 --- /dev/null +++ b/Databases/SQL/5_UPDATE.md @@ -0,0 +1,35 @@ +--- +tags: + - Programming_Languages + - Databases + - sql +--- + +# SQL: The UPDATE clause + +With UPDATE we modify existing records. + +## Schematic syntax + +````sql +UPDATE [table_name] +SET [field] +WHERE [conditional expression/filter] +```` + +## Real example + +````sql +UPDATE manufacturer +SET url = '' +WHERE manufacturer_id = 4; --typically this will be the primary key as you are updating and existing record and need to identify it uniquely +```` + +## Multiple fields + +````sql +UPDATE manufacturer +SET url = '', + year_founded = 1977 +WHERE manufacturer_id = 2; +```` \ No newline at end of file diff --git a/Databases/SQL/6_DELETE.md b/Databases/SQL/6_DELETE.md new file mode 100644 index 0000000..65c8e47 --- /dev/null +++ b/Databases/SQL/6_DELETE.md @@ -0,0 +1,12 @@ +--- +tags: + - Programming_Languages + - Databases + - sql +--- + +# SQL: The DELETE query + +````sql +DELETE FROM sales WHERE sale_id = 1; +```` \ No newline at end of file diff --git a/Databases/SQL/7_ALTER.md b/Databases/SQL/7_ALTER.md new file mode 100644 index 0000000..e69de29