CS405 – Database Programming with Oracle: Midterm Important Subjective (24 Solved Q&As)
Computer Science • Read every solved subjective question with its model answer directly on this page. Free, no login required.
All 664 Midterm Packs · CS405 Course Page · CS405 MCQ Pack (43 MCQs)
-
What is the basic structure (syntax) of a PL/SQL block? Explain each section with an example.
Topic: PL/SQL Block Structure & Basics
PL/SQL programs follow a block structure. Basic PL/SQL Block Syntax: DECLARE -- Variable declarations BEGIN -- Executable statements EXCEPTION -- Exception handling (optional) END; Example: DECLARE message VARCHAR2(50) := 'Hello, PL/SQL!'; BEGIN DBMS_OUTPUT.PUT_LINE(message); END; Explanation: DECLARE (Optional) -> Declare variables. ; BEGIN -> Contains the main executable code. ; EXCEPTION (Optional) -> Handles errors. ; END -> Ends the block. Tip: The DECLARE and EXCEPTION sections are optional.
-
Which PL/SQL command is used to print a message? Write an example and show how to enable its output.
Topic: PL/SQL Block Structure & Basics
In PL/SQL, we use DBMS_OUTPUT.PUT_LINE to print messages. Example: BEGIN DBMS_OUTPUT.PUT_LINE('Hello, PL/SQL!'); END; Output: Hello, PL/SQL! DBMS_OUTPUT.PUT_LINE is used to print output in PL/SQL programs. Enable Server Output using: SET SERVEROUTPUT ON;
-
Write the syntax of the IF-THEN-ELSE statement in PL/SQL with an example.
Topic: Conditional Statements & Loops in PL/SQL
The IF-THEN-ELSE statement in PL/SQL is used for conditional execution. Syntax: IF condition THEN -- Execute when condition is TRUE ELSE -- Execute when condition is FALSE END IF; Example: DECLARE salary NUMBER := 5000; BEGIN IF salary > 4000 THEN DBMS_OUTPUT.PUT_LINE('High Salary'); ELSE DBMS_OUTPUT.PUT_LINE('Low Salary'); END IF; END; Output (if salary = 5000): High Salary
-
Write a PL/SQL code to check whether a number is ODD or EVEN using IF-ELSE.
Topic: Conditional Statements & Loops in PL/SQL
Scenario: A table is given, and we check if a total number is ODD or EVEN using IF-ELSE. PL/SQL Code: DECLARE total_number NUMBER := 10; -- Change as needed BEGIN IF MOD(total_number, 2) = 0 THEN DBMS_OUTPUT.PUT_LINE(total_number || ' is Even'); ELSE DBMS_OUTPUT.PUT_LINE(total_number || ' is Odd'); END IF; END; Explanation: MOD(total_number, 2) returns 0 for even numbers and 1 for odd numbers. IF-ELSE is used to check the condition and print the result. Example Output for total_number = 10: 10 is Even Example Output for total_number = 15: 15 is Odd
-
Write a PL/SQL code, using a loop, to print the multiplication of the first 10 natural numbers with 10.
Topic: Conditional Statements & Loops in PL/SQL
Loop-Based PL/SQL Code: DECLARE i NUMBER; BEGIN FOR i IN 1..10 LOOP DBMS_OUTPUT.PUT_LINE(i || ' x 10 = ' || (i * 10)); END LOOP; END; Output: 1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 6 x 10 = 60 7 x 10 = 70 8 x 10 = 80 9 x 10 = 90 10 x 10 = 100
-
Write a PL/SQL code to retrieve and display the names of the employees whose salary is greater than the average salary.
Topic: Conditional Statements & Loops in PL/SQL
DECLARE avg_sal NUMBER; BEGIN -- Get the average salary SELECT AVG(salary) INTO avg_sal FROM employees; -- Display employees earning more than the average salary FOR emp IN (SELECT ename, salary FROM employees WHERE salary > avg_sal) LOOP DBMS_OUTPUT.PUT_LINE(emp.ename || ' earns ' || emp.salary); END LOOP; END; Purpose: Finds and prints all employees earning above the average salary.
-
Write the syntax of the HAVING clause with an example. How is the HAVING clause different from the WHERE clause?
Topic: SQL: GROUP BY, HAVING, ORDER BY & Group Functions
The HAVING clause is used in SQL to filter groups of records created by the GROUP BY clause. Syntax: SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING condition; Example: SELECT deptno, AVG(salary) FROM employees GROUP BY deptno HAVING AVG(salary) > 50000; Explanation: GROUP BY deptno groups records based on department number. ; HAVING AVG(salary) > 50000 filters only those departments where average salary is greater than 50,000. Key Difference: WHERE is used to filter individual rows. ; HAVING is used to filter groups after aggregation.
-
What is the difference between a GROUP FUNCTION and the GROUP BY clause? Explain with examples.
Topic: SQL: GROUP BY, HAVING, ORDER BY & Group Functions
Feature / GROUP FUNCTION / GROUP BY Clause ; Definition / Performs calculations on multiple rows / Groups rows based on a column ; Used With / Works with SUM(), AVG(), COUNT(), etc. / Used in conjunction with GROUP FUNCTIONS ; Example / SELECT AVG(salary) FROM employees; / SELECT deptno, AVG(salary) FROM employees GROUP BY deptno; Example of GROUP FUNCTION (No GROUP BY): SELECT COUNT(*) FROM employees; Output: Returns the total number of employees. Example of GROUP BY Clause: SELECT deptno, COUNT(*) FROM employees GROUP BY deptno; Output: Groups employees by deptno and counts the number of employees in each department. Key Difference: GROUP FUNCTIONS perform operations on multiple rows. GROUP BY groups rows based on a column and then applies group functions.
-
Write an SQL query to sort faculty names in ascending order.
Topic: SQL: GROUP BY, HAVING, ORDER BY & Group Functions
To sort FacultyName in ascending order, we use the ORDER BY clause. SQL Query to Sort Faculty Names: SELECT FacultyName FROM Faculty ORDER BY FacultyName ASC; Explanation: ORDER BY FacultyName ASC sorts names in A-Z order. ASC is default (so ORDER BY FacultyName works the same).
-
What is the difference between a Primary Key and a Unique Key?
Topic: Keys & Constraints (Primary, Unique & Foreign Keys)
Feature / Primary Key / Unique Key ; Uniqueness / Ensures each row is unique / Ensures each row is unique ; NULL Allowed? / No (NOT NULL constraint) / Yes (can contain NULL values) ; Number of Keys per Table / Only One Primary Key / Multiple Unique Keys allowed ; Index Type / Clustered Index / Non-clustered Index ; Example / PRIMARY KEY (emp_id) / UNIQUE (email) Example Table: CREATE TABLE employees ( emp_id NUMBER PRIMARY KEY, -- Primary Key (No NULL, Unique) email VARCHAR2(100) UNIQUE -- Unique Key (NULL Allowed, Unique) ); Accuracy note: The 'Clustered Index / Non-clustered Index' row above is the source file's own wording, but that is SQL Server terminology. In Oracle, a primary key or unique constraint is automatically backed by a unique B-tree index; Oracle does not classify indexes as clustered or non-clustered (an index-organized table is the closest Oracle concept). The rest of the comparison is correct for Oracle.
-
What is a Foreign Key? Explain with an example.
Topic: Keys & Constraints (Primary, Unique & Foreign Keys)
A foreign key is a column (or a set of columns) that references the primary key of another table. It ensures referential integrity between tables. Key Points: Used to create relationships between two tables. ; Helps prevent orphan records (records in the child table without a corresponding parent). ; Referenced column must be a primary key in the parent table. Example of Foreign Key: CREATE TABLE Students ( student_id NUMBER PRIMARY KEY, name VARCHAR2(50), class_id NUMBER ); CREATE TABLE Classes ( class_id NUMBER PRIMARY KEY, class_name VARCHAR2(50) ); ALTER TABLE Students ADD CONSTRAINT fk_class FOREIGN KEY (class_id) REFERENCES Classes(class_id); Here, class_id in Students references class_id in Classes. If a class is deleted, it prevents students from referencing a non-existing class.
-
What is the ROLLBACK command? Write its syntax with an example.
Topic: Transactions, SELECT INTO & Common SQL Errors
ROLLBACK is a SQL command used to undo uncommitted transactions. Syntax of ROLLBACK: ROLLBACK; Example Usage: BEGIN INSERT INTO employees (emp_id, name, salary) VALUES (101, 'Ali', 50000); ROLLBACK; -- Undo the INSERT operation END; Tip: If COMMIT is not executed, ROLLBACK undoes the changes.
-
Write a PL/SQL code that inserts a new row into a table, updates an existing row, and displays the number of rows before and after ROLLBACK.
Topic: Transactions, SELECT INTO & Common SQL Errors
This PL/SQL block will: (1) Insert a new row into a table. (2) Update an existing row. (3) Display the number of rows before and after ROLLBACK. PL/SQL Code: DECLARE row_count NUMBER; BEGIN -- Insert a new employee INSERT INTO employees (emp_id, name, salary) VALUES (102, 'Sara', 60000); -- Update salary of an employee UPDATE employees SET salary = 70000 WHERE emp_id = 101; -- Display number of rows affected SELECT COUNT(*) INTO row_count FROM employees; DBMS_OUTPUT.PUT_LINE('Total Rows Before ROLLBACK: ' || row_count); -- Undo changes ROLLBACK; -- Check row count after rollback SELECT COUNT(*) INTO row_count FROM employees; DBMS_OUTPUT.PUT_LINE('Total Rows After ROLLBACK: ' || row_count); END; Expected Output: Total Rows Before ROLLBACK: X -- (X includes new row count) Total Rows After ROLLBACK: Y -- (Y is original row count) Tip: ROLLBACK undoes all changes made after the last COMMIT. SELECT COUNT(*) is used to count total rows before and after rollback.
-
Write a PL/SQL code to display a student's name and address using SELECT INTO.
Topic: Transactions, SELECT INTO & Common SQL Errors
Scenario: A student table is given, and we need to retrieve Student Name and Address using SELECT INTO. PL/SQL Code: DECLARE student_name VARCHAR2(50); student_address VARCHAR2(100); student_id NUMBER := 101; -- Assuming we fetch details for Student ID 101 BEGIN SELECT name, address INTO student_name, student_address FROM Students WHERE student_id = 101; DBMS_OUTPUT.PUT_LINE('Student Name: ' || student_name); DBMS_OUTPUT.PUT_LINE('Student Address: ' || student_address); END; Explanation: SELECT INTO is used to fetch data into variables. It must return exactly one row, otherwise an error occurs (NO_DATA_FOUND or TOO_MANY_ROWS).
-
Find the error in the given SQL command and write the correct query: SELECT * FROM employee WHERE name = Ali;
Topic: Transactions, SELECT INTO & Common SQL Errors
Error: Missing single quotes ('Ali' should be in quotes). A string value must be enclosed in single quotes. Correct Query: SELECT * FROM employee WHERE name = 'Ali'; Other Common Errors: Using SELECT without FROM clause. ; Incorrect column names that don't exist in the table. ; Forgetting WHERE condition for UPDATE or DELETE queries.
-
Identify the entities and their attributes for a Library System scenario (for an Entity-Relationship Diagram).
Topic: Database Design & Entity-Relationship Diagrams
In database design, Entities represent real-world objects. For example, if the scenario was about a Library System, the entities might be: Book ; Student ; Librarian ; Loan Transaction Example ERD (Entity-Relationship Diagram) Entities: Entity / Attributes ; Student / Student_ID, Name, Age, Address ; Book / Book_ID, Title, Author, ISBN ; Loan / Loan_ID, Student_ID, Book_ID, Issue_Date, Return_Date
-
What is the COMMIT command? Write its syntax and explain when it is used.
Topic: Transactions & COMMIT
COMMIT is a transaction control (TCL) command used to permanently save all the changes made in the current transaction. Syntax: COMMIT; Once COMMIT is executed, the changes become permanent and ROLLBACK can no longer undo them. If COMMIT is not executed, ROLLBACK undoes the changes.
-
Differentiate between COMMIT and ROLLBACK.
Topic: Transactions & COMMIT
COMMIT / ROLLBACK ; Saves the changes of the current transaction permanently. / Undoes the uncommitted changes of the current transaction. ; Changes become permanent and visible as final data. / Changes made after the last COMMIT are reversed. ; After COMMIT, the changes cannot be undone by ROLLBACK. / Works only on changes that have not been committed.
-
Differentiate between the WHERE clause and the HAVING clause.
Topic: SQL Clauses & Group Functions
WHERE is used to filter individual rows; HAVING is used to filter groups after aggregation. ; WHERE is applied before grouping (before GROUP BY); HAVING is applied after the GROUP BY clause. ; WHERE cannot be used with group functions such as AVG() or COUNT(); HAVING is used with group functions, e.g. HAVING AVG(salary) > 50000.
-
What are group functions? Name the common group functions and give an example query.
Topic: SQL Clauses & Group Functions
Group functions perform operations (calculations) on multiple rows — a set or group of rows — and return a single result for the group. Common group functions: SUM(), AVG(), COUNT(), MAX() and MIN(). Example: SELECT COUNT(*) FROM employees; This returns the total number of employees.
-
Differentiate between a Primary Key and a Foreign Key.
Topic: Keys, SELECT INTO & Database Design
A Primary Key uniquely identifies each row of its own table; a Foreign Key is a column that references the primary key of another table. ; A table can have only one Primary Key; a table can have multiple Foreign Keys. ; A Primary Key cannot contain NULL values; a Foreign Key creates a relationship between two tables and ensures referential integrity.
-
Explain the NO_DATA_FOUND and TOO_MANY_ROWS exceptions that can occur with SELECT INTO.
Topic: Keys, SELECT INTO & Database Design
SELECT INTO is used to fetch data into variables and it must return exactly one row. NO_DATA_FOUND: occurs when the SELECT INTO query returns no row. ; TOO_MANY_ROWS: occurs when the SELECT INTO query returns more than one row.
-
What is referential integrity? How does a foreign key enforce it?
Topic: Keys, SELECT INTO & Database Design
Referential integrity means that a value in one table that refers to another table must always point to an existing record in the referenced (parent) table. A foreign key enforces referential integrity because the referenced column must be a primary key in the parent table. This prevents orphan records — records in the child table without a corresponding parent — for example, if a class is deleted, it prevents students from referencing a non-existing class.
-
What is an entity and what is an attribute? Give examples from a Library System.
Topic: Keys, SELECT INTO & Database Design
An entity is a real-world object in database design, for example Book, Student, Librarian and Loan Transaction in a Library System. An attribute is a property or characteristic of an entity. For example, the Book entity has the attributes Book_ID, Title, Author and ISBN, and the Student entity has Student_ID, Name, Age and Address.