Understanding SQL: Essential Concepts
for Grade 12
Introduction to SQL
SQL (Structured Query Language) is a standard language used to communicate with
relational databases. It allows users to create, read, update, and delete data using
commands such as SELECT, INSERT, UPDATE, and DELETE.
For Grade 12, students are expected to know and use the following SQL components:
• SELECT
• FROM
• WHERE
• AND
• BETWEEN
• LIKE
• WILDCARDS
• ORDER BY
Basic SQL Syntax
SELECT and FROM Clauses
The SELECT and FROM clauses are the foundation of SQL queries. They tell the
database what information to display and from which table. Essentially, you are
instructing the database: “Show me these columns.”
The basic structure is:
SELECT eld FROM TableName;
Example:
SELECT Name FROM tblStudents;
To retrieve more than one eld, separate the eld names with commas:
SELECT eld1, eld2 FROM TableName;
Example:
SELECT name, age FROM tblStudents;
1
Displaying All Fields in a Table
To view every eld in a table, use the * symbol, which stands for “all.” This displays all
columns contained in the speci ed table.
SELECT * FROM TableName;
Example:
SELECT * FROM tblStudents;
WHERE Clause
The WHERE clause is used to lter the rows returned by your query. Use it when you
want to nd or select records that meet speci c criteria. The general format is:
SELECT * FROM TableName WHERE eld Logical_Operator Criteria;
Example:
SELECT * FROM tblStudents WHERE Age < 18;
AND Clause
The AND clause is used within a WHERE statement to apply more than one condition.
Both conditions must be true for the row to be selected.
SELECT * FROM TableName WHERE eld condition AND eld condition;
Example:
SELECT * FROM tblStudents WHERE Age > 16 AND Gender = 'F';
BETWEEN Clause
The BETWEEN clause is used to lter rows where a column’s value falls within a
speci ed range, including both the start and end values. This is useful for selecting
records with values that are greater than or equal to the starting point and less than or
equal to the endpoint.
SELECT columns
FROM table
WHERE column BETWEEN value1 AND value2;
This is equivalent to:
WHERE column >= value1 AND column <= value2;
To select values outside the range, use NOT BETWEEN:
WHERE column NOT BETWEEN value1 AND value2;
2
Examples:
Numeric Example:
SELECT ProductID, Price FROM Products: WHERE Price BETWEEN 10 AND 20;
These returns products priced from 10 through 20, inclusive.
Date Example:
SELECT OrderID, OrderDate FROM Orders WHERE OrderDate BETWEEN '2024-01-01'
AND '2024-03-31';
This returns orders placed on or after 2024-01-01 and on or before 2024-03-31.
Text Example:
SELECT Name FROM Customers WHERE LastName BETWEEN 'Adams' AND
'Henderson';
This returns last names that sort between Adams and Henderson according to the
database’s collation rules.
Important points to remember:
Order matters: value1 should be the lower bound and value2 the upper bound; some
SQL dialects return no rows if the rst value is greater than the second.
Inclusivity: Both endpoints are included. Use > and < for exclusive bounds.
NULLs: If the column or either bound is NULL, the BETWEEN test yields unknown and
the row is not returned.
Performance: BETWEEN can use indexes like >=/<=, making it e icient for range
searches.
Date formatting: Use clear date formats or parameterized queries to avoid locale or
format issues.
For complex ranges or exclusive endpoints, use comparison operators (>=, <=) or
combine conditions with OR, IN, or UNION as needed.
3
LIKE Clause and Wildcards
The LIKE clause is valuable for pattern matching, especially when the exact value is
unknown. Wildcards represent variable characters in search patterns.
• % – Any number of characters (including zero)
• _ – Exactly one unknown or missing character
• * – Any number of characters (used in MS Access)
• ? – Exactly one character (used in MS Access)
Examples:
• SELECT * FROM tblStudents WHERE name LIKE 'J%';Finds names starting with J.
• SELECT * FROM tblStudents WHERE name LIKE '%A';Finds names ending with A.
• SELECT * FROM tblStudents WHERE name LIKE '%E%';Finds names containing E
anywhere.
ORDER BY Clause
Use the ORDER BY clause to sort the results of your query. By default, rows are returned
in the order they were added to the table. You can sort by one or more columns, either
ascending (ASC) or descending (DESC). If not speci ed, the order defaults to
ascending.
• SELECT * FROM tblStudents ORDER BY Age;
• SELECT * FROM tblStudents ORDER BY Age ASC;
• SELECT name FROM tblStudents ORDER BY name DESC;