Skip to content

Commit

Permalink
markdown source builds
Browse files Browse the repository at this point in the history
Auto-generated via {sandpaper}
Source  : c33bc66
Branch  : main
Author  : James Foster <[email protected]>
Time    : 2023-10-19 04:25:40 +0000
Message : Move cheat sheet for updated workbench (#368)
  • Loading branch information
actions-user committed Oct 19, 2023
1 parent 5dda556 commit c47e1ec
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
3 changes: 2 additions & 1 deletion md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
134 changes: 134 additions & 0 deletions sql-cheat-sheet.md
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c47e1ec

Please sign in to comment.