Hello, data detective! In Module Four, you learned the basics of MySQL โ how to create tables, add data, read data, update data, and delete data. Now, in Module Five, we will go further. We will learn advanced techniques to search, filter, and connect data. These skills will help you find hidden clues and investigate security incidents like a pro!
In Module Four, you learned the basic commands of MySQL. You learned how to create tables, add data, read data, update data, and delete data. But in real security investigations, we need more powerful tools. We need to search for patterns, find relationships, and analyse large amounts of data quickly.
In this module, we will learn advanced MySQL skills. We will learn how to use JOIN to combine tables, GROUP BY to group data, and subqueries to ask complex questions. We will also learn how to use MySQL to find suspicious patterns and investigate security incidents. By the end of this module, you will be a MySQL expert!
Remember: Great detectives connect the dots. MySQL helps you connect the dots!
After this module, you will be able to:
Chidi is 10 years old and lives in Abuja. His school has a new computer lab. One day, the lab manager noticed that someone had been logging in late at night and trying to guess passwords. He had two tables of data: one with login logs and one with student information. But he couldn't figure out who the suspicious person was.
Chidi's older brother, Emeka, is a SOC Analyst. He came to help. He used a special MySQL command called JOIN to combine the two tables. He searched for all failed logins after 10 PM and connected them to student names. In just a few seconds, he found that only one student had failed logins late at night โ a student who had been caught before.
Chidi was amazed. He asked Emeka to teach him advanced MySQL. Emeka said, "It's like putting together pieces of a puzzle. Let me show you how to use JOIN and GROUP BY!"
Question for you: Have you ever had to connect pieces of information to solve a puzzle?
Definition: In Module Four, we learned basic MySQL commands: CREATE TABLE, INSERT, SELECT, UPDATE, DELETE, WHERE, and ORDER BY.
Why it is important: These are the foundation for advanced commands.
Simple explanation: Before we learn to run, we need to learn to walk.
Real-life example: You use basic commands to create a table of students.
School example: You create a table of test scores.
Home example: You create a table of chores.
Nigerian example: You create a table of customers.
Basic Commands: - CREATE TABLE - INSERT INTO - SELECT - UPDATE - DELETE - WHERE - ORDER BY
Mini summary: Basic commands are the building blocks of MySQL.
Definition: A JOIN combines data from two or more tables based on a common column.
Why it is important: JOIN helps us connect information that is stored in different tables.
Simple explanation: It's like matching puzzle pieces โ you find the pieces that fit together.
Real-life example: Combining a table of students with a table of their grades.
School example: Combining a table of teachers with a table of classes.
Home example: Combining a table of family members with a table of chores.
Nigerian example: Combining a table of customers with a table of orders.
JOIN Example: Students table: name, age, class_id Classes table: class_id, class_name JOIN them to get: name, age, class_name
Mini summary: JOIN helps us combine data from different tables.
Definition: INNER JOIN returns only the rows that match in both tables.
Why it is important: It helps us find data that exists in both tables.
Simple explanation: It's like finding friends who have the same favourite colour.
Real-life example: Finding students who have grades recorded.
School example: Finding teachers who are assigned to a class.
Home example: Finding family members who have chores assigned.
Nigerian example: Finding customers who have placed orders.
INNER JOIN Syntax: SELECT students.name, grades.grade FROM students INNER JOIN grades ON students.id = grades.student_id;
Mini summary: INNER JOIN matches data that exists in both tables.
Definition: LEFT JOIN returns all rows from the left table, and matching rows from the right table. If there is no match, it shows NULL.
Why it is important: It helps us find data that is missing from one table.
Simple explanation: It's like a guest list โ you have all guests, and some might not have RSVP'd.
Real-life example: Finding students who have no grades recorded.
School example: Finding teachers who are not assigned to a class.
Home example: Finding family members who have no chores.
Nigerian example: Finding customers who have never placed an order.
LEFT JOIN Syntax: SELECT students.name, grades.grade FROM students LEFT JOIN grades ON students.id = grades.student_id;
Mini summary: LEFT JOIN includes all rows from the left table, even if there's no match.
Definition: GROUP BY groups rows that have the same value in a column. It is often used with aggregate functions like COUNT, SUM, AVG.
Why it is important: It helps us find patterns and summaries.
Simple explanation: It's like counting how many of each colour you have in a box of crayons.
Real-life example: Counting how many students are in each class.
School example: Finding the average test score for each subject.
Home example: Counting how many chores each person has.
Nigerian example: Counting how many orders each customer has placed.
GROUP BY Syntax: SELECT class_id, COUNT(*) AS student_count FROM students GROUP BY class_id;
Mini summary: GROUP BY groups data and helps us find patterns.
Definition: HAVING is like WHERE, but it filters groups created by GROUP BY.
Why it is important: It helps us filter groups based on a condition.
Simple explanation: It's like saying "only show me groups that have more than 5 students."
Real-life example: Finding classes that have more than 20 students.
School example: Finding subjects with an average score above 80.
Home example: Finding family members with more than 5 chores.
Nigerian example: Finding customers with more than 10 orders.
HAVING Syntax: SELECT class_id, COUNT(*) AS student_count FROM students GROUP BY class_id HAVING student_count > 10;
Mini summary: HAVING filters groups based on a condition.
Definition: A subquery is a query inside another query. It helps us ask complex questions.
Why it is important: It allows us to answer questions that need multiple steps.
Simple explanation: It's like asking a friend a question, and then asking them another question based on the answer.
Real-life example: Finding students who scored higher than the average.
School example: Finding subjects where the average score is above the overall average.
Home example: Finding chores that take longer than the average.
Nigerian example: Finding customers who have spent more than the average.
Subquery Syntax: SELECT name FROM students WHERE grade > (SELECT AVG(grade) FROM students);
Mini summary: Subqueries help us ask multiple-step questions.
Definition: LIKE is used to search for a pattern in a column. Wildcards like % (any characters) and _ (one character) help us search.
Why it is important: It helps us find data that matches a pattern, not just an exact value.
Simple explanation: It's like finding all words that start with "A".
Real-life example: Finding all students whose names start with "A".
School example: Finding all emails that end with ".edu".
Home example: Finding all movies that start with "The".
Nigerian example: Finding all customers with "Lagos" in their address.
LIKE Syntax: SELECT * FROM students WHERE name LIKE 'A%'; SELECT * FROM students WHERE email LIKE '%@gmail.com';
Mini summary: LIKE helps us search for patterns in data.
Definition: LIMIT restricts the number of rows returned by a query.
Why it is important: It helps us manage large datasets and see only the most important results.
Simple explanation: It's like only looking at the first page of a search result.
Real-life example: Showing only the top 10 students.
School example: Showing the top 5 test scores.
Home example: Showing the next 3 movies to watch.
Nigerian example: Showing the top 10 customers by spending.
LIMIT Syntax: SELECT * FROM students ORDER BY grade DESC LIMIT 5;
Mini summary: LIMIT controls how many rows we see.
Definition: We can combine JOIN, GROUP BY, and HAVING for powerful analysis.
Why it is important: It helps us answer complex security questions.
Simple explanation: It's like using all your detective tools at once.
Real-life example: Finding classes with more than 5 students who scored above 80.
School example: Finding subjects where the average score is above 75.
Home example: Finding family members with more than 3 chores and an average duration of 30 minutes.
Nigerian example: Finding customers who have placed more than 5 orders with a total spending above โฆ10,000.
Combined Query: SELECT classes.name, COUNT(students.id) AS student_count, AVG(grades.grade) AS avg_grade FROM classes JOIN students ON classes.id = students.class_id JOIN grades ON students.id = grades.student_id GROUP BY classes.name HAVING student_count > 5 AND avg_grade > 70;
Mini summary: Combining tools gives us powerful analysis.
Definition: SOC Analysts use MySQL to analyse login logs and find suspicious activities.
Why it is important: It helps us detect and respond to security incidents.
Simple explanation: It's like searching for footprints to find the culprit.
Real-life example: Finding all failed login attempts after 10 PM.
School example: Finding who accessed a file after hours.
Home example: Finding who logged into a smart device.
Nigerian example: Finding suspicious transactions in a bank.
Security Query: SELECT username, login_time, success FROM login_logs WHERE success = false AND login_time > '22:00:00' ORDER BY login_time DESC;
Mini summary: MySQL helps us investigate security incidents.
Definition: Sometimes we need to combine data from multiple tables to find suspicious patterns.
Why it is important: It helps us connect clues from different sources.
Simple explanation: It's like putting together puzzle pieces from different boxes.
Real-life example: Combining login logs with user information to find who made failed attempts.
School example: Combining access logs with student information.
Home example: Combining device logs with family member information.
Nigerian example: Combining transaction logs with customer information.
Security Query with JOIN: SELECT users.name, logs.login_time, logs.success FROM login_logs logs JOIN users ON logs.user_id = users.id WHERE logs.success = false ORDER BY logs.login_time DESC;
Mini summary: Combining tables helps us find hidden connections.
Definition: We can use advanced MySQL to find unusual patterns, like logins at odd times or from strange locations.
Why it is important: Unusual patterns often indicate a security threat.
Simple explanation: It's like noticing that someone is wearing a disguise.
Real-life example: Finding logins from a country where the company doesn't operate.
School example: Finding access to files that a student shouldn't see.
Home example: Finding a device that is online when no one is home.
Nigerian example: Finding transactions from unusual locations.
Unusual Pattern Query: SELECT user_id, login_time, ip_address FROM login_logs WHERE login_time BETWEEN '00:00:00' AND '05:00:00' AND ip_address NOT LIKE '192.168.%';
Mini summary: Advanced MySQL helps us find unusual patterns.
Definition: The more you practise advanced MySQL, the better you become at security investigations.
Why it is important: Practice helps you think like a detective and find clues faster.
Simple explanation: Like practising a sport โ you get better with time.
Real-life example: A SOC Analyst practises queries every day.
School example: Students practise writing complex queries.
Home example: A parent practises with a home database.
Nigerian example: A bank analyst practises with customer data.
Practice Exercises: - Write a query with JOIN and GROUP BY - Write a query with a subquery - Write a query with LIKE and wildcards - Write a query with HAVING
Mini summary: Practice makes you a faster, better detective.
Definition: You have learned advanced MySQL skills. You can now investigate security incidents like a pro.
Why it is important: These skills are essential for any SOC Analyst.
Simple explanation: You are now a master of data โ you can find hidden clues.
Real-life example: You can help a company investigate a security breach.
School example: You can help your school analyse access logs.
Home example: You can help your family monitor their devices.
Nigerian example: You can help a bank detect fraud.
You โ Learned Advanced MySQL โ Can Investigate Threats! ๐ต๏ธโโ๏ธ
Mini summary: You are now an advanced MySQL user โ ready to investigate!
Step 1: Find failed logins and user names Step 2: Tables: login_logs, users Step 3: Common column: user_id Step 4: SELECT users.name, logs.login_time Step 5: JOIN users ON logs.user_id = users.id Step 6: WHERE logs.success = false Step 7: ORDER BY logs.login_time DESC Step 8: Run the query!
Identify Question
|
V
Identify Tables
|
V
Choose JOIN Type
|
V
Write SELECT Statement
|
V
Add GROUP BY (if needed)
|
V
Add HAVING (if needed)
|
V
Add ORDER BY
|
V
Run Query
|
V
Analyse Results
| JOIN Type | What It Returns | Use Case |
|---|---|---|
| INNER JOIN | Only matching rows | Find students with grades |
| LEFT JOIN | All rows from left table | Find students without grades |
| RIGHT JOIN | All rows from right table | Rarely used |
| FULL JOIN | All rows from both tables | Not supported in MySQL |
| Function | What It Does | Example |
|---|---|---|
| COUNT | Counts rows | COUNT(*) |
| SUM | Adds values | SUM(amount) |
| AVG | Calculates average | AVG(grade) |
| MIN | Finds minimum | MIN(grade) |
| MAX | Finds maximum | MAX(grade) |
Lesson 1: Review basic commands
|
Lesson 2: Introduction to JOIN
|
Lesson 3: INNER JOIN
|
Lesson 4: LEFT JOIN
|
Lesson 5: GROUP BY
|
Lesson 6: HAVING
|
Lesson 7: Subqueries
|
Lesson 8: LIKE and Wildcards
|
Lesson 9: LIMIT
|
Lesson 10: Combining commands
|
Lesson 11: Security with MySQL (Logs)
|
Lesson 12: Security with MySQL (Multiple tables)
|
Lesson 13: Finding unusual patterns
|
Lesson 14: Practice
|
YOU ARE AN ADVANCED MYSQL USER! ๐
Congratulations! You have completed Module Five of your Security Operation Centre Analyst training. You have learned advanced MySQL skills โ JOIN, GROUP BY, HAVING, subqueries, LIKE, wildcards, and LIMIT. You have learned how to combine data from multiple tables, group data to find patterns, and ask complex questions. You have also learned how to use these skills in security investigations. You are now a true MySQL expert!
Match the command to its description:
| Command | Description |
|---|---|
| JOIN | Combines tables |
| GROUP BY | Groups rows |
| HAVING | Filters groups |
| LIKE | Searches for a pattern |
| LIMIT | Restricts rows |
Scenario 1: You are a SOC Analyst. You have two tables: login_logs (user_id, login_time, success) and users (id, name). Write a query to find all failed logins after 10 PM and the user names.
Scenario 2: You have a table of transactions (customer_id, amount, date). Write a query to find the total spending for each customer, and only show customers who have spent more than โฆ10,000.
Scenario 3: You have a table of emails (id, sender, subject). Write a query to find all emails that contain the word "phishing" in the subject.
Security Investigation Challenge: In groups of 4, you are given a sample database with login logs, user information, and transaction data. Your task is to write queries to find suspicious activities โ failed logins, unusual transaction amounts, and logins from unusual locations. Present your findings to the class.
My Advanced SQL Practice: Create a database with at least two tables (e.g., students and grades). Write queries that use JOIN, GROUP BY, HAVING, subqueries, and LIKE. Write a report on what you found.
Task: Create a security log database with tables for login logs, user information, and IP addresses. Write at least 5 advanced queries to find suspicious activities, such as failed logins, logins from unusual countries, and users with multiple failed attempts. Present your queries and findings.
Assignment: Write a report on a real-world security incident that could have been detected using MySQL. Explain how MySQL queries could have been used to identify the suspicious activity.
Challenge: Write a complex MySQL query that finds all users who have had more than 3 failed login attempts in the last 24 hours, and then joined by a user who logged in successfully from a different IP address within 5 minutes. Use JOIN, GROUP BY, HAVING, and subqueries.
Fill-in-the-Blank Answers: 1. JOIN, 2. INNER, 3. LEFT, 4. GROUP BY, 5. HAVING, 6. subquery, 7. LIKE, 8. LIMIT, 9. aggregate, 10. Security, 11. %, 12. _, 13. WHERE, 14. Practice, 15. advanced.
True or False: 1T, 2F, 3F, 4T, 5F, 6T, 7F, 8T, 9T, 10F.
Multiple Choice Answers: 1A, 2B, 3A, 4B, 5B, 6A, 7B, 8A, 9A, 10B, 11B, 12A, 13B, 14C, 15B.
In Module Six, we will learn about threat detection tools and how SOC Analysts use them to monitor networks and detect attacks. We will look at tools like SIEM, intrusion detection systems, and endpoint detection. We will also learn how to use MySQL to analyse security data.
Before next class: Think about how you would monitor a network for suspicious activities. What would you look for? We will explore this together in the next module.
๐๏ธ You have completed Module Five โ keep up the great work! ๐๏ธ