Resources | Subject Notes | Information Communication Technology ICT
This section focuses on using queries to extract specific information from a database. Queries allow you to select subsets of data based on defined criteria. You can use single criteria or multiple criteria to achieve this.
A query is a request for data from a database. It specifies what data you want to retrieve and any conditions that the data must meet.
A single criteria query selects records based on a single condition applied to a specific field. For example, finding all customers who live in a particular city.
Example: Find all students with a mark greater than 70 in the 'Marks' field.
SQL Syntax (Conceptual):
SELECT * FROM Students WHERE Marks > 70;
Multiple criteria queries use logical operators (AND, OR, NOT) to combine several conditions. This allows for more complex and targeted data retrieval. For example, finding all customers who live in London and have placed an order in the last month.
Example: Find all products that are either red OR blue AND have a price less than £20.
SQL Syntax (Conceptual):
SELECT * FROM Products WHERE (Colour = 'Red' OR Colour = 'Blue') AND Price < 20;
Here's a summary of common operators used in queries:
The following table illustrates how queries can be used with different criteria.
Query | Description | Example Data | Result | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Find all employees in the 'Sales' department. | Selects records where the 'Department' field equals 'Sales'. |
|
|
||||||||||||||||||||||||||||
Find all products with a price greater than £50. | Selects records where the 'Price' field is greater than 50. |
|
|
||||||||||||||||||||||||||||
Find all customers who live in London AND have placed an order. | Selects records where the 'City' field equals 'London' AND the 'HasPlacedOrder' field is true. |
|
|
When writing queries, it's important to: