0% found this document useful (0 votes)
16 views4 pages

SQL and PL/SQL Q&A Guide

Answer

Uploaded by

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

SQL and PL/SQL Q&A Guide

Answer

Uploaded by

khaparderahil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL and PL/SQL Questions with Answers

1. Use of Four SET Operators

- UNION: Combines results from two or more queries, returning only unique rows.

- UNION ALL: Combines results from multiple queries, including duplicate rows.

- INTERSECT: Returns only rows that are common to both queries.

- MINUS: Returns rows from the first query that aren't in the second.

2. Four Aggregate Functions

- SUM(): Calculates the total sum of a numeric column.

- AVG(): Calculates the average of a numeric column.

- MAX(): Returns the highest value in a column.

- MIN(): Returns the lowest value in a column.

3. Two Advantages of PL/SQL

- Improved Performance: Allows for batch processing, reducing network traffic.

- Enhanced Security: PL/SQL code is stored and executed on the database, hiding internal logic

from users.

4. Use of Four Aggregate Functions

- SUM: Adds up all the values in a numeric column.

- AVG: Finds the average of values in a numeric column.

- COUNT: Counts the number of rows.

- MAX: Retrieves the maximum value in a column.

5. Syntax for Creating a Trigger


CREATE OR REPLACE TRIGGER trigger_name

BEFORE|AFTER INSERT|UPDATE|DELETE ON table_name

FOR EACH ROW

BEGIN

-- Trigger logic here

END;

6. PL/SQL Block Structure

- DECLARE: For declaring variables (optional).

- BEGIN: For executable statements (mandatory).

- EXCEPTION: For handling exceptions (optional).

- END: Terminates the block (mandatory).

7. Syntax for Creating and Updating a View

Creating a View:

CREATE VIEW view_name AS

SELECT column1, column2, ...

FROM table_name

WHERE condition;

Updating a View:

CREATE OR REPLACE VIEW view_name AS

SELECT column1, column2, ...

FROM table_name

WHERE condition;
8. Syntax for Creating Synonyms with Example

Syntax:

CREATE SYNONYM synonym_name FOR object_name;

Example:

CREATE SYNONYM emp_syn FOR employee;

(a) SQL Queries for stud Table

1. Display names of students with minimum marks in sub1:

SELECT name

FROM stud

WHERE sub1 = (SELECT MIN(sub1) FROM stud);

2. Display names of students with above 40 marks in sub2:

SELECT name

FROM stud

WHERE sub2 > 40;

3. Display count of students failed in sub2 (assuming passing marks are 40):

SELECT COUNT(*)

FROM stud

WHERE sub2 < 40;

4. Display names of students whose names start with 'A' in ascending order of sub1 marks:

SELECT name
FROM stud

WHERE name LIKE 'A%'

ORDER BY sub1 ASC;

(b) Four DML Commands with Examples

1. INSERT: Adds a new record.

INSERT INTO stud (roll_no, name, sub1, sub2, sub3)

VALUES (1, 'John', 80, 75, 85);

2. UPDATE: Modifies data.

UPDATE stud

SET sub1 = 90

WHERE roll_no = 1;

Common questions

Powered by AI

Aggregate functions in SQL condense and summarize data, making them essential for comprehensive data analysis . SUM() is used for calculating the total sum of a numeric column, providing insights into the cumulative values . AVG() calculates the average, essential for assessing central tendency across data points . MAX() returns the highest value, which is critical for identifying peak observations, while MIN() does the opposite, retrieving the lowest value to find the minimum points . These functions enable users to perform complex data analyses efficiently by processing data at a high abstraction level.

The structure of a PL/SQL block includes DECLARE, BEGIN, EXCEPTION, and END sections. DECLARE is optional and used for variable declaration needed within the block . BEGIN, which is mandatory, contains the executable statements, forming the core of processing logic . EXCEPTION allows for handling errors and exceptions, enabling control flow to manage issues like non-existent data or calculation errors, thereby preventing system crashes or unexpected behavior . The END statement marks the block's termination, ensuring that the control flow is well-defined and logical coherence is maintained . Together, these components form a robust structure allowing comprehensive control over database operations, including error management.

Creating a view in SQL utilizes the syntax: CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition; updating a view is done via CREATE OR REPLACE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition; . Views offer an abstracted representation of data, allowing users to simplify complex queries by encapsulating them into a single query readable as a table . They can enhance security by restricting user access to specific data or simplifying data presentation for reporting and analytics . Views are widely used for joining multiple tables or filtering data dynamically without altering the underlying database structure.

SQL synonyms function as aliases for database objects such as tables, functions, or views, providing a simplified and less complex reference system that can hide the real names and locations of database objects . By using syntax like CREATE SYNONYM synonym_name FOR object_name, developers can ease the integration of complex or nested databases by allowing more intuitive names . Synonyms also facilitate application and query portability since changes to database object names or structures do not require widespread modifications across applications or queries, thus ensuring seamless evolution of the database schema.

The syntax for creating a trigger in SQL is as follows: CREATE OR REPLACE TRIGGER trigger_name BEFORE|AFTER INSERT|UPDATE|DELETE ON table_name FOR EACH ROW BEGIN -- Trigger logic here END; . Triggers are crucial in automatically enforcing business rules and ensuring data integrity in database operations . By executing predefined sets of operations in response to specific events (such as INSERT, UPDATE, DELETE), triggers help automate processes like logging, auditing, and enforcing constraints or custom logic without manual intervention . This automation optimizes operations by reducing the potential for human error and providing real-time responses to database changes.

Triggers complement DML operations such as INSERT, UPDATE, and DELETE by automatically executing specific actions in response to these events, thus ensuring data consistency and integrity adhering to predefined rules . For instance, a trigger can validate data upon insertion or update to ensure constraints, or automatically update related tables to keep data aligned across the database . They also manage audit logs by recording changes, which is vital for compliance and operational transparency. By embedding logic directly to respond to these operations, triggers streamline processes and minimize error risks associated with manual interventions.

PL/SQL improves performance by allowing batch processing, which reduces network traffic between the application and database server, leading to faster execution of operations . In terms of security, PL/SQL code is stored and executed on the database server, keeping the internal business logic hidden from users, minimizing the risk of unauthorized access to sensitive operations or data . These enhancements make database management more efficient by streamlining operations and safeguarding critical processes against potential security breaches.

In scenarios involving complex business logic, such as financial transactions requiring multiple condition checks and operations, PL/SQL blocks are more beneficial than simple SQL queries . PL/SQL allows grouping of operations with embedded exception handling and control structures like loops and conditionals . This enables seamless execution of compound logic where multiple SQL commands need to be executed in a sequence, with rollback capabilities if errors occur, preserving data integrity. Exception handling within PL/SQL blocks efficiently manages runtime errors, offering robust transaction management and ensuring that application logic is processed accurately without client-side complexities.

PL/SQL executes code directly on the database server, allowing batch processing that reduces the frequency and volume of data transferred between the client and server, thus enhancing network efficiency for large-scale applications . This server-side processing encapsulates application logic within the database environment, which centralizes control and reduces complexity in application maintenance as business rules and data processing remain consistent and are not dispersed across client applications . This not only optimizes resource utilization but also strengthens data security by limiting the exposure of sensitive operations to end users.

UNION and UNION ALL both combine results from two or more queries. The difference lies in that UNION returns only unique rows, omitting duplicates, whereas UNION ALL includes all rows, allowing duplicates . INTERSECT instead focuses on identifying rows common to both queries, providing only the overlapping data . MINUS serves a different purpose by returning rows from the first query that do not appear in the second, essentially performing a subtractive operation on the sets . Whereas UNION and UNION ALL deal with aggregation, INTERSECT and MINUS handle selection based on exclusive criteria.

You might also like