Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Basic Queries in SQL ยท Lesson 3 of 10

Module Two

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3

MODULE 2: UNDERSTANDING SQL FUNDAMENTALS

Module Overview

Welcome to Module 2 of Basic Queries! In the previous module, you learned what databases, tables, and queries are. You also discovered that SQL is a language used to communicate with databases.

In this module, you will learn how SQL works, how to write SQL statements, understand important SQL keywords, and create your first database query.

Think of SQL as a language you use to give instructions to a computer database. When you write the correct instruction, the database performs the task you requested.

Lesson 1: Introduction to SQL Syntax

Syntax means the rules used when writing something. Just as English has grammar rules, SQL has rules that help the database understand your instructions.

For example, look at this SQL statement:

SELECT Name FROM Students;

Let's understand each part:

  • SELECT: Tells the database that you want to retrieve information.
  • Name: Tells the database which information you want to see.
  • FROM: Tells the database where to find the information.
  • Students: The name of the table containing student records.
  • Semicolon (;): Commonly used to mark the end of an SQL statement.

The complete statement means: "Show me the names of all students stored in the Students table."

Remember: SQL statements must follow the correct syntax. If you make mistakes, the database may display an error instead of performing your request.

Lesson 2: Understanding SQL Statements

An SQL statement is an instruction given to a database to perform a particular task.

Imagine telling your younger brother to bring your school bag. That is an instruction. In the same way, SQL statements give instructions to databases.

Here are some common SQL statements:

1. SELECT Statement

Used to retrieve information from a database.

SELECT * FROM Students;

This displays all records in the Students table.

2. INSERT Statement

Used to add new information to a database.

INSERT INTO Students (Name, Age)
VALUES ('John', 13);

This adds a new student named John who is 13 years old.

3. UPDATE Statement

Used to change information already stored in a database.

UPDATE Students
SET Age = 14
WHERE Name = 'John';

This changes John's age to 14.

4. DELETE Statement

Used to remove records from a database.

DELETE FROM Students
WHERE Name = 'John';

This removes John's record from the Students table.

Important: UPDATE and DELETE commands can change or remove important information. Always use the correct WHERE condition and check your records before executing these commands.

Lesson 3: SQL Keywords and Commands

SQL keywords are special words that have specific meanings in SQL. They tell the database what action to perform.

Some important SQL keywords include:

SQL Keyword Meaning
SELECT Retrieves information.
FROM Identifies the table to use.
WHERE Filters information using a condition.
INSERT INTO Adds new records.
UPDATE Changes existing records.
DELETE Removes records.
ORDER BY Arranges information in a particular order.
GROUP BY Groups records with similar values.

For example, if you want to display students who are 13 years old, you can write:

SELECT * FROM Students
WHERE Age = 13;

Here, SELECT retrieves the information, FROM identifies the table, and WHERE tells the database to display only students whose age is 13.

Lesson 4: Database Names and Table Names

Before writing SQL queries, you need to understand databases and table names.

Imagine a school called Bright Future Academy. The school uses a database called SchoolDB.

Inside this database, there may be different tables:

  • Students
  • Teachers
  • Subjects
  • Results
  • SchoolFees

Each table stores a different type of information.

For example, the Students table contains student records, while the Teachers table contains information about teachers.

In MySQL, you can select a database using:

USE SchoolDB;

This tells MySQL that you want to work with the SchoolDB database.

You can also ask MySQL to show the available databases:

SHOW DATABASES;

To see the tables inside the selected database, use:

SHOW TABLES;

Lesson 5: Understanding Rows and Columns

A database table is organised into rows and columns. Understanding these two terms will help you write better SQL queries.

Consider the following Students table:

StudentID Name Age Class
101 Daniel 13 JSS 2
102 Mary 12 JSS 1
103 Peter 14 JSS 3
104 Aisha 13 JSS 2

What Is a Row?

A row contains information about one particular student. For example, the first row contains Daniel's student ID, name, age, and class.

What Is a Column?

A column contains one category of information. For example, the Age column contains the ages of all students.

What Is a Record?

A record is a complete set of information about one person or item. Daniel's complete information represents one record.

Quick Tip: Rows go across horizontally, while columns go down vertically.

Lesson 6: Writing Your First SQL Query

Now it is time to write your first SQL query!

Imagine you are helping a school administrator find information about students.

The school has a database called SchoolDB and a table called Students. You want to display all the students.

Step 1: Select the database.

USE SchoolDB;

Step 2: Display all student records.

SELECT * FROM Students;

Let's understand the second command:

  • SELECT means retrieve information.
  • The asterisk (*) means all columns.
  • FROM tells the database where the information is stored.
  • Students is the table name.

The database will display every column and every record in the Students table.

Selecting Only One Column

What if you only want to see the names of the students?

SELECT Name FROM Students;

This displays only the Name column.

Selecting Two Columns

What if you want to see the names and ages of the students?

SELECT Name, Age
FROM Students;

This displays only the Name and Age columns.

Practical Activity: Your First Database Queries

Imagine that you are working as a junior database assistant in a school. Your task is to retrieve information from the Students table.

Use the following sample records:

ID Name Age Class
201John13JSS 2
202Grace12JSS 1
203Michael14JSS 3
204Sarah13JSS 2
205David12JSS 1

Try writing SQL statements to perform these tasks:

  1. Display all the records in the Students table.
  2. Display only the names of all students.
  3. Display the names and ages of all students.
  4. Display only the Class column.
  5. Display the StudentID and Name columns.

Module Summary

In this module, you have learned:

  • SQL syntax refers to the rules for writing SQL commands.
  • An SQL statement is an instruction given to a database.
  • SQL keywords have special meanings.
  • Databases contain tables that organise information.
  • Tables contain rows and columns.
  • The SELECT command retrieves information.
  • The asterisk (*) selects all columns.
  • The FROM keyword identifies the table from which information is retrieved.
  • MySQL uses commands such as SHOW DATABASES and SHOW TABLES.

Module Assessment

Answer the following questions to test your understanding:

  1. What does SQL stand for?
  2. What is SQL syntax?
  3. What is an SQL statement?
  4. Which SQL keyword is used to retrieve information?
  5. What does the FROM keyword do?
  6. What does the asterisk (*) mean in an SQL query?
  7. Which SQL command adds new records to a database?
  8. Which SQL command changes existing records?
  9. What is the difference between a row and a column?
  10. Write an SQL query that displays all records from a table called Employees.

Congratulations!

You have successfully completed Module 2 of Basic Queries! You now understand SQL fundamentals and can write simple SQL statements to retrieve information from a database.

Keep practising. The more SQL commands you write, the better you become!

Coming Up Next

Module 3: Retrieving Data with SELECT

You will learn how to retrieve specific information, select multiple columns, use aliases, and become more confident working with database records.

CRMNuggets โ€“ Learn. Practice. Get Certified.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’