SQL Fundamentals
- 131Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What does SQL stand for, and what is it used for?
SQL stands for Structured Query Language. It is used to communicate with databases and retrieve, create, update, and delete data.
Or swipe the card
131 cards
What does SQL stand for, and what is it used for?
SQL stands for Structured Query Language. It is used to communicate with databases and retrieve, create, update, and delete data.
What is a database?
A database is software that makes it easier to store and manage large amounts of data in a computer’s file system.
How does SQL differ from manually using a spreadsheet?
SQL lets developers write code that automates database operations instead of manually pointing, clicking, dragging, and dropping data.
What is a SQL result set?
A SQL result set is the table-like collection of rows and columns returned by a query.
Which SQL statement is primarily used for a read operation?
The SELECT statement.
How are SQL keywords treated with respect to capitalization?
SQL keywords are generally case insensitive, although uppercase keywords are a common convention.
How does a SELECT statement retrieve one column?
Specify the column after
SELECT, followed by the table afterFROM; for example,SELECT id FROM users;.How does a SELECT statement retrieve multiple columns?
List the column names after
SELECT, separated by commas; for example,SELECT id, name FROM users;.What does the
*operator mean in a SELECT statement?It is a wildcard that selects all columns from the specified table.
How do SQL statements conventionally end?
SQL statements conventionally end with a semicolon.
What is a SQL dialect?
A SQL dialect is a database-specific variation of SQL that may support different data types, features, or syntax.
What is the main difference between SQL and NoSQL databases?
SQL databases support Structured Query Language; NoSQL databases do not necessarily use SQL and may use different query languages.
Why are SQL databases generally more similar to one another than NoSQL databases?
SQL databases share SQL as a common interface, whereas NoSQL databases do not all use the same query language.
What type of database is often a good starting point for a simple web application?
A general-purpose SQL database such as PostgreSQL.
What is SQLite especially well suited for?
Embedded, small, or resource-constrained applications, such as mobile or edge deployments.
How does PostgreSQL generally differ from SQLite?
PostgreSQL runs as a database server, supports larger-scale applications, and offers more features and stricter type checking than SQLite.
What is a common use of Redis?
Caching and in-memory key-value storage.
What is Elasticsearch commonly used for?
Efficient search and aggregations.
What are common use cases for Firebase and DynamoDB?
Firebase is especially associated with real-time and mobile applications; DynamoDB is Amazon’s database for data at scale.
What does a database table contain?
A table contains columns, which define attributes, and rows, which contain individual records.
What is a database schema?
A schema is the structure used to organize data in a database, including its tables, columns, data types, and relationships.
What does the
CREATE TABLEstatement do?It creates a new table and defines its columns, data types, and optional constraints.
What is the general structure of a
CREATE TABLEstatement?CREATE TABLE table_name (column_name data_type, ...);What is SQLite’s Boolean representation?
SQLite stores Boolean values as integers: 0 for false and 1 for true.
What are SQLite’s principal data types?
SQLite supports
NULL, integers, real numbers, text, and blobs.What is a
VARCHARtype used for in databases that support it?It stores text with a specified maximum character length.
What is a
NULLvalue in SQL?NULLrepresents the absence of a value.How is
NULLdifferent from an empty string?An empty string is a string with length zero;
NULLindicates that no value exists.Why is
NULLoften preferred to an empty string, zero, or false when data is missing?It explicitly represents the absence of a value rather than pretending that a meaningful value exists.
What does a
NOT NULLconstraint enforce?It prevents a column from containing
NULLvalues.What does a primary key guarantee?
A primary key identifies rows uniquely, cannot be
NULL, and can occur only once as the primary-key constraint for a table.Why is an
idcolumn commonly used as a primary key instead of an email address?An
idprovides a stable identifier even if business rules later change, such as allowing duplicate email addresses.What does a
UNIQUEconstraint enforce?It prevents duplicate values in a column or duplicate combinations of values across specified columns.
What is a foreign key?
A foreign key is a column whose values reference the primary key of another table.
What does a foreign key constraint enforce?
It ensures that a referenced value exists in the related table.
What is a database migration?
A database migration is an incremental change to a database schema, such as adding, removing, or changing a table or column.
Why are migrations that add a table or column generally considered safer?
Existing application code usually does not depend on the newly added resource, so adding it is unlikely to break current queries.
What is the recommended order for adding a database resource?
First update the database, then deploy code that uses the new table or column.
What is the recommended order for removing a database resource?
First update the application code so it no longer uses the resource, then remove it from the database.
Why are renaming or updating database resources more dangerous than adding or removing them?
Old code expects the old name or structure while new code expects the updated one, creating a period when the two versions may be incompatible.
What are four approaches to handling a schema update that changes names?
Scheduled downtime, deploying database and code changes simultaneously, copying the table and migrating data, or aliasing the table when supported.
What is an up migration?
An up migration moves the database schema forward to a newer state.
What is a down migration?
A down migration reverses a related up migration, usually to roll back an emergency or failed change.
Why should production databases be handled more carefully than application code?
Changing persisted data can be harder to reverse than reverting code, and restoring the previous code may not restore the previous database state.
What does CRUD stand for?
CRUD stands for Create, Read, Update, and Delete.
How do common HTTP methods map to CRUD operations?
POST commonly maps to create, GET to read, PUT to update, and DELETE to delete.
What does the
INSERTstatement do?It adds one or more new rows to a table.
Why does column order matter in an
INSERTstatement?Each value corresponds positionally to the column listed in the statement, so the values must appear in the same order.
What does
AUTOINCREMENTaccomplish?It allows the database to generate successive identifier values automatically, commonly for a primary-key column.
What is a UUID, and how does it differ from an auto-incrementing integer ID?
A UUID is a very large randomly generated identifier; unlike an auto-incrementing ID, it does not advance sequentially.
What does the
UPDATEstatement do?It changes values in existing rows.
Why should an
UPDATEorDELETEstatement usually use a specificWHEREclause?Without a sufficiently specific condition, the statement may modify or remove unintended rows.
What does the
DELETEstatement do?It removes rows from a table.
What is a safe practice before executing a production
DELETEstatement?Run the equivalent
SELECTquery first to verify exactly which rows will be deleted.What is an ORM?
An Object-Relational Mapper is a library that maps programming-language objects or structs to database rows and can generate SQL operations.
What is a major advantage and disadvantage of an ORM?
An ORM makes code less verbose and simpler, but it can reduce low-level control and sometimes generate inefficient SQL.
What is the usual rule of thumb for using an ORM?
Use one in many production projects, but choose based on the project and team and retain the ability to write raw SQL.
What does the
WHEREclause do?It filters rows according to a condition.
Which comparison operator tests equality in SQL?
A single equals sign:
=.How do
IS NULLandIS NOT NULLwork?They test whether a value is absent or present, respectively.
What does the
BETWEENoperator do?It tests whether a value falls within a specified lower and upper bound.
How do the SQL logical operators
ANDandORwork?ANDrequires both conditions to be true;ORrequires at least one condition to be true.What does the
INoperator do?It returns true when a value matches any value in a specified list or result set.
What does the
LIKEoperator provide?It performs pattern-based or partial string matching.
What does
%mean in aLIKEpattern?It matches zero or more characters.
What does
_mean in aLIKEpattern?It matches exactly one character.
What does the
LIMITclause do?It caps the number of rows returned by a query.
Does
LIMIT 5always return five rows?No. It returns at most five rows and may return fewer if the query matches fewer rows.
What does the
ORDER BYclause do?It sorts query results by one or more columns.
What is the difference between ascending and descending order?
Ascending order moves from lower to higher values; descending order moves from higher to lower values.
In what order should
ORDER BYandLIMITgenerally appear conceptually?Sort the full result set first, then limit it to the desired number of rows.
What does
DISTINCTdo?It removes duplicate values from the selected result so that each distinct value appears once.
What is an SQL alias?
An alias is a temporary alternate name assigned to a column or table within a query.
How long does a column alias last?
Only for the duration of the query; it does not rename or modify the database schema.
What is an SQL aggregation?
An aggregation combines multiple rows or values into a summary value.
What does
COUNT(*)return?It returns the number of rows in the result set.
What does
SUMreturn?It returns the total of the values in a specified numeric column.
What do
MAXandMINreturn?MAXreturns the largest value;MINreturns the smallest value in a set.What does
AVGreturn, and how does it treatNULLvalues?AVGreturns the average of the available values and ignoresNULLvalues.Why should raw data generally be stored instead of only storing precomputed aggregates?
Raw data preserves the information needed to calculate new reports or analyses later.
What does
GROUP BYdo?It divides rows into groups based on one or more columns so that aggregate functions can be calculated separately for each group.
What is the difference between
WHEREandHAVING?WHEREfilters individual rows before grouping;HAVINGfilters grouped or aggregated results afterGROUP BY.When should a condition on a specific group identifier use
WHERErather thanHAVING?Use
WHEREbecause the identifier is available before the rows are grouped.What does the
ROUNDfunction do?It rounds a numeric value to a specified precision; if no precision is supplied, SQLite rounds to the nearest whole number.
What is a subquery?
A subquery is a query nested inside another query whose result is used by the outer query.
When is
INuseful with a subquery?It is useful when the nested query can return multiple values and the outer query should match any of them.
What is the difference between a subquery using
=and one usingIN?=is appropriate when the subquery returns one value;INis appropriate when it may return multiple values.Can SQL operate without a backing table?
Yes. SQL can evaluate expressions such as
SELECT 5 + 10 AS sum;without reading from a table.What is a relational database?
A relational database stores data in related tables and provides mechanisms for connecting records across those tables.
What is a non-relational database?
A non-relational database does not generally support the same complex table-based relationships as a relational database.
What are the three main relationship types in a database?
One-to-one, one-to-many, and many-to-many.
How is a one-to-one relationship commonly modeled?
It is usually represented as another column on the same table.
How is a one-to-many relationship usually modeled?
Place a foreign-key column on the table representing the many side.
What does “one-to-many” usually mean in practice?
One record can be related to zero, one, or many records on the other side.
How is a many-to-many relationship modeled in a relational database?
Use a separate joining table containing foreign keys for the two related entities.
What is the purpose of a joining table?
Its purpose is to record relationships between records from two other tables.
Why should a joining table often have a composite unique constraint?
To prevent the same relationship between two entities from being recorded more than once.
What is database normalization intended to accomplish?
It reduces data redundancy and improves data integrity.
What are the four normalization levels discussed?
First normal form, second normal form, third normal form, and Boyce-Codd normal form.
What are the two rules of first normal form?
Every row must have a unique primary key, and there can be no nested tables.
What additional rule defines second normal form?
Every non-key column must depend on the entire primary key, not merely part of a composite primary key.
What additional rule defines third normal form?
Non-key columns must depend only on the primary key.
What additional rule defines Boyce-Codd normal form?
A column that is part of a primary key cannot depend on a column that is not part of the primary key.
In database theory, what can a primary key contain?
A primary key can consist of one or more columns, including a combination called a composite key.
What is data redundancy?
Data redundancy is the unnecessary storage of the same fact in multiple places.
Why is storing a user’s age less reliable than storing their birthday?
Age becomes outdated as time passes, while a birthday remains stable and can be used to calculate the current age.
What should developers generally optimize for before database speed?
Correct modeling, reduced data duplication, and data integrity.
What is an inner join?
An inner join returns rows from both tables only when matching related rows exist in both.
What does the
ONclause specify in a join?It specifies the condition used to match rows from the joined tables.
What is a left join?
A left join returns every row from the left table and matching rows from the right table; missing matches on the right produce
NULLvalues.What is a full join?
A full join returns every row from both tables, matching related rows where possible.
Why is a right join often unnecessary?
A right join can usually be replaced by swapping the table order and using a left join.
What does table namespacing mean in SQL?
It means qualifying a column with its table name, such as
users.name, to identify exactly which table it belongs to.What happens when a join matches multiple rows on the joined side?
The result can contain multiple rows for the same row from the other table—one for each matching relationship.
Can multiple tables be joined in one query?
Yes. A query can chain multiple joins to combine data from more than two tables.
What is a database index?
An index is an in-memory database structure that makes lookups faster.
What lookup complexity can a binary-tree index provide?
A lookup can take O(logn) time instead of scanning O(n) rows.
Why is a primary-key column commonly indexed automatically?
Primary keys are frequently used for lookups, and their uniqueness makes them suitable for efficient indexing.
When should an additional column be indexed?
When the application frequently performs lookups on that column and those lookups are becoming slow.
Why should developers avoid indexing every column?
Indexes consume memory and add work to inserts and other writes because each index must be updated.
How do indexes affect reads and writes?
They generally speed up lookups but can slow down writes.
What is a multi-column index?
It is an index built over two or more columns for queries that commonly filter by that combination.
Why does the order of columns in a multi-column index matter?
The index is organized primarily by its first column, so lookups using that column benefit most directly.
What is denormalization?
Denormalization intentionally introduces duplicated or precomputed data to improve query performance.
When should a database generally be denormalized?
Only after a measurable performance problem demonstrates that denormalization is needed.
What is SQL injection?
SQL injection is a security vulnerability in which attacker-controlled input changes a SQL query’s meaning or adds unintended SQL statements.
How can SQL injection be prevented?
Use parameterized queries or a trusted database library that safely interpolates user-provided values.
What common coding practice creates SQL-injection risk?
Constructing SQL with ordinary string concatenation or unprotected string interpolation.
What is the difference between a database snapshot and an append-only log backup?
A snapshot captures the database state at a point in time; an append-only log records each change so events can be replayed.
What is a soft delete?
A soft delete marks a row as deleted, often with a
deleted_attimestamp, instead of physically removing it.What are two drawbacks of soft deletes?
They add application complexity and can conflict with privacy requirements; retained deleted rows can also make large tables slower.

Ask anything
We've read this whole study set, so questions about it get the best answers.