Fixing data type error in SQL creation
[swc-sql.git] / sql / novice / reference.md
1 ---
2 layout: lesson
3 root: ../..
4 title: SQL Reference
5 level: novice
6 ---
7 Basic Queries
8 -------------
9
10 Select one or more columns from a table:
11
12     SELECT column_name_1, column_name_2 FROM table_name;
13
14 Select all columns from a table:
15
16     SELECT * FROM table_name;
17
18 Get only unique results in a query:
19
20     SELECT DISTINCT column_name FROM table_name;
21
22 Perform calculations in a query:
23
24     SELECT column_name_1, ROUND(column_name_2 / 1000.0) FROM table_name;
25
26 Sort results in ascending order:
27
28     SELECT * FROM table_name ORDER BY column_name_1;
29
30 Sort results in ascending and descending order:
31
32     SELECT * FROM table_name ORDER BY column_name_1 ASC, column_name_2 DESC;
33
34 Filtering
35 ---------
36
37 Select only data meeting a condition:
38
39     SELECT * FROM table_name WHERE column_name > 0;
40
41 Select only data meeting a combination of conditions:
42
43     SELECT * FROM table_name WHERE (column_name_1 >= 1000) AND (column_name_2 = 'A' OR column_name_2 = 'B');
44
45 Missing Data
46 ------------
47
48 Use `NULL` to represent missing data.
49
50 `NULL` is not 0, false, or any other specific value.
51 Operations involving `NULL` produce `NULL`, so `1+NULL`, `2>NULL`, and `3=NULL` are all `NULL`.
52
53 Test whether a value is null:
54
55     SELECT * FROM table_name WHERE column_name IS NULL;
56
57 Test whether a value is not null:
58
59     SELECT * FROM table_name WHERE column_name IS NOT NULL;
60
61 Grouping and Aggregation
62 ------------------------
63
64 Combine values using aggregation functions:
65
66     SELECT SUM(column_name_1) FROM table_name;
67
68 Combine data into groups and calculate combined values in groups:
69
70     SELECT column_name_1, SUM(column_name_2), COUNT(*) FROM table_name GROUP BY column_name_1;
71
72 Joins
73 -----
74
75 Join data from two tables:
76
77     SELECT * FROM table_name_1 JOIN table_name_2 ON table_name_1.column_name = table_name_2.column_name;
78
79 Writing Queries
80 ---------------
81
82 SQL commands must be combined in the following order:
83 `SELECT`, `FROM`, `JOIN`, `ON`, `WHERE`, `GROUP BY`, `ORDER BY`.
84
85 Creating Tables
86 ---------------
87
88 Create tables by specifying column names and types.
89 Include primary and foreign key relationships and other constraints.
90
91     CREATE TABLE survey(
92         taken   INTEGER NOT NULL,
93         person  TEXT,
94         quant   REAL NOT NULL,
95         PRIMARY KEY(taken, quant),
96         FOREIGN KEY(person) REFERENCES person(ident)
97     );
98
99 Programming
100 -----------
101
102 Execute queries in a general-purpose programming language by:
103
104 *   loading the appropriate library
105 *   creating a connection
106 *   creating a cursor
107 *   repeatedly:
108     *   execute a query
109     *   fetch some or all results
110 *   disposing of the cursor
111 *   closing the connection
112
113 Python example:
114
115     import sqlite3
116     connection = sqlite3.connect("database_name")
117     cursor = connection.cursor()
118     cursor.execute("...query...")
119     for r in cursor.fetchall():
120         ...process result r...
121     cursor.close()
122     connection.close()