0% found this document useful (0 votes)
35 views12 pages

1) Explain Alter and Create Command With One Example: Syntax

The ALTER TABLE statement is used to modify existing tables by adding, dropping, or modifying columns. The CREATE TABLE statement is used to create new tables and define column names and data types. Common SQL constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and INDEX. Operators perform arithmetic, comparison, and logical operations and include +, -, *, /, =, <>, >, <, >=, <=, AND, OR, and NOT. The GROUP BY clause groups query results by one or more columns, while a subquery allows a query to be nested inside another statement. Inner joins return rows where joined columns are equal, while outer joins return all rows including non-

Uploaded by

Areeba Shaikh
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)
35 views12 pages

1) Explain Alter and Create Command With One Example: Syntax

The ALTER TABLE statement is used to modify existing tables by adding, dropping, or modifying columns. The CREATE TABLE statement is used to create new tables and define column names and data types. Common SQL constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and INDEX. Operators perform arithmetic, comparison, and logical operations and include +, -, *, /, =, <>, >, <, >=, <=, AND, OR, and NOT. The GROUP BY clause groups query results by one or more columns, while a subquery allows a query to be nested inside another statement. Inner joins return rows where joined columns are equal, while outer joins return all rows including non-

Uploaded by

Areeba Shaikh
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/ 12

1) Explain Alter and Create command with one example

 The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.

a) alter-add new column


Syntax:-
->ALTER TABLE table_name
ADD column_name datatype;
Example:-
->ALTER TABLE Customers
ADD Email varchar(255);

b) alter-drop column
Syntax:-
->ALTER TABLE table_name
DROP COLUMN column_name;
Example:-
->ALTER TABLE Customers
DROP COLUMN Email;

c) alter- modify column:-


Syntax:-
->ALTER TABLE table_name
MODIFY column_name datatype;
Example:-
->ALTER TABLE customers
MODIFY Email varchar(12);

 CREATE Command

The CREATE TABLE statement is used to create a new table in a database.

Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The datatype parameter specifies the type of data the column can hold (e.g. varchar, char,
date,number )
The following example creates a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City:
Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

2) Explain all constraint with one example

 SQL constraints are used to specify rules for data in a table.

Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.
Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

 SQL constraints are used to specify rules for the data in a table.
The following constraints are commonly used in SQL:
•NOT NULL - Ensures that a column cannot have a NULL value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in
a table
•FOREIGN KEY - Uniquely identifies a row/record in another table
•CHECK - Ensures that all values in a column satisfies a specific condition
•DEFAULT - Sets a default value for a column when no value is specified
•INDEX - Used to create and retrieve data from the database very quickly

3) Explain operators of SQL with one example

  Operators are used to perform various mathematical, logical and comparison


operations on variables and constants.
 An operator manipulates data items and returns a result. An operator appears
before or after an operand or between two operands.
 Operators manipulate individual data items called operands.
 An operand can be a variable, constant, literal, operator or another expression.
 PL/SQL operators can be divided into the following categories:
1. Arithmetic operators: **, *, /, +, -
2. Comparison operators: = , != , <> , > ,< ,>= ,<=
3. Logical operators:

Arithmetic Operators

 Arithmetic operators are used for computational purposes.

Operator Description
+ Adds two operands
- Subtracts second operand from the first
* Multiplies both operands
/ Divides numerator by de-numerator
** Exponentiation operator, raises one operand to
the power of other

Comparison Operators

 These operators are used to compare values.

Operators Description
= Checks if the values of two operands are equal or not, if yes then
condition becomes true.
!= Checks if the values of two operands are equal or not
<> If values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.

Logical Operators
 If we want to check for more than one condition in a select query then we need to
use Boolean operators. Boolean operators can return only True or False for a given
condition.

Operators Description
AND Called the logical AND operator. If both the operands are true
then condition becomes true.
OR Called the logical OR Operator. If any of the two operands is true
then condition becomes true.
NOT Called the logical NOT Operator. Used to reverse the logical state
of its operand. If a condition is true then Logical NOT operator will
make it false.
4) Explain the difference between group by and sub query with one
example
Group By Sub query
The GROUP BY statement is used with the SQL sub queries are most frequently used
SQL SELECT statement. with the Select statement.
The GROUP BY statement follows the A sub query can be placed in a number of
WHERE clause in a SELECT statement and SQL clauses like WHERE clause, FROM
Precedes the ORDER BY clause. clause,
HAVING clause.
The GROUP BY statement is used with We can use Sub query with SELECT, UPDATE,
aggregation function. INSERT, DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN,
etc.

Example:
 Group By: SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
 Sub query: SELECT ProductName
FROM Products
WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE
Quantity = 10);

5) Explain inner join and outer join with one example


 INNER JOIN
 In SQL, INNER JOIN selects records that have matching values in both tables as long
as the condition is satisfied.
 It returns the combination of all rows from both the tables where the condition
satisfies.
 Syntax:
SELECT table1.column_name(s), table2.column_name(s)
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;
 Example:
SELECT product.prod_id, prod_name, sale_detail.qnty, product.price, cust_name
FROM product INNER JOIN sale_detail
ON product.prod_id=sale_detail.prod_id;
 OUTER JOIN
 Outer join works on both matching and non-matching data.
Types of Outer Join:
1. LEFT OUTER JOIN
 The SQL left join returns all the values from left table and the matching
values from the right table. If there is no matching join value, it will return
NULL.
 Syntax: SELECT table1.column_name(s), table2.column_name(s)
FROM table1 LEFT OUTER JOIN table2
ON table1.column_name = table2.column_name;
 Example: SELECT product.prod_id, prod_name, sale_detail.qnty,
product.price, cust_name
FROM product LEFT OUTER JOIN sale_detail
ON product.prod_id=sale_detail.prod_id;
2. RIGHT OUTER JOIN
 In SQL, RIGHT JOIN returns all the values from the values from the rows of
right table and the matched values from the left table.
 If there is no matching rows present in both tables, it will return NULL.
 Syntax: SELECT table1.column_name(s), table2.column_name(s)
FROM table1 RIGHT OUTER JOIN table2
ON table1.column_name = table2.column_name;
 Example: SELECT product.prod_id, prod_name, sale_detail.qnty,
product.price, cust_name
FROM product RIGHT OUTER JOIN sale_detail
ON product.prod_id=sale_detail.prod_id;
3. FULL OUTER JOIN
 In SQL, FULL JOIN is the result of a combination of both left and right outer
join.
 Join tables have all the records from both tables.
 It puts NULL on the place where matches not found.
 Syntax: SELECT table1.column_name(s), table2.column_name(s)
FROM table1 FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
 Example: SELECT product.prod_id, prod_name, sale_detail.qnty,
product.price, cust_name
FROM product FULL OUTER JOIN sale_detail
ON product.prod_id=sale_detail.prod_id;

6) Explain set operators with one example



1. UNION Operator
 The SQL Union operation is used to combine the result of two or more SQL SELECT
queries.
 In the union operation, name and data type of columns must be same in both the
tables on which UNION operation is being applied.
 The union operation eliminates the duplicate rows from its result set.
 Syntax:
SELECT column FROM table_1
UNION
SELECT column FROM table_2;
 Example:
SELECT prod_id FROM product
UNION
SELECT prod_id FROM sale_detail;
2. UNION ALL Operator
 Union All operation is similar to the Union operation, but it returns the set without
removing duplication and sorting the data.
 Name and data type of columns must be same in both the tables on which UNION
ALL operation is being applied.
 Syntax:
SELECT column FROM table_1
UNION ALL
SELECT column FROM table_2;
 Example:
SELECT dept_id FROM emp
UNION ALL
SELECT dept_id FROM dept;
3. INTERSECT Operator
 The Intersect operation returns the common rows from both the SELECT
statements.
 In the Intersect operation, name and data type of columns must be same in both
the tables on which INTERSECT operation is being applied.
 Syntax:
SELECT column FROM table_1
INTERSECT
SELECT column FROM table_2;
 Example:
SELECT dept_id FROM emp
INTERSECT
SELECT dept_id FROM dept;
4. MINUS Operator
 Minus operator is used to display the rows which are present in the first query but
absent in the second query.
 Name and data type of columns must be same in both the tables on which MINUS
operation is being applied.
 Syntax:
SELECT column FROM table_1
MINUS
SELECT column FROM table_2;
 Example:
SELECT dept_id FROM dept
MINUS
SELECT dept_id FROM emp;
7) Write a program to explain the blocks of PL/SQL

 Basic unit of PL/SQL is called as block, which allows grouping of related declarations and
statements.
 PL/SQL program is made up of blocks, and they can be nested within each other as required.
Structure of PL/SQL block:
HEADER
<header section>
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
• Declarations: This section starts with the keyword DECLARE. It is an optional section and
defines all variables, cursors, subprograms, and other elements to be used in the program.
• Executable Commands: This section is enclosed between the keywords BEGIN and END and
it is a mandatory section. It consists of the executable PL/SQL statements of the program. It
should have at least one executable line of code.
• Exception Handling: This section starts with the keyword EXCEPTION. This optional section
contains exception(s) that handle errors in the program.
 Example:
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
8) Explain sequence along with its attributes with one example

 Sequence is a set of integers 1, 2, 3, … that are generated and supported by some database
systems to produce unique values on demand.
 Sequences are frequently used in many databases because many applications require each
row in a table to contain a unique value and sequences provides an easy way to generate
them.
 The sequence of numeric values is generated in an ascending or descending order at defined
intervals can be configured to restart when exceeds max_value
 Syntax:
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
CYCLE|NOCYCLE;
sequence_name: Name of the sequence.
initial_value: starting value from where the sequence starts.
Initial_value should be greater than or equal
to minimum value and less than equal to maximum value.

increment_value: Value by which sequence will increment itself.


Increment_value can be positive or negative.

minimum_value: Minimum value of the sequence.


maximum_value: Maximum value of the sequence.

cycle: When sequence reaches its set_limit


it starts from beginning.

nocycle: An exception will be thrown


if sequence exceeds its max_value.
 Example:
DECLARE
Num1 NUMBER;
Num2 NUMBER;
BEGIN
SELECT seq_temp.NEXTVAL INTO num1 FROM dual;
SELECT seq_temp.CURRVAL INTO num2 FROM dual;
DBMS_OUTPUT.PUT_LINE(num1);
DBMS_OUTPUT.PUT_LINE(num2);
END;
/

9) Explain Control statement block of PL/SQL with one example


 Control structures decide the execution flow of the program depending on the
conditions defined by the developer.
 IF THEN
 IF THEN ELSE
 IF THEN ELSIF

PL/SQL IF THEN statement


 Syntax:
IF condition THEN
Statements;
END IF;
The condition is a Boolean expression that always evaluates to TRUE, FALSE,
or NULL. If the condition evaluates to TRUE, the statements after the THEN
execute. Otherwise, the IF statement does nothing.
 Example:
DECLARE
Num1 NUMBER;
Num2 NUMBER;
BEGIN
Num1:=5; Num2:=10;
IF Num1<Num2 THEN
DBMS_OUTPUT.PUT_LINE(Num1||’ is less than ‘ ||Num2);
END IF;
END;
/

PL/SQL IF THEN ELSE statement

In this type of IF statements there is a keyword ELSE followed by an alternative sequence of


statements which can executes when the condition with if evaluates to false.

 Syntax:
IF condition THEN
statements;
ELSE
else_statements;
END IF;
 Example:
DECLARE
Num1 NUMBER;
Num2 NUMBER;
BEGIN
Num1:=12; Num2:=10;
IF Num1<Num2 THEN
DBMS_OUTPUT.PUT_LINE(Num1||’ is less than ‘ ||Num2);
ELSE
DBMS_OUTPUT.PUT_LINE(Num1||’ is greater than ‘||Num2);
END IF;
END;
/

IF-THEN-ELSIF-THEN-ELSE statements
 Syntax:
IF condition_1 THEN
statements_1
ELSIF condition_2 THEN
statements_2
[ ELSIF condition_3 THEN
statements_3
]
...
[ ELSE
else_statements
]
END IF;
 Example:
DECLARE
percent NUMBER;
BEGIN
percent:=60;
IF percent > 75 THEN
DBMS_OUTPUT.PUT_LINE(‘Excellent’);
ELSIF percent > 50 THEN
DBMS_OUTPUT.PUT_LINE(‘Good’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘Poor’);
END IF;
END;
/

10) Explain procedure and function in PL/SQL with one example

 Procedure:
It is a sub program that consist of group, of PL/SQL each
procedure in oracle has its own unique name. This is stored as a
database object for future reference.
 Syntax:
CREATE [OR REPLACE] PROCEDURE pro_name (paral, para2,
…)
IS
Declare statements
BEGIN
Executable statements
END procedure name;
/
 Example:
CREATE OR REPLACE PROCEDURE p1(e in number)
IS
BEGIN
Dbms_output.put_line(‘square is’||e*e);
END;
/
 Function:
It is stand alone PL/SQL sub program it is mainly use for
calculation purpose it use keyword to return the value
 Syntax
CREATE [OR REPLACE] FUNCTION
function_name(parameter 1, parameter 2…) RETURN
datatype
IS
Declare variable, constant etc.
BEGIN
Executable Statements
Return (Return value);
END;
 Example:
CREATE OR REPLACE FUNCTION f1(n in number)
Return varchar2
IS
BEGIN
If(m mod 2=0)then
Return(‘even’);
ELSE
Return(‘odd’);
End if;
END;
/

11) Explain cursor with one example in PL/SQL


12) Explain Trigger with one example

You might also like