Welcome back, young data explorer! In Module 1, you learned what a database is. In Module 2, you learned how to install PostgreSQL, create databases, create tables, and run simple queries. You learned how to use INSERT, SELECT, UPDATE, and DELETE.
In this module, we are going to go deeper. We will learn intermediate SQL. This means we will learn how to do more powerful things with our data. We will learn about joins. Joins let us combine data from multiple tables. We will learn about subqueries. Subqueries are queries inside queries. We will learn about views. Views are saved queries. We will learn about functions. Functions are reusable pieces of code. We will learn about indexes. Indexes make queries faster. And we will learn about normalization. Normalization is a way to organise data efficiently.
Do not worry if some words sound big. We will explain everything in simple language. We will use stories, examples from Nigeria, examples from school, and examples from home. We will use pictures made from keyboard characters (ASCII art) to help you see what we mean. By the end of this module, you will know:
This module is long because it is a complete chapter, not just short notes. Take your time. Read one lesson at a time. Do the exercises. Ask questions. Practice on your computer. Are you ready? Let us begin!
After finishing this module, you should be able to:
Ngozi was a 13-year-old girl who lived in Ibadan, Nigeria. She helped her mother run a small shop. The shop sold provisions: rice, beans, oil, and soap.
Ngozi's mother kept two registers. The first register had a list of customers and their phone numbers. The second register had a list of sales: what was sold, how much it cost, and who bought it.
One day, Ngozi's mother asked, "Which customer bought the most rice last month?"
Ngozi looked at the two registers. She needed information from both. The sales register had the item. The customer register had the name and phone number. But she could not find the answer quickly. She had to flip back and forth between the two registers.
Ngozi remembered her PostgreSQL lessons. "Mummy," she said, "if we used a database, we could join the two tables. We could ask the database one question and get the answer in seconds."
Her mother was amazed. "Can you teach me?" she asked.
Ngozi smiled. "Yes! Let me show you how joins work."
Moral of the story: Joins let you combine data from multiple tables. They help you find answers quickly. Anyone can learn to use joins.
A join is an SQL command that combines data from two or more tables. It matches rows from one table with rows from another table.
Data is often stored in different tables. To get the full picture, you need to join the tables. Joins help you answer questions that use data from multiple tables.
Imagine you have two lists. One list has names and phone numbers. The other list has names and favourite foods. If you want to know Chika's phone number and favourite food, you need to join the two lists. A join is like putting the two lists side by side.
A bank has a customers table and an accounts table. A join can show you which customer owns which account.
A school has a students table and a scores table. A join can show you each student's name and their score.
At home, you have a recipes table and an ingredients table. A join can show you each recipe and its ingredients.
In a Nigerian market, a trader has a customers table and a sales table. A join can show which customer bought which item.
TABLE 1: CUSTOMERS +----+----------+ | ID | NAME | +----+----------+ | 1 | CHIKA | | 2 | EMEKA | +----+----------+ TABLE 2: SALES +----+----------+----------+ | ID | ITEM | CUST_ID | +----+----------+----------+ | 1 | RICE | 1 | | 2 | BEANS | 2 | +----+----------+----------+ JOIN: +----+----------+----------+ | ID | NAME | ITEM | +----+----------+----------+ | 1 | CHIKA | RICE | | 2 | EMEKA | BEANS | +----+----------+----------+
A join combines data from two or more tables. It matches rows from one table with rows from another table.
You learned what a join is. You saw examples from home, school, and Nigeria. Now you know that joins help you combine data.
An INNER JOIN returns only the rows that match in both tables. If a row in one table has no match in the other table, it is not included.
INNER JOIN is the most common type of join. It helps you find data that exists in both tables.
Imagine you have two lists. One list has students who play football. The other list has students who play music. An INNER JOIN shows only students who play both football and music.
A bank uses INNER JOIN to find customers who have both a savings account and a current account.
A school uses INNER JOIN to find students who are in both the science club and the math club.
At home, you use INNER JOIN to find recipes that use both rice and beans.
In a Nigerian market, a trader uses INNER JOIN to find customers who bought both rice and beans.
SELECT customers.name, sales.item FROM customers INNER JOIN sales ON customers.id = sales.cust_id;
INNER JOIN: TABLE 1 (CUSTOMERS) TABLE 2 (SALES) +----+----------+ +----+----------+----------+ | ID | NAME | | ID | ITEM | CUST_ID | +----+----------+ +----+----------+----------+ | 1 | CHIKA | | 1 | RICE | 1 | | 2 | EMEKA | | 2 | BEANS | 2 | | 3 | NGOZI | | 3 | OIL | 2 | +----+----------+ +----+----------+----------+ INNER JOIN RESULT: +----------+----------+ | NAME | ITEM | +----------+----------+ | CHIKA | RICE | | EMEKA | BEANS | | EMEKA | OIL | +----------+----------+ (NGOZI is not included because she has no sales)
INNER JOIN returns only the rows that match in both tables. It is the most common type of join.
You learned about INNER JOIN. You saw examples from home, school, and Nigeria. Now you can use INNER JOIN to find matching data.
A LEFT JOIN returns all rows from the left table and matching rows from the right table. If there is no match, the right side shows NULL. A RIGHT JOIN returns all rows from the right table and matching rows from the left table.
LEFT JOIN and RIGHT JOIN help you find rows that do not have matches. They show you missing data.
Imagine you have a list of all students and a list of students who play football. A LEFT JOIN shows all students and their football status. Students who do not play football show NULL for football.
A bank uses LEFT JOIN to find customers who have no accounts.
A school uses LEFT JOIN to find students who have not taken any exams.
At home, you use LEFT JOIN to find recipes that have no ingredients listed.
In a Nigerian market, a trader uses LEFT JOIN to find customers who have never bought anything.
SELECT customers.name, sales.item FROM customers LEFT JOIN sales ON customers.id = sales.cust_id;
LEFT JOIN: TABLE 1 (CUSTOMERS) TABLE 2 (SALES) +----+----------+ +----+----------+----------+ | ID | NAME | | ID | ITEM | CUST_ID | +----+----------+ +----+----------+----------+ | 1 | CHIKA | | 1 | RICE | 1 | | 2 | EMEKA | | 2 | BEANS | 2 | | 3 | NGOZI | | 3 | OIL | 2 | +----+----------+ +----+----------+----------+ LEFT JOIN RESULT: +----------+----------+ | NAME | ITEM | +----------+----------+ | CHIKA | RICE | | EMEKA | BEANS | | EMEKA | OIL | | NGOZI | NULL | +----------+----------+ (NGOZI is included with NULL because she has no sales)
LEFT JOIN returns all rows from the left table and matching rows from the right table. RIGHT JOIN does the opposite.
You learned about LEFT JOIN and RIGHT JOIN. You saw examples from home, school, and Nigeria. Now you can find rows that do not have matches.
A FULL JOIN returns all rows from both tables. If there is no match, NULL is shown for the missing side.
FULL JOIN helps you see everything. It shows you all data from both tables, even if there are no matches.
Imagine you have a list of all students and a list of all football players. A FULL JOIN shows all students and all football players. If a student is not a football player, the football column shows NULL. If a football player is not a student, the student column shows NULL.
A bank uses FULL JOIN to see all customers and all accounts, even if some customers have no accounts.
A school uses FULL JOIN to see all students and all exam scores, even if some students have no scores.
At home, you use FULL JOIN to see all recipes and all ingredients, even if some recipes have no ingredients.
In a Nigerian market, a trader uses FULL JOIN to see all customers and all sales, even if some customers have no sales.
SELECT customers.name, sales.item FROM customers FULL JOIN sales ON customers.id = sales.cust_id;
FULL JOIN: TABLE 1 (CUSTOMERS) TABLE 2 (SALES) +----+----------+ +----+----------+----------+ | ID | NAME | | ID | ITEM | CUST_ID | +----+----------+ +----+----------+----------+ | 1 | CHIKA | | 1 | RICE | 1 | | 2 | EMEKA | | 2 | BEANS | 2 | | 3 | NGOZI | | 3 | OIL | 2 | +----+----------+ +----+----------+----------+ FULL JOIN RESULT: +----------+----------+ | NAME | ITEM | +----------+----------+ | CHIKA | RICE | | EMEKA | BEANS | | EMEKA | OIL | | NGOZI | NULL | +----------+----------+ (all rows from both tables are included)
FULL JOIN returns all rows from both tables. It shows NULL for missing matches.
You learned about FULL JOIN. You saw examples from home, school, and Nigeria. Now you can see all data from both tables.
A subquery is a query inside another query. The inner query runs first. Its result is used by the outer query.
Subqueries help you solve complex problems. They let you use the result of one query in another query.
Imagine you want to find the student with the highest score. First, you find the highest score. Then, you find the student with that score. A subquery is like doing two steps in one command.
A bank uses a subquery to find customers who have more money than the average balance.
A school uses a subquery to find students who scored higher than the class average.
At home, you use a subquery to find recipes that take longer than the average cooking time.
In a Nigerian market, a trader uses a subquery to find goods that are more expensive than the average price.
SELECT name, score FROM students WHERE score > (SELECT AVG(score) FROM students);
SUBQUERY:
OUTER QUERY: SELECT name, score FROM students WHERE score > ?
|
V
INNER QUERY: SELECT AVG(score) FROM students
|
V
RESULT: 82.5
|
V
OUTER QUERY RUNS WITH RESULT: SELECT name, score FROM students WHERE score > 82.5
|
V
FINAL RESULT: STUDENTS WITH SCORE > 82.5
A subquery is a query inside another query. The inner query runs first, and its result is used by the outer query.
You learned what a subquery is. You saw examples from home, school, and Nigeria. Now you can use subqueries to solve complex problems.
You can use subqueries in different parts of a query. You can use them in the WHERE clause. You can use them in the FROM clause.
Subqueries in WHERE help you filter data. Subqueries in FROM help you create temporary tables.
Imagine you want to find students who scored higher than the class average. You use a subquery in the WHERE clause. Imagine you want to create a temporary table of high scorers. You use a subquery in the FROM clause.
A bank uses a subquery in WHERE to find customers with above-average balances.
A school uses a subquery in FROM to create a list of top students.
At home, you use a subquery in WHERE to find recipes with above-average ratings.
In a Nigerian market, a trader uses a subquery in FROM to create a list of best-selling goods.
-- Subquery in WHERE SELECT name, score FROM students WHERE score > (SELECT AVG(score) FROM students); -- Subquery in FROM SELECT * FROM (SELECT name, score FROM students WHERE score > 80) AS top_students;
SUBQUERY IN WHERE: SELECT name, score FROM students WHERE score > (SELECT AVG(score) FROM students); SUBQUERY IN FROM: SELECT * FROM (SELECT name, score FROM students WHERE score > 80) AS top_students;
Subqueries can be used in WHERE and FROM clauses. They help you filter data and create temporary tables.
You learned how to use subqueries in WHERE and FROM. You saw examples from home, school, and Nigeria. Now you can use subqueries in different parts of your query.
A view is a saved query. It looks like a table, but it does not store data. It runs the query every time you use it.
Views make complex queries easy to reuse. You can save a query as a view and use it again and again.
Imagine you have a long recipe. You write it down once. Then you can use it many times. A view is like that. You write a query once. You save it as a view. You can use the view many times.
A bank creates a view of all customers with high balances. The bank can use the view every day.
A school creates a view of all students with high scores. The school can use the view every term.
At home, you create a view of all quick recipes. You can use the view when you are in a hurry.
In a Nigerian market, a trader creates a view of all best-selling goods. The trader can use the view every week.
CREATE VIEW top_students AS SELECT name, score FROM students WHERE score > 80; -- Use the view SELECT * FROM top_students;
VIEW:
[ QUERY: SELECT name, score FROM students WHERE score > 80 ]
|
V
[ SAVE AS VIEW: top_students ]
|
V
[ USE VIEW: SELECT * FROM top_students ]
|
V
[ RESULTS SHOWN ]
A view is a saved query. It looks like a table but does not store data. You can use it many times.
You learned what a view is. You saw examples from home, school, and Nigeria. Now you can create and use views.
A function is a reusable piece of code. You write it once. You can use it many times. Functions can take inputs and return outputs.
Functions save time. They make code cleaner. They help you avoid writing the same code again and again.
Imagine you have a recipe for jollof rice. You write it down once. You can use it many times. A function is like that recipe. You write it once. You use it many times.
A bank creates a function to calculate interest. The bank uses the function for all accounts.
A school creates a function to calculate grades. The school uses the function for all students.
At home, you create a function to convert measurements. You use the function for all your recipes.
In a Nigerian market, a trader creates a function to calculate discounts. The trader uses the function for all customers.
CREATE FUNCTION add_numbers(a INTEGER, b INTEGER)
RETURNS INTEGER AS $$
BEGIN
RETURN a + b;
END;
$$ LANGUAGE plpgsql;
-- Use the function
SELECT add_numbers(5, 3);
FUNCTION:
[ CREATE FUNCTION add_numbers(a, b) ]
|
V
[ SAVE THE FUNCTION ]
|
V
[ USE FUNCTION: SELECT add_numbers(5, 3) ]
|
V
[ RESULT: 8 ]
A function is a reusable piece of code. You write it once and use it many times.
You learned what a function is. You saw examples from home, school, and Nigeria. Now you can create and use functions.
An index is a special data structure that makes queries faster. It works like the index at the back of a book. Instead of searching every page, you look at the index to find the page number.
Without an index, PostgreSQL must search every row in a table. With an index, PostgreSQL can find data much faster.
Imagine you have a big book. You want to find a topic. Without an index, you must read every page. With an index, you find the page number quickly. An index in a database is like the index in a book.
A bank creates an index on customer account numbers. The bank can find any account in seconds.
A school creates an index on student names. The school can find any student quickly.
At home, you create an index on recipe names. You can find any recipe quickly.
In a Nigerian market, a trader creates an index on item names. The trader can find any item quickly.
CREATE INDEX idx_customer_name ON customers (name); -- Now queries on customer name are faster SELECT * FROM customers WHERE name = 'Chika';
WITHOUT INDEX: [ SEARCH EVERY ROW ] --> [ SLOW ] WITH INDEX: [ LOOK UP IN INDEX ] --> [ FIND ROW ] --> [ FAST ]
An index makes queries faster. It works like the index at the back of a book.
You learned what an index is. You saw examples from home, school, and Nigeria. Now you can create indexes to speed up your queries.
Normalization is a way to organise data in a database. It reduces duplication. It makes data easier to maintain. It follows a set of rules called normal forms.
Without normalization, data can be duplicated. This wastes space and causes errors. Normalization keeps data clean and accurate.
Imagine you have a list of students and their classes. If a student takes three classes, their name appears three times. If you need to change the student's name, you must change it in three places. Normalization separates the data into two tables: students and classes. This avoids duplication.
A bank normalizes its data. Customers are in one table. Accounts are in another table.
A school normalizes its data. Students are in one table. Scores are in another table.
At home, you normalize your recipes. Recipes are in one table. Ingredients are in another table.
In a Nigerian market, a trader normalizes data. Customers are in one table. Sales are in another table.
WITHOUT NORMALIZATION: +----+----------+----------+ | ID | NAME | CLASS | +----+----------+----------+ | 1 | CHIKA | MATH | | 2 | CHIKA | ENGLISH | | 3 | CHIKA | SCIENCE | +----+----------+----------+ (name is duplicated) WITH NORMALIZATION: TABLE 1: STUDENTS +----+----------+ | ID | NAME | +----+----------+ | 1 | CHIKA | +----+----------+ TABLE 2: CLASSES +----+----------+----------+ | ID | STUDENT | CLASS | +----+----------+----------+ | 1 | 1 | MATH | | 2 | 1 | ENGLISH | | 3 | 1 | SCIENCE | +----+----------+----------+ (no duplication)
Normalization organises data to reduce duplication. It keeps data clean and accurate.
You learned what normalization is. You saw examples from home, school, and Nigeria. Now you know how to organise data efficiently.
There are several normal forms. The most common are the first three: 1NF, 2NF, and 3NF.
Following normal forms keeps your data clean. It stops duplication. It stops errors.
1NF: Each column has one value. No lists in a column. 2NF: Every column depends on the primary key. 3NF: No column depends on another non-key column.
A bank normalizes its data. Customer names are in one table. Account balances are in another table.
A school normalizes its data. Student names are in one table. Scores are in another table.
At home, you normalize your recipes. Recipe names are in one table. Ingredients are in another table.
In a Nigerian market, a trader normalizes data. Customer names are in one table. Sales are in another table.
| Normal Form | Rule | Example |
|---|---|---|
| 1NF | Each column has one value | No "Math, English" in one cell |
| 2NF | Every column depends on the primary key | No partial dependencies |
| 3NF | No column depends on another non-key column | No transitive dependencies |
NORMAL FORMS:
[ 1NF ] --> [ ONE VALUE PER COLUMN ]
|
V
[ 2NF ] --> [ EVERY COLUMN DEPENDS ON PRIMARY KEY ]
|
V
[ 3NF ] --> [ NO COLUMN DEPENDS ON NON-KEY COLUMN ]
1NF, 2NF, and 3NF are the first three normal forms. They keep data clean and accurate.
You learned about the first three normal forms. You saw a table of rules. Now you can normalize your data.
A primary key is a unique column that identifies each row. A foreign key is a column that links to a primary key in another table.
Primary keys stop duplication. Foreign keys create relationships between tables. They keep data accurate.
Imagine a school. Each student has a unique student number. That is a primary key. Each student belongs to a class. The class has a unique class number. The student table has a column for class number. That column is a foreign key.
A bank has a customers table and an accounts table. The customers table has a primary key (customer ID). The accounts table has a foreign key (customer ID).
A school has a students table and a scores table. The students table has a primary key (student ID). The scores table has a foreign key (student ID).
At home, you have a recipes table and an ingredients table. The recipes table has a primary key (recipe ID). The ingredients table has a foreign key (recipe ID).
In a Nigerian market, a trader has a customers table and a sales table. The customers table has a primary key (customer ID). The sales table has a foreign key (customer ID).
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
item VARCHAR(50),
price NUMERIC(10, 2),
cust_id INTEGER REFERENCES customers(id)
);
PRIMARY KEY AND FOREIGN KEY: TABLE 1: CUSTOMERS +----+----------+ | ID | NAME | <-- ID is primary key +----+----------+ | 1 | CHIKA | | 2 | EMEKA | +----+----------+ TABLE 2: SALES +----+----------+----------+ | ID | ITEM | CUST_ID | <-- CUST_ID is foreign key +----+----------+----------+ | 1 | RICE | 1 | | 2 | BEANS | 2 | +----+----------+----------+
A primary key uniquely identifies each row. A foreign key links to a primary key in another table.
You learned about primary keys and foreign keys. You saw examples from home, school, and Nigeria. Now you can create relationships between tables.
A schema is the design of a database. It shows the tables, their columns, and the relationships between them.
A good schema makes a database efficient. A bad schema causes problems later.
Imagine you are building a house. You need a plan. The plan shows the rooms and doors. A database schema is like that plan.
A bank designs a schema with a customers table, an accounts table, and a transactions table.
A school designs a schema with a students table, a teachers table, and a scores table.
At home, you design a schema with a recipes table and an ingredients table.
In a Nigerian market, a trader designs a schema with a customers table and a sales table.
SCHEMA DESIGN:
[ CUSTOMERS ] <-- [ SALES ]
| |
V V
(customer_id) (cust_id)
| |
+----[ LINK ]----+
|
V
[ PRODUCTS ]
|
V
(product_id)
A schema is the design of a database. It shows tables, columns, and relationships.
You learned how to design a database schema. You saw examples from home, school, and Nigeria. Now you can design your own schema.
Putting it all together means using joins, subqueries, views, functions, indexes, and normalization to build a complete database.
A complete database solves real problems. It helps you store, find, and protect data.
Imagine you are building a car. You need an engine, wheels, and a steering wheel. Putting it all together means assembling all the parts. In the same way, you assemble all your SQL skills to build a complete database.
A bank builds a complete database with customers, accounts, and transactions.
A school builds a complete database with students, teachers, and scores.
At home, you build a complete database with recipes and ingredients.
In a Nigerian market, a trader builds a complete database with customers and sales.
COMPLETE DATABASE:
[ TABLES ] --> [ JOINS ] --> [ SUBQUERIES ]
| | |
V V V
[ VIEWS ] --> [ FUNCTIONS ] --> [ INDEXES ]
| | |
V V V
[ NORMALIZATION ] --> [ SCHEMA DESIGN ]
|
V
[ COMPLETE DATABASE! ๐ ]
Putting it all together means using all your SQL skills to build a complete database.
You learned how to put everything together. You saw examples from home, school, and Nigeria. Now you can build a complete database.
Best practices are the best ways to do things. They help you write clean, efficient, and correct SQL.
Following best practices makes your code easier to read. It makes your queries faster. It reduces errors.
Imagine you are writing a letter. You use neat handwriting. You use correct spelling. Best practices in SQL are like neat handwriting and correct spelling.
A bank follows best practices. Queries are fast and accurate.
A school follows best practices. Data is clean and organised.
At home, you follow best practices. Your recipes are easy to find.
In a Nigerian market, a trader follows best practices. Sales records are accurate.
BEST PRACTICES:
[ MEANINGFUL NAMES ] --> [ PRIMARY KEYS ]
| |
V V
[ FOREIGN KEYS ] --> [ NORMALIZATION ]
| |
V V
[ INDEXES ] --> [ VIEWS ] --> [ FUNCTIONS ]
| |
V V
[ TEST QUERIES ] --> [ BACK UP DATA ]
|
V
[ CLEAN, FAST, ACCURATE DATABASE ]
Best practices help you write clean, efficient, and correct SQL.
You learned best practices for intermediate SQL. You saw a list of practices. Now you can write better SQL.
| Word | Simple Definition |
|---|---|
| Join | An SQL command that combines data from two or more tables. |
| INNER JOIN | Returns only rows that match in both tables. |
| LEFT JOIN | Returns all rows from the left table and matching rows from the right table. |
| RIGHT JOIN | Returns all rows from the right table and matching rows from the left table. |
| FULL JOIN | Returns all rows from both tables. |
| Subquery | A query inside another query. |
| View | A saved query that looks like a table. |
| Function | A reusable piece of code. |
| Index | A data structure that makes queries faster. |
| Normalization | Organising data to reduce duplication. |
| Primary key | A unique column that identifies each row. |
| Foreign key | A column that links to a primary key in another table. |
| Schema | The design of a database. |
JOIN
|
+---- INNER JOIN (only matching rows)
|
+---- LEFT JOIN (all left rows + matching right rows)
|
+---- RIGHT JOIN (all right rows + matching left rows)
|
+---- FULL JOIN (all rows from both tables)
Step 1: Learn what a join is. Step 2: Learn INNER JOIN. Step 3: Learn LEFT JOIN and RIGHT JOIN. Step 4: Learn FULL JOIN. Step 5: Learn what a subquery is. Step 6: Learn subqueries in WHERE and FROM. Step 7: Learn what a view is. Step 8: Learn what a function is. Step 9: Learn what an index is. Step 10: Learn what normalization is. Step 11: Learn the first three normal forms. Step 12: Learn primary keys and foreign keys. Step 13: Learn how to design a schema. Step 14: Put it all together. Step 15: Learn best practices.
TABLE 1: CUSTOMERS +----+----------+ | ID | NAME | +----+----------+ | 1 | CHIKA | | 2 | EMEKA | +----+----------+ TABLE 2: SALES +----+----------+----------+ | ID | ITEM | CUST_ID | +----+----------+----------+ | 1 | RICE | 1 | | 2 | BEANS | 2 | +----+----------+----------+ JOIN ON CUSTOMERS.ID = SALES.CUST_ID +----------+----------+ | NAME | ITEM | +----------+----------+ | CHIKA | RICE | | EMEKA | BEANS | +----------+----------+
| Join Type | What It Returns | When to Use |
|---|---|---|
| INNER JOIN | Only matching rows | When you need data that exists in both tables |
| LEFT JOIN | All rows from left table + matching rows from right | When you need all data from the left table |
| RIGHT JOIN | All rows from right table + matching rows from left | When you need all data from the right table |
| FULL JOIN | All rows from both tables | When you need all data from both tables |
| Feature | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
| Cost | Free | Free | Free |
| Power | Very powerful | Powerful | Simple |
| Best for | Large apps | Websites | Small apps |
| Security | Very strong | Strong | Basic |
| Users | Many at once | Many at once | One at a time |
In this module, you learned intermediate SQL and data modeling. You learned about joins. Joins combine data from multiple tables. You learned about INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
You learned about subqueries. Subqueries are queries inside queries. You learned how to use subqueries in WHERE and FROM clauses.
You learned about views. Views are saved queries. They look like tables but do not store data. You learned about functions. Functions are reusable pieces of code. You learned about indexes. Indexes make queries faster. They work like the index in a book.
You learned about normalization. Normalization organises data to reduce duplication. You learned about the first three normal forms: 1NF, 2NF, and 3NF. You learned about primary keys and foreign keys. Primary keys uniquely identify rows. Foreign keys link tables. You learned how to design a database schema.
You learned how to put it all together. You learned best practices for intermediate SQL. Now you are ready to move on to Module 4, where you will learn about performance tuning and optimization. But before you go, make sure you understand the key takeaways below.
Match the word in Column A with the correct definition in Column B.
| Column A | Column B |
|---|---|
| 1. Join | A. A saved query that looks like a table |
| 2. Subquery | B. Organising data to reduce duplication |
| 3. View | C. An SQL command that combines data from two or more tables |
| 4. Function | D. A unique column that identifies each row |
| 5. Index | E. A query inside another query |
| 6. Normalization | F. A reusable piece of code |
| 7. Primary key | G. A data structure that makes queries faster |
Answers: 1-C, 2-E, 3-A, 4-F, 5-G, 6-B, 7-D
Title: Design a School Database
Instructions: In groups of 3 or 4, design a database for your school.
List the tables you need. Choose primary keys. Choose foreign keys. Normalize your data.
Present your design to the class.
Example:
Tables: students, teachers, classes, scores.
Primary keys: student_id, teacher_id, class_id, score_id.
Foreign keys: class_id in students, student_id in scores.
Normalized: Yes.
Title: My First Join
Instructions: Create two tables. Add data. Write a join to combine them.
Write down what you did and what you learned.
Example:
Table 1: students (id, name)
Table 2: scores (id, student_id, score)
Join: SELECT students.name, scores.score FROM students INNER JOIN scores ON students.id = scores.student_id;
Title: Build a Shop Database with Joins
Materials: Computer with PostgreSQL installed.
Steps:
Assignment: For one week, practice using joins, subqueries, views, functions, and indexes. Keep a diary. Each day, write down:
At the end of the week, write a short report. Share it with your teacher or parent.
In Module 4, you will learn about performance tuning and optimization. You will learn about:
Before you start Module 4, make sure you understand everything in this module. Practice joins. Create views and functions. Create indexes. Normalize your data. Design a schema. When you are ready, turn the page and continue your journey to becoming a Certified PostgreSQL Expert!
End of Module 3.