SQL (OCR GCSE Computer Science): Revision Notes
📚 Revision Notes
SQL
SQL (Structured Query Language) is a standard language used to manage and query databases. It allows users to retrieve, manipulate, and update data stored in relational databases. One of the most common uses of SQL is to search for data within tables.
Using SQL to Search for Data
- SQL allows you to extract specific information from a database using queries.
- The most basic SQL query uses the SELECT statement to choose which columns of data you want to retrieve.
- You can refine your search by using the WHERE clause, which sets conditions for the data returned.
Key SQL Commands
SELECT
- This command is used to specify the columns of data you want to retrieve.
- You can select specific columns or all columns using * (which means all columns). Example:
SELECT * FROM Customers;
This will retrieve all columns from the Customers table.
FROM
- This command specifies the table you want to retrieve the data from.
- It follows the SELECT statement to indicate where the data is stored. Example:
SELECT ContactName, Address FROM Customers;
This retrieves only the ContactName and Address columns from the Customers table.
WHERE
- This command is used to philtre the results of a query based on specific conditions.
- It limits the data returned by the SELECT statement, ensuring that only rows meeting the condition are displayed. Example:
SELECT ContactName, Address FROM Customers WHERE ContactName = 'Mr Smith';
This retrieves the ContactName and Address of customers where the ContactName is "Mr Smith."
SQL Operators
| Operator | Description | Example |
|---|---|---|
| = | Equal to | WHERE City = 'London' |
| <> or != | Not equal to | WHERE Age != 30 |
| > | Greater than | WHERE Age > 18 |
| < | Less than | WHERE Age < 65 |
| >= | Greater than or equal to | WHERE Price >= 10 |
| <= | Less than or equal to | WHERE Price <= 50 |
| BETWEEN | Used to search for values within a range | WHERE Age BETWEEN 18 AND 30 |
| LIKE | Searches for patterns in text fields | WHERE ContactName LIKE 'Smi%' |
| IN | Specify multiple values for a column | WHERE City IN ('London', 'Paris') |
Boolean Operators
| Boolean Operator | Description | Example |
|---|---|---|
| AND | Both conditions must be true | WHERE Country = 'UK' AND Age >= 18 |
| OR | At least one condition must be true | WHERE Age < 18 OR Age > 65 |
| Parentheses () | Used to group conditions for complex queries | WHERE Country = 'England' AND (City = 'London' OR City = 'Oxford') |
infoNote
Key Points to Remember
- SELECT, FROM, and WHERE are the core SQL commands used to retrieve and philtre data from a database.
- SQL operators like =, LIKE, and IN help refine searches by setting conditions and patterns for the data you want.
- Boolean operators (AND, OR) allow you to combine multiple conditions for more complex queries.