Published by Patrick Mutisya · 14 days ago
Be able to use a single criterion, or multiple criteria to select subsets of data using a query.
A query is a request for information from a database. In SQL (Structured Query Language) the most common form of query is the SELECT statement, which retrieves rows that meet specified conditions.
SELECT column1, column2, …
FROM table_name
WHERE condition;
The WHERE clause is where you define the criteria that rows must satisfy to be included in the result set.
Common comparison operators:
= – equal to<> or != – not equal to> – greater than< – less than>= – greater than or equal to<= – less than or equal toCombine conditions with logical operators:
AND – both conditions must be true.OR – either condition may be true.NOT – reverses the truth value of a condition.LIKE – pattern matching with % (any number of characters) and _ (single character).BETWEEN … AND … – selects values within a range (inclusive).IN (value1, value2, …) – matches any value in a list.| StudentID | Name | Age | Gender | Score | Course |
|---|---|---|---|---|---|
| 101 | Alice | 17 | F | 84 | Mathematics |
| 102 | Bob | 18 | M | 76 | Science |
| 103 | Clara | 17 | F | 91 | Mathematics |
| 104 | David | 19 | M | 68 | History |
| 105 | Eve | 18 | F | 73 | Science |
Single criterion – students who scored above 80:
SELECT Name, Score
FROM Students
WHERE Score > 80;
Multiple criteria – female students aged 17 or 18 who study Mathematics:
SELECT Name, Age, Course
FROM Students
WHERE Gender = 'F'
AND Age IN (17, 18)
AND Course = 'Mathematics';
Range criterion – scores between 70 and 85 inclusive:
SELECT Name, Score
FROM Students
WHERE Score BETWEEN 70 AND 85;
Pattern matching – names that start with “A”:
SELECT Name, Age
FROM Students
WHERE Name LIKE 'A%';
Combined AND/OR – students who either scored above 90 or are in the History course:
SELECT Name, Score, Course
FROM Students
WHERE Score > 90 OR Course = 'History';
SELECT clause with the required columns.FROM clause with the table name.WHERE clause using appropriate operators.StudentID and Name of all male students who scored less than 70.Name and Score for students whose scores are not between 75 and 85.WHERE Gender = 'F').AND when OR is required, which can return an empty result set.LIKE without the wildcard symbols, which behaves like an equality test.Selecting subsets of data involves:
SELECT.FROM.WHERE clause using comparison, logical, and special operators.Mastering these skills enables you to retrieve exactly the information you need from any database.