Database Management Systems Quiz

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary goal of database normalization?

  • To improve query performance by combining tables.
  • To increase data redundancy.
  • To create complex, hard to understand tables.
  • To reduce data redundancy and improve data integrity. (correct)

In what situation is denormalization typically used?

  • When data integrity and consistency are the top priorities.
  • When you need to minimize storage space.
  • When the database is small and simple.
  • When complex queries and joins are slowing down data retrieval. (correct)

Which SQL statement is primarily used for fetching data from a database?

  • SELECT (correct)
  • DELETE
  • INSERT
  • UPDATE

Which of the following is NOT a type of SQL operator?

<p>Spatial Operators (C)</p> Signup and view all the answers

What is a SQL view?

<p>A virtual table created by a SELECT query, without storing data directly. (B)</p> Signup and view all the answers

What is the function of the UNIQUE constraint in SQL?

<p>To prevent duplicate values in a column. (C)</p> Signup and view all the answers

Which of the following SQL JOIN types only returns rows that have matching values in both tables?

<p>INNER JOIN (A)</p> Signup and view all the answers

Which SQL join returns all rows from the left table and matching rows from the right table?

<p>LEFT JOIN (D)</p> Signup and view all the answers

What benefit does an index provide when querying a database?

<p>Allows for direct access to specific rows based on query conditions (D)</p> Signup and view all the answers

What is a PRIMARY KEY in the context of database tables?

<p>A unique identifier for each row in a table (B)</p> Signup and view all the answers

Which of the following is NOT a disadvantage of using indexes in SQL databases?

<p>Faster read operations (B)</p> Signup and view all the answers

How does a clustered index affect the physical storage of data in a table?

<p>It organizes the data based on the order of indexed columns (D)</p> Signup and view all the answers

Which statement accurately describes a FOREIGN KEY?

<p>It establishes a relationship with another table to maintain referential integrity (B)</p> Signup and view all the answers

What is a primary drawback when using indexes during data manipulation operations such as INSERT and UPDATE?

<p>Indexes must be updated, creating additional overhead (B)</p> Signup and view all the answers

In terms of SQL indexing, how many clustered indexes can exist on a table?

<p>One (A)</p> Signup and view all the answers

Which operation benefits most from the presence of an index?

<p>SELECT queries with filtering conditions (D)</p> Signup and view all the answers

What is one strategy to handle deadlocks in SQL databases?

<p>Consistent ordering of resource access (A)</p> Signup and view all the answers

Which characteristic best describes OLTP systems?

<p>Handles large volumes of simple transactions (C)</p> Signup and view all the answers

How can reducing lock contention help manage deadlocks?

<p>By breaking transactions into smaller steps (C)</p> Signup and view all the answers

What is a primary use of a database snapshot?

<p>To provide a stable dataset for testing (C)</p> Signup and view all the answers

What is one advantage of using proper isolation levels in SQL?

<p>They can help reduce deadlock risks (D)</p> Signup and view all the answers

What effect does a denormalized schema have in OLAP systems?

<p>It improves read-heavy workloads (B)</p> Signup and view all the answers

What does deadlock detection in SQL databases involve?

<p>Detecting and terminating one of the conflicting transactions (D)</p> Signup and view all the answers

Which of the following is NOT a method to reduce deadlocks?

<p>Maximizing lock duration (C)</p> Signup and view all the answers

What is a primary function of a non-clustered index in a database?

<p>To reference the physical data while allowing multiple index structures. (C)</p> Signup and view all the answers

Which statement about local temporary tables is true?

<p>They are prefixed with # and are only visible to the session that creates them. (D)</p> Signup and view all the answers

How does a materialized view differ from a standard view in SQL?

<p>A materialized view physically stores data; a standard view does not store data. (A)</p> Signup and view all the answers

What is the primary purpose of a sequence in SQL?

<p>To generate unique numeric values. (B)</p> Signup and view all the answers

What distinguishing feature do global temporary tables have?

<p>They can be accessed by multiple sessions concurrently. (D)</p> Signup and view all the answers

What is a key advantage of using sequences over identity columns in SQL?

<p>Sequences allow for both ascending and descending order values. (B)</p> Signup and view all the answers

Which of the following accurately describes a standard view?

<p>It executes the underlying query each time it is accessed. (C)</p> Signup and view all the answers

What happens to local temporary tables after the session that created them ends?

<p>They are dropped automatically. (A)</p> Signup and view all the answers

When would you consider denormalization in database design?

<p>When you want to improve performance for read-intensive workloads with frequent complex joins. (C)</p> Signup and view all the answers

What is a major disadvantage of denormalization?

<p>It can lead to increased complexity in maintaining data consistency. (D)</p> Signup and view all the answers

Which of these is NOT a key component of a recursive CTE?

<p>Index Member (B)</p> Signup and view all the answers

What is the purpose of the 'Termination Condition' in a recursive CTE?

<p>To ensure that the recursive query eventually stops processing data. (B)</p> Signup and view all the answers

Which type of query is primarily focused on maintaining data integrity and supporting day-to-day operations?

<p>Transactional Query (D)</p> Signup and view all the answers

Which of these is NOT a characteristic of analytical queries?

<p>They prioritize high-throughput and low-latency. (D)</p> Signup and view all the answers

What is a common approach to ensuring data consistency across distributed databases that requires all participating databases to commit changes simultaneously or roll back if any part fails?

<p>Two-Phase Commit (2PC) (A)</p> Signup and view all the answers

Which approach to data consistency in distributed databases allows data to become consistent over time, making it suitable for situations where strong consistency isn't a critical requirement?

<p>Eventual Consistency (A)</p> Signup and view all the answers

Which of these options are considered a key performance metric for database monitoring?

<p>Query duration (C)</p> Signup and view all the answers

What is a major disadvantage of using indexing?

<p>Slower write operations (A)</p> Signup and view all the answers

Which of these tools is primarily used to monitor query performance in MySQL?

<p>EXPLAIN (C)</p> Signup and view all the answers

What is the primary advantage of breaking down complex queries into smaller, staged queries?

<p>Increased query speed (B)</p> Signup and view all the answers

Which of these is NOT a common approach to monitor query performance?

<p>Using a data warehouse for historical analysis (B)</p> Signup and view all the answers

Before you optimize query performance, what is the most important thing to do first?

<p>Identify the causes of slow performance (B)</p> Signup and view all the answers

Denormalization is most beneficial when you need to...

<p>Simplify query logic (C)</p> Signup and view all the answers

When is it best to consider using denormalization instead of indexing?

<p>When complex joins are required for queries (A)</p> Signup and view all the answers

Flashcards

Normalization

Organizing data in a database to reduce redundancy and improve consistency.

Denormalization

Combining normalized tables into larger tables for performance reasons.

SQL Query

A SQL statement used to retrieve, update, or manipulate data in a database.

SELECT Statement

A SQL statement that fetches data from one or more tables based on specified conditions.

Signup and view all the flashcards

SQL View

A virtual table based on a SELECT query, presenting data without storing it.

Signup and view all the flashcards

UNIQUE Constraint

A database constraint that ensures all values in a column or combination of columns are unique.

Signup and view all the flashcards

INNER JOIN

A join that returns rows with matching values in both tables.

Signup and view all the flashcards

LEFT JOIN

A join that returns all rows from the left table and matching rows from the right table.

Signup and view all the flashcards

Key in a Database

A logical concept that enforces rules for uniqueness or relationships in data. Examples include PRIMARY KEY and FOREIGN KEY.

Signup and view all the flashcards

PRIMARY KEY

Unique identifier for each row in a table. Prevents duplicate rows and NULL values in the key column.

Signup and view all the flashcards

FOREIGN KEY

Maintains referential integrity by linking rows in one table to rows in another. Ensures data consistency and prevents data loss during updates or deletes.

Signup and view all the flashcards

Index in a Database

A data structure that speeds up data retrieval operations. Helps find specific rows quickly without scanning the entire table.

Signup and view all the flashcards

Clustered Index

Index that organizes the physical data in the table itself in the order of the indexed column(s). Improves range queries and queries that sort data.

Signup and view all the flashcards

Non-clustered Index

Index that stores the data values and their corresponding row pointers separately from the actual data. A table can have multiple non-clustered indexes.

Signup and view all the flashcards

Disadvantages of using Indexes

Increases storage space for the index structures. Can slow down write operations, bulk data loads, and batch inserts.

Signup and view all the flashcards

Advantages of using Indexes

Indexes improve query performance by enabling faster data retrieval for SELECT queries, JOIN operations, and ORDER BY clauses.

Signup and view all the flashcards

What is a SQL sequence?

A database object that generates a series of unique numeric values, often used for creating primary keys or other columns.

Signup and view all the flashcards

What is a non-clustered index?

A separate structure that stores pointers to the actual table data, allowing for fast lookups based on specific columns.

Signup and view all the flashcards

What are temporary tables?

Tables that exist only for the duration of a session or transaction, used for storing temporary results or performing operations on subsets of data.

Signup and view all the flashcards

What are local temporary tables?

Temporary tables that are only visible to the session that created them. They disappear when the session ends.

Signup and view all the flashcards

What are global temporary tables?

Temporary tables that are visible to all sessions. They disappear when all sessions referencing them close.

Signup and view all the flashcards

What is a materialized view?

A physical table that stores the results of a query, providing faster data retrieval compared to standard views.

Signup and view all the flashcards

What is a standard view?

Virtual tables defined by a query that don't store data, executing the query each time they're accessed. They provide a real-time view of the data.

Signup and view all the flashcards

How does a materialized view differ from a standard view?

A materialized view stores precomputed results, making reads faster but requiring periodic refreshes to keep data up to date. A standard view shows real-time data but requires executing the query each time.

Signup and view all the flashcards

Deadlock

A situation where two or more transactions block each other, waiting for resources held by the others, leading to a standstill.

Signup and view all the flashcards

Deadlock Detection and Retry

A database mechanism that detects deadlocks and terminates one of the involved transactions, allowing the others to proceed.

Signup and view all the flashcards

Reducing Lock Contention

Strategies like using indexes, optimized queries, and breaking transactions into smaller steps to reduce the chances of transactions needing to lock the same resources simultaneously.

Signup and view all the flashcards

Isolation Levels

Setting the appropriate level of isolation for transactions. Lower levels allow for less locking but can increase the risk of conflicts.

Signup and view all the flashcards

Consistent Resource Ordering

Ensuring that transactions acquire resources in a consistent order to prevent cyclical dependencies that lead to deadlocks.

Signup and view all the flashcards

Database Snapshot

A read-only copy of a database at a specific point in time, often used for reporting, backup, or testing.

Signup and view all the flashcards

OLTP (Online Transaction Processing)

A type of database system designed for high-volume, real-time transactions like order entry or banking.

Signup and view all the flashcards

OLAP (Online Analytical Processing)

A system optimized for complex queries and analysis of large datasets, often used for business intelligence and reporting.

Signup and view all the flashcards

What is a Query Execution Plan?

Analyze the steps SQL Server takes to execute a query. Helps spot inefficiencies, missing indexes, or bottlenecks. Similar to a step-by-step guide for your query.

Signup and view all the flashcards

What is Query Duration?

Measures how long a query takes to process, giving insights into performance.

Signup and view all the flashcards

What is an Index in a Database?

Data structures that speed up data retrieval without altering the table's structure. Like an index in a book.

Signup and view all the flashcards

What are Join Types?

Optimizing how data is joined in queries, considering the relationships and performance needs. Similar to choosing the right path for a road trip.

Signup and view all the flashcards

How to Break Down Complex Queries?

Breaking down complex queries into smaller, manageable stages using temporary tables or CTEs. Like breaking a big recipe into smaller tasks.

Signup and view all the flashcards

What is Optimizing Aggregations?

Using SQL functions like SUM, AVG, or COUNT efficiently to aggregate data. Similar to summarizing data in a spreadsheet.

Signup and view all the flashcards

Why is Continuous Query Tuning Important?

Regularly monitoring query performance, identifying slowdowns, and fine-tuning queries as data grows. Similar to monitoring a car's engine for performance issues.

Signup and view all the flashcards

What Tools Can Help Monitor Query Performance?

Using built-in tools to understand query behavior, waiting times, and performance metrics. Similar to a dashboard in a car.

Signup and view all the flashcards

Recursive Query

A type of SQL query that references itself to process hierarchical or tree-structured data, allowing you to traverse data in a recursive manner.

Signup and view all the flashcards

Common Table Expression (CTE)

A feature in SQL that allows you to define a temporary named result set (like a table) without storing it permanently, useful for breaking down complex queries into smaller, reusable parts.

Signup and view all the flashcards

Transactional Query

Queries focused on individual, short-term operations such as inserting, updating, or deleting data, typically used in systems that need quick, frequent transactions.

Signup and view all the flashcards

Analytical Query

Queries designed for exploring and analyzing large datasets, often involving complex calculations and aggregations to gain insights.

Signup and view all the flashcards

Distributed Transaction

A method for ensuring consistency across multiple databases by requiring all participating databases to commit changes simultaneously or roll back if any part fails.

Signup and view all the flashcards

Eventual Consistency

A strategy where data consistency is achieved over time rather than immediately, allowing some degree of data divergence in distributed databases.

Signup and view all the flashcards

Study Notes

SQL Basic Interview Questions

  • SQL is a standard programming language used to communicate with relational databases. It allows users to create, read, update, and delete data, as well as define database schemas and manage security.
  • A database is an organized collection of electronically stored data, typically structured in tables with rows and columns. It's managed by a DBMS (Database Management System).
  • SQL commands are broadly categorized:
    • DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE.
    • DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE.
    • DCL (Data Control Language): GRANT, REVOKE.
    • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT.
  • CHAR data type stores fixed-length data, padding with spaces if needed.
  • VARCHAR2 data type stores variable-length data, saving space when data is shorter than the defined length.
  • A primary key is a unique identifier for each record in a table. It ensures no duplicate values and does not allow NULLS
  • A foreign key is a column (or set of columns) in one table referring to the primary key in another table. This enforces relationships and ensures data integrity.
  • The DEFAULT constraint sets a default value for a column when no other value is provided during insertion.

Database Normalization

  • Normalization is organizing data to reduce redundancy and improve data integrity.
  • Denormalization combines normalized tables into larger tables, sometimes to improve performance if the performance benefits outweigh the redundancy.

SQL Operators

  • Arithmetic operators include (+, -, *, /, %).
  • Comparison operators include (=, !=, <>, >, <, >=, <=).
  • Logical operators include (AND, OR, NOT).
  • Set operators include (UNION, INTERSECT, EXCEPT).
  • Special operators include (BETWEEN, IN, LIKE, IS NULL).

SQL Views

  • Views are virtual tables created from SELECT queries. They don't store data, but present data from one or more source tables structured in a way simpler than the source(s). Views improve readability and security.

UNIQUE Constraints

  • The UNIQUE constraint ensures all values in a column are distinct. It helps maintain data integrity by preventing duplicate values.

SQL Joins

  • INNER JOIN returns only matching rows from both tables.
  • OUTER JOIN (LEFT, RIGHT, FULL) returns all rows from one table and matching rows from another.
  • CROSS JOIN returns the Cartesian product of two tables.

Aggregate Functions

  • Aggregate functions perform calculations on a set of values and return a single value. Common examples include COUNT(), SUM(), AVG(), MIN(), and MAX().

Subqueries

  • Subqueries are queries nested within another query. They are useful for filtering data based on the results of another query.

WHERE vs. HAVING

  • WHERE filters rows before grouping (in a GROUP BY).
  • HAVING filters grouped data (after a GROUP BY).

Indexes

  • Indexes are database objects that improve query performance by allowing faster data retrieval. Think of them as a book's index.

DELETE vs. TRUNCATE

  • DELETE removes rows one-at-a-time.
  • TRUNCATE removes all rows at once and is faster.

ORDER BY

  • The ORDER BY clause sorts the result set of a query either ascending or descending based on one or more columns.

SQL and NoSQL Databases

  • SQL databases use structured tables with rows and columns, and rely on fixed schemas. They offer ACID properties.
  • NoSQL databases use flexible, schema-less structures (e.g. key-value stores, document stores) and are designed for horizontal scaling.

Constraints

  • NOT NULL ensures columns cannot have NULL values.
  • UNIQUE ensures unique values in a column.
  • PRIMARY KEY uniquely identifies each row.
  • FOREIGN KEY enforces referential integrity, connecting to a primary key in another table.
  • CHECK validates data against conditions.
  • DEFAULT sets a default value for columns.

Cursors

  • Cursors are database objects used to retrieve, manipulate, traverse rows one at a time.

Triggers

  • Triggers are sets of SQL statements that automatically execute in response to events on a table.

Stored Procedures

  • Stored procedures are precompiled and compiled SQL statements that are stored in the database.

SQL Data Types

  • Data types define the kind and format of data that can be stored. (e.g. INTEGER, VARCHAR, DATE).

SQL SELECT Statements

  • SELECT retrieves data from one or more tables, offering filtering, sorting, and display options.

NULL Values

  • NULL in SQL represents a missing or unknown value. It is different from zero or an empty string.

Materialized Views

  • Materialized views are physical tables storing results of queries.
  • Compared to standard views, materialized views improve performance, but data needs to be refreshed periodically.

Sequences

  • Sequences are database objects that generate a series of unique numeric values.

Temporary Tables

  • Temporary tables exist only for a session or transaction.

Partitioned Tables

  • Partitioned tables divide data into smaller, more manageable pieces.

ACID Properties

  • Atomicity: All operations in a transaction succeed or fail completely.
  • Consistency: Transactions preserve all database constraints.
  • Isolation: Transactions don't affect each other undesirably
  • Durability: Committed transactions permanently affect results, even in failures

Isolation Levels

  • Read uncommitted: Allows dirty reads.
  • Read committed: Prevents dirty reads but allows non-repeatable reads.
  • Repeatable read: Doesn't allow dirty reads or non-repeatable reads.
  • Serializable: The highest isolation level, preventing all issues above.

Window Functions

  • Window functions perform calculations across a set of related rows, without collapsing the results into aggregate values (unlike aggregate functions).

Data Integrity

  • Data integrity refers to the accuracy, consistency, and reliability of data within the database, and is often maintained using integrity constraints and rules.

SQL EXCEPT

  • The EXCEPT operator returns rows present in the first query (source) but not present in the second query (comparison dataset).

SQL PIVOT

  • The PIVOT operator transforms rows into columns, often used for summarizing data.

Bitmap vs B-Tree Indexes

  • Bitmap indexes work efficiently with low-cardinality data (e.g., yes/no)
  • B-Tree indexes excel with high-cardinality data (e.g., large ranges of numbers).

Query Optimization Techniques

  • Appropriate indexing
  • Avoiding unnecessary joins/subqueries
  • Avoiding SELECT * (select only required columns).
  • Using appropriate operators.
  • Careful use of CTEs/temporary tables.
  • Using appropriate query structure for query type.

Trade-offs of Indexing

  • Faster read speed
  • Slower write speed
  • Additional storage space

Trade-offs of Denormalization

  • Simpler query logic
  • Increased data redundancy/storage space
  • Slower write speed
  • Potentially more complex maintenance

Common Table Expressions (CTEs)

  • temporary reusable in-memory result sets.

Dynamic SQL

  • Code that constructs SQL during execution, instead of being pre-defined.
  • Useful for situations requiring adaptability during runtime
  • Increases flexibility but carries increased risk of vulnerabilities. (e.g. SQL Injection attacks)

Views

  • Virtual, not physical, tables.
  • Create views to hide complexity and improve query organization.

Data Partitioning and Sharding

  • Partitioning: Splits data within a single database to improve query performance. Partitioning remains within a single database.
  • Sharding: Divides data across multiple separate databases for horizontal scaling for high volume, often large, datasets.

Transaction Isolation levels (SQL Server)

  • Read Uncommitted
  • Read Committed
  • Repeatable Read
  • Serializable

Deadlocks

  • A deadlock occurs when two or more transactions wait indefinitely for each other to release locks, preventing further progress.

Databases Snapshots

  • Read-only static views of a database at a specific point in time.

OLTP vs OLAP systems

  • OLTP (Online Transaction Processing): Focus on handling frequent, relatively simple transactions.
  • OLAP (Online Analytical Processing): Emphasis on complex query processing spanning large datasets for data analysis.

SQL Server Hints

  • Using database hints (e.g., WITH (NOLOCK)) can offer speed improvements, but might lead to data inconsistency.

General SQL Best practices

  • Write simple, maintainable queries, minimizing complex structures.
  • Filter early to reduce processing load.
  • Select only necessary columns (avoid selecting *).
  • Choose appropriate indexes to improve query response times.
  • Optimize for frequent operations and lookups.
  • Plan for maintenance and tuning of indexes during database operation periods. (e.g., off-peak hours)

Specific SQL Query Examples (from the text)

  • Find employees earning more than the average salary
  • Fetch duplicate values from a column
  • Find employees who joined within 30 days
  • Fetch top 3 highest earners
  • Delete duplicate rows (without using a ROWID column)
  • Find common records in two tables.
  • Find employees with names starting and ending with ‘A’
  • Display departments with fewer than five employees, ordered by that count (in descending order)
  • Find employees with no manager.
  • Fetch the third and fourth highest-paid employees
  • Fetch records updated during the past hour

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser