0% found this document useful (0 votes)
19 views11 pages

Easy Exam

Uploaded by

Kirolos Talat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Easy Exam

Uploaded by

Kirolos Talat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Question (1):

Normalization Exercise [6 Marks]

Below is a table structure for a database:

| StudentID | StudentName | CourseName | InstructorName | InstructorPhone


| Grade |

|-----------|-------------|------------|----------------|-----------------|-------|

a. Identify the normal form the given table is in. Explain your reasoning.

b. Normalize the table to the *Third Normal Form (3NF)*. Show all
intermediate steps.

*Question (2):*

Multiple-Choice Questions [5 Marks]

Select the correct answer for each question. (0.5 marks each)

1. Which SQL keyword is used to retrieve unique values from a table?

a) SELECT

b) UNIQUE

c) DISTINCT

d) DIFFERENT

2. What is the correct syntax to delete a table from a database?

a) DELETE TABLE table_name;

b) DROP TABLE table_name;

c) REMOVE table_name;
d) DESTROY TABLE table_name;

3. What is the primary function of a primary key in a database?

a) To allow duplicate records

b) To uniquely identify a record

c) To establish relationships between tables

d) To sort data

4. Which SQL command is used to add a new column to an existing table?

a) MODIFY TABLE

b) ADD COLUMN

c) ALTER TABLE

d) CHANGE TABLE

5. What is the result of the following query? SELECT COUNT(*) FROM


Students;

a) Returns the total number of columns

b) Returns the total number of rows

c) Returns the number of unique rows

d) Returns all rows

(Add five more questions in this style, covering joins, constraints, functions,
etc.)

*Question (3):*

Situational and Practical SQL Queries [9 Marks]


*Part A: Situational Solutions [4 Marks]*

Answer the following situations with one or two words:

1. If we want to permanently store data in a structured format, we...

*Answer:* Create Table

2. If we need to retrieve specific rows from a table, we use...

*Answer:* SELECT

3. To remove duplicate entries from a query result, we use...

*Answer:* DISTINCT

4. To combine rows from two tables, we use...

*Answer:* JOIN

(Add six more situations here)

*Part B: Write SQL Commands [5 Marks]*

Write the SQL commands for the following scenarios:

1. Create a table named "Employees" with columns ID, Name, Position, and
Salary.

2. Add a new column "Department" to the "Employees" table.

3. Insert a new employee record into the "Employees" table.

4. Retrieve all employees earning more than $5000.

5. Delete an employee with ID = 10 from the "Employees" table.

(Add five more scenarios like these)


Answers:
### *Question (1): Normalization Exercise [6 Marks]*

#### *Part A: Identify the Normal Form*

The table is currently in *1NF* because:

- All columns contain atomic values (e.g., StudentName contains a single


name, CourseName contains a single course).

- There are no repeating groups in the table.

However, the table is *not in 2NF* because:

- There are partial dependencies. For example:

- InstructorName and InstructorPhone depend only on CourseName, not the


full composite key (StudentID, CourseName).

The table is also *not in 3NF* because:

- There are transitive dependencies. For example:

- InstructorPhone depends on InstructorName, which in turn depends on


CourseName.

---

#### *Part B: Normalize the Table to 3NF*

*Step 1: Convert to 2NF*

Remove partial dependencies by creating separate tables:

1. *Student Table*:

- Columns: StudentID, StudentName


2. *Course Table*:

- Columns: CourseName, InstructorName, InstructorPhone

3. *Enrollment Table*:

- Columns: StudentID, CourseName, Grade

*Step 2: Convert to 3NF*

Remove transitive dependencies:

1. *Instructor Table*:

- Columns: InstructorName, InstructorPhone

2. *Course Table*:

- Columns: CourseName, InstructorName

*Final Tables and Relationships*:

1. *Student Table*:

- StudentID (Primary Key), StudentName

2. *Instructor Table*:

- InstructorName (Primary Key), InstructorPhone

3. *Course Table*:

- CourseName (Primary Key), InstructorName (Foreign Key to


InstructorTable)
4. *Enrollment Table*:

- StudentID (Foreign Key to StudentTable), CourseName (Foreign Key to


CourseTable), Grade

---

### *Question (2): Multiple-Choice Questions [5 Marks]*

1. *Which SQL keyword is used to retrieve unique values from a table?*

*Answer:* c) DISTINCT

2. *What is the correct syntax to delete a table from a database?*

*Answer:* b) DROP TABLE table_name;

3. *What is the primary function of a primary key in a database?*

*Answer:* b) To uniquely identify a record

4. *Which SQL command is used to add a new column to an existing table?*

*Answer:* c) ALTER TABLE

5. *What is the result of the following query? SELECT COUNT() FROM


Students;**

*Answer:* b) Returns the total number of rows

*Additional Questions:*
6. *Which join returns only matching rows from both tables?*

*Answer:* b) INNER JOIN

7. *Which constraint ensures that a column cannot have a NULL value?*

*Answer:* c) NOT NULL

8. *Which function returns the current date in SQL?*

*Answer:* b) GETDATE()

9. *What does the GROUP BY clause do in SQL?*

*Answer:* a) Groups rows with the same values in specified columns

10. *What is the difference between WHERE and HAVING in SQL?*

*Answer:* c) WHERE filters rows before aggregation, HAVING filters rows


after aggregation

---

### *Question (3): Situational and Practical SQL Queries [9 Marks]*

#### *Part A: Situational Solutions [4 Marks]*

1. *If we want to permanently store data in a structured format, we...*

*Answer:* Create Table

2. *If we need to retrieve specific rows from a table, we use...*

*Answer:* SELECT
3. *To remove duplicate entries from a query result, we use...*

*Answer:* DISTINCT

4. *To combine rows from two tables, we use...*

*Answer:* JOIN

5. *To ensure a column always contains a value, we use...*

*Answer:* NOT NULL

6. *To calculate the average of a numeric column, we use...*

*Answer:* AVG

7. *To count the number of rows in a table, we use...*

*Answer:* COUNT

8. *To add a condition to filter rows, we use...*

*Answer:* WHERE

9. *To limit the number of rows returned by a query, we use...*

*Answer:* LIMIT

10. *To combine the result of two queries, we use...*

*Answer:* UNION

---
#### *Part B: Write SQL Commands [5 Marks]*

1. *Create a table named "Employees":*

sql

CREATE TABLE Employees (

ID INT PRIMARY KEY,

Name VARCHAR(255),

Position VARCHAR(255),

Salary DECIMAL(10, 2)

);

2. *Add a new column "Department" to the "Employees" table:*

sql

ALTER TABLE Employees ADD Department VARCHAR(255);

3. *Insert a new employee record into the "Employees" table:*

sql

INSERT INTO Employees (ID, Name, Position, Salary, Department)

VALUES (1, 'John Doe', 'Manager', 6000.00, 'HR');

4. *Retrieve all employees earning more than $5000:*

sql

SELECT * FROM Employees WHERE Salary > 5000;


5. *Delete an employee with ID = 10:*

sql

DELETE FROM Employees WHERE ID = 10;

*Additional Scenarios:*

6. *Update the salary of employees in the HR department by 10%:*

sql

UPDATE Employees SET Salary = Salary * 1.10 WHERE Department = 'HR';

7. *Find the total salary paid to employees in each department:*

sql

SELECT Department, SUM(Salary) AS TotalSalary

FROM Employees

GROUP BY Department;

8. *Retrieve the highest salary in each department:*

sql

SELECT Department, MAX(Salary) AS HighestSalary

FROM Employees

GROUP BY Department;
9. *Find all employees whose name contains 'John':*

sql

SELECT * FROM Employees WHERE Name LIKE '%John%';

10. *Drop the "Employees" table:*

sql

DROP TABLE Employees;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy