Unit 2 - Practice Quiz

INT306 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which of the following is a Data Definition Language (DDL) command in SQL?

introduction to data definition language Easy
A. CREATE
B. SELECT
C. INSERT
D. UPDATE

2 Which SQL statement is used to extract data from a database?

data manipulation Easy
A. GET
B. EXTRACT
C. SELECT
D. OPEN

3 The command to make all changes made during the current transaction permanent is:

data control and transaction control language Easy
A. SAVEPOINT
B. GRANT
C. ROLLBACK
D. COMMIT

4 Which constraint ensures that all values in a column are different?

integrity constraints Easy
A. NOT NULL
B. UNIQUE
C. CHECK
D. PRIMARY KEY

5 A key that uniquely identifies each record in a table and cannot contain NULL values is called a:

database keys Easy
A. Alternate Key
B. Super Key
C. Primary Key
D. Foreign Key

6 In a SELECT statement, which clause is used to filter the results based on a specified condition?

SQL basic operations Easy
A. WHERE
B. GROUP BY
C. ORDER BY
D. FROM

7 To completely remove a table and its structure from the database, which command is used?

introduction to data definition language Easy
A. REMOVE TABLE
B. DROP TABLE
C. ERASE TABLE
D. DELETE TABLE

8 To add a new row of data into a database table, you should use the _____ statement.

data manipulation Easy
A. UPDATE
B. INSERT INTO
C. CREATE ROW
D. ADD INTO

9 Which of the following commands belongs to Data Control Language (DCL)?

data control and transaction control language Easy
A. GRANT
B. COMMIT
C. CREATE
D. UPDATE

10 A constraint that enforces a link between data in two tables is a:

integrity constraints Easy
A. PRIMARY KEY
B. DEFAULT Constraint
C. CHECK Constraint
D. FOREIGN KEY

11 A column or a set of columns in a table that is a candidate for a primary key but was not chosen is called a(n):

database keys Easy
A. Super Key
B. Composite Key
C. Foreign Key
D. Alternate Key

12 What is the basic structure of an SQL SELECT query?

SQL basic operations Easy
A. FROM, SELECT, WHERE
B. SELECT, WHERE, FROM
C. SELECT, FROM, WHERE
D. WHERE, SELECT, FROM

13 Which DDL command is used to modify the structure of an existing database table?

introduction to data definition language Easy
A. CHANGE TABLE
B. UPDATE TABLE
C. MODIFY TABLE
D. ALTER TABLE

14 To change existing data in a table, which DML command is used?

data manipulation Easy
A. MODIFY
B. UPDATE
C. ALTER
D. CHANGE

15 What is the purpose of the ROLLBACK command in SQL?

data control and transaction control language Easy
A. To save a transaction
B. To grant user permissions
C. To create a restore point
D. To undo a transaction that is not yet saved

16 The constraint that ensures a column cannot accept NULL values is:

integrity constraints Easy
A. DEFAULT
B. UNIQUE
C. NOT NULL
D. CHECK

17 What does SQL stand for?

SQL basic operations Easy
A. Structured Question Language
B. Structured Query Language
C. Standard Query Language
D. Simple Query Language

18 Which command removes rows from a table based on a WHERE clause condition?

data manipulation Easy
A. REMOVE
B. TRUNCATE
C. DROP
D. DELETE

19 Which command is used to take back permissions from a user?

data control and transaction control language Easy
A. DENY
B. UNGRANT
C. REVOKE
D. TAKE

20 Any set of one or more attributes that can uniquely identify a tuple (row) in a relation is called a:

database keys Easy
A. Attribute Key
B. Foreign Key
C. Super Key
D. Domain Key

21 A database administrator needs to add a new column named HireDate of type DATE to an existing Employees table. After adding it, they realize it should not accept null values. Which sequence of DDL commands is appropriate?

introduction to data definition language Medium
A. ALTER TABLE Employees ADD HireDate DATE; followed by ALTER TABLE Employees MODIFY HireDate DATE NOT NULL;
B. UPDATE Employees SET HireDate = NOT NULL;
C. ALTER TABLE Employees ADD HireDate DATE NOT NULL; (This will fail if the table already has rows)
D. CREATE TABLE Employees (HireDate DATE NOT NULL);

22 A Products table has a CHECK constraint defined as CHECK (UnitPrice > 0 AND StockLevel >= 0). Which of the following INSERT statements will be rejected by the database?

integrity constraints Medium
A. INSERT INTO Products (ProductName, UnitPrice, StockLevel) VALUES ('Widget', 10.50, 0);
B. INSERT INTO Products (ProductName, UnitPrice, StockLevel) VALUES ('Gadget', -5.00, 100);
C. INSERT INTO Products (ProductName, UnitPrice, StockLevel) VALUES ('Thingamajig', 0.50, 50);
D. INSERT INTO Products (ProductName, UnitPrice, StockLevel) VALUES ('Doohickey', 99.99, 1);

23 A table STUDENTS has the columns StudentID (unique, not null), SSN (unique, not null), and Email (unique, can be null). The DBA chooses StudentID as the Primary Key. What is the correct term for the SSN column in this context?

database keys Medium
A. Foreign Key
B. Composite Key
C. Alternate Key
D. Super Key

24 Which SQL query correctly identifies all departments that have more than 5 employees and also have a total salary budget exceeding $200,000? Assume an Employees table with DepartmentID, EmployeeID, and Salary columns.

data manipulation Medium
A. SELECT DepartmentID FROM Employees GROUP BY DepartmentID WHERE COUNT(EmployeeID) > 5 AND SUM(Salary) > 200000;
B. SELECT DepartmentID FROM Employees GROUP BY DepartmentID HAVING COUNT(EmployeeID) > 5 AND SUM(Salary) > 200000;
C. SELECT DepartmentID FROM Employees HAVING COUNT(EmployeeID) > 5 AND SUM(Salary) > 200000;
D. SELECT DepartmentID FROM Employees WHERE COUNT(EmployeeID) > 5 AND SUM(Salary) > 200000 GROUP BY DepartmentID;

25 Consider the following sequence of TCL commands within a single transaction:
1. START TRANSACTION;
2. UPDATE ...;
3. SAVEPOINT A;
4. INSERT ...;
5. DELETE ...;
6. ROLLBACK TO SAVEPOINT A;
7. COMMIT;
Which operations' effects are permanently saved to the database?

data control and transaction control language Medium
A. The UPDATE and INSERT statements.
B. The UPDATE, INSERT, and DELETE statements.
C. None of the statements.
D. Only the UPDATE statement.

26 What is the primary difference in functionality between TRUNCATE TABLE Sales; and DELETE FROM Sales;?

introduction to data definition language Medium
A. TRUNCATE is a DDL operation that is minimally logged and cannot be easily rolled back, whereas DELETE is a DML operation that logs each row deletion and can be rolled back.
B. TRUNCATE allows a WHERE clause to selectively remove rows, while DELETE removes all rows.
C. TRUNCATE is a DML command that can be rolled back, while DELETE is a DDL command.
D. DELETE removes all rows and the table structure, while TRUNCATE only removes rows.

27 An Orders table contains CustomerID as a foreign key that references the Customers table's primary key (ID). The foreign key is defined with ON DELETE SET NULL. What happens if a row in the Customers table is deleted?

integrity constraints Medium
A. An error is raised, and the transaction is rolled back.
B. All corresponding rows in the Orders table are also deleted.
C. The CustomerID field in all corresponding Orders rows is set to NULL.
D. The deletion is prevented if there are any matching orders.

28 Which query finds all employees whose first name is exactly 4 characters long and ends with the letter 'n'? Consider an Employees table with a FirstName column.

SQL basic operations Medium
A. SELECT * FROM Employees WHERE FirstName LIKE '__n_';
B. SELECT * FROM Employees WHERE FirstName LIKE 'n___';
C. SELECT * FROM Employees WHERE FirstName LIKE '___n';
D. SELECT * FROM Employees WHERE FirstName LIKE '%n' AND LENGTH(FirstName) = 4;

29 An Enrollment table has StudentID, CourseID, and Semester. A student can take many courses in a semester, and a course can have many students. However, a student can enroll in a specific course only once per semester. What is the most appropriate primary key for the Enrollment table?

database keys Medium
A. A composite key of (StudentID, CourseID, Semester)
B. A single-column key on CourseID
C. A composite key of (StudentID, CourseID)
D. A single-column key on StudentID

30 A junior DBA is granted SELECT and INSERT privileges on the Employees table. The lead DBA now wants to remove the junior's ability to add new employees but maintain their read access. Which DCL command achieves this?

data control and transaction control language Medium
A. REVOKE INSERT ON Employees FROM junior_dba;
B. REVOKE ALL ON Employees FROM junior_dba;
C. DELETE PRIVILEGE INSERT ON Employees FROM junior_dba;
D. GRANT SELECT ON Employees TO junior_dba;

31 Given a Sales table with a CommissionPct column that can contain NULL values, what will be the result of SELECT AVG(CommissionPct) FROM Sales; if the column contains the values (0.10, 0.20, NULL, 0.10)?

SQL basic operations Medium
A. 0.10
B. 0.0
C. An error, because AVG cannot handle NULL values.
D. 0.1333...

32 What is a key functional difference between a PRIMARY KEY constraint and a UNIQUE constraint on a column?

integrity constraints Medium
A. A UNIQUE constraint does not create an index, while a PRIMARY KEY does.
B. A table can have multiple PRIMARY KEY constraints but only one UNIQUE constraint.
C. A PRIMARY KEY constraint enforces both uniqueness and non-nullability, whereas a UNIQUE constraint only enforces uniqueness and typically allows one NULL value.
D. A PRIMARY KEY cannot be a foreign key in another table, but a UNIQUE key can.

33 Which of the following DDL statements is used to permanently remove a database index named idx_lastname from the Employees table?

introduction to data definition language Medium
A. DROP INDEX idx_lastname ON Employees;
B. DELETE INDEX idx_lastname ON Employees;
C. REMOVE INDEX idx_lastname FROM Employees;
D. ALTER TABLE Employees DROP INDEX idx_lastname;

34 A banking transaction involves withdrawing money from a savings account and depositing it into a checking account. If the system crashes after the withdrawal is complete but before the deposit is made, which ACID property ensures the database is returned to a consistent state (i.e., the withdrawal is undone)?

data control and transaction control language Medium
A. Consistency
B. Durability
C. Isolation
D. Atomicity

35 In a relational database schema for a university, a Professors table has a DeptID column. This DeptID column contains values that must match an existing ID in the Departments table's primary key column. What is the role of Professors.DeptID?

database keys Medium
A. It is a super key that includes department information.
B. It is a candidate key for the Professors table.
C. It is the primary key of the Professors table.
D. It is a foreign key used to enforce referential integrity.

36 You need to retrieve a list of employees from the Employees table, sorted by their Salary from highest to lowest. For employees with the same salary, they should be sorted by LastName alphabetically (A-Z). What is the correct ORDER BY clause?

SQL basic operations Medium
A. ORDER BY Salary DESC, LastName ASC;
B. ORDER BY Salary, LastName;
C. ORDER BY Salary ASC, LastName DESC;
D. ORDER BY Salary, LastName DESC;

37 A user executes the command UPDATE Products SET Price = Price * 0.90; on a table with 1,000 products. What is the most likely result of this operation?

data manipulation Medium
A. The price of every product in the table will be decreased by 10%.
B. The statement will fail because it lacks a WHERE clause.
C. A new table with the updated prices will be created.
D. The price of the single most expensive product will be decreased by 10%.

38 A column ProductCode in a Products table is defined with a NOT NULL constraint. What is the direct consequence of this constraint during a data manipulation operation?

integrity constraints Medium
A. An INSERT statement into the Products table must provide a non-null value for the ProductCode column.
B. The ProductCode is automatically indexed for faster lookups.
C. Any UPDATE statement on the Products table must include the ProductCode column.
D. The ProductCode must be unique across all rows in the table.

39 In the context of database security, the GRANT CREATE SESSION command given to a user is an example of what kind of privilege?

data control and transaction control language Medium
A. A system privilege
B. A role-based privilege
C. An object privilege
D. A table-level privilege

40 A table Orders contains a Country column with the following values across 7 rows: 'USA', 'UK', 'Canada', 'USA', 'Mexico', 'UK', 'USA'. What is the result of the query SELECT COUNT(DISTINCT Country) FROM Orders;?

SQL basic operations Medium
A. 4
B. An error, as COUNT and DISTINCT cannot be used together this way.
C. 3
D. 7

41 Consider a table Employees with a constraint defined as CONSTRAINT chk_salary CHECK (salary > (SELECT AVG(salary) FROM Employees WHERE department_id = NEW.department_id)). Why is this constraint definition invalid in standard SQL, and what is the fundamental issue it presents?

Integrity constraints Hard
A. The constraint creates a potential for infinite recursion during an INSERT or UPDATE.
B. The use of NEW.department_id is only valid in triggers, not CHECK constraints.
C. The AVG function cannot be used within a row-level CHECK constraint as it requires a full table scan.
D. Standard SQL CHECK constraints cannot contain subqueries.

42 A transaction executes the following sequence of commands in an autocommit-disabled session:

1. START TRANSACTION;
2. UPDATE products SET price = 100 WHERE id = 1;
3. CREATE TABLE temp_log (msg TEXT);
4. INSERT INTO temp_log VALUES ('Update done');
5. ROLLBACK;

What is the state of the database after the ROLLBACK command is executed?

Data control and transaction control language Hard
A. The products update is committed, and the temp_log table exists with one row.
B. The products update is rolled back, and the temp_log table does not exist.
C. The entire sequence fails at step 3 because DDL commands are not allowed inside a transaction.
D. The products update is rolled back, but the temp_log table exists and is empty.

43 Given a table Hierarchy (emp_id, manager_id), a recursive Common Table Expression (CTE) is written to find all subordinates of a manager with emp_id = 1. The recursive member of the CTE contains ... JOIN Hierarchy h ON cte.emp_id = h.manager_id. If the data contains a cycle (e.g., A manages B, and B manages A), what is the most likely outcome when the query is executed without an explicit cycle detection mechanism?

SQL basic operations Hard
A. The query will enter an infinite loop and eventually be terminated by the database due to exceeding a recursion limit or timeout.
B. The query will execute successfully but will return an incomplete or incorrect hierarchy tree.
C. The query will return the rows involved in the cycle multiple times until the UNION ALL operator implicitly removes them.
D. The database will detect the cycle and return an error before execution begins.

44 A table R has attributes (A, B, C, D, E). The following functional dependencies hold: {AB -> C, C -> D, D -> A, E -> B}. Which of the following is a candidate key for R?

Database keys Hard
A. {C, E}
B. {D, E}
C. {A, B}
D. {A, E}

45 You have a table salaries (emp_id, department, salary). You want to find the second highest salary in each department. Which of the following queries is the most robust and standard-compliant way to achieve this?

Data manipulation Hard
A. sql
SELECT s.department, s.salary
FROM salaries s
WHERE 1 = (SELECT COUNT(DISTINCT salary) FROM salaries s2 WHERE s2.salary > s.salary AND s2.department = s.department);
B. sql
WITH RankedSalaries AS (
SELECT department, salary,
DENSE_RANK() OVER(PARTITION BY department ORDER BY salary DESC) as rnk
FROM salaries
)
SELECT department, salary FROM RankedSalaries WHERE rnk = 2;
C. sql
SELECT department, salary
FROM salaries
GROUP BY department
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
D. sql
SELECT department, MAX(salary)
FROM salaries s1
WHERE salary < (SELECT MAX(salary) FROM salaries s2 WHERE s1.department = s2.department)
GROUP BY department;

46 Consider three tables: A (PK: aid), B (PK: bid, FK aid on A.aid with ON DELETE CASCADE), and C (PK: cid, FK bid on B.bid with ON DELETE SET NULL). If a row with aid=1 is deleted from table A, and this aid is referenced by a row bid=10 in table B, which in turn is referenced by a row cid=100 in table C, what is the final state of the row in table C?

Data definition language Hard
A. The row cid=100 in table C is unchanged, but the row in B is deleted.
B. The operation fails due to a conflict between CASCADE and SET NULL actions.
C. The bid column for the row cid=100 in table C is set to NULL.
D. The row cid=100 in table C is deleted.

47 User Admin executes GRANT SELECT ON mytable TO UserA WITH GRANT OPTION;. Then, UserA executes GRANT SELECT ON mytable TO UserB;. Later, Admin executes REVOKE SELECT ON mytable FROM UserA CASCADE;. What is the status of UserB's select privilege on mytable?

Data control and transaction control language Hard
A. UserB's privilege is revoked because the CASCADE keyword propagates the revoke down the grant chain.
B. UserB can still select from mytable because their grant came from UserA, not Admin.
C. UserB's privilege is unaffected, but UserA's grant option is revoked, so UserA cannot grant further privileges.
D. The REVOKE command fails because UserA has granted the privilege to another user.

48 A table T has a column C with a UNIQUE constraint. What is the behavior of this constraint with respect to NULL values according to the SQL standard, and how does it differ in common implementations like PostgreSQL and SQL Server?

Integrity constraints Hard
A. The SQL standard is ambiguous, but most implementations like PostgreSQL and Oracle allow multiple NULLs, whereas SQL Server treats NULLs as equal and allows only one NULL.
B. The SQL standard mandates that only one NULL is allowed; both PostgreSQL and SQL Server follow this strictly.
C. The SQL standard considers each NULL as distinct from every other NULL, thus allowing multiple NULLs. Both PostgreSQL and SQL Server adhere to this interpretation.
D. The SQL standard considers NULL as not equal to anything, including another NULL, thus allowing multiple NULLs. SQL Server deviates by treating NULLs as equal in the context of a UNIQUE constraint, allowing only one.

49 Which transaction isolation level is specifically designed to prevent the 'Phantom Read' anomaly, and how does it typically achieve this?

Data control and transaction control language Hard
A. READ UNCOMMITTED, as it does not take any locks and thus avoids conflicts that cause phantoms.
B. READ COMMITTED, by taking shared locks on rows as they are read.
C. SERIALIZABLE, by using predicate locks or range locks on the WHERE clause conditions.
D. REPEATABLE READ, by holding read locks on all rows it reads until the transaction ends.

50 Consider a query SELECT department, job_title, AVG(salary) FROM employees GROUP BY CUBE (department, job_title);. If there are 3 distinct departments and 5 distinct job titles, how many grouping levels (rows) will be in the result set, assuming every combination of department and job title exists?

SQL basic operations Hard
A. It depends on the number of employees, not the number of distinct departments and titles.
B. (3 * 5) + 3 + 5 = 23 rows
C. (3 * 5) + 3 + 5 + 1 = 24 rows
D. 3 * 5 = 15 rows

51 Which of the following statements about the TRUNCATE TABLE command is generally false in most major RDBMS implementations (e.g., PostgreSQL, SQL Server)?

Data definition language Hard
A. TRUNCATE can be rolled back if it is enclosed within a user-defined transaction block.
B. TRUNCATE does not fire ON DELETE triggers associated with the table.
C. TRUNCATE is a DDL command and often causes an implicit commit.
D. TRUNCATE resets identity columns (auto-increment) back to their seed value.

52 A table A has a composite primary key (c1, c2). Table B has a foreign key (f1, f2) referencing A(c1, c2) with the MATCH FULL referential integrity rule. Which of the following INSERT statements into B is guaranteed to be valid under this rule?

Integrity constraints Hard
A. INSERT INTO B (f1, f2) VALUES (NULL, 20);
B. INSERT INTO B (f1, f2) VALUES (10, NULL);
C. INSERT INTO B (f1, f2) VALUES (NULL, NULL);
D. INSERT INTO B (f1, f2) VALUES (10, 20);, assuming a row (10, 20) exists in A.

53 What is the primary purpose of the WHEN NOT MATCHED BY SOURCE THEN DELETE clause in a SQL MERGE statement?

Data manipulation Hard
A. To delete rows from the target table that do not have a corresponding match in the source table based on the join condition.
B. To delete rows from the source table that do not have a match in the target table.
C. To delete rows from the target table that exist in the source table.
D. It is not a standard clause; MERGE statements can only insert, update, or delete rows from the target table that are matched by the source.

54 Given a table of employee salaries, emp_salary (emp_name, salary), how would you express the salary of each employee as a percentage of the total salary expenditure using a window function?

SQL basic operations Hard
A. sql
SELECT emp_name, salary, (salary * 100.0) / (SELECT SUM(salary) FROM emp_salary)
FROM emp_salary;
B. sql
SELECT emp_name, salary, AVG(salary * 100.0 / SUM(salary)) OVER ()
FROM emp_salary;
C. sql
SELECT emp_name, salary, (salary / SUM(salary)) * 100
FROM emp_salary
GROUP BY emp_name, salary;
D. sql
SELECT emp_name, salary, (salary * 100.0) / SUM(salary) OVER ()
FROM emp_salary;

55 A table DEPARTMENTS has dept_id as its PRIMARY KEY. A second table PROJECTS has proj_id as its PRIMARY KEY and a column lead_dept_id which has a UNIQUE constraint. An EMPLOYEES table has a dept_id column. Can EMPLOYEES.dept_id have a foreign key constraint that references PROJECTS.lead_dept_id?

Database keys Hard
A. No, foreign keys can only reference columns that are a PRIMARY KEY.
B. Yes, but only if lead_dept_id is also declared as NOT NULL.
C. No, because lead_dept_id does not uniquely identify a department, it identifies a project's leading department.
D. Yes, a foreign key can reference any column as long as it has a UNIQUE constraint.

56 Consider the following UPDATE statement meant to give the top 10% of earners a 'Top Earner' status. Why is this statement syntactically invalid or logically flawed in most standard SQL dialects?

sql
UPDATE Employees
SET status = 'Top Earner'
WHERE employee_id IN (
SELECT employee_id
FROM Employees
ORDER BY salary DESC
LIMIT (SELECT COUNT() 0.1 FROM Employees)
);

Data manipulation Hard
A. The LIMIT clause cannot accept a subquery as its argument.
B. ORDER BY is not allowed in a subquery used with an IN operator.
C. The entire logic is flawed; LIMIT does not guarantee a precise percentile in the presence of salary ties.
D. The subquery inside the IN clause attempts to modify the same table (Employees) being updated, causing a mutation error.

57 Consider the following transaction:
sql
START TRANSACTION;
INSERT INTO T VALUES (1);
SAVEPOINT s1;
INSERT INTO T VALUES (2);
SAVEPOINT s2;
DELETE FROM T;
ROLLBACK TO SAVEPOINT s1;
INSERT INTO T VALUES (3);
COMMIT;

Assuming table T was empty before this transaction, what are the final contents of T?

Data control and transaction control language Hard
A. {1, 3}
B. {3}
C. {1, 2, 3}
D. The table is empty.

58 A table Events has a constraint CONSTRAINT check_dates CHECK (start_date <= end_date). The constraint is created as DEFERRABLE INITIALLY DEFERRED. What does this allow a user to do within a single transaction?

Integrity constraints Hard
A. It has no effect; DEFERRABLE is only for foreign key constraints, not CHECK constraints.
B. The constraint is checked only when the session is closed, allowing invalid data to persist temporarily.
C. The constraint can be temporarily disabled for a single statement using SET CONSTRAINTS ... DEFERRED.
D. The constraint check is postponed until the transaction is committed, allowing rows to be temporarily invalid within the transaction.

59 What is the key difference in how RANK() and DENSE_RANK() handle ties when used as window functions?

SQL basic operations Hard
A. RANK() assigns the same rank to tied rows and skips subsequent ranks, while DENSE_RANK() assigns consecutive ranks to tied rows.
B. RANK() assigns the same rank to tied rows and leaves a gap in the rank sequence, while DENSE_RANK() assigns the same rank to tied rows and does not leave a gap.
C. DENSE_RANK() is computationally less expensive than RANK() because it does not need to calculate gaps.
D. RANK() breaks ties arbitrarily based on physical row storage, while DENSE_RANK() requires a second ORDER BY column to break ties.

60 In a relational schema with attributes (A, B, C, D) and functional dependencies {A -> B, B -> C, C -> D}, which of the following sets of attributes is a superkey but not a candidate key?

Database keys Hard
A. {D}
B. {B, D}
C. {A, B}
D. {A}