From 75258ff6647bb78eda0266ed3f779082a0dc6942 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 19 Oct 2023 00:49:22 +0000 Subject: [PATCH] differences for PR #368 --- md5sum.txt | 3 +- reference.md | 2 +- sql-cheat-sheet.md | 134 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 sql-cheat-sheet.md diff --git a/md5sum.txt b/md5sum.txt index 5ffefb47..c6f539dc 100644 --- a/md5sum.txt +++ b/md5sum.txt @@ -11,6 +11,7 @@ "episodes/03-sql-joins.md" "4c87f9451d13eaa88c1b38fe29c7a7f3" "site/built/03-sql-joins.md" "2023-06-30" "instructors/instructor-notes.md" "105728ef72c768e968ca9d11c2a14109" "site/built/instructor-notes.md" "2023-04-21" "learners/discuss.md" "c5994f9f17e9da0d08a530600084ba55" "site/built/discuss.md" "2023-04-21" -"learners/reference.md" "a9e2e8b2be027afbeb360f1bbd966da2" "site/built/reference.md" "2023-06-09" +"learners/reference.md" "83dc790413398fce3bb07518546928fa" "site/built/reference.md" "2023-10-19" "learners/setup.md" "ba0ada65a673ca1d95ed722b0711fa87" "site/built/setup.md" "2023-06-30" +"learners/sql-cheat-sheet.md" "fe94973871efccd88f8bc193d359bdb1" "site/built/sql-cheat-sheet.md" "2023-10-19" "profiles/learner-profiles.md" "60b93493cf1da06dfd63255d73854461" "site/built/learner-profiles.md" "2023-04-21" diff --git a/reference.md b/reference.md index 7b5b780c..c033bb24 100644 --- a/reference.md +++ b/reference.md @@ -96,7 +96,7 @@ Glosario](https://glosario.carpentries.org/) (CC-BY-4.0)* ## Commands -See [this cheat sheet](files/sql-cheat-sheet.md) for an list of the commands +See [this cheat sheet](sql-cheat-sheet.md) for an list of the commands covered in this lesson. ### Keywords diff --git a/sql-cheat-sheet.md b/sql-cheat-sheet.md new file mode 100644 index 00000000..91f48fa8 --- /dev/null +++ b/sql-cheat-sheet.md @@ -0,0 +1,134 @@ +--- +title: SQL Cheat Sheet +--- + +Basic Queries +------------- + +Select one or more columns of data from a table: + + SELECT column_name_1, column_name_2 FROM table_name; + +Select all of the columns in a table: + + SELECT * FROM table_name; + +Get only unique lines in a query: + + SELECT DISTINCT column_name FROM table_name; + +Perform calculations in a query: + + SELECT column_name_1, ROUND(column_name_2 / 1000.0) FROM table_name; + + +Filtering +--------- + +Select only the data meeting certain criteria: + + SELECT * FROM table_name WHERE column_name = 'Hello World'; + +Combine conditions: + + SELECT * FROM table_name WHERE (column_name_1 >= 1000) AND (column_name_2 = 'A' OR column_name_2 = 'B'); + + +Sorting +------- + +Sort results using `ASC` for ascending order or `DESC` for descending order: + + SELECT * FROM table_name ORDER BY column_name_1 ASC, column_name_2 DESC; + + +Missing Data +------------ + +Use `NULL` to represent missing data. + +`NULL` is neither true nor false. +Operations involving `NULL` produce `NULL`, e.g., `1+NULL`, `2>NULL`, and `3=NULL` are all `NULL`. + +Test whether a value is null: + + SELECT * FROM table_name WHERE column_name IS NULL; + +Test whether a value is not null: + + SELECT * FROM table_name WHERE column_name IS NOT NULL; + + +Grouping and Aggregation +------------------------ + +Combine data into groups and calculate combined values in groups: + + SELECT column_name_1, SUM(column_name_2), COUNT(*) FROM table_name GROUP BY column_name_1; + + +Joins +----- + +Join data from two tables: + + SELECT * FROM table_name_1 JOIN table_name_2 ON table_name_1.column_name = table_name_2.column_name; + + +Combining Commands +------------------ + +SQL commands must be combined in the following order: +`SELECT`, `FROM`, `JOIN`, `ON`, `WHERE`, `GROUP BY`, `ORDER BY`. + + +Creating Tables +--------------- + +Create tables by specifying column names and types. +Include primary and foreign key relationships and other constraints. + + CREATE TABLE survey( + taken INTEGER NOT NULL, + person TEXT, + quant REAL NOT NULL, + PRIMARY KEY(taken, quant), + FOREIGN KEY(person) REFERENCES person(ident) + ); + +Transactions +------------ + +Put multiple queries in a transaction to ensure they are ACID +(atomic, consistent, isolated, and durable): + + BEGIN TRANSACTION; + DELETE FROM table_name_1 WHERE condition; + INSERT INTO table_name_2 values(...); + END TRANSACTION; + +Programming +----------- + +Execute queries in a general-purpose programming language by: + +* loading the appropriate library +* creating a connection +* creating a cursor +* repeatedly: + * execute a query + * fetch some or all results +* disposing of the cursor +* closing the connection + +Python example: + + import sqlite3 + connection = sqlite3.connect("database_name") + cursor = connection.cursor() + cursor.execute("...query...") + for r in cursor.fetchall(): + ...process result r... + cursor.close() + connection.close() +