diff --git a/SUMMARY.md b/SUMMARY.md index 19567084..c5bd509d 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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) diff --git a/courses/foundation/databases/pre-read/intro-sql.md b/courses/foundation/databases/pre-read/intro-sql.md index 1b1aa15e..9cad2137 100644 --- a/courses/foundation/databases/pre-read/intro-sql.md +++ b/courses/foundation/databases/pre-read/intro-sql.md @@ -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)._ + ## CRUD: Create, Update, Delete - Data Manipulation queries ### INSERT INTO diff --git a/courses/foundation/databases/pre-read/optional--sql.md b/courses/foundation/databases/pre-read/optional--sql.md index 9a164034..9a0eaac4 100644 --- a/courses/foundation/databases/pre-read/optional--sql.md +++ b/courses/foundation/databases/pre-read/optional--sql.md @@ -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 diff --git a/courses/foundation/databases/week1/preparation.md b/courses/foundation/databases/week1/preparation.md index 3a1c19b8..8ccd5ccf 100644 --- a/courses/foundation/databases/week1/preparation.md +++ b/courses/foundation/databases/week1/preparation.md @@ -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/) diff --git a/courses/foundation/databases/week1/assets/database-diagram.jpg b/courses/foundation/databases/week1/session-materials/database-diagram.jpg similarity index 100% rename from courses/foundation/databases/week1/assets/database-diagram.jpg rename to courses/foundation/databases/week1/session-materials/database-diagram.jpg diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/choose-sqlite.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/choose-sqlite.png new file mode 100644 index 00000000..6d0e6d1d Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/choose-sqlite.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/create-new-connection.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/create-new-connection.png new file mode 100644 index 00000000..89f18e84 Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/create-new-connection.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/create-save-path.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/create-save-path.png new file mode 100644 index 00000000..ce46ebd0 Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/create-save-path.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/database-view.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/database-view.png new file mode 100644 index 00000000..ffaa2e33 Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/database-view.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/errors.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/errors.png new file mode 100644 index 00000000..9ec3d07a Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/errors.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/new-sql-script.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/new-sql-script.png new file mode 100644 index 00000000..9dff3a42 Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/new-sql-script.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/run-queries.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/run-queries.png new file mode 100644 index 00000000..0418e0a8 Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/run-queries.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/save-queries.png b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/save-queries.png new file mode 100644 index 00000000..ac254325 Binary files /dev/null and b/courses/foundation/databases/week1/session-materials/dbeaver-screenshots/save-queries.png differ diff --git a/courses/foundation/databases/week1/session-materials/dbeaver-ui.md b/courses/foundation/databases/week1/session-materials/dbeaver-ui.md new file mode 100644 index 00000000..4f969e9f --- /dev/null +++ b/courses/foundation/databases/week1/session-materials/dbeaver-ui.md @@ -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) diff --git a/courses/foundation/databases/week1/assets/session-data.sql b/courses/foundation/databases/week1/session-materials/session-data.sql similarity index 100% rename from courses/foundation/databases/week1/assets/session-data.sql rename to courses/foundation/databases/week1/session-materials/session-data.sql diff --git a/courses/foundation/databases/week1/assets/tasks.sqlite3 b/courses/foundation/databases/week1/session-materials/tasks.sqlite3 similarity index 100% rename from courses/foundation/databases/week1/assets/tasks.sqlite3 rename to courses/foundation/databases/week1/session-materials/tasks.sqlite3 diff --git a/courses/foundation/databases/week1/session-plan.md b/courses/foundation/databases/week1/session-plan.md index ace1d9c1..1854a276 100644 --- a/courses/foundation/databases/week1/session-plan.md +++ b/courses/foundation/databases/week1/session-plan.md @@ -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 @@ -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 @@ -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 @@ -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. @@ -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!