L.08 Flashcards

Finishing up SQL and Normalization

1
Q

What are aggregate functions in SQL?

A

Functions like COUNT, SUM, MAX, MIN, and AVG that summarize multiple tuples into a single value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you find the sum, max, min, and average salary from an EMPLOYEE table?

A

SELECT SUM(Salary), MAX(Salary), MIN(Salary), AVG(Salary) FROM EMPLOYEE;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What SQL clause is used to group rows based on a specific attribute?

A

GROUP BY clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a query to find the number of employees and their average salary per department.

A

SELECT Dno, COUNT(*), AVG(Salary)
FROM EMPLOYEE
GROUP BY Dno;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What SQL clause filters groups instead of individual rows?

A

HAVING clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is redundancy a problem in database design?

A

It wastes space and can cause update anomalies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are functional dependencies (FDs)?

A

A relationship where one attribute uniquely determines another, written as X → Y.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of a functional dependency.

A

SSN → ENAME (Social Security Number determines Employee Name).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is normalization?

A

The process of refining database schemas to remove redundancies and ensure proper keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the First Normal Form (1NF)?

A

It eliminates composite, multivalued, and nested attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Second Normal Form (2NF)?

A

It ensures that non-key attributes are fully functionally dependent on the entire primary key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the Third Normal Form (3NF)?

A

It prevents non-key attributes from depending on other non-key attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are acceptable functional dependencies?

A

Those involving primary or foreign keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What key takeaways should you remember about normalization?

A

Define meaningful relations, avoid redundancy, and properly define keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of aliasing in SQL aggregate functions?

A

Aliasing renames result columns for better readability.
Example:
SELECT SUM(Salary) AS Total_Sal, AVG(Salary) AS Avg_Sal
FROM EMPLOYEE;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a common mistake when using GROUP BY?

A

Including non-aggregate attributes in the SELECT clause that are not part of GROUP BY.

17
Q

What is the order of execution in a SQL query with WHERE, GROUP BY, and HAVING?

A

WHERE (filters individual rows)
GROUP BY (groups rows)
HAVING (filters entire groups)

18
Q

What is an update anomaly?

A

A data inconsistency that occurs when redundant data is updated incorrectly.

19
Q

What is a deletion anomaly?

A

Loss of useful data when a tuple is deleted.
Example: Deleting the last employee working on a project removes project details.

20
Q

What is an insertion anomaly?

A

Difficulty in inserting new data due to unnecessary dependencies.
Example: Unable to add a new project until at least one employee is assigned to it.

21
Q

How do foreign keys help in normalization?

A

They link related tables, reducing redundancy and ensuring referential integrity.

22
Q

What is a bad database design example related to foreign keys?

A

Removing Super_ssn from EMPLOYEE makes it impossible to track managers.

23
Q

What are redundant functional dependencies?

A

FDs that store information already available through joins.
Example:
WORKS_ON(Emp#, Proj#, Ename, Pname, No_hours)

Here, Ename and Pname are redundant since they can be derived from EMPLOYEE and PROJECT.

24
Q

What is the trade-off between redundancy and performance?

A

Redundancy can improve query performance but risks data inconsistencies.

25
What is an example of a composite primary key?
{SSN, PNUMBER} → HOURS (An employee's work hours depend on both their SSN and Project Number).
26
Why is 1NF automatically enforced in relational databases?
Because relational models do not support nested or multi-valued attributes.
27
How does 2NF prevent partial dependencies?
It ensures non-key attributes depend on the entire primary key, not just part of it.
28
What is an example of a 3NF violation?
If EmployeeSalary → EmployeeTaxBracket, then EmployeeTaxBracket should be in a separate table.
29
What are higher normal forms beyond 3NF?
BCNF, 4NF, and 5NF handle more complex dependencies, but 3NF is usually sufficient.
30
What are key takeaways from normalization?
✔ Define tables based on real-world meaning ✔ Eliminate redundancy ✔ Identify functional dependencies ✔ Ensure primary and foreign keys are properly defined