0% found this document useful (0 votes)
4 views

Compilation of Lab Exercises

The document outlines the steps for creating a database and tables for managing faculties and programs, including integrity constraints such as primary keys and foreign keys. It details SQL commands for inserting, updating, and deleting records, as well as performing various types of joins and sub-queries. Additionally, it includes instructions for creating triggers, views, and stored procedures for advanced SQL operations.
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)
4 views

Compilation of Lab Exercises

The document outlines the steps for creating a database and tables for managing faculties and programs, including integrity constraints such as primary keys and foreign keys. It details SQL commands for inserting, updating, and deleting records, as well as performing various types of joins and sub-queries. Additionally, it includes instructions for creating triggers, views, and stored procedures for advanced SQL operations.
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/ 5

I.

Creation of Database
II. Creation of Table
III. Insert 10 records
IV. Update Multiple Records
V. Update a Record
VI. Update all Records
VII. Delete a Record
VIII. Delete All Records
IX. Delete a table

INTEGRITY CONSTRAINTS

X. Create a Database: Start by creating a database that manages


faculties and programs.
XI. Create the programs Table: Create a table for the programs,
ensuring that each program has a unique ID and name.
a. Constraints to include:
i. Primary Key
ii. Unique Constraint
iii. Not Null Constraint
XII. Create the faculties Table: Create a table for faculties, where
each faculty is assigned to a program.
a. Constraints to include:
i. Primary Key
ii. Foreign Key
iii. Not Null Constraint
iv. Check Constraint (to ensure salary is positive)
XIII. Insert Sample Data: Insert records into both tables, ensuring that
the constraints are respected.
XIV. Test Integrity Constraints: Attempt to perform the following
actions, and observe how the integrity constraints prevent incorrect
data from being entered:
a. Insert a faculty with a negative salary.

b. Insert a program with a duplicate program name.


c. Insert a faculty without assigning a program (null foreign key).

XV. Modify Tables: Update the table definitions to add additional


constraints (e.g., adding a default value for salary, or further
checks on data).

ADVANCE SQL COMMAND


Laboratory Exercises:
 Create a database with two tables.
 Name table 1 as Courses with columns: course_id (interger, primary key), course_name
 In Courses table add 6 courses BSIT, BEED, BSED, BSBA-HRM, BSBA-MM, BSOA
 Name table 2 as Students wth columns: student_id (integer, primary key), student_name,
course_id (integer)
 In the Students table add 20 students: 4 students without course_id, 4 students each
BSIT, BEED, BSOA, BSED.
Perform:
1. Inner Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
INNER JOIN Courses
ON Students.course_id = Courses.course_id;

2. Full Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
FULL JOIN Courses
ON Students.course_id = Courses.course_id;

SELECT S.student_id, S.student_name, C.course_id, C.course_name FROM Students S FULL


JOIN Courses C ON S.course_id = C.course_id;

3. Full Join with Where Clause


SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
FULL JOIN Courses
ON Students.course_id = Courses.course_id
WHERE Students.course_id IS NULL OR Courses.course_id IS NULL;
4. Left Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
LEFT JOIN Courses
ON Students.course_id = Courses.course_id;

5. Left Join with Where Clause


SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
LEFT JOIN Courses
ON Students.course_id = Courses.course_id
WHERE Courses.course_id IS NULL;

6. Right Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
RIGHT JOIN Courses
ON Students.course_id = Courses.course_id;

7. Right Join with Where Clause


SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
RIGHT JOIN Courses
ON Students.course_id = Courses.course_id
WHERE Students.student_id IS NULL;
SUB-QUERY
Direction: Choose (2) from the real-world scenarios below and perform the indicated query for
each.
1. Employee Management:
Scenario: Retrieve the details of employees who have salaries greater than the average salary for
their department.
Query: Use a subquery to calculate the average salary for each department and then compare it
with individual employee salaries.

2. Product Pricing:
Scenario: Find products with prices higher than the average price across all categories.
Query: Utilize a subquery to calculate the average price and filter products based on this value.

3. Customer Orders:
Scenario: Identify customers who made more than three orders in the past month.
Query: Use a subquery to count the number of orders for each customer and filter the results
accordingly.

4. Healthcare Management:
Scenario: Retrieve patient details along with their most recent diagnosis.
Query: Utilize a subquery to find the latest diagnosis date for each patient and join this
information with the patient details.

5. Inventory Replenishment:
Scenario: Identify products that need to be reordered by finding those with quantities below a
certain threshold.
Query: Use a subquery to filter products based on their stock levels.

6. Marketing Analysis:
Scenario: Find customers who have purchased more than once and determine which products
they bought.
Query: Utilize a subquery to identify repeat customers and then join this information with the
products they purchased.

7. Project Management:
Scenario: Retrieve details of projects where the budget exceeds the average budget for projects in
the same category.
Query: Use a subquery to calculate the average budget for each project category and filter
projects accordingly.

8. Social Media Analytics:


Scenario: Find users who have posted more than the average number of times in a specific
month.
Query: Utilize a subquery to calculate the average posting frequency and filter users based on
their posting activity.

9. Educational Institution:
Scenario: Retrieve a list of students who scored higher than the average score in a particular
subject.
Query: Use a subquery to calculate the average score and compare it with individual student
scores.
10. Supplier Evaluation:
Scenario: Identify suppliers with a rating higher than the average supplier rating.
Query: Utilize a subquery to calculate the average supplier rating and filter suppliers accordingly.

CREATION OF QUERY ON TRIGGERS, VIEWS AND STORED PROCEDURES

You might also like