From 8503610c2c46a275fbc991893cec89a2d5a3f82f Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 19 Oct 2023 04:27:10 +0000 Subject: [PATCH] site deploy Auto-generated via {sandpaper} Source : c47e1ecf8f9fdec1822e604c4624ff750311e5f4 Branch : md-outputs Author : GitHub Actions Time : 2023-10-19 04:26:50 +0000 Message : markdown source builds Auto-generated via {sandpaper} Source : c33bc663157fb5c0bfb07e211c00772c8e0dc692 Branch : main Author : James Foster <38274066+jd-foster@users.noreply.github.com> Time : 2023-10-19 04:25:40 +0000 Message : Move cheat sheet for updated workbench (#368) --- 00-sql-introduction.html | 6 +- 01-sql-basic-queries.html | 14 +- 02-sql-aggregation.html | 14 +- 03-sql-joins.html | 26 +- 404.html | 6 +- CODE_OF_CONDUCT.html | 6 +- CONTRIBUTORS.html | 6 +- LICENSE.html | 6 +- NEWS.html | 6 +- aio.html | 42 +-- discuss.html | 6 +- images.html | 6 +- index.html | 8 +- instructor-notes.html | 6 +- instructor/00-sql-introduction.html | 2 +- instructor/01-sql-basic-queries.html | 10 +- instructor/02-sql-aggregation.html | 10 +- instructor/03-sql-joins.html | 22 +- instructor/404.html | 6 +- instructor/CODE_OF_CONDUCT.html | 2 +- instructor/CONTRIBUTORS.html | 2 +- instructor/LICENSE.html | 2 +- instructor/NEWS.html | 2 +- instructor/aio.html | 40 +-- instructor/discuss.html | 2 +- instructor/images.html | 4 +- instructor/index.html | 4 +- instructor/instructor-notes.html | 4 +- instructor/key-points.html | 4 +- instructor/profiles.html | 4 +- instructor/reference.html | 10 +- instructor/sql-cheat-sheet.html | 476 +++++++++++++++++++++++++++ key-points.html | 6 +- md5sum.txt | 3 +- pkgdown.yml | 2 +- profiles.html | 8 +- reference.html | 14 +- sitemap.xml | 6 + sql-cheat-sheet.html | 475 ++++++++++++++++++++++++++ 39 files changed, 1124 insertions(+), 154 deletions(-) create mode 100644 instructor/sql-cheat-sheet.html create mode 100644 sql-cheat-sheet.html diff --git a/00-sql-introduction.html b/00-sql-introduction.html index b5d8f958..96a6e665 100644 --- a/00-sql-introduction.html +++ b/00-sql-introduction.html @@ -65,7 +65,7 @@ -
@@ -206,7 +206,7 @@

  • Learner Profiles
  • -
  • Discussion
  • Reference
  • +
  • Discussion
  • Reference
  • SQL Cheat Sheet
  • @@ -767,7 +767,7 @@

    Keypoints +
    + Data Management with SQL for Ecologists +
    + +
    +
    + + + + + +
    +
    +

    SQL Cheat Sheet

    +

    Last updated on 2023-10-19 | + + Edit this page

    + + + + + +
    + +
    + + + +

    Basic Queries +

    +

    Select one or more columns of data from a table:

    +
    +

    SH +

    +
    SELECT column_name_1, column_name_2 FROM table_name;
    +
    +

    Select all of the columns in a table:

    +
    +

    SH +

    +
    SELECT * FROM table_name;
    +
    +

    Get only unique lines in a query:

    +
    +

    SH +

    +
    SELECT DISTINCT column_name FROM table_name;
    +
    +

    Perform calculations in a query:

    +
    +

    SH +

    +
    SELECT column_name_1, ROUND(column_name_2 / 1000.0) FROM table_name;
    +
    +

    Filtering +

    +

    Select only the data meeting certain criteria:

    +
    +

    SH +

    +
    SELECT * FROM table_name WHERE column_name = 'Hello World';
    +
    +

    Combine conditions:

    +
    +

    SH +

    +
    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:

    +
    +

    SH +

    +
    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:

    +
    +

    SH +

    +
    SELECT * FROM table_name WHERE column_name IS NULL;
    +
    +

    Test whether a value is not null:

    +
    +

    SH +

    +
    SELECT * FROM table_name WHERE column_name IS NOT NULL;
    +
    +

    Grouping and Aggregation +

    +

    Combine data into groups and calculate combined values in groups:

    +
    +

    SH +

    +
    SELECT column_name_1, SUM(column_name_2), COUNT(*) FROM table_name GROUP BY column_name_1;
    +
    +

    Joins +

    +

    Join data from two tables:

    +
    +

    SH +

    +
    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.

    +
    +

    SH +

    +
    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):

    +
    +

    SH +

    +
    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:

    +
    +

    SH +

    +
    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()
    +
    +
    +
    + + +
    +
    + + + diff --git a/key-points.html b/key-points.html index aff27a4e..0cbbce22 100644 --- a/key-points.html +++ b/key-points.html @@ -88,6 +88,7 @@ @@ -246,6 +247,7 @@

  • Discussion
  • Reference
  • +
  • SQL Cheat Sheet
  • @@ -374,8 +376,8 @@

    "url": "https://datacarpentry.github.io/sql-ecology-lesson/key-points.html", "identifier": "https://datacarpentry.github.io/sql-ecology-lesson/key-points.html", "dateCreated": "2015-02-10", - "dateModified": "2023-10-17", - "datePublished": "2023-10-17" + "dateModified": "2023-10-19", + "datePublished": "2023-10-19" } +
    + Data Management with SQL for Ecologists +
    + +
    +
    + + + + + +
    +
    +

    SQL Cheat Sheet

    +

    Last updated on 2023-10-19 | + + Edit this page

    + + + +
    + +
    + + + +

    Basic Queries +

    +

    Select one or more columns of data from a table:

    +
    +

    SH +

    +
    SELECT column_name_1, column_name_2 FROM table_name;
    +
    +

    Select all of the columns in a table:

    +
    +

    SH +

    +
    SELECT * FROM table_name;
    +
    +

    Get only unique lines in a query:

    +
    +

    SH +

    +
    SELECT DISTINCT column_name FROM table_name;
    +
    +

    Perform calculations in a query:

    +
    +

    SH +

    +
    SELECT column_name_1, ROUND(column_name_2 / 1000.0) FROM table_name;
    +
    +

    Filtering +

    +

    Select only the data meeting certain criteria:

    +
    +

    SH +

    +
    SELECT * FROM table_name WHERE column_name = 'Hello World';
    +
    +

    Combine conditions:

    +
    +

    SH +

    +
    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:

    +
    +

    SH +

    +
    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:

    +
    +

    SH +

    +
    SELECT * FROM table_name WHERE column_name IS NULL;
    +
    +

    Test whether a value is not null:

    +
    +

    SH +

    +
    SELECT * FROM table_name WHERE column_name IS NOT NULL;
    +
    +

    Grouping and Aggregation +

    +

    Combine data into groups and calculate combined values in groups:

    +
    +

    SH +

    +
    SELECT column_name_1, SUM(column_name_2), COUNT(*) FROM table_name GROUP BY column_name_1;
    +
    +

    Joins +

    +

    Join data from two tables:

    +
    +

    SH +

    +
    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.

    +
    +

    SH +

    +
    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):

    +
    +

    SH +

    +
    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:

    +
    +

    SH +

    +
    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()
    +
    +
    +
    + + +
    +
    + + +