From 5120f113391ffecb30eba41646b5bd3d61ea2377 Mon Sep 17 00:00:00 2001 From: mrsh Date: Thu, 22 Jan 2026 06:16:02 +0100 Subject: [PATCH] end of day 3 week 1 --- week 1/day 2/exo1.py | 2 + week 1/day 2/exo5.md | 6 ++ week 1/day3/exo1.sql | 2 + week 1/day3/exo2.sql | 3 + week 1/day3/exo3.sql | 10 +++ week 1/day3/exo4.sql | 3 + week 1/day3/exo5.md | 17 ++++ week 1/day3/init_db.sql | 14 ++++ week 1/day3/readme.md | 168 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 225 insertions(+) create mode 100644 week 1/day3/exo1.sql create mode 100644 week 1/day3/exo2.sql create mode 100644 week 1/day3/exo3.sql create mode 100644 week 1/day3/exo4.sql create mode 100644 week 1/day3/exo5.md create mode 100644 week 1/day3/init_db.sql create mode 100644 week 1/day3/readme.md diff --git a/week 1/day 2/exo1.py b/week 1/day 2/exo1.py index adf7086..99a250b 100644 --- a/week 1/day 2/exo1.py +++ b/week 1/day 2/exo1.py @@ -1,3 +1,5 @@ +# Exo 1 + number = input("please enter an number :") if int(number) % 2: diff --git a/week 1/day 2/exo5.md b/week 1/day 2/exo5.md index e69de29..d835901 100644 --- a/week 1/day 2/exo5.md +++ b/week 1/day 2/exo5.md @@ -0,0 +1,6 @@ +if the user name double in size +the time of execution will double + +so the time of execution will be x 2 + +T(m, n) = m x n diff --git a/week 1/day3/exo1.sql b/week 1/day3/exo1.sql new file mode 100644 index 0000000..bff28a9 --- /dev/null +++ b/week 1/day3/exo1.sql @@ -0,0 +1,2 @@ +-- exo 1 +SELECT * FROM users WHERE length(username) > 5 \ No newline at end of file diff --git a/week 1/day3/exo2.sql b/week 1/day3/exo2.sql new file mode 100644 index 0000000..7890e25 --- /dev/null +++ b/week 1/day3/exo2.sql @@ -0,0 +1,3 @@ +-- exo 2 + +SELECT username FROM users WHERE username ~ '^[A-Za-z]+$'; \ No newline at end of file diff --git a/week 1/day3/exo3.sql b/week 1/day3/exo3.sql new file mode 100644 index 0000000..6f1dc85 --- /dev/null +++ b/week 1/day3/exo3.sql @@ -0,0 +1,10 @@ +-- exo 3 + +SELECT username, + CASE + WHEN length(username) <= 20 + AND username ~ '^[A-Za-z]+$' + THEN 'Accepted' + ELSE 'Refused' + END AS status +FROM users; diff --git a/week 1/day3/exo4.sql b/week 1/day3/exo4.sql new file mode 100644 index 0000000..dda4e29 --- /dev/null +++ b/week 1/day3/exo4.sql @@ -0,0 +1,3 @@ +SELECT Count(*) FROM users +WHERE length(username) <= 20 +AND username ~ '^[A-Za-z]+$'; \ No newline at end of file diff --git a/week 1/day3/exo5.md b/week 1/day3/exo5.md new file mode 100644 index 0000000..ec3f280 --- /dev/null +++ b/week 1/day3/exo5.md @@ -0,0 +1,17 @@ +Answer in plain text: + +If the users table doubles in size, what happens to execution time? + +Write the runtime formula using: + +n = number of rows +m = average username length + +if the number of user double a size +the it mutiple by two the number of execution + +$$ +T(2n,m) = 2 \times n \times m +$$ + +Growth is linear \ No newline at end of file diff --git a/week 1/day3/init_db.sql b/week 1/day3/init_db.sql new file mode 100644 index 0000000..fdef53c --- /dev/null +++ b/week 1/day3/init_db.sql @@ -0,0 +1,14 @@ +CREATE TABLE users( + id SERIAL PRIMARY KEY, + username TEXT +); + +INSERT INTO users (username) VALUES +('admin'), +('root1'), +('John_Doe'), +('Alice'), +('Bob42'), +('charlie'), +('eve99'), +('Mallory'); diff --git a/week 1/day3/readme.md b/week 1/day3/readme.md new file mode 100644 index 0000000..1d69a6d --- /dev/null +++ b/week 1/day3/readme.md @@ -0,0 +1,168 @@ +# πŸ“˜ Week 1 Β· Day 3 β€” SQL Exercises + +This document describes **all exercises for Week 1 Day 3**, focused on **SQL conditionals, filtering, and algorithmic thinking** using **PostgreSQL**. + +The goal of today’s exercises is to translate the control-flow logic you used in **C (Day 1)** and **Python (Day 2)** into **declarative SQL queries**, while keeping the same performance and security mindset. + +--- + +## 🎯 Learning Objectives + +By completing today’s exercises, you practice: + +* Replacing loops with SQL filtering (`WHERE`) +* Using conditions to select and classify data +* Understanding how databases execute algorithms internally +* Reasoning about runtime complexity in a data-driven context +* Applying validation logic at the database layer (security best practice) + +--- + +## πŸ›  Database Setup (Reference) + +All exercises assume the following PostgreSQL table: + +inti_db.sql + +```sql +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username TEXT +); +``` + +Sample data: + +```sql +INSERT INTO users (username) VALUES +('admin'), +('root1'), +('John_Doe'), +('Alice'), +('Bob42'), +('charlie'), +('eve99'), +('Mallory'); +``` + +--- + +## πŸ§ͺ Exercises + +### Exercise 1 β€” Length Filtering + +**Task:** +Select all usernames that are **longer than 5 characters**. + +**Concepts practiced:** + +* `WHERE` clause +* String length evaluation + +--- + +### Exercise 2 β€” Letters-Only Usernames + +**Task:** +Select usernames that: + +* Contain **only letters (A–Z, a–z)** +* Contain **no digits** +* Contain **no underscores or symbols** + +**Concepts practiced:** + +* Regular expressions in PostgreSQL +* Character validation at the database level +* Replacing character-by-character loops with pattern matching + +--- + +### Exercise 3 β€” Accepted vs Refused Classification + +**Task:** +Return a result set containing: + +* `username` +* `status` column with values: + + * `Accepted` + * `Refused` + +**Rules:** + +* Username length ≀ 20 +* Username contains letters only + +**Concepts practiced:** + +* `CASE WHEN` expressions +* Conditional logic inside SQL queries +* Translating `if / else` into declarative form + +--- + +### Exercise 4 β€” Count Valid Usernames + +**Task:** +Return **only the number** of usernames that are valid according to the same rules: + +* Length ≀ 20 +* Letters only + +**Concepts practiced:** + +* Aggregate functions (`COUNT`) +* Combining filtering and aggregation +* Thinking about algorithm cost without explicit loops + +--- + +### Exercise 5 β€” Algorithm Analysis (Written) + +**Task:** +Answer the following questions: + +1. If the `users` table **doubles in size**, what happens to the execution time? +2. Write the runtime formula using: + + * `n` = number of rows + * `m` = average username length + +**Expected reasoning:** + +* SQL engines still scan rows internally +* Regex checks still evaluate characters + +**Expected formula:** + +``` +T(n, m) = n Γ— m +``` + +Explain this **in plain language**, from a sysadmin or security perspective. + +--- + +## πŸ” Security Perspective + +These exercises demonstrate why **validating data in SQL** is powerful: + +* Reduces reliance on frontend-only checks +* Prevents bad data from entering critical systems +* Improves consistency across applications +* Aligns with real-world authentication, logging, and SIEM pipelines + +--- + +## βœ… Completion Criteria + +Day 3 is considered complete when: + +* All four SQL queries execute correctly +* The algorithm explanation clearly explains scaling behavior +* The student can explain how SQL replaces procedural loops + +--- + +**Next Step:** Week 1 Day 4 β€” deeper control flow and scripting (Bash or Rust, depending on track).