Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
- [Assignment](courses/foundation/intro-to-web-architecture/assignment.md)
- [Databases](courses/foundation/databases/README.md)
- [Week 1](courses/foundation/databases/week1/README.md)
- [Session Materials](courses/foundation/databases/week1/session-materials/)
- [DBeaver UI](courses/foundation/databases/week1/session-materials/dbeaver-ui.md)
- [Preparation](courses/foundation/databases/week1/preparation.md)
- [Session Plan](courses/foundation/databases/week1/session-plan.md)
- [Assignment](courses/foundation/databases/week1/assignment.md)
Expand Down
19 changes: 19 additions & 0 deletions courses/foundation/databases/pre-read/intro-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ Use cases:
**GROUP BY is needed when:**
- You want one row per group (like "count of students per course").

### JOIN

- Combines data from multiple tables.

**Example:**

```sql
SELECT students.name, enrollments.grade
FROM students
JOIN enrollments ON students.student_id = enrollments.student_id;
```

- JOIN lets you fetch combined info from several tables in one query.
- Without JOIN, `SELECT * FROM A, B` creates a giant mess (every row paired with every other row — Cartesian product).
- JOIN links only matching rows based on a condition\*.
- Good idea is to JOIN on the **foreign key**!

\*_there are different types of JOIN. [Read more different types of JOIN in this article (out of scope)](https://www.w3schools.com/sql/sql_join.asp)._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice to call this out


## CRUD: Create, Update, Delete - Data Manipulation queries

### INSERT INTO
Expand Down
66 changes: 50 additions & 16 deletions courses/foundation/databases/pre-read/optional--sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,67 @@ SELECT course_id, AVG(grade) FROM enrollments GROUP BY course_id;
- `WHERE` filters rows **before** grouping.
- `HAVING` filters groups **after** the `GROUP BY`.

```sql
-- Group and filter groups
SELECT customer_id, SUM(total) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(total) > 1000;
SELECT customer_id, SUM(total) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(total) > 1000;
```

## Aliases

## JOIN
Aliases in SQL can serve two purposes:

- Combines data from multiple tables.
### 1. Aliases on tables for easier access to values

When writing complex joins or other types of convoluted queries, especially when naming of the tables is very detailed, creating an alias on the table name can make your query writing time faster.

**Example:**
Example:

```sql
-- Filter rows first
SELECT * FROM orders WHERE total > 100;
SELECT students.name, enrollments.grade
FROM students
JOIN enrollments ON students.student_id = enrollments.student_id;
SELECT course.name, enrollments.date, enrollments.term
FROM course
JOIN enrollments
ON course.id = enrollments.course_id
WHERE enrollments.date > '2025-11-01'
```

the above query using aliases can be shortened like so:

```sql
SELECT c.name, e.date, e.term
FROM course c -- using alias on table "course": alias = "c"
JOIN enrollments e -- using alias on table "enrollments"
ON c.id = e.course_id -- using aliases to access values
WHERE e.date > '2025-11-01'
```

Two queries above are exactly the same in terms of functionality. Aliases were used only to avoid retyping long words like "enrollments" over and over.

#### 2. Aliases on values for cleaner display

You can also add aliases to selected values for cleaner display of the query output. To do that, you must use keyword `as`.

```sql
SELECT c.name as COURSE_NAME, e.date as ENROLLMENT_DATE, e.term as TERM
FROM course c -- using alias on table "course": alias = "c"
JOIN enrollments e -- using alias on table "enrollments"
ON c.id = e.course_id -- using aliases to access values
WHERE e.date > '2025-11-01'
```

- JOIN lets you fetch combined info from several tables in one query.
the output of your query will be provided with column names in the output table like so:

### Compare JOIN WHERE vs Cartesian Product
```sql
COURSE_NAME | ENROLLMENT_DATE | TERM
```

instead of:

- Without JOIN, `SELECT * FROM A, B` creates a giant mess (every row paired with every other row — Cartesian product).
- JOIN links only matching rows based on a condition.
```sql
c.name | e.date | e.term
```

## Data Definition and Manipulation

Expand Down
1 change: 1 addition & 0 deletions courses/foundation/databases/week1/preparation.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ We're going to use Dbeaver, a free and open-source database management tool that
- [DBeaver Playlist](https://www.youtube.com/playlist?list=PLkh7-EMxQiV2DAiruEWgh-i4jreuyX1rP)
- [SQLite Tutorial](https://www.sqlitetutorial.net/)
- [w3schools SQL Tutorial](https://www.w3schools.com/sql/)
- [SQL Tutorial](https://www.sqltutorial.org/)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Dbeaver UI details

## Create a New SQLite Connection

1. Click the **"New Database Connection"** button (plug icon) in the toolbar or `Ctrl+Shift+N`
![Create new connection](dbeaver-screenshots/create-new-connection.png)
2. Select **SQLite** from the list of databases
![Choose sqlite](dbeaver-screenshots/choose-sqlite.png)
3. Click **Next**
4. In the **Path** field, click **Browse** and choose where to save your database file
![Create save path](dbeaver-screenshots/create-save-path.png)
5. Name your database file: `tasks.sqlite3` **EXTENTION IS IMPORTANT**
6. Click **Test Connection** to verify everything works
7. Click **Finish**

## Browse through database

![View your database](dbeaver-screenshots/database-view.png)

## Create and run queries

Open a new SQL script to write your queries in.
![Open new script](dbeaver-screenshots/new-sql-script.png)

When running queries it is important to remember few things:
![Run your queries](dbeaver-screenshots/run-queries.png)

1. Orange triangle will only **run the selected query**.
2. Little document with triangle will **run the entire script file**.
3. **Query is selected** when it is **manually selected** or **the cursor is standing on the query**.
4. **Always remember to end queries with semi-colon**: `;`! Otherwise the edit does not know when query ends!

## Errors will be marked

In case of errors, DBMS will mark them red. If you hover on the error, **DBMS will tell you the exact issue with your query**!
![Errors](dbeaver-screenshots/errors.png)

## Saving queries for homework

When done with queries, save them to your homework repo using this "Save As" like from any other software. Remember the `.sql` extention!
![Save as for homework](dbeaver-screenshots/save-queries.png)
29 changes: 25 additions & 4 deletions courses/foundation/databases/week1/session-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## Session Materials

- [Database Diagram](assets/database-diagram.jpg) - The diagram of the database to be created in this session
- [Sample Tasks database](assets/tasks.sqlite3) - Pre-created SQLite database with the contents of [this SQL file](assets/session-data.sql)
- [Database Diagram](session-materials/database-diagram.jpg) - The diagram of the database to be created in this session
- [Sample Tasks database](session-materials/tasks.sqlite3) - Pre-created SQLite database with the contents of [this SQL file](session-materials/session-data.sql)

## Session Outline

Expand Down Expand Up @@ -132,6 +132,8 @@ The next step is creating a database to have a playground for learning SQL. Also
6. Click **Test Connection** to verify everything works
7. Click **Finish**

[Guide with screenshot and additional DBeaver information](./session-materials/dbeaver-ui.md).

You can find more information in the [DBeaver documentation for Sqlite](https://dbeaver.com/docs/dbeaver/Database-driver-SQLite/).

### Step 2: Understanding Database Structure
Expand All @@ -158,7 +160,7 @@ So we just created our database. Before we create tables, let's understand the k

After executing the SQL file, you should see a database like the diagram this one.

![Database Diagram](assets/database-diagram.jpg)
![Database Diagram](session-materials/database-diagram.jpg)

#### Exercise 1

Expand Down Expand Up @@ -297,6 +299,25 @@ Now that your database is set up, you're ready to practice SQL queries. Below ar
> [!NOTE] Interested in more options?
> Refer back to [intro to sql cheatsheet](../pre-read/intro-sql.md) and practice some more complex queries!

### JOIN

JOIN allows to combine data from multiple tables. To join tables, you can use **foreign keys**!

**Example:**

```sql
-- Find task and it's status name
SELECT task.title, status.name
FROM task
JOIN status
ON task.status_id = status.id;
```

#### Exercise 3

1. Select all tasks which have status `In progress`.
2. Display list of tasks with the user names of the users attached to the tasks.

### INSERT - Adding Data

The INSERT statement is used to add new records to a table.
Expand Down Expand Up @@ -402,7 +423,7 @@ Now, let's practice these operations together:
SELECT * FROM task WHERE title = 'Prepare presentation';
```

#### Exercise 3
#### Exercise 4

Combine all the queries you learned!

Expand Down