SQL & Databases
Learn to query databases using SQL - the standard language for working with data!
What you'll learn:
- What databases are and why we use them
- The SELECT command to retrieve data
- The FROM clause to specify which table
- The WHERE clause to filter results
What is a Database?
A database is an organised collection of data stored electronically. Think of it like a powerful spreadsheet that can store millions of records and retrieve them instantly!
Example: students table
| id | name | age | year_group | house |
|---|---|---|---|---|
| 1 | Alice | 14 | 9 | Red |
| 2 | Bob | 15 | 10 | Blue |
| 3 | Charlie | 14 | 9 | Red |
| 4 | Diana | 16 | 11 | Green |
| 5 | Eve | 15 | 10 | Blue |
What is SQL?
SQL (Structured Query Language) is the standard language for communicating with databases. It lets you:
- Retrieve data (SELECT)
- Insert new records (INSERT)
- Update existing records (UPDATE)
- Delete records (DELETE)
SQL is used by almost every website and app you use - from social media to online shopping!
The SELECT Statement
SELECT is the most common SQL command - it retrieves data from a database.
SELECT ... FROM
Choose columns and tableSELECT specifies which columns you want. FROM specifies which table.
SELECT name, age
FROM students;
Result:
| name | age |
|---|---|
| Alice | 14 |
| Bob | 15 |
| Charlie | 14 |
| Diana | 16 |
| Eve | 15 |
SELECT * (All Columns)
Get everythingUse * (asterisk) to select ALL columns from a table.
SELECT *
FROM students;
This returns ALL columns: id, name, age, year_group, house
WHERE (Filter Results)
Only matching rowsWHERE filters the results to only include rows that match a condition.
SELECT name, house
FROM students
WHERE house = 'Red';
Result:
| name | house |
|---|---|
| Alice | Red |
| Charlie | Red |
WHERE Comparison Operators
Use these operators in WHERE clauses to filter data:
| Operator | Meaning | Example |
|---|---|---|
= |
Equal to | WHERE age = 15 |
!= or <> |
Not equal to | WHERE house != 'Red' |
< |
Less than | WHERE age < 15 |
> |
Greater than | WHERE age > 14 |
<= |
Less than or equal | WHERE year_group <= 10 |
>= |
Greater than or equal | WHERE age >= 15 |
Combining Conditions: AND, OR
Use AND and OR to combine multiple conditions in WHERE.
AND - Both conditions must be true
SELECT name, age, house
FROM students
WHERE age = 14 AND house = 'Red';
| name | age | house |
|---|---|---|
| Alice | 14 | Red |
| Charlie | 14 | Red |
OR - Either condition can be true
SELECT name, house
FROM students
WHERE house = 'Red' OR house = 'Green';
| name | house |
|---|---|
| Alice | Red |
| Charlie | Red |
| Diana | Green |
SQL Syntax Rules
SQL keywords are NOT case-sensitive
SELECT, select, and Select all work the same. Convention is UPPERCASE.
Text values need quotes
Use single quotes for text: WHERE name = 'Alice'
Numbers don't need quotes
WHERE age = 15 (not '15')
Statements end with semicolon
Always end your SQL statements with ;
Learning Path
Follow these activities in order to master SQL basics.
1. Predict: Query Results
Predict what data SQL queries will return.
2. Study: Worked Example
See how to build SQL queries step by step.
3. Investigate: Trace Query Execution
Track how SQL processes data step by step.
4. Modify: Build SQL Queries
Arrange SQL clauses to create working queries.
5. Test: Knowledge Quiz
Check your understanding of SQL basics.
Key Vocabulary
database
An organised collection of data stored electronically.
table
A collection of related data organised in rows and columns.
record (row)
A single entry in a table containing data for one item.
field (column)
A specific category of data in a table (e.g., name, age).
SQL
Structured Query Language - the standard language for querying databases.
query
A request to retrieve or manipulate data in a database.
SELECT
SQL command to specify which columns to retrieve.
FROM
SQL clause specifying which table to query.
WHERE
SQL clause to filter results based on conditions.
Quick Reference
-- Select specific columns
SELECT column1, column2 FROM table_name;
-- Select all columns
SELECT * FROM table_name;
-- Filter with WHERE
SELECT * FROM table_name WHERE condition;
-- Multiple conditions with AND
SELECT * FROM students
WHERE age > 14 AND house = 'Blue';
-- Multiple conditions with OR
SELECT * FROM students
WHERE house = 'Red' OR house = 'Blue';