SQL is the standard language for interacting with relational databases. It allows you to create, query, update, and delete data.
Definition
SQL (Structured Query Language) is a declarative programming language used to manage and manipulate relational databases.
SELECT Queries
Worked Example
Basic SELECT:
This query:
• Selects three columns
• From the Student table
• Only where Year equals 12
• Sorted alphabetically by LastName
sql
SELECT FirstName, LastName, Email
FROM Student
WHERE Year = 12
ORDER BY LastName ASC;
• Selects three columns
• From the Student table
• Only where Year equals 12
• Sorted alphabetically by LastName
Worked Example
Using Wildcards:
Wildcards:
• % matches any number of characters
• _ matches exactly one character
• LIKE 'A%' finds anything starting with A
• LIKE '_ones' finds 5-letter words ending in 'ones'
sql
-- Select ALL columns from Student
SELECT * FROM Student;
-- Select products where name starts with 'A'
SELECT * FROM Product WHERE Name LIKE 'A%';
• % matches any number of characters
• _ matches exactly one character
• LIKE 'A%' finds anything starting with A
• LIKE '_ones' finds 5-letter words ending in 'ones'
Combining Conditions with AND and OR
A WHERE clause can test more than one condition. With AND a record is returned only if both conditions are true. With OR it is returned if at least one of them is true.
Worked Example
AND and OR:
sql
-- Year 12 students whose last name begins with S
SELECT FirstName, LastName
FROM Student
WHERE Year = 12 AND LastName LIKE 'S%';
-- Students who are in Year 12 or Year 13
SELECT FirstName, LastName
FROM Student
WHERE Year = 12 OR Year = 13;
JOIN Queries
Definition
A JOIN combines rows from two or more tables based on a related column (usually a foreign key relationship).
Worked Example
Worked Example: JOIN Query
Tables:
• Student: StudentID, Name, TutorID
• Tutor: TutorID, TutorName, Department
Result: Names of students and their tutors, but only for the Computer Science department
Tables:
• Student: StudentID, Name, TutorID
• Tutor: TutorID, TutorName, Department
sql
SELECT Student.Name, Tutor.TutorName, Tutor.Department
FROM Student
JOIN Tutor ON Student.TutorID = Tutor.TutorID
WHERE Tutor.Department = 'Computer Science';
Nested SELECT
Definition
A nested SELECT (also called a subquery) is a SELECT statement written inside another query. The inner query runs first and its result is used by the outer query.
Worked Example
Worked Example: Nested SELECT
How it runs:
• The inner query finds Mr Brown's TutorID in the Tutor table
• The outer query then returns the name of every student who has that TutorID
• The same result could be produced with a JOIN: a nested SELECT is another way to use data from a second table
sql
SELECT Name
FROM Student
WHERE TutorID = (
SELECT TutorID
FROM Tutor
WHERE TutorName = 'Mr Brown'
);
• The inner query finds Mr Brown's TutorID in the Tutor table
• The outer query then returns the name of every student who has that TutorID
• The same result could be produced with a JOIN: a nested SELECT is another way to use data from a second table
Data Modification Commands
Worked Example
INSERT - Add new records:
UPDATE - Modify existing records:
DELETE - Remove records:
CREATE TABLE - Make a new table:
ALTER TABLE - Change table structure:
sql
INSERT INTO Student (Name, Year)
VALUES ('Alice', 12);
sql
UPDATE Student
SET Year = 13
WHERE StudentID = 'S001';
sql
DELETE FROM Student
WHERE Year = 11;
sql
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Year INT,
Email VARCHAR(100)
);
sql
ALTER TABLE Student
ADD Email VARCHAR(100);
DROP
DELETE removes records but leaves the table in place. DROP removes the table itself: its structure and every record in it.
Worked Example
DROP TABLE - Remove a whole table:
Compare with DELETE:
After DELETE with no WHERE clause the Student table still exists but is empty. After DROP there is no Student table at all.
sql
DROP TABLE Student;
sql
DELETE FROM Student;
Key Point
SQL Command Summary:
• SELECT - retrieve data from tables
• INSERT INTO - add new records
• UPDATE - modify existing records
• DELETE FROM - remove records
• CREATE TABLE - create a new table
• ALTER TABLE - modify table structure
• DROP TABLE - delete a table entirely
• JOIN - combine data from multiple tables
• WHERE - filter results by condition
• ORDER BY - sort results (ASC/DESC)
• LIKE - pattern matching with wildcards
• SELECT - retrieve data from tables
• INSERT INTO - add new records
• UPDATE - modify existing records
• DELETE FROM - remove records
• CREATE TABLE - create a new table
• ALTER TABLE - modify table structure
• DROP TABLE - delete a table entirely
• JOIN - combine data from multiple tables
• WHERE - filter results by condition
• ORDER BY - sort results (ASC/DESC)
• LIKE - pattern matching with wildcards