Section7 - SQL
Section7 - SQL
roster.db
roster.db
… … … …
roster.db
students
id student_name house head
… … … …
Selecting
SELECT *
FROM students;
roster.db
students
id student_name house head
… … … …
SELECT student_name
FROM students;
roster.db
students
id student_name house head
… … … …
SELECT student_name
FROM students
WHERE house = 'Slytherin';
roster.db
students
id student_name house head
… … … …
SELECT student_name
FROM students
WHERE student_name LIKE "%Potter";
Find the names and
houses of students whose
names begin with "H".
Ordering
SELECT *
FROM students;
SELECT *
FROM students
ORDER BY student_name;
SELECT *
FROM students
ORDER BY student_name ASC;
SELECT *
FROM students
ORDER BY student_name DESC;
Sort the student data in
alphabetical order, first by
house and then by
student name.
Limiting
...
LIMIT 1;
Aggregating
SELECT COUNT(*)
FROM students;
Count the number of
students in Gryffindor
Count the number of
students in each house
SELECT house, COUNT(*)
FROM students
GROUP BY house;
Database Design
Design Principles
● Each table should be a collection of a
single entity.
● For example, we should have a different
table for each of students, houses, and
student-house assignments.
roster.db
students
id student_name house head
… … … …
roster.db
students
id student_name
1 Adelaide Murton
2 Adrian Pucey
3 Anthony Goldstein
… …
houses
roster.db id house head
3 Anthony Goldstein
… …
Design Principles
● Each piece of data should be stored in a
single location, and thereafter referred to
by its ID ("primary key").
● For example, we should ensure every
student and house has an ID, then use
those IDs in the house assignments table.
houses
roster.db id house head
3 Anthony Goldstein
assignments
… …
id student_id house_id
1 1 4
… … …
CREATE TABLE houses (
id,
house,
head,
PRIMARY KEY(id)
);
CREATE TABLE houses (
id INTEGER,
house TEXT,
head TEXT,
PRIMARY KEY(id)
);
CREATE TABLE houses (
id INTEGER NOT NULL,
house TEXT NOT NULL,
head TEXT NOT NULL,
PRIMARY KEY(id)
);
NOT NULL
"required"
roster.db
students houses
id student_name house head id house head
… … … …
INSERT INTO tablename (column1, column2)
VALUES (value1, value2);
INSERT INTO houses (house, head)
VALUES ('Gryffindor', 'McGonagall');
roster.db
students houses
id student_name house head id house head
… … … …
…
houses
roster.db id student_name head
3 Anthony Goldstein
assignments
… …
id student_id house_id
1 1 4
… … …
Count the number of
students in Gryffindor
SELECTs
JOINs
SELECTs
JOINs
houses
roster.db id student_name head
3 Anthony Goldstein
assignments
… …
id student_id house_id
1 1 4
… … …
houses
roster.db id student_name head
3 Anthony Goldstein
assignments
… …
id student_id house_id
1 1 4
… … …
SELECT *
FROM songs
WHERE artist_id =
(
SELECT id
FROM houses
WHERE house = 'Gryffindor'
);
houses
roster.db id student_name head
3 Anthony Goldstein
assignments
… …
id student_id house_id
1 1 4
… … …
SELECT COUNT(student_id)
FROM assignments
WHERE house_id =
(
SELECT id
FROM houses
WHERE house = 'Gryffindor'
);
SELECT COUNT(student_id)
FROM assignments
WHERE house_id =
(
1
);
SELECTs
JOINs
Count the number of
students in Gryffindor
houses
roster.db id student_name head
3 Anthony Goldstein
assignments
… …
id student_id house_id
1 1 4
… … …
houses
roster.db id student_name head
3 Anthony Goldstein
assignments
… …
id student_id house_id
1 1 4
… … …
assignments JOIN houses
assignments houses