Initial commit

Mira Kovac 1 year ago
commit 34c204544898eb230538e13f56da1c0a79bb3135
5 changed files with 104 additions and 0 deletions
+2-0.gitignore ADDED
@@ -0,0 +1,2 @@
1+*.db
2+*.sqlite
+21-0LICENSE ADDED
@@ -0,0 +1,21 @@
1+MIT License
2+
3+Copyright (c) 2025 glibCode
4+
5+Permission is hereby granted, free of charge, to any person obtaining a copy
6+of this software and associated documentation files (the "Software"), to deal
7+in the Software without restriction, including without limitation the rights
8+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+copies of the Software, and to permit persons to whom the Software is
10+furnished to do so, subject to the following conditions:
11+
12+The above copyright notice and this permission notice shall be included in all
13+copies or substantial portions of the Software.
14+
15+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+SOFTWARE.
+33-0README.md ADDED
@@ -0,0 +1,33 @@
1+# SQL Essentials
2+
3+Example code and exercises for the glibCode course **[SQL Essentials](https://glibcode.net/courses/sql-essentials/)** (Beginner, about 7 hours).
4+
5+Ask questions of real data: SELECT, JOIN, GROUP BY and window functions on a store's orders database.
6+
7+## Lessons
8+
9+| # | Lesson | Code |
10+| --- | --- | --- |
11+| 1 | Tables, rows and SELECT | [`lessons/01-tables-rows-and-select`](lessons/01-tables-rows-and-select/) |
12+| 2 | Filtering with WHERE | [`lessons/02-filtering-with-where`](lessons/02-filtering-with-where/) |
13+| 3 | Sorting and LIMIT | [`lessons/03-sorting-and-limit`](lessons/03-sorting-and-limit/) |
14+| 4 | JOINs explained with Venn diagrams (sort of) | [`lessons/04-joins-explained-with-venn-diagrams-sort`](lessons/04-joins-explained-with-venn-diagrams-sort/) |
15+| 5 | GROUP BY and aggregates | [`lessons/05-group-by-and-aggregates`](lessons/05-group-by-and-aggregates/) |
16+| 6 | Subqueries | [`lessons/06-subqueries`](lessons/06-subqueries/) |
17+| 7 | Window functions | [`lessons/07-window-functions`](lessons/07-window-functions/) |
18+| 8 | Indexes and why queries get slow | [`lessons/08-indexes-and-why-queries-get-slow`](lessons/08-indexes-and-why-queries-get-slow/) |
19+
20+## Getting started
21+
22+```sh
23+git clone https://git.glibcode.net/glibcode/sql-essentials.git
24+cd sql-essentials
25+```
26+
27+SQLite 3. Create the database once with `setup/schema.sql` and `setup/seed.sql`.
28+
29+Questions? Ask in the course forum or write to help@glibcode.net.
30+
31+## License
32+
33+The code is MIT licensed (see `LICENSE`). The lesson texts are © glibCode.
+29-0setup/schema.sql ADDED
@@ -0,0 +1,29 @@
1+-- The shop database every SQL lesson uses (SQLite)
2+CREATE TABLE customers (
3+ id INTEGER PRIMARY KEY,
4+ name TEXT NOT NULL,
5+ email TEXT UNIQUE,
6+ city TEXT,
7+ signed_up DATE
8+);
9+
10+CREATE TABLE products (
11+ id INTEGER PRIMARY KEY,
12+ name TEXT NOT NULL,
13+ category TEXT,
14+ price REAL
15+);
16+
17+CREATE TABLE orders (
18+ id INTEGER PRIMARY KEY,
19+ customer_id INTEGER REFERENCES customers (id),
20+ ordered_at DATETIME,
21+ total REAL
22+);
23+
24+CREATE TABLE employees (
25+ id INTEGER PRIMARY KEY,
26+ name TEXT,
27+ department TEXT,
28+ salary INTEGER
29+);
+19-0setup/seed.sql ADDED
@@ -0,0 +1,19 @@
1+INSERT INTO customers (name, email, city, signed_up) VALUES
2+ ('Ana Silva', 'ana@example.com', 'Lisbon', '2025-01-12'),
3+ ('Tom Berg', 'tom@example.com', 'Oslo', '2025-02-03'),
4+ ('Ines Lopes', 'ines@example.com', 'Lisbon', '2025-03-21'),
5+ ('Raj Mehta', 'raj@example.com', 'Pune', '2025-04-02');
6+
7+INSERT INTO products (name, category, price) VALUES
8+ ('SQL pocket guide', 'books', 14.90),
9+ ('Board game night', 'games', 39.00),
10+ ('Mechanical keyboard', 'hardware', 119.00),
11+ ('Desk lamp', 'home', 24.50);
12+
13+INSERT INTO orders (customer_id, ordered_at, total) VALUES
14+ (1, '2025-05-01 10:12', 54.80), (1, '2025-06-14 18:40', 119.00),
15+ (2, '2025-06-15 09:05', 39.00), (4, '2025-07-02 21:17', 143.50);
16+
17+INSERT INTO employees (name, department, salary) VALUES
18+ ('Mia', 'support', 41000), ('Leo', 'support', 43500),
19+ ('Zoe', 'engineering', 72000), ('Kai', 'engineering', 69000);