PostgreSQL Database Quiz Questions

Course Name:PostgreSQL
Chapter Name:Chapter 8 - RDBMS Concepts
Lesson Content Link:Cardinality
Current Quiz Count:30
Progress
0%
Q1
True / False

In PostgreSQL, cardinality refers to the number of rows in a table.

Q2
True / False

In PostgreSQL, high cardinality means that a column has many unique values.

Q3
True / False

In PostgreSQL, low cardinality means that a column has few unique values.

Q4
True / False

In PostgreSQL, indexes on high cardinality columns are generally more effective than on low cardinality columns.

Q5
True / False

PostgreSQL uses cardinality information in the query planner to optimize query execution.

Q6
True / False

PostgreSQL’s ANALYZE command updates the statistics about the cardinality of columns in a table.

Q7
True / False

In PostgreSQL, high cardinality columns should always be indexed to improve query performance.

Q8
True / False

In PostgreSQL, composite indexes can be useful for queries involving multiple high cardinality columns.

Q9
True / False

PostgreSQL automatically adjusts query execution plans based on changes in cardinality without requiring manual intervention.

Q10
True / False

PostgreSQL allows the use of expression indexes to handle cases where cardinality needs to be optimized for complex queries.

Q11
Single Choice

What does the term "cardinality" refer to in PostgreSQL?

Q12
Single Choice

In PostgreSQL, how can you determine the cardinality of a table?

Q13
Single Choice

Cardinality is crucial for which of the following PostgreSQL operations?

Q14
Single Choice

Given the following PostgreSQL query, what does the COUNT(DISTINCT column_name) function return?

SQL Code
SELECT COUNT(DISTINCT column_name) FROM table_name;
Q15
Single Choice

How does high cardinality of a column impact PostgreSQL index performance?

Q16
Single Choice

In PostgreSQL, which SQL statement would you use to analyze the cardinality of a table's data distribution?

Q17
Single Choice

In PostgreSQL, what impact does cardinality have on the query planner's choice between a sequential scan and an index scan?

Q18
Single Choice

Which PostgreSQL index type is most beneficial for a column with low cardinality?

Q19
Single Choice

Consider the following PostgreSQL query:

SQL Code
SELECT COUNT(*) FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE region = 'North');
How does cardinality affect the performance of this query?
Q20
Single Choice

In a highly normalized PostgreSQL database, how can cardinality influence the decision to denormalize tables?

Q21
Multiple Choice

What does cardinality refer to in the context of PostgreSQL databases?

SQL Code
Given two tables, 'students' and 'courses', with a many-to-many relationship, create an association table 'enrollments' to track the relationship.

CREATE TABLE students (
 student_id SERIAL PRIMARY KEY,
 student_name VARCHAR(100)
);

CREATE TABLE courses (
 course_id SERIAL PRIMARY KEY,
 course_name VARCHAR(100)
);

CREATE TABLE enrollments (
 student_id INT REFERENCES students(student_id),
 course_id INT REFERENCES courses(course_id),
 PRIMARY KEY (student_id, course_id)
);
Q22
Multiple Choice

How would you describe high cardinality in a PostgreSQL database table?

SQL Code
Create a table 'orders' with a 'customer_id' that has high cardinality, meaning each 'customer_id' is unique.

CREATE TABLE customers (
 customer_id SERIAL PRIMARY KEY,
 customer_name VARCHAR(100)
);

CREATE TABLE orders (
 order_id SERIAL PRIMARY KEY,
 order_date DATE,
 customer_id INT REFERENCES customers(customer_id)
);

INSERT INTO customers (customer_name) VALUES ('John Doe');
INSERT INTO orders (order_date, customer_id) VALUES ('2024-01-01', 1);
Q23
Multiple Choice

What impact does cardinality have on database performance in PostgreSQL?

SQL Code
Create an index on a column 'email' in a 'users' table, considering the impact of cardinality on performance.

CREATE TABLE users (
 user_id SERIAL PRIMARY KEY,
 email VARCHAR(100) UNIQUE,
 username VARCHAR(50)
);

CREATE INDEX idx_email ON users(email);
Q24
Multiple Choice

How do you define cardinality in a many-to-many relationship in PostgreSQL?

SQL Code
Create a many-to-many relationship between 'authors' and 'books' using an association table 'author_books'.

CREATE TABLE authors (
 author_id SERIAL PRIMARY KEY,
 author_name VARCHAR(100)
);

CREATE TABLE books (
 book_id SERIAL PRIMARY KEY,
 book_title VARCHAR(100)
);

CREATE TABLE author_books (
 author_id INT REFERENCES authors(author_id),
 book_id INT REFERENCES books(book_id),
 PRIMARY KEY (author_id, book_id)
);
Q25
Multiple Choice

What is the difference between one-to-one and one-to-many cardinality in PostgreSQL?

SQL Code
Create two tables, 'employees' and 'employee_details', where each employee has one corresponding record in 'employee_details' (one-to-one). Then create another table 'departments' with a one-to-many relationship with 'employees'.

CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 employee_name VARCHAR(100)
);

CREATE TABLE employee_details (
 detail_id SERIAL PRIMARY KEY,
 employee_id INT REFERENCES employees(employee_id) UNIQUE,
 address VARCHAR(200)
);

CREATE TABLE departments (
 department_id SERIAL PRIMARY KEY,
 department_name VARCHAR(100)
);

ALTER TABLE employees ADD department_id INT REFERENCES departments(department_id);
Q26
Multiple Choice

How do you ensure referential integrity in a one-to-many relationship in PostgreSQL?

SQL Code
Create a 'teams' table and a 'players' table where each team can have multiple players (one-to-many), ensuring referential integrity.

CREATE TABLE teams (
 team_id SERIAL PRIMARY KEY,
 team_name VARCHAR(100)
);

CREATE TABLE players (
 player_id SERIAL PRIMARY KEY,
 player_name VARCHAR(100),
 team_id INT REFERENCES teams(team_id)
);
Q27
Multiple Choice

What role does cardinality play in optimizing SQL queries in PostgreSQL?

SQL Code
Analyze a query that joins two tables with different cardinalities and suggest an index to improve performance.

CREATE TABLE customers (
 customer_id SERIAL PRIMARY KEY,
 customer_name VARCHAR(100)
);

CREATE TABLE orders (
 order_id SERIAL PRIMARY KEY,
 order_date DATE,
 customer_id INT REFERENCES customers(customer_id)
);

-- Query to optimize
SELECT customer_name, order_date
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id;

Suggestion: Create an index on orders.customer_id to optimize the join
CREATE INDEX idx_orders_customer_id ON orders(customer_id);
Q28
Multiple Choice

How do you model a many-to-many relationship with cardinality in PostgreSQL?

SQL Code
Given 'students' and 'courses', create a many-to-many relationship with a cardinality constraint using an association table.

CREATE TABLE students (
 student_id SERIAL PRIMARY KEY,
 student_name VARCHAR(100)
);

CREATE TABLE courses (
 course_id SERIAL PRIMARY KEY,
 course_name VARCHAR(100)
);

CREATE TABLE enrollments (
 student_id INT REFERENCES students(student_id),
 course_id INT REFERENCES courses(course_id),
 PRIMARY KEY (student_id, course_id)
);
Q29
Multiple Choice

What is the impact of low cardinality on database indexes in PostgreSQL?

SQL Code
Create an index on a column with low cardinality and analyze its effect on query performance.

CREATE TABLE products (
 product_id SERIAL PRIMARY KEY,
 product_name VARCHAR(100),
 category VARCHAR(50)
);

-- Low cardinality index
CREATE INDEX idx_category ON products(category);

Query to analyze
SELECT product_name FROM products WHERE category = 'Electronics';
Q30
Multiple Choice

How can you handle high cardinality in a large PostgreSQL table to optimize query performance?

SQL Code
Consider a large 'transactions' table. Create a partitioned table to handle high cardinality and improve query performance.

CREATE TABLE transactions (
 transaction_id SERIAL PRIMARY KEY,
 transaction_date DATE,
 customer_id INT
) PARTITION BY RANGE (transaction_date);

CREATE TABLE transactions_2024 PARTITION OF transactions
FOR VALUES FROM ('2024-01-01') TO ('2024-12-31');

CREATE TABLE transactions_2025 PARTITION OF transactions
FOR VALUES FROM ('2025-01-01') TO ('2025-12-31');