Fall 2026
  • Discord
  • Gradescope
  • Syllabus

On this page

  • Database
  • Question 1
  • Question 2
  • Question 3
  • Question 4
  • Question 5
  • Question 6
  • Question 7
  • Question 8
  • Question 9
  • Question 10
  • Question 11
  • Question 12
  • Question 13
  • Question 14
  • Question 15
  • Question 16
  • Question 17
  • Question 18
  • Question 19
  • Question 20
  • Question 21
  • Question 22
  • Question 23
  • Question 24
  • Question 25
  • Question 26
  • Question 27
  • Question 28
  • Question 29
  • Question 30
  • Question 31
  • Question 32

SQL: Practice Questions

Write the output rows unless the question asks about an error. If it returns no rows, write “No Output”. Separate columns with |. Python answers use Python’s printed format.

Database

SQL questions start with a fresh database containing these rows. Python questions create their own tables.

Ages are as of November 16, 2026. Passwords and messages are made up; the guest’s age is unrecorded (NULL).

users
id username password age
1 Elon Musk marsbars 55
2 Sam Altman moregpus 41
3 Dario Amodei claudepaws 43
4 Sundar Pichai tabhoarder 54
5 Satya Nadella clippyrules 59
6 Tim Cook dongleking 66
7 Demis Hassabis chesssnacks 50
8 Guest whodis NULL
messages
id sender_id message created_at
1 1 Need more GPUs 2026-11-14 14:30
2 2 Need more GPUs 2026-11-14 14:30
3 3 Need more GPUs 2026-11-14 14:33
4 4 Too many tabs 2026-11-15 14:35
5 3 Claude wants snacks 2026-11-16 14:35
6 6 One more dongle 2026-11-11 11:00
7 6 Sent from my fridge 2026-11-17 14:35
8 6 SQL is the best 2026-11-17 15:52
9 7 Checkmate, database 2026-11-17 16:12
10 7 One more query 2026-11-17 15:53

Both id columns are primary keys, and usernames must be unique. username, password, sender_id, message, and created_at cannot be NULL; created_at defaults to the current timestamp. messages.sender_id must refer to an existing users.id.

SQL setup to copy and run
-- Ages as of November 16, 2026. Passwords and messages are fictional.
PRAGMA foreign_keys = ON;

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL UNIQUE,
    password TEXT NOT NULL,
    age INTEGER
);

INSERT INTO users (username, password, age) VALUES
    ('Elon Musk', 'marsbars', 55),
    ('Sam Altman', 'moregpus', 41),
    ('Dario Amodei', 'claudepaws', 43),
    ('Sundar Pichai', 'tabhoarder', 54),
    ('Satya Nadella', 'clippyrules', 59),
    ('Tim Cook', 'dongleking', 66),
    ('Demis Hassabis', 'chesssnacks', 50),
    ('Guest', 'whodis', NULL);

CREATE TABLE messages (
    id INTEGER PRIMARY KEY,
    sender_id INTEGER NOT NULL REFERENCES users(id),
    message TEXT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO messages (sender_id, message, created_at) VALUES
    (1, 'Need more GPUs', '2026-11-14 14:30'),
    (2, 'Need more GPUs', '2026-11-14 14:30'),
    (3, 'Need more GPUs', '2026-11-14 14:33'),
    (4, 'Too many tabs', '2026-11-15 14:35'),
    (3, 'Claude wants snacks', '2026-11-16 14:35'),
    (6, 'One more dongle', '2026-11-11 11:00'),
    (6, 'Sent from my fridge', '2026-11-17 14:35'),
    (6, 'SQL is the best', '2026-11-17 15:52'),
    (7, 'Checkmate, database', '2026-11-17 16:12'),
    (7, 'One more query', '2026-11-17 15:53');

Question 1

Use the database above.

SELECT id FROM users WHERE username = 'Elon Musk';
Answer
1

Question 2

Use the database above.

Does this insert succeed? If not, which rule does it violate?

INSERT INTO users (id, username, password) VALUES (1, 'Jensen Huang', 'gpugoblin');
Answer
Rejected: id 1 already exists. A primary key must be unique.

Question 3

Use the database above.

SELECT id FROM users WHERE username LIKE 'elon musk';
Answer
1

Question 4

Use the database above.

SELECT id FROM users WHERE age < 45 OR age > 60 ORDER BY id;
Answer
2
3
6

Question 5

Use the database above.

SELECT id FROM users WHERE username LIKE 'S%' ORDER BY id;
Answer
2
4
5

Question 6

Use the database above.

Does this insert succeed? If not, which rule does it violate?

INSERT INTO users (username, password) VALUES (NULL, 'beepboop');
Answer
Rejected: username is NULL, but the column requires a value (NOT NULL).

Question 7

Use the database above.

SELECT id FROM users WHERE username LIKE '%a%' ORDER BY username;
Answer
3
7
2
5
4

Question 8

Use the database above.

SELECT id FROM users WHERE age IS NULL;
Answer
8

Question 9

Use the database above.

Does this insert succeed? If not, which rule does it violate?

INSERT INTO messages (sender_id, message) VALUES (99, 'Hello');
Answer
Rejected: no user has id 99. The foreign key requires an existing user.

Question 10

Use the database above.

SELECT id FROM users ORDER BY age ASC LIMIT 4;
Answer
8
2
3
7

Question 11

Use the database above.

SELECT username, age FROM users WHERE id = 3;
Answer
Dario Amodei|43

Question 12

Use the database above.

SELECT username FROM users WHERE age >= 55 ORDER BY age DESC;
Answer
Tim Cook
Satya Nadella
Elon Musk

Question 13

Use the database above.

SELECT id FROM users
WHERE age < 55 AND (username LIKE 'S%' OR username LIKE 'D%')
ORDER BY id;
Answer
2
3
4
7

Question 14

Use the database above.

SELECT sender_id FROM messages
ORDER BY created_at ASC, id DESC LIMIT 3;
Answer
6
2
1

Question 15

Use the database above.

SELECT username FROM users WHERE age IS NOT NULL AND age < 45 ORDER BY age;
Answer
Sam Altman
Dario Amodei

Question 16

Use the database above.

SELECT id FROM users WHERE age != 50 AND age <= 54 ORDER BY id;
Answer
2
3
4

Question 17

Use the database above.

SELECT count(*) FROM messages;
Answer
10

Question 18

Use the database above.

INSERT INTO users (username, password, age) VALUES ('Jensen Huang', 'gpugoblin', 63);
SELECT username FROM users WHERE age >= 60 AND age < 65;
Answer
Jensen Huang

Question 19

What does this Python code print?

import sqlite3

connection = sqlite3.connect(':memory:')
connection.execute('CREATE TABLE notes (id INTEGER PRIMARY KEY, text TEXT)')
connection.execute('INSERT INTO notes VALUES (?, ?)', (1, "I'm ready"))
rows = connection.execute('SELECT text FROM notes WHERE id = ?', (1,)).fetchall()
print(rows)
connection.close()
Answer
[("I'm ready",)]

Question 20

Use the database above.

DELETE FROM messages;
SELECT count(*) FROM messages;
Answer
0

Question 21

Use the database above.

DELETE FROM messages WHERE sender_id=1;
SELECT id FROM messages WHERE sender_id <= 2 ORDER BY id;
Answer
2

Question 22

What does this Python code print after reopening the database?

import sqlite3

connection = sqlite3.connect('notes.db')  # A new file for this question.
connection.execute('CREATE TABLE notes (text TEXT)')
connection.execute('INSERT INTO notes VALUES (?)', ('Saved',))
connection.commit()
connection.execute('INSERT INTO notes VALUES (?)', ('Draft',))
connection.close()

connection = sqlite3.connect('notes.db')
print(connection.execute('SELECT text FROM notes').fetchall())
connection.close()
Answer
[('Saved',)]

Question 23

Use the database above.

UPDATE users SET age = 99 WHERE username = 'Nobody';
SELECT age FROM users WHERE id = 3;
Answer
43

Question 24

Use the database above.

UPDATE users SET password='@moregpus';
SELECT count(*) FROM users WHERE password LIKE '@%';
Answer
8

Question 25

Use the database above.

UPDATE users SET username='@elon' WHERE id=1;
SELECT count(*) FROM users WHERE username LIKE '@%';
Answer
1

Question 26

Use the database above.

SELECT users.username, messages.id
FROM messages JOIN users ON messages.sender_id = users.id
WHERE users.id = 3 ORDER BY messages.id;
Answer
Dario Amodei|3
Dario Amodei|5

Question 27

Use the database above.

SELECT users.id FROM users
JOIN messages ON users.id = messages.sender_id
WHERE users.id = 5;
Answer
No Output

Question 28

Use the database above.

SELECT messages.id FROM messages
JOIN users ON messages.sender_id = users.id
WHERE users.age >= 55 ORDER BY messages.id;
Answer
1
6
7
8

Question 29

Use the database above.

SELECT count(*) FROM messages
JOIN users ON messages.sender_id = users.id
WHERE users.age < 55;
Answer
6

Question 30

Use the database above.

SELECT count(*) FROM messages WHERE sender_id = 0;
Answer
0

Question 31

Use the database above.

UPDATE users SET age = age + 2 WHERE age < 55;
UPDATE users SET age = 40 WHERE age < 45;
SELECT id FROM users WHERE age >= 45 AND age <= 55 ORDER BY id;
Answer
1
3
7

Question 32

Use the database above.

DELETE FROM messages WHERE sender_id = 3;
INSERT INTO messages (id, sender_id, message, created_at)
VALUES (11, 3, 'See you soon', '2026-11-18 12:00');
SELECT sender_id FROM messages ORDER BY created_at DESC LIMIT 2;
Answer
3
7