Practice
import sqlite3
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
cursor.execute('CREATE TABLE Employee(Id INTEGER, Salary INTEGER)')
cursor.executemany('INSERT INTO Employee VALUES (?, ?)', [(1, 100), (2, 200), (3, 300), (4, 300)])
query = '''
SELECT Salary FROM (
SELECT DISTINCT Salary FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1
)
'''
cursor.execute(query)
result = cursor.fetchone()
print(result[0] if result else 'NULL')
conn.close()Solution
Step 1: Identify distinct salaries and order descending
Distinct salaries are [300, 200, 100] ordered descending.Step 2: Apply LIMIT 1 OFFSET 1
OFFSET 1 skips the highest (300), LIMIT 1 picks the next, which is 200.Final Answer:
Option D -> Option DQuick Check:
Distinct ordering and offset correctly select second highest salary [OK]
- Confusing OFFSET 1 to pick highest
- Not using DISTINCT leads to duplicate highest salary
Solution
Step 1: Review each statement
Denormalization can lead to data anomalies if updates are not carefully synchronized is true; unsynchronized updates cause anomalies.Step 2: Analyze storage impact
Denormalization always reduces the total storage space required by the database is false; denormalization increases storage due to redundancy.Step 3: Consider NoSQL usage
Denormalization is often used in NoSQL databases to optimize read performance is true; NoSQL systems often denormalize for performance.Step 4: Evaluate consistency mechanisms
Denormalization requires additional mechanisms to maintain data consistency is true; extra mechanisms are needed to maintain consistency.Final Answer:
Option B -> Option BQuick Check:
Denormalization increases, not reduces, storage space [OK]
- Assuming denormalization saves storage
- Ignoring the need for consistency mechanisms
- Believing denormalization eliminates anomalies
Solution
Step 1: Review B+ tree properties
B+ trees keep keys sorted, store data in leaves, and link leaves for sequential access.Step 2: Analyze time complexity
Equality lookups require O(log n) time due to tree traversal, not O(1).Step 3: Evaluate distractors
Options B, C, and D correctly describe B+ tree features.Final Answer:
Option C -> Option CQuick Check:
B+ tree equality lookup is O(log n), not O(1) [OK]
- Confusing B+ tree lookup with hash index lookup time
- Assuming internal nodes store data
- Ignoring leaf node linkage
Solution
Step 1: Analyze each statement
SQL databases always guarantee strong consistency but may sacrifice availability during network partitions correctly states SQL prioritizes consistency over availability during partitions.Step 2: Evaluate NoSQL and ACID
NoSQL databases never support ACID transactions and thus cannot guarantee any consistency is incorrect because some NoSQL databases support ACID transactions, though often limited or scoped.Step 3: Confirm other options
NoSQL databases often sacrifice immediate consistency to achieve higher availability and partition tolerance correctly describes NoSQL trade-offs. SQL databases are better suited for complex queries and relational data integrity is true about SQL strengths.Final Answer:
Option A -> Option AQuick Check:
NoSQL can support ACID in some cases, so 'never' is false [OK]
- Believing NoSQL never supports ACID
- Assuming SQL always maintains availability during partitions
- Confusing consistency guarantees with query complexity
Solution
Step 1: Check lossless join condition
Each pair of relations shares a key attribute: R1 and R2 share B (key in R2), R2 and R3 share C (key in R3), so lossless join holds.Step 2: Check dependency preservation
All FDs appear in one of the relations: A -> B in R1, B -> C in R2, C -> D in R3.Step 3: Confirm no FD requires join enforcement
Each FD is enforceable in its own relation, so dependency preservation holds.Final Answer:
Option B -> Option BQuick Check:
Options A, B, and C incorrectly claim loss or preservation failures.
- Assuming chain dependencies require joins to enforce
- Believing lossless join requires all keys in all relations
- Confusing dependency preservation with global key presence
