🗄️ 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
1Alice149Red
2Bob1510Blue
3Charlie149Red
4Diana1611Green
5Eve1510Blue
Table: A collection of related data organised in rows and columns (like the students table above)
Record (Row): A single entry in a table (e.g., one student's information)
Field (Column): A specific piece of information (e.g., name, age, house)

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 table

SELECT specifies which columns you want. FROM specifies which table.

SQL Query
SELECT name, age
FROM students;
Result:
nameage
Alice14
Bob15
Charlie14
Diana16
Eve15

SELECT * (All Columns)

Get everything

Use * (asterisk) to select ALL columns from a table.

SQL Query
SELECT *
FROM students;

This returns ALL columns: id, name, age, year_group, house

WHERE (Filter Results)

Only matching rows

WHERE filters the results to only include rows that match a condition.

SQL Query
SELECT name, house
FROM students
WHERE house = 'Red';
Result:
namehouse
AliceRed
CharlieRed

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';
nameagehouse
Alice14Red
Charlie14Red

OR - Either condition can be true

SELECT name, house
FROM students
WHERE house = 'Red' OR house = 'Green';
namehouse
AliceRed
CharlieRed
DianaGreen

SQL Syntax Rules

1

SQL keywords are NOT case-sensitive

SELECT, select, and Select all work the same. Convention is UPPERCASE.

2

Text values need quotes

Use single quotes for text: WHERE name = 'Alice'

3

Numbers don't need quotes

WHERE age = 15 (not '15')

4

Statements end with semicolon

Always end your SQL statements with ;

Learning Path

Follow these activities in order to master 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

SQL 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';