1. Home
  2. Explore
  3. Computer Science
  4. SQL and Database Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

SQL and Database Fundamentals

  • 85Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: SQL Querying
  • 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?

Tap to flipClick to flipspace

SELECT specifies which columns to return; FROM specifies the table or tables to retrieve them from.

Or swipe the card

85 cards

  1. 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.

  2. What does the * wildcard mean in a SELECT clause?

    It requests all columns from the specified table or tables.

  3. 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.

  4. What does the WHERE clause do?

    It filters individual rows, returning only those that meet the specified condition.

  5. What kinds of comparisons can SQL operators in a WHERE clause make?

    They can test equality or inequality and compare values as less than, greater than, or otherwise ordered.

  6. How does the LIKE operator find text patterns, and what does % mean?

    LIKE matches a text pattern; % matches any sequence of characters, including no characters.

  7. When does AND include a row in the query results?

    Only when every condition joined by AND is true.

  8. When does OR include a row in the query results?

    When at least one condition joined by OR is true.

  9. Why should parentheses be used when a WHERE clause mixes AND and OR?

    They make the intended grouping explicit and prevent SQL's operator precedence from producing unexpected results.

  10. What does the NOT operator do in a condition?

    It reverses the truth of the condition.

  11. What does SELECT DISTINCT do?

    It removes duplicate result rows and returns only unique values or combinations.

  12. When DISTINCT is 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.

  13. 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.

  14. How can ORDER BY sort by multiple columns, including in different directions?

    List the columns in priority order and specify ASC or DESC for each as needed.

  15. Does ORDER BY change which rows a query returns?

    No. It changes only the order in which the results are presented.

  16. 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.

  17. What do the SQL aggregate functions COUNT, SUM, AVG, MIN, and MAX calculate?

    COUNT counts rows; SUM totals numeric values; AVG calculates their average; MIN finds the minimum; MAX finds the maximum.

  18. 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.

  19. What does the HAVING clause do?

    It filters groups after GROUP BY and aggregation have been performed.

  20. How do WHERE and HAVING differ?

    WHERE filters individual rows before grouping; HAVING filters aggregated groups after grouping.

  21. Why are GROUP BY and HAVING important for business intelligence and reporting?

    They let analysts summarize data into groups and filter those summaries to answer questions about performance.

  22. What is database normalization?

    Organizing data into multiple related tables to minimize repetition and keep information consistent.

  23. 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.

  24. What is a primary key?

    A column, or set of columns, that uniquely identifies each row in a table.

  25. What is a foreign key?

    A column in one table whose values refer to a key, typically the primary key, in another table.

  26. 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.

  27. What rows does an INNER JOIN return?

    Only rows with matching values in both joined tables according to the join condition.

  28. What does the ON clause 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.

  29. 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.

  30. How can a query join three or more related tables?

    Chain joins by adding a join condition for each relationship between the tables.

  31. 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.

  32. What does a LEFT JOIN return?

    All rows from the left table and matching rows from the right table; unmatched right-side columns are NULL.

  33. How do RIGHT JOIN and FULL OUTER JOIN handle unmatched rows?

    RIGHT JOIN keeps every row from the right table; FULL OUTER JOIN keeps rows from both tables. Missing matches are represented by NULL values.

  34. 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.

  35. 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.

  36. What is DML, and which commands discussed here are examples?

    Data Manipulation Language changes data in tables; INSERT, UPDATE, and DELETE are DML commands.

  37. What is the basic purpose and structure of an INSERT statement?

    It adds rows to a table, typically using INSERT INTO with an explicit list of columns followed by VALUES.

  38. Why is it best practice to explicitly list columns in an INSERT statement?

    It improves clarity and helps the statement remain valid if the table's column structure changes.

  39. What happens to columns omitted from an INSERT statement?

    They receive their defined default value, if any; otherwise, they typically receive NULL if allowed.

  40. What does NULL mean in a database?

    The value is unknown, not applicable, or missing; it does not mean zero or an empty string.

  41. How can INSERT copy selected rows from one table into another?

    Use INSERT INTO the destination table followed by a SELECT that returns the rows to insert.

  42. 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.

  43. What does an UPDATE statement do, and how does its WHERE clause help?

    It changes specified column values in existing rows; WHERE identifies which rows to change.

  44. What is the risk of running UPDATE without a WHERE clause?

    Every row in the table is updated.

  45. What safety check should you perform before running a data-changing UPDATE?

    Run a SELECT using the same condition to preview the rows and values that the update will affect.

  46. 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.

  47. What does DELETE FROM do, and what is the role of its WHERE clause?

    It removes rows from a table; WHERE specifies which rows to remove.

  48. What happens if DELETE is run without a WHERE clause?

    Every row is removed, though the table structure remains.

  49. When might deleted rows be recoverable?

    They may be recoverable from a backup or by rolling back a transaction; otherwise, deletion is typically permanent.

  50. How does DELETE differ from DROP TABLE?

    DELETE removes rows but leaves the table structure; DROP TABLE removes the table structure and its data.

  51. What is DDL, and how does it differ from DML?

    Data Definition Language changes database structures; DML changes data within tables.

  52. What precautions should be taken before dropping a table?

    Make a reliable backup, check dependencies, and test the operation in a non-production environment.

  53. What is the purpose of DROP TABLE IF EXISTS?

    It avoids an error when the named table does not exist.

  54. 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.

  55. What does a RESTRICT foreign-key delete rule do?

    It prevents deleting a parent row while related child rows still exist.

  56. What does ON DELETE CASCADE do, 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.

  57. What do SET NULL and SET DEFAULT foreign-key delete rules do?

    They set the child row's foreign-key value to NULL or a predefined default when the referenced parent is deleted.

  58. 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.

  59. In what order should correctness, readability, and optimization be prioritized when writing SQL?

    Correctness first, readability second, and optimization third.

  60. 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.

  61. What is the conceptual processing order of a typical SQL query?

    FROM and joins, WHERE, GROUP BY, HAVING, SELECT, UNION if used, then ORDER BY.

  62. Why can filtering rows early improve query performance?

    Fewer rows need to pass through later operations such as joins, grouping, and sorting.

  63. 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.

  64. Which columns are common candidates for indexes?

    Columns frequently used in WHERE conditions, join conditions, and sometimes ORDER BY clauses.

  65. 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.

  66. 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.

  67. Why can applying a function to an indexed column in a WHERE clause 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.

  68. 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.

  69. How can the placement of a wildcard in a LIKE pattern affect index use?

    A leading wildcard, such as %smith%, usually prevents efficient index use; a trailing wildcard, such as Smith%, can often use an index.

  70. What are two practices for optimizing joins?

    Join only tables needed for the result or conditions, and filter data before joining when possible.

  71. Why may an INNER JOIN be faster than an outer join?

    An inner join does not need to preserve unmatched rows or produce NULL values for them.

  72. 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.

  73. How do UNION and UNION ALL differ?

    UNION removes duplicate rows; UNION ALL retains them and is generally faster.

  74. 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.

  75. What is a common table expression (CTE)?

    A named temporary result set defined with WITH and available for use within a single query.

  76. 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.

  77. What is database partitioning?

    Physically dividing a large table into smaller sections based on a criterion such as date range.

  78. Where should SQL scripts that modify or delete data be tested?

    In a dedicated non-production environment, never on a production database.

  79. 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.

  80. How can a transaction make manual testing of an update or delete safer?

    Begin a transaction, make the change, inspect the results, then COMMIT to keep the change or ROLLBACK to undo it.

  81. What is the difference between COMMIT and ROLLBACK?

    COMMIT makes transaction changes permanent; ROLLBACK undoes changes made since the transaction began.

  82. 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.

  83. 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.

  84. 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.

  85. 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.

Card 1 of 85
Card 1 of 85

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent