Unit 3 - Practice Quiz

INT306 49 Questions
0 Correct 0 Wrong 49 Left
0/49

1 Which SQL aggregate function is used to calculate the total number of rows in a result set?

Aggregate functions Easy
A. NUM()
B. COUNT()
C. SUM()
D. TOTAL()

2 Which function would you use to find the smallest value in a column?

Aggregate functions Easy
A. MIN()
B. MAX()
C. LEAST()
D. BOTTOM()

3 Which type of SQL JOIN returns only the rows where the joined columns in both tables have matching values?

Sql joins Easy
A. INNER JOIN
B. RIGHT JOIN
C. FULL OUTER JOIN
D. LEFT JOIN

4 To retrieve all records from the left table and the matched records from the right table, which JOIN should you use?

Sql joins Easy
A. LEFT JOIN
B. CROSS JOIN
C. INNER JOIN
D. RIGHT JOIN

5 Which SQL set operator combines the results of two SELECT statements and returns only distinct rows?

set operators Easy
A. UNION
B. INTERSECT
C. JOIN
D. UNION ALL

6 Which operator is used to return only the rows that are common to both result sets of two SELECT statements?

set operators Easy
A. MINUS
B. EXCEPT
C. INTERSECT
D. UNION

7 What is a view in a database?

views Easy
A. A virtual table based on the result-set of an SQL statement
B. A backup of a database
C. A physical copy of a table
D. An index on a table

8 Which SQL command is used to create a view?

views Easy
A. SELECT VIEW
B. CREATE VIEW
C. MAKE VIEW
D. ALTER VIEW

9 A subquery in SQL is a query that is...

subqueries Easy
A. always executed first in a batch
B. a query that runs outside the database
C. nested inside another query
D. used only for creating tables

10 In the statement SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);, the part (SELECT AVG(salary) FROM employees) is an example of what?

subqueries Easy
A. A view
B. An aggregate function
C. A subquery
D. A join

11 Which fundamental operation in relational algebra is used to select a subset of columns from a table?

relational algebra Easy
A. Projection ()
B. Cartesian Product ()
C. Union ()
D. Selection ()

12 The Selection operator in relational algebra, denoted by , is equivalent to which SQL clause?

relational algebra Easy
A. SELECT
B. WHERE
C. GROUP BY
D. FROM

13 Which clause is required to use any window function in SQL?

Window functions Easy
A. PARTITION BY()
B. GROUP BY()
C. WINDOW()
D. OVER()

14 What does the ROW_NUMBER() window function do?

Window functions Easy
A. Assigns a unique sequential integer to each row
B. Calculates the rank of a row, without gaps for ties
C. Calculates the rank of a row, with gaps for ties
D. Calculates the total number of rows

15 In database hashing, what is a 'collision'?

Hashing Easy
A. When a transaction fails to commit
B. When two different keys produce the same hash value
C. When the database server crashes
D. When a query returns no results

16 What is the primary purpose of a hash function in a database context?

Hashing Easy
A. To encrypt data for security
B. To compress large data files
C. To sort data alphabetically
D. To map a key to a specific location for faster access

17 What is the primary benefit of creating an index on a table column?

Indexing Easy
A. It reduces the physical storage size of the table
B. It slows down data retrieval operations
C. It enforces data type constraints
D. It speeds up data retrieval operations (SELECT queries)

18 Which SQL statement is used to create a new index?

Indexing Easy
A. MAKE INDEX
B. ADD INDEX
C. NEW INDEX
D. CREATE INDEX

19 What is the main goal of a query optimizer?

Query Optimization Easy
A. To rewrite the query to be shorter
B. To check the SQL syntax for errors
C. To find the most efficient execution plan for a given query
D. To add more data to the query result

20 A 'query execution plan' is best described as:

Query Optimization Easy
A. A graphical representation of the database schema
B. A sequence of steps used by the database to access data for a query
C. A user's plan for writing a complex query
D. A backup plan in case a query fails

21 Consider the following Sales table:

| Salesperson | Product | Amount |
|-------------|---------|--------|
| Alice | Apple | 100 |
| Bob | Orange | 150 |
| Alice | Orange | 200 |
| Charlie | Apple | 120 |
| Bob | Apple | 50 |

What is the result of the following SQL query?

sql
SELECT Salesperson
FROM Sales
GROUP BY Salesperson
HAVING SUM(Amount) > 200 AND COUNT(*) >= 2;

Aggregate functions Medium
A. Alice, Bob
B. Alice
C. The query will result in an error.
D. Bob

22 Given an Employees table where the commission_pct column can contain NULL values. Which of the following statements is true regarding the behavior of COUNT?

Aggregate functions Medium
A. COUNT(DISTINCT commission_pct) will include NULL as one distinct value.
B. COUNT(commission_pct) is equivalent to COUNT(*).
C. COUNT(*) excludes rows where all columns are NULL.
D. COUNT(commission_pct) will count all rows where commission_pct is not NULL.

23 You have two tables, Employees (emp_id, name, dept_id) and Departments (dept_id, dept_name). dept_id in Employees can be NULL if an employee is not assigned to a department. What is the most accurate description of the result of this query?

sql
SELECT e.name, d.dept_name
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.dept_id
WHERE d.dept_name = 'Sales';

Sql joins Medium
A. It lists all employees, including those without a department, and their department name if it's 'Sales'.
B. It lists all employees from the 'Sales' department.
C. It lists all employees and the name of their department if it is 'Sales', otherwise the department name is NULL.
D. It lists all employees from all departments, but only shows the department name for 'Sales'.

24 Consider a table Employee with columns id, name, salary, and manager_id (which is the id of another employee). Which query correctly finds all employees who earn more than their direct managers?

Sql joins Medium
A. sql
SELECT e.name
FROM Employee e
JOIN Employee m ON e.manager_id = m.id
WHERE e.salary > m.salary;
B. sql
SELECT e.name
FROM Employee e, Employee m
WHERE e.manager_id = m.id AND e.salary > m.salary;
C. All of the above queries are functionally equivalent and correct.
D. sql
SELECT e.name
FROM Employee e
WHERE e.salary > (SELECT salary FROM Employee WHERE id = e.manager_id);

25 Table T1 has 5 rows, including 2 rows that are identical. Table T2 has 4 rows. T1 and T2 share 3 common rows (these 3 rows are unique within themselves). How many rows will be returned by (SELECT * FROM T1) UNION (SELECT * FROM T2)?

set operators Medium
A. 4
B. 6
C. 5
D. 9

26 Consider the following Orders table:

| customer_id | order_value | product_category |
|-------------|-------------|------------------|
| 101 | 50 | 'Books' |
| 102 | 200 | 'Electronics' |
| 101 | 150 | 'Electronics' |
| 103 | 80 | 'Books' |
| 101 | 30 | 'Books' |
| 102 | 75 | 'Home Goods' |

What is the result of the following SQL query?

sql
SELECT customer_id
FROM Orders
GROUP BY customer_id
HAVING COUNT(DISTINCT product_category) > 1 AND SUM(order_value) > 200;

Aggregate functions
A. 101, 102
B. No rows are returned.
C. 101
D. 102

27 Given a table Projects with columns project_id, employee_id, and completion_date, where completion_date can be NULL if a project is ongoing. Which of the following queries correctly counts the number of projects, the number of completed projects, and the number of distinct employees involved in any project?

Aggregate functions Medium
A. SELECT COUNT(*), COUNT(completion_date), COUNT(DISTINCT employee_id) FROM Projects;
B. SELECT COUNT(*), COUNT(completion_date), COUNT(ALL employee_id) FROM Projects;
C. SELECT COUNT(project_id), COUNT(completion_date), COUNT(employee_id) FROM Projects;
D. SELECT COUNT(project_id), COUNT(*), COUNT(employee_id) FROM Projects;

28 Given TableA (id, value) with rows (1, 'A'), (2, 'B') and TableB (id, value) with rows (2, 'X'), (3, 'Y'). What will be the row count of the result from SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.id = TableB.id?

Sql joins Medium
A. 2
B. 4
C. 1
D. 3

29 You have a table Employees(id, name, manager_id). To list each employee alongside their manager's name, you need to join the table to itself. Which of the following WHERE clauses is necessary to ensure that employees who do not have a manager are excluded from the result set of a self-join?

Sql joins Medium
A. WHERE e.manager_id = m.id
B. WHERE e.id <> m.id
C. WHERE e.manager_id IS NOT NULL
D. WHERE m.id IS NOT NULL

30 Table T1 contains the integer values (10), (20), (20), (30). Table T2 contains (20), (30), (40). How many rows are returned by the query (SELECT * FROM T1) EXCEPT (SELECT * FROM T2)?

set operators Medium
A. 4
B. 2
C. 3
D. 1

31 Given two tables, StudentsA and StudentsB, both with a single column student_id. Which query will return a list of student IDs that are present in one table but NOT in both?

set operators Medium
A. (SELECT student_id FROM StudentsA) INTERSECT (SELECT student_id FROM StudentsB)
B. (SELECT student_id FROM StudentsA) UNION ALL (SELECT student_id FROM StudentsB)
C. (SELECT student_id FROM StudentsA) UNION (SELECT student_id FROM StudentsB)
D. (SELECT student_id FROM StudentsA EXCEPT SELECT student_id FROM StudentsB) UNION (SELECT student_id FROM StudentsB EXCEPT SELECT student_id FROM StudentsA)

32 A view named HighValueCustomers is created with the following definition:

sql
CREATE VIEW HighValueCustomers AS
SELECT customer_id, SUM(order_amount) as total_spent
FROM Orders
GROUP BY customer_id
HAVING SUM(order_amount) > 1000;


Why will an INSERT statement on this view fail?

views Medium
A. Because views are always read-only.
B. Because the view contains a GROUP BY clause and an aggregate function.
C. Because the view does not include all columns from the base table Orders.
D. Because the view has a HAVING clause.

33 What is the primary trade-off when choosing to use a materialized view over a standard view?

views Medium
A. Real-time data access vs. increased storage cost.
B. Increased query performance vs. potential for stale data.
C. Reduced storage cost vs. increased query complexity.
D. Simplified permissions management vs. slower data modification on base tables.

34 Consider the query:

sql
SELECT product_name
FROM Products p
WHERE EXISTS (
SELECT 1
FROM OrderItems oi
WHERE oi.product_id = p.product_id AND oi.quantity > 100
);

What is the most accurate description of what this query does?

subqueries Medium
A. It produces an error because the subquery selects a constant value 1.
B. It selects products that have a total ordered quantity across all orders greater than 100.
C. It selects products that have been ordered in a quantity greater than 100 in at least one order.
D. It selects all products and joins them with orders having a quantity greater than 100.

35 Which of the following is a key difference in how a database system might process a non-correlated subquery using IN versus a correlated subquery using EXISTS?

subqueries Medium
A. The IN subquery is typically executed once, while the EXISTS subquery is executed for each row of the outer query.
B. The IN subquery is generally faster for large inner result sets.
C. The EXISTS subquery must return only one column, while the IN subquery can return multiple columns.
D. The EXISTS subquery cannot handle NULL values, whereas IN can.

36 Which relational algebra expression is equivalent to the following SQL query?

sql
SELECT name, salary
FROM Employee
WHERE dept_id = 10 AND salary > 50000;

relational algebra Medium
A.
B.
C.
D.

37 The operation of finding all tuples that are in relation R or in relation S is a Union (). For this operation to be valid in relational algebra, what condition must relations R and S satisfy?

relational algebra Medium
A. They must have the same number of tuples.
B. They must have at least one common attribute name.
C. They must be union-compatible (same number of attributes and corresponding attributes have the same domain).
D. They must have the same primary key.

38 Given a table of student scores with ties:

| name | score |
|-------|-------|
| Ann | 95 |
| Bob | 90 |
| Chloe | 90 |
| David | 85 |

Which window function, when used as FUNC() OVER (ORDER BY score DESC), would produce the rank sequence 1, 2, 2, 4?

Window functions Medium
A. DENSE_RANK()
B. NTILE(4)
C. RANK()
D. ROW_NUMBER()

39 A query is designed to find the salary difference between each employee and the employee with the next highest salary within the same department. Which pair of window functions is most suitable for this task?

Window functions Medium
A. RANK() and PARTITION BY
B. LEAD() and GROUP BY
C. SUM() and ORDER BY
D. LAG() and PARTITION BY

40 In a dynamic hashing scheme like Extendible Hashing, what is the primary event that triggers a doubling of the directory size?

Hashing Medium
A. When the total number of records doubles.
B. When a bucket that is full splits, and its local depth becomes greater than the global depth of the directory.
C. Whenever a collision occurs, regardless of whether the bucket is full.
D. When a new bucket is added to the hash file.

41 Using a static hash structure with 8 buckets, indexed 0-7, and a hash function h(key) = key mod 8. If linear probing is used to resolve collisions, what bucket will the key 25 be placed in, given that buckets 1, 2, and 3 are already occupied?

Hashing Medium
A. Bucket 3
B. Bucket 4
C. Bucket 2
D. Bucket 1

42 For a query SELECT * FROM Orders WHERE order_date BETWEEN '2023-01-01' AND '2023-01-31';, which type of index on the order_date column would provide the most significant performance benefit?

Indexing Medium
A. A B+ Tree index
B. A Hash index
C. A Full-text index
D. A Spatial index

43 You have a table Users(user_id, country, city, last_login). The most frequent query is SELECT user_id FROM Users WHERE country = 'USA' AND city = 'New York';. Which index would be the most efficient for this query?

Indexing Medium
A. A composite index on (city, country)
B. A composite index on (country, city)
C. Two separate indexes: one on country and one on city
D. A single index on user_id

44 A query optimizer is given the following SQL query:

sql
SELECT e.name
FROM employees e
WHERE e.department_id IN (SELECT d.id FROM departments d WHERE d.location = 'Boston');

Which of the following rewritten queries is a common and effective optimization that a query planner might perform?

Query Optimization Medium
A. The original query is already in its most optimal form.
B. sql
SELECT e.name
FROM employees e
WHERE e.department_id = (SELECT d.id FROM departments d WHERE d.location = 'Boston' LIMIT 1);
C. sql
SELECT e.name
FROM employees e, departments d
WHERE d.location = 'Boston';
D. sql
SELECT e.name
FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE d.location = 'Boston';

45 Consider three relations to be joined: R(100,000 rows), S(500 rows), and T(2,000 rows). The join selectivity between S and T is very high (produces an estimated 10 rows). The join selectivity between R and S is low (produces an estimated 20,000 rows). Which join order would a cost-based optimizer most likely choose?

Query Optimization Medium
A. (R JOIN S) JOIN T
B. The order does not matter for performance.
C. (S JOIN T) JOIN R
D. (R JOIN T) JOIN S

46 Consider the table Sales with columns (ProductID, Region, SaleDate, Amount). Some Amount values are NULL. What does the following query compute?

sql
SELECT
Region,
COUNT() AS A,
COUNT(Amount) AS B,
SUM(Amount) / COUNT(
) AS C,
AVG(Amount) AS D
FROM Sales
GROUP BY Region;

Aggregate functions Hard
A. A and B will always be identical because COUNT treats NULL values like any other value.
B. C and D will always be identical because AVG(X) is syntactically equivalent to SUM(X)/COUNT(X).
C. C will cause a division-by-zero error if a region has sales, while D will return NULL.
D. C represents the average sale amount treating NULLs as zero, while D is the average of non-NULL sale amounts.

47 Given two tables, Employees (ID, Name) and Managers (ID, ManagerID), where Managers.ID and Managers.ManagerID are foreign keys to Employees.ID. You want to find employees who manage at least one person but are not themselves managed by anyone (i.e., they are top-level managers). Which query correctly identifies them?

Managers.ManagerID can be NULL for the top-most manager.

Sql joins Hard
A. sql
SELECT E.Name FROM Employees E
WHERE E.ID IN (SELECT ManagerID FROM Managers)
AND E.ID NOT IN (SELECT ID FROM Managers WHERE ManagerID IS NOT NULL);
B. sql
SELECT E.Name FROM Employees E
WHERE EXISTS (SELECT 1 FROM Managers M WHERE M.ManagerID = E.ID)
AND E.ID NOT IN (SELECT ID FROM Managers);
C. sql
SELECT E.Name
FROM Employees E
JOIN Managers M1 ON E.ID = M1.ManagerID
LEFT JOIN Managers M2 ON E.ID = M2.ID
WHERE M2.ManagerID IS NULL;
D. sql
SELECT E.Name
FROM Employees E, Managers M
WHERE E.ID = M.ManagerID
AND M.ManagerID IS NULL;

48 Consider tables T1(id) with values (1), (2), (3) and T2(id) with values (2), (3), (4). What is the final row count of the following SQL query?

sql
(SELECT id FROM T1 EXCEPT SELECT id FROM T2)
UNION ALL
(SELECT id FROM T2 EXCEPT SELECT id FROM T1)
UNION
(SELECT id FROM T1 INTERSECT SELECT id FROM T2);

set operators Hard
A. 3
B. 2
C. 5
D. 4

49 Consider a query SELECT * FROM A JOIN B ON A.id = B.id WHERE A.val > 10 AND B.val < 100;. The optimizer has a choice between a Nested Loop Join and a Hash Join. Under which scenario would a Hash Join almost certainly be a worse choice than an Indexed Nested Loop Join?

Query Optimization Hard
A. When both tables fit entirely in memory.
B. When the join condition A.id = B.id is highly selective, returning only a few rows.
C. When both tables A and B are very large and no indexes exist.
D. When table A is very small and table B has an index on id and val.