1. Home
  2. Explore
  3. Computer Science
  4. CS50 Week 7: Data Tools

Study methods

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

CS50 Week 7: Data Tools

  • 121Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Data Foundations
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

Why is SQL introduced as another programming language?

Tap to flipClick to flipspace

Because different tools are suited to different jobs; SQL is especially useful for working with data stored in databases at scale.

Or swipe the card

121 cards

  1. Why is SQL introduced as another programming language?

    Because different tools are suited to different jobs; SQL is especially useful for working with data stored in databases at scale.

  2. What is a flat-file database?

    A text file containing data, usually organized with delimiters such as commas and newline characters.

  3. What does CSV stand for?

    Comma-separated values.

  4. How do CSV files represent rows and columns in plain text?

    Newline characters represent separate rows, while commas or other delimiters separate columns.

  5. How are commas inside a CSV value handled?

    The value is enclosed in quotation marks so the internal comma is not mistaken for a column separator.

  6. What is a header row in a CSV file?

    The first row that names or describes the columns rather than containing ordinary data.

  7. What is metadata in a tabular dataset?

    Information that describes the actual data, such as column names in a header row.

  8. What columns were present in the collected favorites CSV file?

    Timestamp, language, and problem.

  9. What is the purpose of exporting a spreadsheet as CSV?

    To save the data in a globally portable, non-proprietary text format.

  10. How many data rows were in the example favorites CSV file?

    398 data rows, plus one header row, for 399 total lines.

  11. What is a TSV file?

    A tab-separated values file that uses tab characters instead of commas to separate columns.

  12. Why can spreadsheet designs become fragile?

    Users can easily move, rename, delete, or add columns, causing code that depends on fixed column positions to break.

  13. How does Python's open function correspond to C file operations?

    Python's open is similar to C's fopen, and Python's close corresponds to C's fclose.

  14. What does the with open(...) pattern do in Python?

    It opens a file and automatically closes it when execution leaves the with block.

  15. What is the purpose of Python's csv module?

    It provides functions for reading and working with CSV files.

  16. What does Python's csv.reader return for each row?

    A list containing the values in that row.

  17. Why is next(reader) used after creating a CSV reader?

    To skip the first row, which is the header rather than actual data.

  18. How are Python lists indexed?

    They use zero-based indexing: the first element is at index zero.

  19. What does row[1] represent in the original favorites CSV?

    The language value in the row.

  20. What problem does Python's csv.reader approach have when using numeric indices?

    The code depends on columns remaining in fixed positions.

  21. What does Python's csv.DictReader provide?

    Rows represented as dictionaries whose keys come from the header row.

  22. Why is DictReader more robust than reader?

    It accesses values by column name rather than column position, so column order can change without breaking the code.

  23. How can Python manually count responses for several known languages?

    Create a counter for each language, inspect every row, and increment the matching counter.

  24. What Python operator can replace repeated increment operations such as x++?

    The augmented assignment operator +=, such as x += 1.

  25. What is tuple assignment useful for when initializing counters?

    It allows several variables to be assigned values in one statement.

  26. Why is a dictionary useful for counting values?

    It associates each value, such as a language, with its count.

  27. How can a dictionary-based counter handle a value seen for the first time?

    Initialize its key to one.

  28. How can Python test whether a key exists in a dictionary?

    Use membership syntax such as if favorite in counts.

  29. What happens when Python iterates directly over a dictionary?

    The loop iterates over the dictionary's keys.

  30. What does Python's sorted function do when given a dictionary by default?

    It sorts the dictionary's keys.

  31. How can a dictionary be sorted by its values using sorted?

    Use the dictionary's get method as the sorting key, such as key=counts.get.

  32. How can sorted output be reversed in Python?

    Pass the named argument reverse=True to sorted.

  33. What is a positional argument?

    An argument whose meaning is determined by its position in a function call.

  34. What is a named parameter or keyword argument?

    An argument supplied using an explicit parameter name.

  35. What is Python's Counter?

    A collection type with built-in counting behavior.

  36. What does Counter.most_common() return?

    Key-value pairs ordered from the most common value to the least common value.

  37. How can the same Python counting program analyze problems instead of languages?

    Change the dictionary key from the language field to the problem field.

  38. What is a relational database?

    A database organized into tables that can be related to one another.

  39. What does SQL stand for?

    Structured Query Language.

  40. What does it mean that SQL is declarative?

    The programmer describes what data is wanted rather than specifying procedural loops and conditionals for obtaining it.

  41. What does the CRUD paradigm represent?

    Create, Read, Update, and Delete.

  42. Which SQL keywords correspond to CRUD operations?

    Create or insert data with INSERT, read it with SELECT, modify it with UPDATE, and remove it with DELETE or DROP.

  43. What is a table in a relational database?

    A collection of rows and columns, analogous to a spreadsheet sheet.

  44. What is SQLite?

    A lightweight implementation of SQL that stores databases in files.

  45. How does SQLite differ from SQL?

    SQL is the language standard, while SQLite is a particular program and implementation of that language.

  46. What does the SQLite .schema command show?

    The structure or table-definition commands for a database or specified table.

  47. What does SELECT * FROM favorites; mean?

    Return every column and every row from the favorites table.

  48. What does LIMIT do in a SQL query?

    Restricts the number of rows returned.

  49. What does COUNT(*) calculate?

    The number of rows in the result or table being queried.

  50. Why is COUNT(*) conventionally used to count rows?

    It directly expresses that the goal is to count rows rather than a particular column.

  51. What does DISTINCT do in SQL?

    Returns only unique values, removing duplicates from the result.

  52. How can SQL count the number of unique languages?

    Use a nested function such as COUNT(DISTINCT language).

  53. What does the SQL WHERE clause do?

    Filters rows according to a condition.

  54. How does SQL express equality in a WHERE clause?

    With a single equals sign, such as language = 'C'.

  55. How does SQL combine conditions with logical AND?

    Use the keyword AND, not C's && operator.

  56. What does GROUP BY do?

    Groups rows with the same value so aggregate functions can be applied to each group.

  57. How can SQL rank languages by popularity?

    Select the language and its count, group by language, and order by the count in descending order.

  58. What does ORDER BY do by default?

    Sorts results in ascending order.

  59. How does SQL sort results from largest to smallest?

    Use ORDER BY ... DESC.

  60. What is a SQL alias?

    A temporary alternative name assigned to a column or expression using AS.

  61. How can SQL return only the most popular item?

    Sort by the count in descending order and apply LIMIT 1.

  62. How is a new row inserted into a SQL table?

    Use INSERT INTO table (columns) VALUES (values).

  63. What does SQL NULL represent?

    The absence of a value.

  64. What happens when DELETE FROM table; is executed without a WHERE clause?

    Every row in the table is deleted.

  65. How can selected rows be deleted safely?

    Use DELETE FROM table WHERE condition;.

  66. How does SQL update existing data?

    Use UPDATE table SET column = value WHERE condition;.

  67. Why are unrestricted UPDATE and DELETE statements dangerous?

    They can change or remove all rows, and SQL generally has no built-in undo operation.

  68. What are the five representative SQL data types discussed?

    BLOB, integer, numeric, real, and text.

  69. What is a BLOB?

    A Binary Large Object, generally representing binary data such as a file.

  70. What is the difference between numeric and real SQL data types?

    Numeric represents number-like values such as dates and times, while real represents values with decimal points.

  71. What does a NOT NULL constraint enforce?

    It prevents a column from containing missing NULL values.

  72. What does a UNIQUE constraint enforce?

    It prevents duplicate values in a column.

  73. What is a primary key?

    A column or set of columns whose value uniquely identifies each row in a table.

  74. What is a foreign key?

    A column containing values that refer to primary keys in another table.

  75. Why are unique IDs useful in relational database design?

    They identify records reliably and allow tables to be connected without repeating long text values.

  76. What is database normalization?

    Organizing data to factor out unnecessary duplication and preserve a canonical source of truth.

  77. Why is repeating strings in a database undesirable?

    It wastes storage and can create inconsistencies when one copy is changed but others are not.

  78. What is a one-to-one relationship?

    A relationship in which each record in one table corresponds to one record in another table.

  79. What is a one-to-many relationship?

    A relationship in which one record in one table can correspond to multiple records in another table.

  80. What is a many-to-many relationship?

    A relationship in which each side can be associated with multiple records on the other side.

  81. Why is an intermediary table needed for a many-to-many relationship?

    It stores pairs of related keys, allowing any number of records on either side to be associated.

  82. What was the purpose of the stars table?

    To associate show IDs with person IDs for actors who starred in those shows.

  83. What was the purpose of the writers table?

    To associate shows with the people who wrote them.

  84. What does the SQL JOIN operation do?

    Combines rows from related tables using matching columns.

  85. What is the purpose of the ON clause in a join?

    It specifies the condition used to match rows from the joined tables.

  86. Why is table-qualified column notation useful in SQL?

    It makes clear which table a column comes from and avoids ambiguity.

  87. What happens when tables with a one-to-many relationship are joined?

    The result can contain repeated information for the one-side record.

  88. Why is duplication in a one-to-many join acceptable?

    It occurs only in the temporary result set, not in the stored source tables.

  89. What is a subquery?

    A query nested inside another query.

  90. Why are parentheses important in nested SQL queries?

    They identify the inner query and indicate which operation should be evaluated first.

  91. How can a subquery find the titles of highly rated shows?

    Select titles from shows where the show ID is in a subquery selecting IDs from ratings above the chosen rating threshold.

  92. How can nested queries find the actors in a particular show?

    Find the show's ID, use it to retrieve person IDs from stars, then use those IDs to retrieve names from people.

  93. How can nested queries find all shows featuring a particular actor?

    Find the actor's person ID, use it to find show IDs in stars, then retrieve the matching titles from shows.

  94. How can three tables be joined explicitly?

    Join the first table to the second, then join the second to the third, specifying each matching key with ON.

  95. What is an alternative to explicit JOIN syntax for combining tables?

    List the tables after FROM and use WHERE conditions to match their related columns.

  96. Are SQL keywords case-sensitive?

    No, SQL keywords are generally not case-sensitive.

  97. What is an index in a database?

    A data structure that makes searches and other queries faster.

  98. What data structure commonly underlies a database index in SQLite?

    A B-tree.

  99. Why are B-trees faster than linear search for indexed columns?

    They reduce the number of comparisons needed to locate a value instead of scanning every row from top to bottom.

  100. How is an index created in SQL?

    Use CREATE INDEX index_name ON table(column);.

  101. Why can indexing a frequently searched column improve scalability?

    Queries finish faster, allowing the same server to handle more users and requests per unit of time.

  102. Which columns are automatically indexed in SQLite?

    Primary-key columns.

  103. Are foreign keys indexed automatically in SQLite?

    No, foreign keys are not indexed by default.

  104. What is the trade-off of creating indexes?

    Indexes use additional storage and can slow inserts, updates, and deletes because the index must be maintained.

  105. What does SQLite's .timer ON command do?

    It reports how long database queries take to execute.

  106. How can Python use SQL instead of reading a CSV directly?

    Python can connect to a database, execute SQL queries, and process the returned rows.

  107. What is the purpose of a placeholder such as ? in a SQL query executed from Python?

    It marks a value that will be supplied separately by the program.

  108. What does a SQL query executed from Python return?

    A result set containing zero or more rows.

  109. Why combine Python and SQL in an application?

    SQL efficiently reads and manipulates database data, while Python can provide application logic, interaction, or a user interface.

  110. What is a race condition?

    A situation in which the result depends on the timing or order of concurrent operations.

  111. How can a race condition cause likes to be lost?

    Multiple requests may read the same old like count, increment it independently, and overwrite one another with the same new count.

  112. What is a database transaction?

    A group of database operations treated as a single unit.

  113. What does it mean for a transaction to be atomic?

    Its operations occur completely without interruption, or none of them occur.

  114. How can transactions prevent lost updates?

    Wrap the related read and update operations in a transaction and commit them as one atomic unit.

  115. What is SQL injection?

    An attack in which malicious SQL syntax is inserted into user input and becomes part of a database query.

  116. Why is directly interpolating user input into SQL queries unsafe?

    User input can contain quotes, comments, or other SQL syntax that changes the query's behavior.

  117. What does -- mean in SQLite?

    It begins a comment, causing the remainder of the line to be ignored.

  118. How can an unsafe login query be bypassed with SQL injection?

    An attacker can close the username string with a quote and use -- to comment out the password condition.

  119. What is the recommended defense against SQL injection?

    Use parameterized queries with placeholders and supply user values separately.

  120. How are literal single quotes escaped in SQL?

    A single quote is represented by two consecutive single quotes.

  121. Why is DROP especially dangerous in SQL?

    It can delete an entire database table rather than merely removing selected rows.

Card 1 of 121
Card 1 of 121

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent