🌍 CS50x: Introduction to Computer Science · subject
CS50x: Introduction to Computer Science SQL and Databases Syllabus
The SQL and Databases syllabus for CS50x: Introduction to Computer Science in full: 4 chapters and 12 topics, every one listed below, plus 50 SQL and Databases flashcards with answers.
SQL and Databases syllabus: all 4 chapters
Expand any chapter to see its topics and sub-topics.
-
Database Fundamentals
3 topics- Tables and Schemas
- SQL Data Types
- Constraints
-
Querying and Manipulation
4 topics- SELECT Statements
- INSERT, UPDATE, and DELETE
- Keywords and Functions
- JOIN Operations
-
Performance and Concurrency
3 topics- Indexes
- Transactions
- Race Conditions
-
Database Security
2 topics- SQL Injection Attacks
- Parameterized Queries
CS50x: Introduction to Computer Science SQL and Databases flashcards — 50 questions with answers
What is a relational database?
A database that organizes data into one or more tables of rows and columns, where relationships between tables are expressed through shared key values (e.g., foreign keys referencing primary keys).
In database design, what is a schema?
The formal structure (blueprint) of a database: which tables exist, the columns in each table, each column's data type, and the constraints applied to them. In SQLite it is defined with CREATE TABLE statements and viewable with the .schema command.
In a relational table, what do rows and columns each represent?
Each row (record/tuple) represents one entity or instance of the data (e.g., one student); each column (field/attribute) represents one property shared by all rows (e.g., name, year).
What is the general SQL syntax for creating a new table?
CREATE TABLE table_name (column1 TYPE constraints, column2 TYPE constraints, ...); for example: CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
What is SQLite, and why is it commonly used in courses like CS50?
SQLite is a lightweight, serverless relational database engine that stores an entire database in a single file on disk. It requires no separate database server, making it simple to embed in applications and ideal for learning and small projects.
List the five storage classes (data types) supported by SQLite.
NULL, INTEGER, REAL, TEXT, and BLOB.
What is a BLOB in SQL?
A Binary Large OBject: raw binary data stored exactly as input (e.g., images, audio, files), with no interpretation as text or numbers.
What is the difference between the INTEGER and REAL types in SQLite?
INTEGER stores whole numbers (signed integers); REAL stores floating-point numbers (8-byte IEEE floating point), used for values with decimal parts.
What does a NULL value mean in a database column?
The absence of any value for that field — it is not zero and not an empty string; the data is simply missing or unknown.
In SQL databases like MySQL, what is the difference between CHAR(n) and VARCHAR(n)?
CHAR(n) stores fixed-length strings that always occupy exactly n characters (padded if shorter); VARCHAR(n) stores variable-length strings up to a maximum of n characters, using only the space needed.
What is a PRIMARY KEY constraint?
A column (or set of columns) that uniquely identifies each row in a table. Its values must be unique and non-NULL; a table can have only one primary key, and it is often an auto-incrementing integer id.
What is a FOREIGN KEY constraint?
A column in one table whose values refer to the primary key of another table, linking the two tables and enforcing that referenced rows actually exist (referential integrity).
What is the difference between the UNIQUE and NOT NULL column constraints?
UNIQUE requires that no two rows have the same value in that column (duplicates rejected); NOT NULL requires that every row must have some value in that column (NULL rejected). A column can have both, which is essentially what a primary key enforces.
What is referential integrity?
The guarantee that every foreign key value in a table matches an existing primary key value in the referenced table, so no row can point to a nonexistent record.
What SQL statement retrieves all columns of all rows from a table named movies?
SELECT * FROM movies; — the asterisk selects every column, and with no WHERE clause every row is returned.
What is the purpose of the WHERE clause in a SELECT statement?
It filters rows so that only those satisfying a given condition are returned, e.g., SELECT title FROM movies WHERE year = 2011; Conditions can use =, <, >, AND, OR, NOT, etc.
In SQL's LIKE operator, what do the wildcards % and _ match?
% matches any sequence of zero or more characters; _ matches exactly one character. Example: WHERE title LIKE 'Toy Story%' matches all titles beginning with "Toy Story".
What does ORDER BY do, and what is its default sort direction?
It sorts the result rows by one or more columns. The default is ascending (ASC); add DESC for descending, e.g., SELECT * FROM shows ORDER BY rating DESC;
What does the LIMIT clause do in a SQL query?
It restricts the number of rows returned, e.g., SELECT * FROM shows ORDER BY rating DESC LIMIT 10; returns only the top 10 rows.
What does the DISTINCT keyword do in a SELECT statement?
It removes duplicate values from the result, returning each distinct value only once, e.g., SELECT DISTINCT(name) FROM stars;
Show the other 30 SQL and Databases flashcards
What is the general syntax of an INSERT statement?
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); for example: INSERT INTO students (name, year) VALUES ('Alice', 2026);
What is the syntax of an UPDATE statement, and what is the danger of omitting its WHERE clause?
UPDATE table_name SET column = value WHERE condition; Without the WHERE clause, the update is applied to every row in the table, potentially overwriting all data.
What is the difference between DELETE FROM table_name and DROP TABLE table_name?
DELETE FROM removes rows (all rows if no WHERE clause is given) but keeps the table and its schema; DROP TABLE removes the entire table, including its structure, from the database.
What does CRUD stand for, and which SQL commands implement each operation?
Create, Read, Update, Delete — implemented in SQL by INSERT (create), SELECT (read), UPDATE (update), and DELETE (delete).
Name five common SQL aggregate functions and what they compute.
COUNT (number of rows/values), AVG (average of values), MIN (smallest value), MAX (largest value), and SUM (total of values). Example: SELECT AVG(rating) FROM shows;
What does the GROUP BY clause do?
It groups rows that share a value in a column so aggregate functions are computed per group rather than over the whole table, e.g., SELECT show_id, AVG(rating) FROM ratings GROUP BY show_id;
What is the difference between the WHERE and HAVING clauses?
WHERE filters individual rows before grouping and cannot use aggregate functions; HAVING filters groups after GROUP BY and can use aggregates, e.g., HAVING COUNT(*) > 10.
What is a nested query (subquery), and how is it used with IN?
A query placed inside another query, with the inner result feeding the outer one. Example: SELECT title FROM shows WHERE id IN (SELECT show_id FROM ratings WHERE rating >= 9);
What is the difference between COUNT(*) and COUNT(column_name)?
COUNT(*) counts all rows in the result, including rows with NULLs; COUNT(column_name) counts only rows where that column's value is not NULL.
What is the purpose of a JOIN in SQL?
To combine rows from two or more tables into a single result set by matching related values (typically a foreign key in one table with a primary key in another).
What rows does an INNER JOIN return?
Only rows where the join condition matches in both tables; rows in either table without a matching partner are excluded from the result.
What is a LEFT JOIN, and how does it differ from an INNER JOIN?
A LEFT JOIN returns all rows from the left (first) table plus matching rows from the right table; where no match exists, the right table's columns are filled with NULL. An INNER JOIN would drop those unmatched left-table rows entirely.
What do RIGHT JOIN and FULL OUTER JOIN return?
RIGHT JOIN returns all rows from the right table with NULLs for unmatched left-table columns; FULL OUTER JOIN returns all rows from both tables, filling in NULLs wherever there is no match on either side.
Write an example JOIN query linking a shows table to a ratings table on the show's id.
SELECT title, rating FROM shows JOIN ratings ON shows.id = ratings.show_id; The ON clause specifies which columns must match.
How is a many-to-many relationship (e.g., students and courses) represented in a relational database?
With a third junction (join/associative) table containing foreign keys to both tables — e.g., an enrollments table with student_id and course_id columns — since neither original table can hold multiple references directly.
How is a one-to-many relationship implemented in a relational schema?
By placing a foreign key column on the "many" side that references the primary key of the "one" side — e.g., each row in an albums table stores the artist_id of its single artist, while one artist can appear in many album rows.
What is a database index, and what data structure typically implements it?
An auxiliary data structure that lets the database find rows matching a column value quickly without scanning the whole table. It is typically implemented as a B-tree, a balanced wide tree that keeps lookups fast.
What SQL statement creates an index on the title column of a movies table?
CREATE INDEX title_index ON movies (title); Subsequent queries filtering on title can then use the index instead of a full table scan.
In terms of time complexity, how does searching with an index compare to a full table scan?
An index (B-tree) lookup runs in roughly $O(\log n)$ time, whereas a full table scan over $n$ rows takes $O(n)$ time — a dramatic speedup on large tables.
What are the main costs (trade-offs) of adding indexes to a table?
Indexes consume extra storage space, and they slow down INSERT, UPDATE, and DELETE operations because the index structure must be updated whenever the data changes. So they should be created only on columns frequently used in searches/joins.
What is a transaction in a database?
A group of one or more SQL statements executed as a single, indivisible unit of work: either all of its statements take effect, or none do.
What do the ACID properties of transactions stand for, and what does each mean?
Atomicity (all-or-nothing execution), Consistency (the database moves from one valid state to another), Isolation (concurrent transactions do not interfere with each other), and Durability (once committed, changes survive crashes).
What do the SQL statements BEGIN TRANSACTION, COMMIT, and ROLLBACK do?
BEGIN TRANSACTION starts a transaction; COMMIT permanently applies all its changes; ROLLBACK undoes all changes made since the transaction began, restoring the prior state (used when an error or invalid condition occurs).
What does atomicity mean for a transaction, illustrated with a bank transfer?
The transaction cannot be interrupted partway: transferring money must both debit one account and credit the other, or do neither. It is impossible to observe or end up in a state where only one of the two updates happened.
What is a race condition in the context of databases?
A bug that occurs when two or more concurrent operations read and write shared data in an interleaved order, so the final result depends on timing and updates can be lost or incorrect.
Give the classic race-condition example of two users liking the same post at the same time.
Both processes SELECT the current like count (say 2), each computes 2 + 1 = 3, and each UPDATEs the count to 3. One increment is lost: the count should be 4 but is stored as 3, because the second write was based on a stale read.
How do databases prevent race conditions?
By wrapping the read-modify-write sequence in a transaction and/or using locks: the database locks the affected rows so other operations must wait until the transaction commits, making the sequence execute as if it were uninterrupted. (Performing the change in one atomic statement, e.g., UPDATE ... SET likes = likes + 1, also avoids the stale read.)
What is a SQL injection attack?
An attack in which a user supplies input containing SQL syntax that gets concatenated into a query string, causing the database to execute the attacker's SQL rather than treating the input as plain data — potentially bypassing logins, reading, or deleting data.
How could an attacker use a comment sequence like -- (or ' OR '1'='1) to bypass a login query built by string concatenation?
If the app builds SELECT * FROM users WHERE username = '{input}' AND password = '{input}', entering something like malan@harvard.edu'-- closes the string with the quote and comments out the rest of the query (including the password check), so the query returns the user without verifying the password. Similarly, ' OR '1'='1 makes the condition always true.
What is a parameterized (prepared) query, and why does it prevent SQL injection?
A query written with placeholders (e.g., ? or %s) for user input — such as db.execute("SELECT * FROM users WHERE username = ?", username) — where the database library binds the input separately, escaping it and treating it strictly as data, never as executable SQL. Attacker-supplied quotes or comments therefore cannot alter the query's structure.
CS50x: Introduction to Computer Science SQL and Databases FAQ
What is in the CS50x: Introduction to Computer Science SQL and Databases syllabus?
CS50x: Introduction to Computer Science SQL and Databases is split into 4 chapters: Database Fundamentals, Querying and Manipulation, Performance and Concurrency and Database Security. Together they contain 12 topics.
How many chapters are in CS50x: Introduction to Computer Science SQL and Databases?
4 chapters, containing 12 topics. The largest chapter is Querying and Manipulation, with 4 topics.
Are there flashcards for CS50x: Introduction to Computer Science SQL and Databases?
Yes — a 50-card SQL and Databases deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.