SQL — Structured Query Language Grade 12

SQL (pronounced "S-Q-L" or "sequel") is the language used to communicate with a database. It lets you retrieve, add, update and delete records. In Delphi, SQL is added to a TADOQuery component.

T1Term 1 · SQL Basics, SELECT & WHERE Queries
What is SQL?

Think of a database as a massive spreadsheet with thousands of rows. SQL is how you ask the database a question: "Give me all students in Grade 12 with a mark above 80, sorted by surname." Without SQL, you would have to scroll through every row manually.

Anatomy of a SQL Statement

SELECT Name, Surname, Mark FROM tblStudents WHERE Grade = 12 ORDER BY Surname Which fields Which table Filter condition Sort order

Using SQL in Delphi

SQL goes into the SQL property of a TADOQuery component. Always close the query before changing the SQL, then open it again.

Delphi — two ways to run a query
// Method 1: assign as one string
qryStudents.Close;
qryStudents.SQL.Text := 'SELECT * FROM tblStudents WHERE Grade = 12';
qryStudents.Open;

// Method 2: build line-by-line (easier to read for long queries)
qryStudents.Close;
qryStudents.SQL.Clear;
qryStudents.SQL.Add('SELECT Name, Surname, Mark');
qryStudents.SQL.Add('FROM tblStudents');
qryStudents.SQL.Add('WHERE Grade = 12');
qryStudents.SQL.Add('ORDER BY Surname ASC');
qryStudents.Open;

SELECT — Choosing What to Display

SQL
SELECT * FROM tblCD;                      -- all fields, all records
SELECT CDName, Artist FROM tblCD;          -- only these two fields
SELECT DISTINCT Genre FROM tblCD;          -- each genre shown once only
SELECT TOP 5 * FROM tblCD;               -- first 5 records only

WHERE — Filtering Records

The WHERE clause is how you filter — like asking "show me only the records that match this condition."

SQL — comparison operators
WHERE Genre = "Rock"             -- exact match (text in double quotes)
WHERE Price < 100               -- numbers (no quotes)
WHERE Grade <> 11              -- not equal
WHERE Mark >= 50              -- greater than or equal
WHERE Genre = "Pop" AND Price < 100       -- both must be true
WHERE Genre = "Pop" OR Genre = "Rock"     -- either can be true

Operator Precedence in Compound Conditions

When a WHERE clause mixes AND and OR, SQL doesn't just evaluate left to right — it follows a fixed order of precedence, same as in maths:

OrderOperator(s)
1 (first)Parentheses ( )
2Multiplication, division
3Subtraction, addition
4NOT
5AND
6 (last)OR

AND is evaluated before OR, so a mixed condition may not filter the way you expect unless you add brackets to force the order you want.

SQL — AND evaluates before OR
WHERE Genre = "Pop" OR Genre = "Rock" AND Price < 100
-- reads as: Genre = "Pop"  OR  (Genre = "Rock" AND Price < 100)
-- ALL Pop CDs are included, regardless of price!

-- To make BOTH genres respect the price limit, use brackets:
WHERE (Genre = "Pop" OR Genre = "Rock") AND Price < 100
When in doubt, add brackets

You don't have to memorise the precedence table if you always wrap OR conditions in brackets when combining them with AND. Brackets make your intention explicit and remove any ambiguity — for the reader and for the database.

Special WHERE Operators

OperatorMeaningExample
LIKEPattern match. % = any chars, _ = one charWHERE Name LIKE "S%" — starts with S
INMatch any value in a listWHERE Genre IN ("Rock","Pop","Jazz")
BETWEENWithin a range (inclusive for numbers)WHERE Price BETWEEN 50 AND 150
IS NULLField is empty/blankWHERE CellNo IS NULL
IS NOT NULLField has a valueWHERE CellNo IS NOT NULL
#date#Date values use # symbolsWHERE OrderDate = #2024-05-01#
Dates can be misread — use yyyy/mm/dd

Access SQL can misinterpret a date written as dd/mm/yyyy, reading it as month/day/year instead. Writing the year first removes the ambiguity: prefer #2024/05/01# (yyyy/mm/dd) over #01/05/2024#.

LIKE wildcard examples
WHERE CDName LIKE "S%"        -- starts with S
WHERE CDName LIKE "%love%"    -- contains the word "love"
WHERE CDName LIKE "%s"        -- ends with s
WHERE Code   LIKE "A__"       -- starts with A followed by exactly 2 chars

LIKE with Text Typed by the User

When the search word comes from an Edit, you build the pattern by gluing the wildcard symbols onto the text in Delphi. A % on each side means "the text may appear anywhere in the field"; a % on one side only means "starts with" or "ends with".

Delphi — search a field for the text in an Edit
sFind := edtFind.Text;
sSQL  := 'SELECT Title, Summary ' +
         'FROM tblBooks ' +
         'WHERE Summary LIKE "%' + sFind + '%"';
// if the user types  rugby  the finished SQL ends:  WHERE Summary LIKE "%rugby%"

qryBooks.Close;
qryBooks.SQL.Text := sSQL;
qryBooks.Open;
Check which wildcard your database expects

A Delphi program talking to the database through ADO normally uses % (many characters) and _ (one character), which is what the examples on this page use. Access's own query designer uses * and ? instead. Use whichever symbol the supplied database responds to — if a LIKE query returns nothing when you know there are matches, the wildcard is the first thing to check. Also note that if the typed text itself contains a double quote, the string breaks; the parameterised version further down avoids that problem.

Alternatives and Missing Values in a Built String

IN is a shorthand for several OR tests on the same field. For a missing value, always test with IS NULL — the equals sign never matches a Null.

SQL — IN and IS NULL
SELECT * FROM tblTrips WHERE Destination IN ("Paarl", "Stellenbosch");
-- identical to: Destination = "Paarl" OR Destination = "Stellenbosch"

SELECT * FROM tblTrips WHERE DriverCell IS NULL;     -- no cell number captured
SELECT * FROM tblTrips WHERE DriverCell = NULL;      -- WRONG: matches nothing, ever

When one condition must hold together with a choice between alternatives, put the alternatives in brackets — the same brackets you would use in the SQL window, just inside a Delphi string:

Delphi — grouped alternatives inside a built string
sSQL := 'SELECT * FROM tblTrips ' +
        'WHERE (Destination = "Paarl" OR Destination = "Stellenbosch") ' +
        'AND Fare >= 80';
// without the brackets, the fare test would apply to Stellenbosch trips only

ORDER BY — Sorting Results

SQL
SELECT * FROM tblCD ORDER BY Artist;             -- ascending (default)
SELECT * FROM tblCD ORDER BY Price DESC;        -- most expensive first
SELECT * FROM tblCD ORDER BY Genre, CDName ASC; -- sort by genre, then name

Calculated Fields & Aliases

You can do maths inside a SELECT and give the result a name using AS.

SQL
SELECT Name, Price, Price * 1.15 AS PriceWithVAT FROM tblProducts;
SELECT Name, Mark, ROUND(Mark / 2, 0) AS HalfMark FROM tblStudents;

Aggregate Functions — Calculating Summaries

FunctionWhat it calculatesExample
COUNT(*)Number of recordsSELECT COUNT(*) FROM tblStudents
SUM(field)Total of numeric fieldSELECT SUM(Mark) FROM tblResults
AVG(field)Average of numeric fieldSELECT AVG(Mark) FROM tblResults
MIN(field)Lowest valueSELECT MIN(Price) FROM tblProducts
MAX(field)Highest valueSELECT MAX(Price) FROM tblProducts
SQL — GROUP BY and HAVING
-- Count students per grade
SELECT Grade, COUNT(*) AS Total
FROM tblStudents
GROUP BY Grade;

-- Only show groups with more than 10 students
SELECT Grade, COUNT(*) AS Total
FROM tblStudents
GROUP BY Grade
HAVING COUNT(*) > 10;
WHERE vs HAVING

WHERE filters individual records before grouping. HAVING filters groups after GROUP BY. If you have both, WHERE runs first.

Joined Tables, ROUND(AVG) and HAVING Together

A typical longer query combines everything: two tables linked in the WHERE clause, a group per record of the "one" table, a rounded average, and a filter on that average. Here every taxi rank has many trips, and we want the ranks whose average fare is above R45.

SQL — relationship in WHERE, groups filtered by HAVING
SELECT R.RankName, ROUND(AVG(T.Fare), 2) AS AvgFare
FROM tblRanks R, tblTrips T
WHERE R.RankID = T.RankID
GROUP BY R.RankName
HAVING AVG(T.Fare) > 45;
  1. WHERE R.RankID = T.RankID matches each trip to its rank (the primary key and foreign key). You may add ordinary conditions here too, e.g. AND T.TripDate >= #2026/09/01#, which discard individual trips before any averaging.
  2. GROUP BY collects the remaining trips into one group per rank.
  3. AVG and ROUND are worked out once for each group.
  4. HAVING then keeps only the groups whose average passes the test.
Trap — you cannot use the alias in HAVING

Access does not let HAVING (or WHERE) use a name you created with AS. Repeat the calculation: HAVING AVG(T.Fare) > 45, not HAVING AvgFare > 45. Also never put an aggregate such as AVG(...) in WHERE — the groups do not exist yet at that stage.

SQL String & Math Functions

FunctionReturnsExample
LEN(field)Character countSELECT LEN(Name) FROM tblStudents
LEFT(field, n)First n charactersLEFT(Surname, 3) → first 3 letters
RIGHT(field, n)Last n charactersRIGHT(IDNo, 4)
MID(field, start, n)Substring from positionMID(IDNo, 3, 2) → month of birth (position 1–2 is the year)
ROUND(field, dec)Rounded valueROUND(Avg, 2) → 2 decimal places
INT(field)Integer part (no decimal)INT(75.8) = 75
FORMAT(field, "0.00")Formatted stringFORMAT(Price, "R0.00")

Joining Text with &

In Access SQL the & operator joins pieces of text into one value. Any text you type yourself goes in quotes; field names do not. Give the joined result a heading with AS, otherwise the column gets a meaningless default name.

Delphi — two fields and a separator in one labelled column
qryLearners.Close;
qryLearners.SQL.Text :=
  'SELECT Surname & ", " & Name AS FullName, Grade ' +
  'FROM tblLearners ' +
  'ORDER BY Surname';
qryLearners.Open;
// FullName column shows:  Dlamini, Thandi
Two languages, two joining symbols

The + in the Delphi code joins the Delphi string pieces together while you build the statement. The & sits inside the quotes and is part of the SQL that Access will run. Do not mix them up.

Calculating with Numeric Text

Some codes are stored as text but hold numbers in fixed positions. You can cut the digits out with LEFT, RIGHT or MID and then do arithmetic on them, naming the answer with AS. Suppose a shop's SlotCode is stored as "0817": the first two characters are the opening hour and the last two are the closing hour.

SQL — arithmetic on pieces of a text field
SELECT ShopName, SlotCode,
       RIGHT(SlotCode, 2) - LEFT(SlotCode, 2) AS HoursOpen
FROM tblShops;
-- "0817":  RIGHT = "17",  LEFT = "08"   17 - 8 = 9 hours
Trap — only digits can be subtracted

LEFT, RIGHT and MID return text. Access quietly treats that text as a number when it consists purely of digits, but a stray letter or space in the piece will make the calculation fail. Always check which character positions hold the digits before choosing the numbers in LEFT(field, n) and RIGHT(field, n).

Date Functions

SQL — date examples
SELECT Name, YEAR(BirthDate) AS BirthYear FROM tblStudents;
SELECT * FROM tblOrders WHERE MONTH(OrderDate) = 12;     -- December orders
SELECT * FROM tblStudents WHERE YEAR(BirthDate) = 2008;

Day, Month and Year as Separate Columns

DAY, MONTH and YEAR each return a whole number, so you can show them in their own columns or use them in a condition or calculation.

SQL — all three date parts
SELECT TripID,
       DAY(TripDate)   AS TripDay,
       MONTH(TripDate) AS TripMonth,
       YEAR(TripDate)  AS TripYear
FROM tblTrips;

Today and Date Ranges

Date() gives today's date in Access SQL. To select all the records for one day, do not use =. A field that stores a date and a time (for example 2026/10/02 14:35) is later than the date at midnight, so an equals test skips it. Instead, start from the beginning of the day and stop just before the beginning of the next day.

SQL — whole-day range, including any time of day
-- every trip on 2 October 2026
SELECT * FROM tblTrips
WHERE TripDate >= #2026/10/02# AND TripDate < #2026/10/03#;

-- every trip today (Date() + 1 is tomorrow)
SELECT * FROM tblTrips
WHERE TripDate >= Date() AND TripDate < Date() + 1;
Trap — <= or BETWEEN on the last day

TripDate <= #2026/10/02# (and a BETWEEN ending on that date) also stops at midnight, so a trip at 14:35 on that day is lost. Using "less than the next day" keeps every moment of the last day inside the range.

INSERT, UPDATE, DELETE

SQL — modifying data
-- Add a new record
INSERT INTO tblStudents (Name, Surname, Grade)
VALUES ("Alice", "Smith", 12);

-- Change existing data
UPDATE tblStudents
SET Grade = 12
WHERE StudentID = 45;

-- Remove a record
DELETE FROM tblStudents
WHERE StudentID = 45;
Always use WHERE with UPDATE and DELETE

Without a WHERE clause, UPDATE changes every record and DELETE removes every record in the table. Always double-check your condition.

Building an INSERT String with QuotedStr

Instead of a parameterised query, you will sometimes build the whole SQL statement as one string by joining Delphi variables onto it directly. Text values need quotes around them in the final SQL string — the QuotedStr function wraps a string value in quotes for you, so you don't have to type them by hand.

Delphi — INSERT built with string concatenation + QuotedStr
qryStudents.Close;
qryStudents.SQL.Text := 'INSERT INTO tblStudents (Name, Surname) VALUES (' +
  QuotedStr(edtName.Text) + ', ' + QuotedStr(edtSurname.Text) + ')';
qryStudents.ExecSQL;   // use ExecSQL (not Open) for INSERT/UPDATE/DELETE — they don't return records
QuotedStr vs parameters

QuotedStr is a quick way to build a query from a string variable, but a parameterised query (below) is the safer, preferred technique — it avoids manually matching quotes and is more resistant to SQL injection.

Running a Built SQL String: Open or ExecSQL?

Once the statement is stored in SQL.Text you must run it — and the method depends on whether the statement gives records back.

Kind of statementGives records back?Run it with
SELECTYes — a result set to display or loop through.Open
INSERT, UPDATE, DELETENo — they only change the table.ExecSQL
Delphi — one query component, both kinds of statement
// A query that returns records: Close, set the text, Open
qryTrips.Close;
qryTrips.SQL.Text := sSQL;
qryTrips.Open;

// An action query: Close, set the text, ExecSQL
qryTrips.Close;
qryTrips.SQL.Text := 'UPDATE tblTrips SET Fare = Fare * 1.1 WHERE Destination = "Paarl"';
qryTrips.ExecSQL;

// ExecSQL loads nothing, so run a SELECT again to see the change
qryTrips.Close;
qryTrips.SQL.Text := 'SELECT * FROM tblTrips';
qryTrips.Open;
Trap — Close first, and match the method to the statement

Always call Close before you change SQL.Text. Using Open on an INSERT, UPDATE or DELETE raises an error because there is no result set to open, while using ExecSQL on a SELECT runs it but shows you nothing. Assigning to SQL.Text replaces the old statement completely, so no Clear is needed.

Parameterised Queries

Instead of hardcoding a value, use a parameter that is filled in by the user at runtime.

Delphi — parameterised query (by name method)
qryStudents.Close;
qryStudents.SQL.Clear;
qryStudents.SQL.Add('SELECT * FROM tblStudents');
qryStudents.SQL.Add('WHERE Grade = :pGrade');   // :pGrade is the parameter
qryStudents.Parameters.ParamByName('pGrade').Value := sedGrade.Value;
qryStudents.Open;

SQL in Delphi — Reading Field Values

Delphi
// Read a field value from the current record
sName   := qryStudents['Name'];
iGrade  := qryStudents['Grade'];
rMark   := qryStudents['Average'];

// Loop through all records
qryStudents.First;
while not qryStudents.Eof do
begin
  memOut.Lines.Add(qryStudents['Name'] + ' ' + qryStudents['Surname']);
  qryStudents.Next;
end;

Worked Example — A Search Screen

A classic Paper 1 task: the user types (part of) a surname into edtSearch and clicks a button. The program must build a parameterised query from that input, run it, list every match in memOut, and report how many were found. This combines user input, SQL, looping a dataset and string output.

Delphi — search button click
procedure TfrmMain.btnSearchClick(Sender: TObject);
var
  iFound : integer;
begin
  memOut.Clear;
  iFound := 0;

  // 1. Build a parameterised query from the user's input
  qryStudents.Close;
  qryStudents.SQL.Clear;
  qryStudents.SQL.Add('SELECT Name, Surname, Grade, Average');
  qryStudents.SQL.Add('FROM tblStudents');
  qryStudents.SQL.Add('WHERE Surname LIKE :pName');
  qryStudents.SQL.Add('ORDER BY Surname ASC');
  // add a wildcard so 'coe' matches 'Coetzee'
  qryStudents.Parameters.ParamByName('pName').Value := edtSearch.Text + '%';
  qryStudents.Open;

  // 2. Loop through the result set and display each record
  qryStudents.First;
  while not qryStudents.Eof do
  begin
    memOut.Lines.Add(qryStudents['Name'] + ' ' + qryStudents['Surname']
                     + '  (Gr ' + IntToStr(qryStudents['Grade'])
                     + ', ' + FloatToStrF(qryStudents['Average'], ffFixed, 4, 1) + '%)');
    Inc(iFound);
    qryStudents.Next;
  end;

  // 3. Report how many were found
  if iFound = 0 then
    ShowMessage('No learner found starting with "' + edtSearch.Text + '".')
  else
    lblCount.Caption := IntToStr(iFound) + ' learner(s) found';
end;
Why a parameter, not string-joining?

Using :pName and ParamByName is safer and cleaner than gluing the user's text straight into the SQL string — it avoids quote/type errors and blocks SQL injection (see Cybercrime). The wildcard '%' is added to the value, so a partial surname still matches.

Which SQL clause filters which records are returned?

WHERE filters rows by a condition. ORDER BY sorts; GROUP BY groups for aggregates.

Which clause sorts the results of a query?

ORDER BY sorts the result set (ASC or DESC).

Which function counts the number of records?

COUNT(*) returns the number of records. SUM totals and AVG averages a field.