Be able to use a single criterion, or multiple criteria to select subsets of data using a query

Topic 18 – Databases

Objective

Use one or more criteria to select a subset of records from a database, manipulate the results (sorting, calculations, searching) and present the data in a clear, professional report.

1. Designing a Database (Syllabus 18.1)

1.1. Table definition – example “Students”

CREATE TABLE Students (
    StudentID  INT PRIMARY KEY,
    Name       VARCHAR(30) NOT NULL,
    Age        TINYINT,
    Gender     CHAR(1) CHECK (Gender IN ('M','F')),
    Score      DECIMAL(4,1),
    CourseID   INT
);

1.2. Supporting table – “Courses” (demonstrates a foreign key)

CREATE TABLE Courses (
    CourseID   INT PRIMARY KEY,
    CourseName VARCHAR(20) NOT NULL
);

ALTER TABLE Students
ADD CONSTRAINT FK_Student_Course
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID);

1.3. Choosing appropriate data types

Data typeTypical useRange / storage
INTWhole numbers, IDs-2 147 483 648 to 2 147 483 647 (4 bytes)
TINYINTSmall whole numbers (e.g., ages)0 to 255 (1 byte)
DECIMAL(p,s)Exact numeric values (e.g., marks)p total digits, s after decimal (storage varies)
CHAR(n)Fixed‑length text (e.g., gender code)n bytes, always n characters
VARCHAR(n)Variable‑length text (e.g., names)1–n+1 bytes, only actual characters stored

1.4. Field‑size justification

  • Name VARCHAR(30) – most surnames & first names fit within 30 characters; using VARCHAR saves space.
  • Gender CHAR(1) – only one character needed, fixed length simplifies checks.
  • Score DECIMAL(4,1) – allows values up to 999.9; more than sufficient for a 0‑100 mark system.

1.5. Forms (Document Production)

A simple data‑entry form would display fields for Name, Age, Gender, Score and a drop‑down list of Course (populated from the Courses table). The StudentID can be auto‑generated (e.g., AUTO_INCREMENT) so the user never edits it.

2. What Is a Query? (Syllabus 18.2)

A query requests information from one or more tables. In SQL the most common form is the SELECT statement.

Basic SELECT syntax

SELECT column1, column2, …
FROM   table_name
WHERE  condition
ORDER BY column [ASC|DESC];

3. Selecting Sub‑sets of Data

3.1. Single‑criterion selection

Tests only one field.

SELECT Name, Score
FROM Students
WHERE Score > 80;

3.2. Multiple‑criterion selection

Combine two or more tests with logical operators. Use parentheses to control precedence.

SELECT Name, Age, CourseName
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
WHERE (Gender = 'F' AND Age >= 17)
  AND (CourseName = 'Mathematics' OR Score >= 90);

3.3. Logical operators

  • AND – both conditions must be true.
  • OR – either condition may be true.
  • NOT – reverses the truth value.

4. Operators for Text, Ranges and Lists

  • Comparison: =, <>, !=, >, <, >=, <=
  • LIKE – pattern matching with % (any number of characters) and _ (single character).
  • BETWEEN … AND … – inclusive range.
  • IN (value1, value2, …) – matches any value in the list.
  • IS NULL / IS NOT NULL – tests for missing data.

Examples

-- Names that start with “A”
SELECT Name FROM Students WHERE Name LIKE 'A%';

-- Scores between 70 and 85 (inclusive)
SELECT Name, Score FROM Students WHERE Score BETWEEN 70 AND 85;

-- Students in Science or History
SELECT Name FROM Students WHERE CourseID IN (2,3);

5. Sorting and Calculated Fields (Syllabus 18.2)

5.1. Sorting on one or more columns

SELECT Name, Age, Score
FROM Students
ORDER BY Age ASC, Score DESC;   -- youngest first, highest score within each age

5.2. Calculated (derived) fields

SELECT Name,
       Score,
       Score * 1.10 AS AdjustedScore   -- 10 % bonus added
FROM Students;

6. Importing and Exporting Data (Syllabus 18.2 & 18.3)

6.1. Importing CSV data

Most DBMS provide an Import Wizard. In MySQL you can also use:

LOAD DATA INFILE 'students.csv'
INTO TABLE Students
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY ''
IGNORE 1 ROWS;   -- skip header line

6.2. Exporting query results

  • CSV (comma‑separated values) – easy to open in spreadsheets.
  • TXT (tab‑delimited) – useful for plain‑text reports.
  • HTML – can be viewed in a browser or inserted into a document.
  • PDF – final, non‑editable report (often produced from a spreadsheet).
SELECT *
INTO OUTFILE 'students.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '';

6.3. Step‑by‑step report layout (Excel/Google Sheets)

  1. Run the query and copy the result set.
  2. Paste into a new worksheet (use “Paste Special → Text”).
  3. Apply Header/Footer (Page Layout → Header/Footer):
    • Header: School Name – Student Scores Report
    • Footer: Page &[Page] of & [Pages] – Printed on & [Date]
  4. Format numbers (e.g., one decimal place for scores).
  5. Use Conditional Formatting to highlight scores ≥ 90 (green) or ≤ 60 (red).
  6. Save as .xlsx for editing, then export to .pdf for submission.

7. Practical Skills & e‑Safety (Syllabus 18.2 & 18.3)

  • File management: store database files in a protected folder, back‑up regularly (e.g., to an external drive or cloud).
  • Passwords: protect the DBMS with a strong password; never share it with classmates.
  • Data protection: keep personal data (names, IDs) confidential; follow school policy on data handling.
  • Malware awareness: only download database tools from trusted sources; scan files before opening.

8. Example Database – Students & Courses

StudentIDNameAgeGenderScoreCourseID
101Alice17F841
102Bob18M762
103Clara17F911
104David19M683
105Eve18F732
CourseIDCourseName
1Mathematics
2Science
3History

9. Sample Queries (All use the tables above)

  1. Students scoring above 80
    SELECT Name, Score
    FROM Students
    WHERE Score > 80;
            
  2. Female Mathematics students aged 17‑18
    SELECT s.Name, s.Age, c.CourseName
    FROM Students s
    JOIN Courses c ON s.CourseID = c.CourseID
    WHERE s.Gender = 'F'
      AND s.Age BETWEEN 17 AND 18
      AND c.CourseName = 'Mathematics';
            
  3. Scores between 70 and 85 (inclusive)
    SELECT Name, Score
    FROM Students
    WHERE Score BETWEEN 70 AND 85;
            
  4. Names starting with “A”
    SELECT Name
    FROM Students
    WHERE Name LIKE 'A%';
            
  5. Names with “a” as the second character
    SELECT Name
    FROM Students
    WHERE Name LIKE '_a%';
            
  6. Students who scored > 90 or are enrolled in History
    SELECT s.Name, s.Score, c.CourseName
    FROM Students s
    JOIN Courses c ON s.CourseID = c.CourseID
    WHERE s.Score > 90 OR c.CourseName = 'History';
            
  7. Sorted list – youngest first, highest score within each age
    SELECT Name, Age, Score
    FROM Students
    ORDER BY Age ASC, Score DESC;
            

10. Query‑Building Checklist (Step‑by‑Step)

  1. Identify the required information – which columns are needed?
  2. Choose the table(s) that contain those columns.
  3. Decide the selection criteria (single or multiple).
  4. Write the SELECT clause with the chosen columns.
  5. Add the FROM clause (include JOIN if more than one table).
  6. Construct the WHERE clause using comparison, logical, LIKE, BETWEEN, IN, NOT etc.
  7. Group conditions with parentheses where necessary to control precedence.
  8. Optional: add ORDER BY, calculated fields, or LIMIT.
  9. Test the query; adjust criteria if the result set is empty or too large.
  10. Export or copy the result and format the final report.

11. Practice Questions (Mapped to Assessment Objectives)

#TaskAO Target
1 List StudentID and Name of all male students who scored less than 70. AO1 – recall syntax; AO2 – apply AND with numeric and text criteria.
2 Retrieve the names of students whose course is either “Science” or “History”. AO2 – use IN or OR with a foreign‑key field.
3 Find all students whose names contain the letter “e” (any position) and whose age is greater than 17. AO2 – combine LIKE with AND.
4 Show the Name and Score for students whose scores are not between 75 and 85. AO2 – use NOT BETWEEN.
5 List the distinct courses offered in the Students table. AO2 – use DISTINCT (or SELECT DISTINCT CourseName FROM Courses).
6 Produce a report showing Name, Age, Score sorted by Score descending, then export it as a CSV file. AO3 – present data; AO2 – apply ORDER BY and export command.

12. Common Mistakes to Avoid

  • Omitting single quotes around text values (e.g., WHERE Gender = 'F').
  • Using AND when OR is required – often leads to an empty result set.
  • Incorrect placement of parentheses in complex conditions; always group logical expressions to show the intended order.
  • Writing LIKE without a wildcard – it behaves exactly like =.
  • Assuming BETWEEN excludes the end values – it is inclusive.
  • Forgetting to join related tables when a field resides in another table (e.g., forgetting the JOIN Courses to get CourseName).

13. Summary

  • Design a well‑structured table (appropriate data types, primary key, foreign keys where required).
  • Use SELECT … FROM … WHERE … to retrieve exactly the rows needed.
  • Apply single‑column criteria or combine several using AND, OR, NOT, and parentheses for precedence.
  • Employ special operators – LIKE, BETWEEN, IN – to handle text patterns, ranges, and lists.
  • Sort results, create calculated fields, and limit output when required.
  • Import data safely, export in the format demanded by the task, and format the final report (headers, footers, conditional formatting).
  • Follow good e‑safety practice: protect passwords, back up files, and handle personal data responsibly.
  • Check your query against the checklist, test it, and present the output clearly – this satisfies the assessment objectives for IGCSE 0417.

Create an account or Login to take a Quiz

96 views
0 improvement suggestions

Log in to suggest improvements to this note.