0
0
SQLquery~20 mins

Why table design matters in SQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Table Design Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of missing primary key on query results

Consider a table Employees without a primary key. What will be the result of this query?

SELECT EmployeeID, COUNT(*) FROM Employees GROUP BY EmployeeID;

Assuming EmployeeID is not unique in the table.

SQL
CREATE TABLE Employees (EmployeeID INT, Name VARCHAR(100));
INSERT INTO Employees VALUES (1, 'Alice'), (1, 'Alice'), (2, 'Bob');
SELECT EmployeeID, COUNT(*) FROM Employees GROUP BY EmployeeID;
AEmpty result set
BEmployeeID 1 with count 1, EmployeeID 2 with count 1
CError due to missing primary key
DEmployeeID 1 with count 2, EmployeeID 2 with count 1
Attempts:
2 left
💡 Hint

Think about how GROUP BY works with duplicate rows.

🧠 Conceptual
intermediate
1:30remaining
Why normalization improves data integrity

Which of the following best explains why normalization is important in table design?

AIt reduces data duplication and prevents update anomalies
BIt makes queries run slower but uses less disk space
CIt allows storing multiple data types in one column
DIt automatically creates indexes on all columns
Attempts:
2 left
💡 Hint

Think about what happens when the same data is stored in many places.

📝 Syntax
advanced
2:00remaining
Identify the correct SQL to add a foreign key

Which SQL statement correctly adds a foreign key constraint to the Orders table referencing Customers?

SQL
ALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
AALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
BALTER TABLE Orders ADD FOREIGN KEY CustomerID REFERENCES Customers(CustomerID);
CALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY CustomerID REFERENCES Customers(CustomerID);
DALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (CustomerID) Customers(CustomerID);
Attempts:
2 left
💡 Hint

Check the syntax for adding constraints with parentheses.

optimization
advanced
2:30remaining
Choosing indexes for faster queries

You have a large Sales table with columns SaleID, ProductID, and SaleDate. Which index will speed up this query?

SELECT * FROM Sales WHERE ProductID = 101 AND SaleDate = '2024-01-01';
ACREATE INDEX idx_product ON Sales(ProductID);
BCREATE INDEX idx_product_date ON Sales(ProductID, SaleDate);
CCREATE INDEX idx_date ON Sales(SaleDate);
DCREATE INDEX idx_date_product ON Sales(SaleDate, ProductID);
Attempts:
2 left
💡 Hint

Think about which columns appear together in the WHERE clause and their order.

🔧 Debug
expert
3:00remaining
Diagnose data inconsistency from poor table design

A table Inventory stores product quantities but allows duplicate ProductID rows. What problem can this cause?

AThe database will reject inserts with duplicate ProductID
BQueries will always return zero rows
CTotal quantity calculations may be incorrect due to duplicates
DThe table will automatically merge duplicate rows
Attempts:
2 left
💡 Hint

Think about what happens when the same product appears multiple times with quantities.