SQL and Database Fundamentals
- 85Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What do the SELECT and FROM clauses specify in a SQL query?
SELECT specifies which columns to return; FROM specifies the table or tables to retrieve them from.
Or swipe the card
85 cards
What do the
SELECTandFROMclauses specify in a SQL query?SELECTspecifies which columns to return;FROMspecifies the table or tables to retrieve them from.What does the
*wildcard mean in aSELECTclause?It requests all columns from the specified table or tables.
Why should you generally avoid
SELECT *in production queries or on large tables?It retrieves every column, increasing data read, memory use, and query cost; listing needed columns is usually more efficient and clear.
What does the
WHEREclause do?It filters individual rows, returning only those that meet the specified condition.
What kinds of comparisons can SQL operators in a
WHEREclause make?They can test equality or inequality and compare values as less than, greater than, or otherwise ordered.
How does the
LIKEoperator find text patterns, and what does%mean?LIKEmatches a text pattern;%matches any sequence of characters, including no characters.When does
ANDinclude a row in the query results?Only when every condition joined by
ANDis true.When does
ORinclude a row in the query results?When at least one condition joined by
ORis true.Why should parentheses be used when a
WHEREclause mixesANDandOR?They make the intended grouping explicit and prevent SQL's operator precedence from producing unexpected results.
What does the
NOToperator do in a condition?It reverses the truth of the condition.
What does
SELECT DISTINCTdo?It removes duplicate result rows and returns only unique values or combinations.
When
DISTINCTis used with multiple selected columns, what counts as a duplicate?A row is a duplicate only when the entire combination of selected column values is repeated.
What is the default sort order of
ORDER BY?Ascending: text from A to Z, numbers from smallest to largest, and dates from earliest to latest.
How can
ORDER BYsort by multiple columns, including in different directions?List the columns in priority order and specify
ASCorDESCfor each as needed.Does
ORDER BYchange which rows a query returns?No. It changes only the order in which the results are presented.
What is the purpose of
GROUP BY?It collects rows with matching values in specified columns into groups so calculations can be performed for each group.
What do the SQL aggregate functions
COUNT,SUM,AVG,MIN, andMAXcalculate?COUNTcounts rows;SUMtotals numeric values;AVGcalculates their average;MINfinds the minimum;MAXfinds the maximum.How can a query calculate total revenue for each product category?
Group rows by category and sum the revenue expression, such as price multiplied by quantity, for each group.
What does the
HAVINGclause do?It filters groups after
GROUP BYand aggregation have been performed.How do
WHEREandHAVINGdiffer?WHEREfilters individual rows before grouping;HAVINGfilters aggregated groups after grouping.Why are
GROUP BYandHAVINGimportant for business intelligence and reporting?They let analysts summarize data into groups and filter those summaries to answer questions about performance.
What is database normalization?
Organizing data into multiple related tables to minimize repetition and keep information consistent.
Why can storing customer details repeatedly in a large sales table cause problems?
Repeated details waste storage and can become inconsistent when an update is missed in some rows.
What is a primary key?
A column, or set of columns, that uniquely identifies each row in a table.
What is a foreign key?
A column in one table whose values refer to a key, typically the primary key, in another table.
How can keys connect customer, sale, product, and manufacturer data?
A sale can reference customer and product IDs; a product can reference a manufacturer ID.
What rows does an
INNER JOINreturn?Only rows with matching values in both joined tables according to the join condition.
What does the
ONclause specify in a join?The condition that identifies how rows in the joined tables relate, often by matching a foreign key to a primary key.
Why is it usually better to name specific columns rather than use
SELECT *in a join?It avoids unnecessary columns, makes results clearer, and prevents ambiguity when tables share column names.
How can a query join three or more related tables?
Chain joins by adding a join condition for each relationship between the tables.
What are table aliases, and why are they useful?
Aliases are temporary short names for tables within a query; they make multi-table queries shorter and easier to read.
What does a
LEFT JOINreturn?All rows from the left table and matching rows from the right table; unmatched right-side columns are
NULL.How do
RIGHT JOINandFULL OUTER JOINhandle unmatched rows?RIGHT JOINkeeps every row from the right table;FULL OUTER JOINkeeps rows from both tables. Missing matches are represented byNULLvalues.What is a Cartesian product, and why can an incorrect or missing join condition cause one?
It combines every row from one table with every row from another, potentially creating an enormous result.
Why is an explicit join generally preferable to a
NATURAL JOIN?An explicit join states the matching columns directly, while a natural join guesses based on shared column names and can produce surprises.
What is DML, and which commands discussed here are examples?
Data Manipulation Language changes data in tables;
INSERT,UPDATE, andDELETEare DML commands.What is the basic purpose and structure of an
INSERTstatement?It adds rows to a table, typically using
INSERT INTOwith an explicit list of columns followed byVALUES.Why is it best practice to explicitly list columns in an
INSERTstatement?It improves clarity and helps the statement remain valid if the table's column structure changes.
What happens to columns omitted from an
INSERTstatement?They receive their defined default value, if any; otherwise, they typically receive
NULLif allowed.What does
NULLmean in a database?The value is unknown, not applicable, or missing; it does not mean zero or an empty string.
How can
INSERTcopy selected rows from one table into another?Use
INSERT INTOthe destination table followed by aSELECTthat returns the rows to insert.How do primary-key and foreign-key constraints protect data during insertion?
They reject duplicate primary-key values and foreign-key values that do not refer to an existing related record.
What does an
UPDATEstatement do, and how does itsWHEREclause help?It changes specified column values in existing rows;
WHEREidentifies which rows to change.What is the risk of running
UPDATEwithout aWHEREclause?Every row in the table is updated.
What safety check should you perform before running a data-changing
UPDATE?Run a
SELECTusing the same condition to preview the rows and values that the update will affect.Why should you check the database system's documentation before using a multi-table
UPDATE?Syntax and support for updating across joined tables differ between database systems.
What does
DELETE FROMdo, and what is the role of itsWHEREclause?It removes rows from a table;
WHEREspecifies which rows to remove.What happens if
DELETEis run without aWHEREclause?Every row is removed, though the table structure remains.
When might deleted rows be recoverable?
They may be recoverable from a backup or by rolling back a transaction; otherwise, deletion is typically permanent.
How does
DELETEdiffer fromDROP TABLE?DELETEremoves rows but leaves the table structure;DROP TABLEremoves the table structure and its data.What is DDL, and how does it differ from DML?
Data Definition Language changes database structures; DML changes data within tables.
What precautions should be taken before dropping a table?
Make a reliable backup, check dependencies, and test the operation in a non-production environment.
What is the purpose of
DROP TABLE IF EXISTS?It avoids an error when the named table does not exist.
In what order should related tables be dropped when one references another with a foreign key?
Drop the referencing child table before the referenced parent table.
What does a
RESTRICTforeign-key delete rule do?It prevents deleting a parent row while related child rows still exist.
What does
ON DELETE CASCADEdo, and what is its main risk?It automatically deletes related child rows when a parent row is deleted; it can remove large amounts of data.
What do
SET NULLandSET DEFAULTforeign-key delete rules do?They set the child row's foreign-key value to
NULLor a predefined default when the referenced parent is deleted.What is a soft delete?
Instead of physically deleting a row, update a status field or deletion timestamp so applications can hide it while retaining the data.
In what order should correctness, readability, and optimization be prioritized when writing SQL?
Correctness first, readability second, and optimization third.
What practices can improve the readability of SQL queries?
Explain complex logic with comments, use consistent aliases and explicit joins, and format columns and conditions consistently.
What is the conceptual processing order of a typical SQL query?
FROMand joins,WHERE,GROUP BY,HAVING,SELECT,UNIONif used, thenORDER BY.Why can filtering rows early improve query performance?
Fewer rows need to pass through later operations such as joins, grouping, and sorting.
What is a database index, and how does it speed up searches?
It is a maintained data structure that stores indexed values with pointers to table rows, allowing matching rows to be located without scanning the entire table.
Which columns are common candidates for indexes?
Columns frequently used in
WHEREconditions, join conditions, and sometimesORDER BYclauses.What is the trade-off of adding indexes?
Indexes can speed up reads but slow down inserts, updates, and deletes because the indexes must also be maintained.
What does it mean for a condition to be sargable?
It is written so the database can use an index to find matching rows efficiently.
Why can applying a function to an indexed column in a
WHEREclause make a query slower?The database may need to apply the function to every row before evaluating the condition, preventing effective use of the index.
Why is a date-range condition often more index-friendly than filtering with a function on the date column?
A range comparison leaves the indexed column unmodified, allowing the database to use its index to locate the interval.
How can the placement of a wildcard in a
LIKEpattern affect index use?A leading wildcard, such as
%smith%, usually prevents efficient index use; a trailing wildcard, such asSmith%, can often use an index.What are two practices for optimizing joins?
Join only tables needed for the result or conditions, and filter data before joining when possible.
Why may an
INNER JOINbe faster than an outer join?An inner join does not need to preserve unmatched rows or produce
NULLvalues for them.What is the N+1 query problem, and how can a join help avoid it?
It occurs when an application runs one query to fetch items and then another query for each item; a join can often fetch the related data in one query.
How do
UNIONandUNION ALLdiffer?UNIONremoves duplicate rows;UNION ALLretains them and is generally faster.What information can an execution plan provide when diagnosing a slow query?
It can show access order, index use or table scans, join methods, estimated or actual row counts, and operation costs or times.
What is a common table expression (CTE)?
A named temporary result set defined with
WITHand available for use within a single query.What is denormalization, and why might a database use it?
It intentionally adds controlled redundancy to reduce expensive joins and speed up read-heavy workloads.
What is database partitioning?
Physically dividing a large table into smaller sections based on a criterion such as date range.
Where should SQL scripts that modify or delete data be tested?
In a dedicated non-production environment, never on a production database.
What are the main steps in a repeatable database integration test?
Create the schema, load specific test data, run the code, verify the resulting state, then clean up so the next test starts fresh.
How can a transaction make manual testing of an update or delete safer?
Begin a transaction, make the change, inspect the results, then
COMMITto keep the change orROLLBACKto undo it.What is the difference between
COMMITandROLLBACK?COMMITmakes transaction changes permanent;ROLLBACKundoes changes made since the transaction began.What are examples of tools or environments that can support isolated database testing?
Docker containers can provide isolated database instances, and in-memory databases such as SQLite can support fast unit tests.
What is the ethical difference between correcting a factual error and manipulating historical data?
Correcting a clear error supports accuracy; changing or deleting records to misrepresent past results undermines trust and is unethical.
What is an audit trail, and what information should it record about a significant data change?
An audit trail records who made a change, what changed, when it changed, and why.
Why should direct modifications to production data be rare and traceable?
Approvals and audit trails provide accountability, a verifiable history, and support for data integrity and trust.

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