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

Ilovepdf Merged

The document provides examples of executing DDL, DCL, DML and TCL commands in SQL. It first creates a Student_info table using the CREATE TABLE command and describes the columns. It then demonstrates TRUNCATE, RENAME, ALTER TABLE commands under DDL. Under DML it shows SELECT, INSERT, DISTINCT, ORDER BY, DELETE, UPDATE commands. The document also demonstrates GRANT and REVOKE privileges using DCL commands. It grants ALL and specific privileges to another user and selects data to show the privileges have been granted.

Uploaded by

Harsh Chaudhary
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)
19 views124 pages

Ilovepdf Merged

The document provides examples of executing DDL, DCL, DML and TCL commands in SQL. It first creates a Student_info table using the CREATE TABLE command and describes the columns. It then demonstrates TRUNCATE, RENAME, ALTER TABLE commands under DDL. Under DML it shows SELECT, INSERT, DISTINCT, ORDER BY, DELETE, UPDATE commands. The document also demonstrates GRANT and REVOKE privileges using DCL commands. It grants ALL and specific privileges to another user and selects data to show the privileges have been granted.

Uploaded by

Harsh Chaudhary
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
You are on page 1/ 124

Name - Bhavya Choubey

Reg. No. – 21BCE0976


EXP NO. -1
Submission date-19/5/23

DDL,DCL,DML AND TCL COMMANDS

AIM:-
To execute the different commands for ddl,dcl,dml and tcl and show the
outputs accordingly.
DDL Commands

The Create Table Command


The create table command defines each column of the table uniquely. Each
column has minimum of three attributes.
• Name • Data type • Size(column width).
Each table column definition is a single clause in the create table syntax.
Each table column definition is separated from the other by a comma.
Finally, the SQL statement is terminated with a semicolon.

SQL> CREATE TABLE Student_info (Reg_no varchar2(10),Name char(30),DOB


date,Address varchar2(50));

Table created.

SQL> DESC Student_info;

Name Null? Type

REG_NO VARCHAR2(10)
NAME CHAR(30)
DOB DATE
ADDRESS VARCHAR2(50)
The TRUNCATE Command

SQL> TRUNCATE TABLE Student_info;

Table truncated.

SQL> DESC Student_info;

Name Null? Type

REG_NO VARCHAR2(10)
NAME CHAR(30)
DOB DATE
ADDRESS VARCHAR2(50)

The RENAME Command


Sometimes we may want to rename our table to give it a more relevant name.
For this purpose, we can use ALTER TABLE to rename the name of the table.SQL
ALTER TABLE is a command used to modify the structure of an existing table in a
database.
Note: Syntax may vary in different databases.

SQL> RENAME Student_info TO Student_details;

Table renamed.
SQL> DESC Student_details;

Name Null? Type

REG_NO VARCHAR2(10)
NAME CHAR(30)
DOB DATE
ADDRESS VARCHAR2(50)

The old name table was Student_info now new name is the Student_details.
The ALTER Table Command
Adding New Columns
By The use of ALTER TABLE Command
we can modify our exiting table.

SQL> ALTER TABLE Student_details ADD (Age number(2),Marks number(3));

Table altered.

SQL> DESC Student_details;

Name Null? Type

REG_NO VARCHAR2(10)
NAME CHAR(30)
DOB DATE
ADDRESS VARCHAR2(50)
AGE NUMBER(2)
MARKS NUMBER(3)
Dropping a Column from the Table
SQL> ALTER TABLE Student_details DROP COLUMN Address;

Table altered.

SQL> DESC Student_details;

Name Null? Type

REG_NO VARCHAR2(10)
NAME CHAR(30)
DOB DATE
AGE NUMBER(2)
MARKS NUMBER(3)
Modifying Existing Table
SQL> ALTER TABLE Student_details MODIFY (Name varchar2(40));

Table altered.

SQL> DESC Student_details;

Name Null? Type

REG_NO VARCHAR2(10)
NAME VARCHAR2(40)
DOB DATE
AGE NUMBER(2)
MARKS NUMBER(3)

The DROP Command

SQL> DROP TABLE Student_details;

Table dropped.

SQL> DESC
Student_details;

ERROR:
ORA-04043: object Student_details does not exist
DML Commands

Viewing Data in the Table (Select Command)


Once data has been inserted into a table, the next most logical operation
would be to view what has been inserted. The SELECT SQL verb is used to
achieve this.
SQL> CREATE TABLE Student (Reg_no varchar2(10),Name char(30),DOB
date,Address varchar2(50));

Table created.

SQL> SELECT * FROM Student;

no rows selected

SQL> DESC

Student;

Name

Null? Type

REG_NO
VARCHAR2(1)

NAME

CHAR(30)

DOB

DATE

ADDRESS

VARCHAR2(50)
Insert Command
More than one are there to Insert data into a table.
Note : Character expression placed within the insert into statement must be
enclosed in single quotes (').
In addition to inserting data one row at a time into a table, it is quite possible
to populate a table with data that already exist in another table. You can store
same record in a table that already stored in another table.
SQL> INSERT INTO Student VALUES('21BCE0976','Bhavya','15-Nov-03','Bihar');

1 row created.

SQL> INSERT INTO Student VALUES('21BCE0984','Sneha','28-Apr-03','UP');

1 row created.

SQL> INSERT INTO Student VALUES('21BCI0265','Saumyaa','21-Mar-


03','Rajasthan')

1 row created.

SQL> INSERT INTO Student VALUES('21BCE3351','Ananya','20-Mar-


03','Gujarat');
1 row created.
SQL> SELECT * FROM Student;

REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan

REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat

SQL> SELECT NAME FROM Student WHERE Address='Bihar';

NAME

Bhavya
Eliminating Duplicates:
A table could hold duplicate rows. In such a case, you can eliminate duplicates.
It scans through entire rows, and eliminates rows that have exactly the same
contents in each column.
SQL> SELECT DISTINCT * FROM Student;

REG_NO NAME DOB

ADDRESS

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan

21BCE0976 Bhavya 15-NOV-03


Bihar

REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat
Sorting DATA:
The Rows retrieved from the table will be sorted in either Ascending or
Descending order depending on the condition specified in select statement,
the Keyword has used ORDER BY. It will in show records as alphabetical order
from A to Z ascending order. If you want Descending order means Z to A then
used DESC Keyword at last.
SQL> SELECT *FROM Student ORDER BY Name;

REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCI0265 Saumyaa 21-MAR-03


Rajasthan

REG_NO NAME DOB

ADDRESS

21BCE0984 Sneha 28-APR-03


UP
DELETE Operation
The DELETE command can remove all the rows from the table or a set of rows
from the table.

SQL> DELETE FROM Student WHERE DOB='21-Mar-03';

1 row deleted.

SQL> SELECT * FROM Student;

REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCE3351 Ananya 20-MAR-03


Gujarat

ALTER TABLE command


SQL> ALTER TABLE Student DROP COLUMN Address;

Table altered.
SQL> SELECT * FROM Student;

REG_NO NAME DOB

21BCE0976 Bhavya 15-NOV-03


21BCE0984 Sneha 28-APR-03
21BCE3351 Ananya 20-MAR-03

UPDATE Operation
The UPDATE command is used to change or modify data values in a table and
UPDATE command can Update all the rows from the table or a set of rows
from the table.
SQL> UPDATE Student SET DOB='18-June-04' WHERE Reg_no='21BCE0984';

1 row updated.

SQL> SELECT * FROM Student;

REG_NO NAME DOB

21BCE0976 Bhavya 15-NOV-03


21BCE0984 Sneha 18-JUN-04
21BCE3351 Ananya 20-MAR-03
DCL Commands
This is a standard subset of SQL that is used for security management. Most
databases have their own flavored syntax, but generally, there exist two
commands: GRANT, and REVOKE, these ‘grant’ and ‘remove’ privileges.
SQL> CREATE TABLE Student (Reg_no varchar2(10),Name char(30),DOB
date,Address varchar2(50));

Table created.
SQL> INSERT INTO Student VALUES('21BCE0976','Bhavya','15-Nov-03','Bihar');

1 row created.

SQL> INSERT INTO Student VALUES('21BCE0984','Sneha','28-Apr-03','UP');

1 row created.

SQL> INSERT INTO Student VALUES('21BCI0265','Saumyaa','21-Mar-


03','Rajasthan')

1 row created.
SQL> INSERT INTO Student VALUES('21BCE3351','Ananya','20-Mar-
03','Gujarat');
1 row created.
SQL> SELECT * FROM Student;

REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03

Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan

REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat
GRANT Statement
Objects that are created by a user are owned and controlled by that user. If a
user wishes to access any of the objects belonging to another user, the owner
of the object will have to give permissions for such access. This is called
GRANTING of PRIVILEGES.
Objects that are created by a user are owned and controlled by that user. If a
user wishes to access any of the objects belonging to another user, the owner
of the object will have to give permissions for such access. This is called
GRANTING of PRIVILEGES. Privileges once given can be taken back by the
owner of the object. This is called REVOKING of PRIVILEGES.

SQL> GRANT ALL ON Student TO "21BCE0984" WITH GRANT OPTION;

Grant succeeded.

SQL> GRANT SELECT,UPDATE ON Student TO "21BCE0984" WITH GRANT


OPTION;

Grant succeeded.

SQL> SELECT * FROM


"21BCE0976".Student;
REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan
REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat

REVOKE Statement
Privileges once given can be taken back by the owner of the object. This is
called REVOKING of PRIVILEGES.
The object owner can revoke privileges granted to another user. A user of
an object who is not the owner, but has been granted the GRANT
privileges, has the power to REVOKE the privileges from grantee.
Revoking the Permission using the REVOKE command.

SQL> REVOKE ALL ON Student FROM "21BCE0984";


Revoke succeeded.
SQL> REVOKE UPDATE ON Student FROM "21BCE0984";
Revoke succeeded.
SQL> SELECT * FROM
"21BCE0976".Student;

SELECT * FROM "21BCE0976".Student


*
ERROR at line 1: ORA-00942: table or view does not exist
Since the revoke command is given therefore the table from my oracle id is no
longer available in my friend’s oracle id. And hence the command prompt
gave an error that the table does not exist.

SQL> INSERT INTO "21BCE0976".Student VALUES('21BCE2345','Ritu','27-Aug-


03','Tamil
Nadu');
1 row created.
SQL> SELECT * FROM
"21BCE0976".Student;
REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan
REG_NO NAMEDOBADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat
21BCE2345 Ritu 27-Aug-03
Tamil Nadu
SQL> ALTER TABLE "21BCE0984".Student DROP COLUMN DOB;
Table altered.
MY ID

SQL> SELECT * FROM Student;

REG_NO NAME ADDRESS

21BCE0976 Bhavya BIHAR


21BCE0984 Sneha UP
21BCI0265 Saumyaa RAJASTHAN
21BCE3351 Ananya GUJARAT
21BCE2345 RITU TAMIL NADU

Whenever we insert a new row using someone else’s id in our table, the
changes will only be reflected in the other id and not in the original table. This
is because inserting is a DML command. Whereas all the other commands
such as ALTER, DELETE , DROP, etc will work fine in all the id’s.
TCL Commands
Transactions group a set of tasks into a single execution unit. Each transaction
begins with a specific task and ends when all the tasks in the group successfully
complete. If any of the tasks fail, the transaction fails. Therefore, a transaction
has only two results: success or failure. Hence, the following TCL commands
are used to control the execution of a transaction.
SQL> CREATE TABLE Student (Reg_no varchar2(10),Name char(30),DOB
date,Address varchar2(50));

Table created.
SQL> INSERT INTO Student VALUES('21BCE0976','Bhavya','15-Nov-03','Bihar');

1 row created.

SQL> INSERT INTO Student VALUES('21BCE0984','Sneha','28-Apr-03','UP');

1 row created.

SQL> INSERT INTO Student VALUES('21BCI0265','Saumyaa','21-Mar-


03','Rajasthan')

1 row created.

SQL> INSERT INTO Student VALUES('21BCE3351','Ananya','20-Mar-


03','Gujarat');
1 row created.
SQL> SELECT * FROM Student;

REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03

Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan

REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat
COMMIT
A COMMIT ends the current transaction and makes permanent any changes
made during the transaction. All transaction locks acquired on tables are
released. If everything is in order with all statements within a single
transaction, all changes are recorded together in the database is
called committed. The COMMIT command saves all the transactions to the
database since the last COMMIT or ROLLBACK command.

SQL> UPDATE Student SET Reg_no='21BCE9909' WHERE NAME = 'Ananya';


1 row updated.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM Student;
REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan
REG_NO NAME DOB

ADDRESS

21BCE9909 Ananya 20-MAR-03


Gujarat

ROLLBACK
A ROLLBACK does exactly the opposite of COMMIT. It ends the transaction but
undoes any changes made during the transaction. All transaction locks
acquired on tables are released. If any error occurs with any of the SQL
grouped statements, all changes need to be aborted. The process of reversing
changes is called rollback. This command can only be used to undo
transactions since the last COMMIT or ROLLBACK command was issued.

SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM Student;
REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar
21BCE0984 Sneha 28-APR-03
UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan

REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat

SAVEPOINT
SAVEPOINT marks and saves the current point in the processing of a
transaction. When a SAVEPOINT is used with a ROLLBACK statement, parts of
a transaction can be undone.
SAVEPOINT creates points within the groups of transactions in which to
ROLLBACK.
A SAVEPOINT is a point in a transaction in which you can roll the transaction
back to a certain point without rolling back the entire transaction.

SQL> SAVEPOINT inst1;


Savepoint created.

SQL> INSERT INTO Student VALUES('21BCE2345','Ritu','27-Aug-03','Tamil


Nadu');
1 row created.

SQL> SAVEPOINT inst2;


Savepoint created.
SQL> INSERT INTO Student VALUES('21BCE0001','Richa','1-Feb-03','Punjab');
1 row created.
SQL> SAVEPOINT inst3;
Savepoint created.

SQL> UPDATE Student SET DOB='6-June-05' WHERE NAME = 'Saumyaa';


1 row updated.

SQL> SELECT * FROM Student;


REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 06-June-05


Rajasthan
REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat
21BCE2345 Ritu 27-Aug-03
Tamil Nadu
21BCE0001 Richa 1-Feb-03
Punjab

SQL> ROLLBACK TO inst2;


Savepoint created.

SQL> SELECT * FROM Student;


REG_NO NAME DOB

ADDRESS

21BCE0976 Bhavya 15-NOV-03


Bihar

21BCE0984 Sneha 28-APR-03


UP

21BCI0265 Saumyaa 21-MAR-03


Rajasthan
REG_NO NAME DOB

ADDRESS

21BCE3351 Ananya 20-MAR-03


Gujarat
21BCE2345 Ritu 27-Aug-03
Tamil Nadu
21BCE0001 Richa 1-Feb-03
Punjab

RESULT:-
Hence we performed the basic and important commands on oracle and
attached the outputs for DDL,DCL,DML AND TCL.
Name: Bhavya Choubey
Reg No: 21BCE0976

Date of submission:- 26 May 2023

LAB ASSESSMENT 2
Constraints and Single row function

AIM:
To execute the sql query commands for primary key constraint, foreign
key constraint, unique constraint, single row functions and multiple
row functions.

DESCRIPTION:
Primary key, foreign key and Unique constraints.

Create Table
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.

SQL Query:
CREATE TABLE Student_information(Name varchar2(10),Reg_no
varchar2(10) NOT NULL,Age number(5),DOB date);

Output:
Table created.
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (After implementing a SQL query):


SQL> desc Student_information;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO NOT NULL VARCHAR2(10)
AGE NUMBER(5)
DOB DATE

Creating another table:


Status of table (Before implementing a SQL query):
No table is created.

SQL Query:
CREATE TABLE Student_result(Name varchar2(10),Reg_no
varchar2(10) ,Grades varchar2(4),CGPA number(4));

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> desc Student_result;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO VARCHAR2(10)
GRADES VARCHAR2(4)
CGPA NUMBER(4)

Primary key constraint (without constraint name)


The PRIMARY KEY constraint in Oracle SQL is used to uniquely
identify each row in a table. It is a combination of the NOT NULL and
UNIQUE constraints. The PRIMARY KEY constraint is used to
enforce data integrity by ensuring that each row in a table has a unique
identifier and cannot have a null value.
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO NOT NULL VARCHAR2(10)
AGE NUMBER(5)
DOB DATE

SQL Query:
ALTER TABLE Student_information ADD PRIMARY KEY
(Reg_no);

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> desc Student_information;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO NOT NULL VARCHAR2(10)
AGE NUMBER(5)
DOB DATE

Foreign key constraint (with constraint name)


The purpose of a FOREIGN KEY constraint in Oracle SQL is to
enforce referential integrity between two tables in a database. It is used
to ensure that the values in the foreign key column(s) of a table
correspond to the values in the primary key column(s) of another table.
This constraint helps to maintain the consistency and accuracy of data
between related tables in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO VARCHAR2(10)
Name: Bhavya Choubey
Reg No: 21BCE0976

GRADES VARCHAR2(4)
CGPA NUMBER(4)

SQL Query:
SQL> ALTER TABLE Student_result ADD CONSTRAINT FK_SR
FOREIGN KEY(Reg_no) REFERENCES Student_information
(Reg_no);

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> DESC Student_information;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO VARCHAR2(10)
GRADES VARCHAR2(4)
CGPA NUMBER(4)

Insert
The INSERT command is used to add one or more rows of data to a
table in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO NOT NULL VARCHAR2(10)
AGE NUMBER(5)
DOB DATE

SQL Query:
SQL> INSERT INTO Student_information VALUES
('Bhavya','21BCE0976','19','15-nov-2003');
Name: Bhavya Choubey
Reg No: 21BCE0976

1 row created.

SQL> INSERT INTO Student_information VALUES


('Riya','21BCT2165','20','8-april-2003');
1 row created.

SQL> INSERT INTO Student_information VALUES


('Nancy','21BCE3351','21','24-oct-2002');
1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_information;

NAME REG_NO AGE DOB


---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02

Insert in another table


Status of table (Before implementing a SQL query):
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO VARCHAR2(10)
GRADES VARCHAR2(4)
CGPA NUMBER(4)

SQL Query:
SQL> INSERT INTO Student_result VALUES
Name: Bhavya Choubey
Reg No: 21BCE0976

('Bhavya','21BCE0976','S','9.3');
1 row created.

SQL> INSERT INTO Student_result VALUES


('Riya','21BCT2165','A','8.7');
1 row created.

SQL> INSERT INTO Student_result VALUES


('Nancy','21BCE3351','C','7.4');
1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_result;

NAME REG_NO GRAD CGPA


---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7

Checking if the primary key constraint is working


Since the Reg_no is primary key in the table ‘Student_information’
therefore the table ‘Student_result’ cannot have any value of Reg_no
which is not present in the primary key table.

Status of table (Before implementing a SQL query):


NAME REG_NO GRAD CGPA
---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL Query:
SQL> INSERT INTO Student_result VALUES
('Anaya','21BCI5434','B','7.8');

Output:
SQL>INSERT INTO Student_result VALUES
('Anaya','21BCI5434','B','7.8');
*
ERROR at line 1:
ORA-01735: integrity constraint (SYSTEM.FK_SR) violated - parent
key not found

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_result;

NAME REG_NO GRAD CGPA


---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7

Displaying all the constraints from primary key table


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

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
SQL Query:
SQL> SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
FROM USER_CONSTRAINTS WHERE TABLE_NAME =
'Student_information';
Name: Bhavya Choubey
Reg No: 21BCE0976

Output:
CONSTRAINT_NAME C
------------------------------ -
SYS_C009042 C
SYS_C009043 P

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_result;
NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02

Displaying all the constraints from foreign key table


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

Status of table (Before implementing a SQL query):


NAME REG_NO GRAD CGPA
---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7

SQL Query:
SQL> SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
FROM USER_CONSTRAINTS WHERE TABLE_NAME =
'Student_result';

Output:
CONSTRAINT_NAME C
------------------------------ -
FK_SR R
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_result;
NAME REG_NO GRAD CGPA
---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7

Removing Foreign key constraint from the table


Deleting a foreign key constraint removes the requirement to enforce
referential integrity.

Status of table (Before implementing a SQL query):


NAME REG_NO GRAD CGPA
---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7

SQL Query:
SQL> ALTER TABLE Student_result DISABLE CONSTRAINT
FK_SR;

Output:
Table altered.

Now try inserting:


SQL Query:
SQL>INSERT INTO Student_result VALUES
('Anaya','21BCI5434','B','7.8');

Output:
1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_result;
NAME REG_NO GRAD CGPA
---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7
Anaya 21BCI5434 B 8

Enabling the Foreign key constraint in the table


The FOREIGN KEY constraint identifies the relationships between the
database tables by referencing a column, or set of columns, in the
Child table that contains the foreign key, to the PRIMARY KEY
column or set of columns, in the Parent table.

Status of table (Before implementing a SQL query):


NAME REG_NO GRAD CGPA
---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7
Anaya 21BCI5434 B 8

SQL Query:
SQL> ALTER TABLE Student_result ENABLE CONSTRAINT
FK_SR;

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_result;
NAME REG_NO GRAD CGPA
---------- ---------- ---- ----------
Bhavya 21BCE0976 S 9
Riya 21BCT2165 A 9
Nancy 21BCE3351 C 7
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL>INSERT INTO Student_result VALUES


('Anaya','21BCI5434','B','7.8');
*
ERROR at line 1:
ORA-01735: integrity constraint (SYSTEM.FK_SR) violated - parent
key not found.

Removing PRIMARY key constraint from the table


Deleting a primary key constraint removes the requirement to enforce
referential integrity.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02

SQL Query:
SQL> ALTER TABLE Student_result DISABLE CONSTRAINT
FK_SR;

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_information;
NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Name: Bhavya Choubey
Reg No: 21BCE0976

Adding check constraint


The CHECK constraint is used to limit the value range that can be
placed in a column.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02

SQL Query:
SQL> ALTER TABLE Student_information ADD CONSTRAINT
CHK_Age CHECK(Age>=10);

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> INSERT INTO Student_information VALUES
('Anaya','21BCE3351','8','30-dec-2015');
INSERT INTO Student_information VALUES
('Anaya','21BCE3351','8','30-dec-2015');
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.CHK_AGE) violated

SQL> SELECT * FROM Student_information;


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Name: Bhavya Choubey
Reg No: 21BCE0976

Dropping check constraint


The DROP CONSTRAINT command is used to delete a UNIQUE,
PRIMARY KEY, FOREIGN KEY, or CHECK constraint.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02

SQL Query:
SQL> ALTER TABLE Student_information DROP CONSTRAINT
CHK_Age;

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> INSERT INTO Student_information VALUES
('Anaya','21BCE3351','8','30-dec-2015');
1 row created.

SQL> SELECT * FROM Student_information;


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015
Name: Bhavya Choubey
Reg No: 21BCE0976

Unique constraint
The UNIQUE constraint in Oracle SQL ensures that all values in a
column of a table are different. It requires that no two rows in the table
can have the same value for the column. This constraint is commonly
used to ensure data integrity by preventing the insertion of duplicate
data into a table.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015

SQL Query:
SQL> ALTER TABLE Student_information ADD CONSTRAINT
UQ_SR UNIQUE(Name);

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> INSERT INTO Student_information VALUES
('Niha','21BCE7221','18','30-dec-2015');
INSERT INTO Student_information VALUES
('Niha','21BCE7221','18','30-dec-2015');
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.UQ_SR) violated
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL> SELECT * FROM Student_information;


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015

Dropping the Unique key constraint


Deleting a unique constraint removes the requirement for uniqueness
for values entered in the column or combination of columns included
in the constraint expression and deletes the corresponding unique
index.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015

SQL Query:
SQL> ALTER TABLE Student_information DROP CONSTRAINT
UQ_SR;

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> INSERT INTO Student_information VALUES
('Niha','21BCE7221','18','30-dec-2015');
1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL> SELECT * FROM Student_information;


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015
Niha 21BCE7221 18 30-DEC-2015

Not Null constraint


The NOT NULL constraint in Oracle SQL ensures that a column in a
table cannot have a NULL value. It requires that every row in the table
must have a value for the columnthat is not NULL. This constraint is
commonly used to ensure data integrity by preventing the insertion of
incomplete or missing data into a table.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015
Niha 21BCE7221 18 30-DEC-2015

SQL Query:
SQL> INSERT INTO Student_information VALUES
('Srishti','NULL','18','30-dec-2015');
Name: Bhavya Choubey
Reg No: 21BCE0976

Output:
INSERT INTO Student_information VALUES
('Srishti','NULL','18','30-dec-2015');
*
ERROR at line 1:
ORA-02172: cannot insert NULL into
("SYSTEM"."STUDENT_INFORMATION"."REG_NO")
Status of table (After implementing a SQL query):
SQL> SELECT * FROM Student_information;
NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015
Niha 21BCE7221 18 30-DEC-2015

Default constraint
The DEFAULT constraint in Oracle SQL is used to set a default value
for a column in a table. If no value is specified for the column during
an insert operation, the default value will be used instead. This is useful
when you want to ensure that a specific value is always present in a
column, even if it is not explicitly specified.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015
Niha 21BCE7221 18 30-DEC-2015
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL Query:
SQL> ALTER TABLE Student_information MODIFY AGE
DEFAULT '8';

Output:
Table altered.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_information;
NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 21BCE0976 19 15-NOV-03
Riya 21BCT2165 20 08-APR-03
Nancy 21BCE3351 21 24-OCT-02
Anaya 21BCE3351 8 30-DEC-2015
Niha 21BCE7221 18 30-DEC-2015
Name: Bhavya Choubey
Reg No: 21BCE0976

DESCRIPTION:
Single row functions

Create Table
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.

SQL Query:
SQL> CREATE TABLE Student_data(Name varchar2(10), Reg_no
number(4), Age number, DOB date);

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> desc Student_data;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO NUMBER(4)
AGE NUMBER
DOB DATE

Insert
The INSERT command is used to add one or more rows of data to a
table in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
Name: Bhavya Choubey
Reg No: 21BCE0976

----------------------------------------- -------- ----------------------------


NAME VARCHAR2(10)
REG_NO NUMBER(4)
AGE NUMBER
DOB DATE

SQL Query:
SQL> INSERT INTO Student_data VALUES ('Bhavya','0976','20','15-
nov-2003');
1 row created.
SQL> INSERT INTO Student_data VALUES ('Anaya','2776','19','29-
april-2004');
1 row created.
SQL> INSERT INTO Student_data VALUES ('Nancy','6453','18','8-
jan-2005');
1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_data;

NAME REG_NO AGE DOB


---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6453 18 08-JAN-05

ASCII
This function Returns the ASCII code value of a keyboard button and
the rest etc (@,R,9,*).
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6453 18 08-JAN-05

SQL Query:
SQL> SELECT ASCII(Name) AS NumCodeOfFirstChar FROM
Student_data;

Output and Status of table (After implementing a SQL query):


NUMCODEOFFIRSTCHAR
------------------
66
65
78

SPACE
Returns spaces in your SQL query (you can specify the size of space).

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6453 18 08-JAN-05

SQL Query:
SQL>SELECT (Name)+SPACE(1)+(Reg_no) AS SPACE_1 FROM
Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976

Output and Status of table (After implementing a SQL query):


SPACE_1
----------
Bhavya 976
Anaya 2776
Nancy 6453

CHARINDEX
Returns the starting position of a character string.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6453 18 08-JAN-05

SQL Query:
SQL> SELECT CHARINDEX('N',Name) AS MatchPosition FROM
Student_data;

Output and Status of table (After implementing a SQL query):


MatchPosition
------------------
0
2
3

REPLACE
Replaces all occurrences of the string2 in the string1 with string3.
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6453 18 08-JAN-05

SQL Query:
SQL> UPDATE Student_data SET Reg_no = REPLACE
(Reg_no,'4','0');

Output:
3 rows updated.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_data;
NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

QUOTENAME
Returns a Unicode string with the delimiters added to make the input
string a valid Microsoft® SQL Server™ delimited identifier.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL Query:
SQL> SELECT QUOTENAME(Name, '[]') AS UNICODE FROM
Student_data;
Output and Status of table (After implementing a SQL query):
UNICODE
------------------
[BHAVYA]
[ANAYA]
[NANCY]

STUFF
Deletes a specified length of characters and inserts string at a specified
starting index.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT STUFF(Name, 3, 2, Reg_no) AS STUFFED FROM
Student-data;

Output and Status of table (After implementing a SQL query):


STUFFED
------------------
BH0976YA
AN2776A
NA6053Y

LEFT
Returns left part of a string with the specified number of characters.
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT LEFT(Name, 4) AS LEFT_FUNC FROM
Student_data;

Output and Status of table (After implementing a SQL query):


LEFT_FUNC
------------------
BHAV
ANAY
NANC

RIGHT
Returns right part of a string with the specified number of characters.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT LEFT(Name, 3) AS RIGHT_FUNC FROM
Student_data;

Output and Status of table (After implementing a SQL query):


RIGHT_FUNC
------------------
Name: Bhavya Choubey
Reg No: 21BCE0976

VYA
AYA
NCY

REPLICATE
Repeats string for a specified number of times.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT REPLICATE(Name,2) AS REP_FUNC FROM
Student_data;

Output and Status of table (After implementing a SQL query):


REP_FUNC
--------------
BHAVYABHAVYA
ANAYAANAYA
NANCYNANCY

SUBSTRING
Returns part of a string.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Name: Bhavya Choubey
Reg No: 21BCE0976

Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT SUBSTR(Name,2,4) AS SUBSTR_FUNC FROM
Student_data;

Output and Status of table (After implementing a SQL query):


SUBSTR_FUNC
--------
HAVY
NAYA
ANCY

LEN
Returns number of characters in a string.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT LEN(Name) AS LEN_FUNC FROM Student_data;
Output and Status of table (After implementing a SQL query):
LEN_FUNC
--------
6
5
5

REVERSE
Returns reverse a string.
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT REVERSE(Name) AS REV_FUNC FROM
Student_data;

Output and Status of table (After implementing a SQL query):


REV_FUNC
----------
AYVAHB
AYANA
YCNAN

UNICODE
Returns Unicode standard integer value.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT UNICODE(Name) AS UNICODE_FUNC FROM
Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976

Output and Status of table (After implementing a SQL query):


UNICODE_FUNC
----------
66
65
78

LOWER
Convert string to lowercase.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT LOWER(Name) AS LOW_FUNC FROM
Student_data;
Output and Status of table (After implementing a SQL query):
LOW_FUNC
----------
bhavya
anaya
nancy

UPPER
Convert string to Uppercase.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
Name: Bhavya Choubey
Reg No: 21BCE0976

---------- ---------- ---------- ---------


Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT UPPER(Name) AS UPP_FUNC FROM Student_data;

Output and Status of table (After implementing a SQL query):


UPP_FUNC
----------
BHAVYA
ANAYA
NANCY

LTRIM
Returns a string after removing leading blanks on Left side.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT LTRIM(Name) AS LTR_FUNC FROM Student_data;
Output and Status of table (After implementing a SQL query):
LTR_FUNC
----------
BHAVYA
ANAYA
Name: Bhavya Choubey
Reg No: 21BCE0976

LTR_FUNC
----------
NANCY
RTRIM
Returns a string after removing leading blanks on Right side.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT RTRIM(Name) AS RTR_FUNC FROM
Student_data;
Output and Status of table (After implementing a SQL query):
RTR_FUNC
----------
BHAVYA
ANAYA
NANCY

CONCAT
Adds two or more strings together.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Name: Bhavya Choubey
Reg No: 21BCE0976

Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT CONCAT(Name,Age) FROM Student_data;
Output and Status of table (After implementing a SQL query):
CONCAT(NAME,AGE)
--------------------------------------------------
BHAVYA976
ANAYA2776
NANCY6053
INSTR
To find the index of a substring in a string.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT INSTR(Name,'y') FROM Student_data;

Output and Status of table (After implementing a SQL query):


INSTR(NAME,'N')
---------------
5
4
5
Name: Bhavya Choubey
Reg No: 21BCE0976

LPAD and RPAD


LPAD-To add left padding characters or whitespaces to a column or
string.
RPAD- To add right padding characters or whitespaces to a column or
a string.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT RPAD(Name,2,'_')||LPAD(Name,8,'_') FROM
Student_data;

Output and Status of table (After implementing a SQL query):


RPAD(NAME,2,'_')||LPAD(NAME,8,'_')
------------------------------------
BH_BHAVYA
AN__ANAYA
NA__NANCY

UPPER, INITCAP and LOWER


The UPPER function converts a string to upper case.
The LOWER function converts a string to lowercase.
The INITCAP function capitalises the first letter of each word.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Name: Bhavya Choubey
Reg No: 21BCE0976

Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT UPPER(Name), INITCAP(Name), LOWER(Naame)
FROM Student_data;

Output and Status of table (After implementing a SQL query):


UPPER(NAME) INITCAP(NAME) LOWER(NAME)
---------- ---------- ----------
BHAVYA Bhavya bhavya
ANAYA Anaya anaya
NANCY Nancy nancy

MAX
To find the maximum value in a column

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT MAX(Age) FROM Student_data;

Output and Status of table (After implementing a SQL query):


MAX(AGE)
------------
20

MIN
To find the minimum value in a column

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
Name: Bhavya Choubey
Reg No: 21BCE0976

---------- ---------- ---------- ---------


Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT MIN(Age) FROM Student_data;

Output and Status of table (After implementing a SQL query):


MIN(AGE)
------------
18

NUMBER
The TRUNC function can reduce the precision of its first numeric,
DATE, or DATETIME argument by returning the truncated value.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT TRUNC(Reg_no,-2) FROM Student_data;

Output and Status of table (After implementing a SQL query):


TRUNC(REG_NO,-2)
------------
900
2700
6053

DATE ARITHMETIC OPERATORS


The only arithmetic operations that can be performed on datetime
values are addition and subtraction. If a datetime value is the operand
Name: Bhavya Choubey
Reg No: 21BCE0976

of addition, the other operand must be a duration.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT (Reg_no + Age) ADD FROM Student_data;

Output and Status of table (After implementing a SQL query):


ADD
----------
996
2795
6071

SQL Query:
SQL> SELECT (Reg_no - Age) DIFF FROM Student_data;

Output and Status of table (After implementing a SQL query):


DIFF
----------
966
2757
6035

CHR
Returns the character that has the ASCII code value that is specified by
the argument.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Name: Bhavya Choubey
Reg No: 21BCE0976

Bhavya 976 20 15-NOV-03


Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT CHR(66) || CHR(65) || CHR(78) FROM PERSONS;

Output:
3 rows selected.
Status of table (After implementing a SQL query):
CHR
---
BAN
BAN
BAN

INSTRB
Location of a string, within another string, in bytes.
Searches a string for a substring using bytes and returns the position in
the string that is the first byte of a specified occurrence of the
substring.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT INSTRB(Name, 'B', 1,2) FROM Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976

Output and Status of table (After implementing a SQL query):


INSTRB(NAME,'B',1,2)
--------------------
0
0
0
INSTRC
Location of a string, within another string, in Unicode complete
characters.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT INSTRC(Name, 'B', 1,2) FROM Student_data;

Output and Status of table (After implementing a SQL query):


INSTRC(NAME,'B',1,2)
--------------------
0
0
0

INSTR2
Location of a string, within another string, in UCS2 code points
Returns a numeric value. The first position in the string is 1. If
substring is not found in string, then the INSTR2 function will return 0.
If string is NULL, then the INSTR2 function will return NULL.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
Name: Bhavya Choubey
Reg No: 21BCE0976

---------- ---------- ---------- ---------


Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT INSTR2(Name, 'B', 1,1) FROM Student_data;

Output and Status of table (After implementing a SQL query):


INSTR2(NAME,'B',1,1)
--------------------
1
0
0

INSTR4
Location of a string, within another string, in UCS4 code points
Returns a numeric value. The first position in the string is 1. If
substring is not found in string, then the INSTR4 function will return 0.
If string is NULL, then the INSTR4 function will return NULL..
Status of table (Before implementing a SQL query):
NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT INSTR4(Name, 'B', 1,3) FROM Student_data;

Output and Status of table (After implementing a SQL query):


INSTR4(NAME,'B',1,3)
--------------------
0
0
0
Name: Bhavya Choubey
Reg No: 21BCE0976

LENGTHB
Returns length in bytes

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT LENGTHB(Reg_no) FROM Student_data;

Output and Status of table (After implementing a SQL query):


LENGTHB(REG_NO)
------------
4
4
4

SUBSTRB
Returns a portion of string, beginning at a specified byte position, and a
specified number of bytes long.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT SUBSTRB(NAME,3,2) AS SUB FROM
Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976

Output and Status of table (After implementing a SQL query):


SUB
---
AV
AY
NC

SUBSTRC
Extract a substring from a string starting from specified position. This
function calculates length of the substring using Unicode complete
characters.
Status of table (Before implementing a SQL query):
NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT SUBSTRC(NAME,3,4) AS SUB FROM
Student_data;

Output and Status of table (After implementing a SQL query):


SUB
---
AVYA
AYA
NCY

COALESCE
Handle the Null values.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Name: Bhavya Choubey
Reg No: 21BCE0976

Anaya 2776 19 29-APR-04


Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT COALESCE(NAME,DOB) FROM Student_data;

Output and Status of table (After implementing a SQL query):


COALESCE(NAME,DOB)
---------------
BHAVYA
ANAYA
NANCY
Name: Bhavya Choubey
Reg No: 21BCE0976

DESCRIPTION:
Multiple row functions

SUM
Returns the total sum of a numeric column.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT SUM(Reg_no) FROM Student_data;

Output and Status of table (After implementing a SQL query):


SUM(REG_NO)
----------
9805

AVG
Returns the average value of a numeric column.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT AVG(Reg_no) FROM Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976

Output and Status of table (After implementing a SQL query):


AVG(REG_NO)
----------
3268

COUNT
Returns the number of records returned by a select query.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT COUNT(DOB) FROM Student_data;

Output and Status of table (After implementing a SQL query):


COUNT(DOB)
----------
3

MAX
Returns the largest value of the selected column.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL Query:
SQL> SELECT MAX(Age) FROM Student_data;

Output and Status of table (After implementing a SQL query):


MAX(AGE)
----------
20

MIN
Returns the smallest value of the selected column.

Status of table (Before implementing a SQL query):


NAME REG_NO AGE DOB
---------- ---------- ---------- ---------
Bhavya 976 20 15-NOV-03
Anaya 2776 19 29-APR-04
Nancy 6053 18 08-JAN-05

SQL Query:
SQL> SELECT MIN(Age) FROM Student_data;

Output and Status of table (After implementing a SQL query):


MIN(AGE)
----------
18

RESULT:
All SQL Queries have been executed successfully and the command
line output has been verified.
Name: Bhavya Choubey
Reg No: 21BCE0976
Date of submission:- 07 June 2023

LAB ASSESSMENT 3
Operators and group functions
AIM:
To execute the sql query commands for Operators and group functions.
Create Table
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.

SQL Query:
SQL> CREATE TABLE Student_data (Name varchar2(10),Reg_no
varchar2(5),Age number(2),Marks number(2));

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> DESC Student_data;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO VARCHAR2(5)
AGE NUMBER(2)
MARKS NUMBER(2)
Name: Bhavya Choubey
Reg No: 21BCE0976

Insert
The INSERT command is used to add one or more rows of data to a
table in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
REG_NO VARCHAR2(5)
AGE NUMBER(2)
MARKS NUMBER(2)

SQL Query:

SQL> INSERT INTO Student_data VALUES ('BHAVYA', '0976', '19',


'95');

1 row created.

SQL> INSERT INTO Student_data VALUES ('TANYA','7652','20',


'83');

1 row created.

SQL> INSERT INTO Student_data VALUES ('SWATI','5211','18',


'71');

1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM Student_data;

NAME REG_N AGE MARKS


---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71
Name: Bhavya Choubey
Reg No: 21BCE0976

DESCRIPTION:
SQL Arithmetic Operators.

Addition Operator
The SQL Addition Operator performs the addition on the numerical
columns in the table.If you want to add the values of two numerical
columns in the table, then you have to specify both columns as the first
and second operand. You can also add the new integer value in the
value of the integer column.
Status of table (Before implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT Name,Reg_no,Age,Marks,Marks+50 AS "Marks+50"
FROM Student_data;

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS Marks+50
---------- ----- ---------- ---------- ----------
BHAVYA 0976 19 95 145
TANYA 7652 20 83 133
SWATI 5211 18 71 121
Name: Bhavya Choubey
Reg No: 21BCE0976
Subtraction Operator
The SQL Subtraction Operator performs the subtraction on the
numerical columns in the table.If we want to subtract the values of one
numerical column from the values of another numerical column, then
we have to specify both columns as the first and second operand. We
can also subtract the integer value from the values of the integer
column.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT Name,Reg_no,Age,Marks,Marks-50 AS "Marks-50"
FROM Student_data;
Output and Status of table (After implementing a SQL query):
NAME REG_N AGE MARKS Marks-50
---------- ----- ---------- ---------- ----------
BHAVYA 0976 19 95 45
TANYA 7652 20 83 33
SWATI 5211 18 71 21

Multiplication Operator
The SQL Multiplication Operator performs the multiplication on the
numerical columns in the table.If you want to multiply the values of two
numerical columns, then you have to specify both columns as the first
and second operand. You can also multiply the integer value with the
values of an integer column.
Status of table (Before implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT Name,Reg_no,Age,Marks,Marks*Reg_no AS
"Marks*Reg_no" FROM Student_data;

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS Marks*Reg_no
---------- ----- ---------- ---------- ------------
BHAVYA 0976 19 95 92720
TANYA 7652 20 83 635116
SWATI 5211 18 71 369981

Division Operator
The SQL Division operator divides the numerical values of one column
by the numerical values of another column.
Status of table (Before implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT Name,Reg_no,Age,Marks,Reg_no/2 AS "Reg_no/2"
FROM Student_data;

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS Reg_no/2
---------- ----- ---------- ---------- ----------
BHAVYA 0976 19 95 488
TANYA 7652 20 83 3826
SWATI 5211 18 71 2605.5
Name: Bhavya Choubey
Reg No: 21BCE0976
DESCRIPTION:
SQL Comparison Operators.

Equal to Operator
The equal to operator compares the equality of two expressions.It
returns true if the value of the left expression is equal to the value of the
right expression; otherwise, it returns false.Note that the equal operator
cannot be used to compare null values.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no=7652;

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
TANYA 7652 20 83

Greater than Operator


The greater than operator (>) compares two non-null expressions and
returns true if the left operand is greater than the right operand;
otherwise, the result is false.
Name: Bhavya Choubey
Reg No: 21BCE0976
Status of table (Before implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no>5000;
Output and Status of table (After implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
TANYA 7652 20 83
SWATI 5211 18 71

Less than Operator


The less than operator compares two non-null expressions. The result is
true if the left operand evaluates to a value that is lower than the value
of the right operand; otherwise, the result is false.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no<5000;

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
Name: Bhavya Choubey
Reg No: 21BCE0976
Greater than or equal to Operator
The greater than or equal operator (>=) compares two non-null
expressions. The result is true if the left expression evaluates to a value
that is greater than the value of the right expression.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no>=5211;
Output and Status of table (After implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
TANYA 7652 20 83
SWATI 5211 18 71

Less than or equal to Operator


The less than or equal to operator compares two non-null expressions
and returns true if the left expression has a value less than or equal the
value of the right expression; otherwise, it returns true.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no<=5211;
Output and Status of table (After implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
SWATI 5211 18 71
Name: Bhavya Choubey
Reg No: 21BCE0976
Not equal to Operator
The not equal to (<>) operator compares two non-null expressions and
returns true if the value of the left expression is not equal to the right
one; otherwise, it returns false.You can use the AND operator to
combine multiple expressions that use the not equal to (<>) operator.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no<>5211;

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83

DESCRIPTION:
SQL Logical Operators.
Name: Bhavya Choubey
Reg No: 21BCE0976
All Operator
The ALL operator compares a value to all values in another value set.
The ALL operator must be preceded by a comparison operator and
followed by a subquery.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT NAME FROM Student_data WHERE
Reg_no=ALL(SELECT Reg_no FROM Student_data WHERE
Marks=95);

Output and Status of table (After implementing a SQL query):


NAME
----------
BHAVYA

And Operator
The AND operator allows you to construct multiple conditions in
the WHERE clause of an SQL statement such as SELECT, UPDATE,
and DELETE.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Name='Bhavya' AND
Marks=95;

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
Name: Bhavya Choubey
Reg No: 21BCE0976
Any Operator
The ANY operator compares a value to any value in a set. Similar to the
ALL operator, the ANY operator must be preceded by a comparison
operator and followed by a subquery.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Marks>ANY(SELECT
Marks FROM Student_data WHERE Marks>80);

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95

Between Operator
The BETWEEN operator searches for values that are within a set of
values, given the minimum value and maximum value. Note that the
minimum and maximum values are included as part of the conditional
set.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Marks BETWEEN 80
AND 100;
Name: Bhavya Choubey
Reg No: 21BCE0976
Output and Status of table (After implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83

Exists Operator
The EXISTS operator tests if a subquery contains any rows. If the
subquery returns one or more rows, the result of the EXISTS is true;
otherwise, the result is false.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT Name from Student_data WHERE EXISTS(SELECT
Reg_no FROM Student_data WHERE Student_data.Reg_no<9000
AND Marks<100);

Output and Status of table (After implementing a SQL query):


NAME
----------
BHAVYA
TANYA
SWATI

In Operator
The IN operator compares a value to a list of specified values.
The IN operator returns true if the compared value matches at least one
value in the list; otherwise, it returns false.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT * FROM Student_data WHERE Name
IN('BHAVYA','SWATI');
Output and Status of table (After implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
SWATI 5211 18 71

Like Operator
The LIKE operator compares a value to similar values using a wildcard
operator. SQL provides two wildcards used in conjunction with
the LIKE operator:

• The percent sign ( %) represents zero, one, or multiple characters.


• The underscore sign ( _) represents a single character.
Status of table (Before implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE NAME LIKE 'B%';

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95

SQL Query:
SQL> SELECT * FROM Student_data WHERE NAME LIKE '%an%';

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
TANYA 7652 20 83

SQL Query:
SQL> SELECT * FROM Student_data WHERE NAME LIKE '_h%';
Name: Bhavya Choubey
Reg No: 21BCE0976
Output and Status of table (After implementing a SQL query):
NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95

Not Operator
Reverse the result of any other Boolean operator. NOT operator in SQL
shows those records from the table where the criteria is not met. NOT
operator is used with where clause in a SELECT query.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE NAME NOT LIKE
'B%';

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
TANYA 7652 20 83
SWATI 5211 18 71

Or Operator
Similar to the AND operator, the OR operator combines multiple
conditions in an SQL statement’s. However, the OR operator returns
true if a least one expression evaluates to true.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE Marks=95 OR
Reg_no=5211;
Name: Bhavya Choubey
Reg No: 21BCE0976

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
SWATI 5211 18 71

Some Operator
SOME operator evaluates the condition between the outer and inner
tables and evaluates to true if the final result returns any one row. If
not, then it evaluates to false.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT * FROM Student_data WHERE
Marks>SOME(SELECT Marks FROM Student_data WHERE
Marks>70);

Output and Status of table (After implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83

DESCRIPTION:
Group Functions.

Aggregate Functions
SQL Aggregate Function is built-in functions for counting and
calculation (perform a calculation on a set of values and return a single
value) Syntax for built-in SQL functions is - SELECT function(column)
FROM table.
Name: Bhavya Choubey
Reg No: 21BCE0976
AVG
The AVG() function returns the average values in a set.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT AVG(MARKS) FROM Student_data;

Output and Status of table (After implementing a SQL query):


AVG(MARKS)
----------
83

COUNT
The COUNT() function returns the number of items in a set.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT COUNT(*) FROM Student_data;

Output and Status of table (After implementing a SQL query):


COUNT(*)
----------
3
Name: Bhavya Choubey
Reg No: 21BCE0976
MAX
The MAX() function returns the maximum value of a set.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT MAX(MARKS) FROM Student_data;

Output and Status of table (After implementing a SQL query):


MAX(MARKS)
----------
95

MIN
The MIN() function returns the minimum value of a set.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT MIN(MARKS) FROM Student_data;

Output and Status of table (After implementing a SQL query):


MIN(MARKS)
----------
71
Name: Bhavya Choubey
Reg No: 21BCE0976
SUM
The SUM() function returns the sum of all values.

Status of table (Before implementing a SQL query):


NAME REG_N AGE MARKS
---------- ----- ---------- ----------
BHAVYA 0976 19 95
TANYA 7652 20 83
SWATI 5211 18 71

SQL Query:
SQL> SELECT SUM(MARKS) FROM Student_data;

Output and Status of table (After implementing a SQL query):


SUM(MARKS)
----------
249

SQL GROUP BY AND HAVING CLAUSE


The GROUP BY clause is often used with aggregate functions (MAX,
SUM, AVG) to group the results by one or more columns or In simple
words we can say that The GROUP BY clause is used in collaboration
with the SELECT statement to arrange required data into
groups. Having Clause is basically like the aggregate function with the
GROUP BY clause. The HAVING clause is used instead of WHERE
with aggregate functions. While the GROUP BY Clause groups rows
that have the same values into summary rows. The having clause is used
with the where clause in order to find rows with certain conditions.

Create Table
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> CREATE TABLE MEM(
2 SNO number,
3 NAME char(20),
4 GEN char(10),
5 DOB varchar(20));

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> DESC MEM;
Name Null? Type
----------------------------------------- -------- ----------------------------
SNO NUMBER
NAME CHAR(20)
GEN CHAR(10)
DOB VARCHAR2(20)

Insert
The INSERT command is used to add one or more rows of data to a
table in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
SNO NUMBER
NAME CHAR(20)
GEN CHAR(10)
DOB VARCHAR2(20)

SQL Query:
SQL> INSERT INTO MEM VALUES (1, 'Bhavya', 'Female', '2004');

1 row created.

SQL> INSERT INTO MEM VALUES (2, 'Sneha', 'Female', '2004');

1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL> INSERT INTO MEM VALUES (3, 'Rohit', 'Male', '2004');

1 row created.

SQL> INSERT INTO MEM VALUES(4, 'Ashwin', 'Male', '2003');

1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM MEM;
SNO NAME GEN DOB
---------- -------------------- ---------- --------------------
1 Bhavya Female 2004
2 Sneha Female 2004
3 Rohit Male 2004
4 Ashwin Male 2003

GROUP BY and HAVE


Status of table (Before implementing a SQL query):
SNO NAME GEN DOB
---------- -------------------- ---------- --------------------
1 Bhavya Female 2004
2 Sneha Female 2004
3 Rohit Male 2004
4 Ashwin Male 2003

SQL Query:
SQL> SELECT GEN FROM MEM;

Output and Status of table (After implementing a SQL query):


GEN
----------
Female
Female
Male
Male
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT GEN FROM MEM GROUP BY GEN;

Output and Status of table (After implementing a SQL query):


GEN
----------
Female
Male

Status of table (Before implementing a SQL query):


SNO NAME GEN DOB
---------- -------------------- ---------- --------------------
1 Bhavya Female 2004
2 Sneha Female 2004
3 Rohit Male 2004
4 Ashwin Male 2003

SQL Query:
SQL> SELECT SNO, DOB FROM MEM;

Output and Status of table (After implementing a SQL query):


SNO DOB
---------- --------------------
1 2004
2 2004
3 2004
4 2003

SQL Query:
SQL> SELECT SNO, DOB FROM MEM GROUP BY SNO, DOB;

Output and Status of table (After implementing a SQL query):


SNO DOB
---------- --------------------
2 2004
3 2004
1 2004
4 2003
Name: Bhavya Choubey
Reg No: 21BCE0976
Status of table (Before implementing a SQL query):
SNO NAME GEN DOB
---------- -------------------- ---------- --------------------
1 Bhavya Female 2004
2 Sneha Female 2004
3 Rohit Male 2004
4 Ashwin Male 2003

SQL Query:
SQL> SELECT GEN, COUNT('gender_count') FROM MEM GROUP
BY GEN;

Output and Status of table (After implementing a SQL query):


GEN COUNT('GENDER_COUNT')
---------- ---------------------
Female 2
Male 2

RESULT:
All SQL Queries have been executed successfully and the command
line output has been verified.
Name: Bhavya Choubey
Reg No: 21BCE0976
Date of submission:- 14 June 2023

LAB ASSESSMENT 4
Sub query, views and joins

AIM:
To execute the sql query commands for sub query, views and joins.

DESCRIPTION:
Subquery.
Create Table LISTS
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.

SQL Query:
SQL> CREATE TABLE LISTS(CUSTOMER_ID number(2),NAME
varchar2(20),ITEM varchar2(10),PRICE number(5));

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> DESC LISTS;
Name Null? Type
----------------------------------------- -------- ----------------------------
CUSTOMER_ID NUMBER(2)
NAME VARCHAR2(20)
ITEM VARCHAR2(10)
PRICE NUMBER(5)
Name: Bhavya Choubey
Reg No: 21BCE0976

Insert in the LISTS


The INSERT command is used to add one or more rows of data to a
table in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
CUSTOMER_ID NUMBER(2)
NAME VARCHAR2(20)
ITEM VARCHAR2(10)
PRICE NUMBER(5)

SQL Query:
SQL> INSERT INTO LISTS VALUES(1,'BHAVYA','PIZZA',50);

1 row created.

SQL> INSERT INTO LISTS VALUES(2,'ANANYA','BURGER',60);

1 row created.

SQL> INSERT INTO LISTS VALUES(3,'SAUMYAA','CAKE',70);

1 row created.

SQL> INSERT INTO LISTS VALUES(4,'ANIKA','PASTA',100);

1 row created.

Output:
1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976

Status of table (After implementing a SQL query):


SQL> SELECT * FROM LISTS;

CUSTOMER_ID NAME ITEM PRICE


----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
Create Another Table Orders
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.

SQL Query:
SQL> CREATE TABLE ORDERS(ORDER_ID
number(2),BATCH_NO number(5),CUSTOMER_ID number(2));

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> DESC ORDERS;
Name Null? Type
----------------------------------------- -------- ----------------------------
ORDER_ID NUMBER(2)
BATCH_NO NUMBER(5)
CUSTOMER_ID NUMBER(2)
Name: Bhavya Choubey
Reg No: 21BCE0976
Insert in ORDERS
The INSERT command is used to add one or more rows of data to atable
in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
ORDER_ID NUMBER(2)
BATCH_NO NUMBER(5)
CUSTOMER_ID NUMBER(2)

SQL Query:
SQL> INSERT INTO ORDERS VALUES(1,10000,8);

1 row created.

SQL> INSERT INTO ORDERS VALUES(2,20000,7);

1 row created.

SQL> INSERT INTO ORDERS VALUES(3,30000,4);

1 row created.

SQL> INSERT INTO ORDERS VALUES(4,40000,2);

1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2
Name: Bhavya Choubey
Reg No: 21BCE0976
Scalar Subquery
A scalar subquery expression is a subquery that returns exactly one
column value from one row. The value of the scalar subquery
expression is the value of the select list item of the subquery. If the
subquery returns 0 rows, then the value of the scalar subquery
expression is NULL. If the subquery returns more than one row, then
Oracle returns an error.

Status of table (Before implementing a SQL query):


ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SQL> SELECT * FROM ORDERS WHERE BATCH_NO >
(SELECT AVG(BATCH_NO) FROM ORDERS);
Output and Status of table (After implementing a SQL query):
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
3 30000 4
4 40000 2

Multiple row Subquery


Multiple row subquery returns one or more rows to the outer SQL
statement. Multiple-row subqueries are used most commonly in
WHERE and HAVING clauses. Since it returns multiple rows, it must
be handled by set comparison operators (IN, ALL, ANY).

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT Name,Reg_no,Age,Marks,Marks-50 AS "Marks-50"
FROM Student_data;
Output and Status of table (After implementing a SQL query):
CUSTOMER_ID NAME
----------- --------------------
2 ANANYA
4 ANIKA

Any Operator
The ANY operator:
• returns a boolean value as a result
• returns TRUE if ANY of the subquery values meet the condition

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100

SQL> SELECT * FROM ORDERS;


ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT CUSTOMER_ID, NAME FROM LISTS WHERE
CUSTOMER_ID =ANY (SELECT CUSTOMER_ID FROM ORDERS
WHERE BATCH_NO<40000);

Output and Status of table (After implementing a SQL query):


CUSTOMER_ID NAME
----------- --------------------
4 ANIKA

All Operator
The ALL operator:
• returns a boolean value as a result
• returns TRUE if ALL of the subquery values meet the condition
• is used with SELECT, WHERE and HAVING statements

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT CUSTOMER_ID, NAME FROM LISTS WHERE
CUSTOMER_ID = ALL (SELECT CUSTOMER_ID FROM ORDERS
WHERE BATCH_NO=40000);

Output and Status of table (After implementing a SQL query):


CUSTOMER_ID NAME
----------- --------------------
2 ANANYA
Name: Bhavya Choubey
Reg No: 21BCE0976

DESCRIPTION:
Views.

Create View
In SQL, a view is a virtual table based on the result-set of an SQL
statement. A view contains rows and columns, just like a real table. We
can add SQL statements and functions to a view and present the data as
if the data were coming from one single table. A view is created with
the CREATE VIEW statement.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> CREATE VIEW DESERT AS SELECT
CUSTOMER_ID,NAME FROM LISTS WHERE ITEM='CAKE';

Output :
View created.

Status of table (After implementing a SQL query):


CUSTOMER_ID NAME
----------- --------------------
3 SAUMYAA
Name: Bhavya Choubey
Reg No: 21BCE0976

Updating a View
Used to modify the data of a view. A view can be updated with the
CREATE OR REPLACE VIEW statement.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> CREATE OR REPLACE VIEW DESERT AS SELECT
CUSTOMER_ID,NAME,PRICE FROM LISTS WHERE
ITEM='CAKE';

Output :
View created.

Status of table (After implementing a SQL query):

CUSTOMER_ID NAME PRICE


----------- -------------------- ----------
3 SAUMYAA 70
Name: Bhavya Choubey
Reg No: 21BCE0976

Dropping a View
When you drop a view, the definition of the view and other information
about the view is deleted from the system catalog. All permissions for
the view are also deleted. A view is deleted with the DROP VIEW
statement.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> DROP VIEW DESERT;

Output :
View dropped.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM DESERT;
SELECT * FROM DESERT
*
ERROR at line 1:
ORA-00942: table or view does not exist
Name: Bhavya Choubey
Reg No: 21BCE0976

DESCRIPTION:
Joins.

Cross join
CROSS JOIN returns the Cartesian product of rows from tables in the
join. In other words, it will produce rows which combine each row
from the first table with each row from the second table.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT * FROM LISTS CROSS JOIN ORDERS;

Output and Status of table (After implementing a SQL query):


CUSTOMER_ID NAME ITEM PRICE ORDER_ID
BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
CUSTOMER_ID
-----------
1 BHAVYA PIZZA 50 1 10000
8

1 BHAVYA PIZZA 50 2 20000


7
Name: Bhavya Choubey
Reg No: 21BCE0976
1 BHAVYA PIZZA 50 3 30000
4

CUSTOMER_ID NAME ITEM PRICE ORDER_ID


BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
CUSTOMER_ID
-----------
1 BHAVYA PIZZA 50 4 40000
2

2 ANANYA BURGER 60 1 10000


8

2 ANANYA BURGER 60 2 20000


7

CUSTOMER_ID NAME ITEM PRICE ORDER_ID


BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
CUSTOMER_ID
-----------
2 ANANYA BURGER 60 3 30000
4

2 ANANYA BURGER 60 4 40000


2

3 SAUMYAA CAKE 70 1 10000


8

CUSTOMER_ID NAME ITEM PRICE ORDER_ID


BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
CUSTOMER_ID
-----------
3 SAUMYAA CAKE 70 2 20000
7
Name: Bhavya Choubey
Reg No: 21BCE0976

3 SAUMYAA CAKE 70 3 30000


4

3 SAUMYAA CAKE 70 4 40000


2

CUSTOMER_ID NAME ITEM PRICE ORDER_ID


BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
CUSTOMER_ID
-----------
4 ANIKA PASTA 100 1 10000
8

4 ANIKA PASTA 100 2 20000


7

4 ANIKA PASTA 100 3 30000


4

CUSTOMER_ID NAME ITEM PRICE ORDER_ID


BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
CUSTOMER_ID
-----------
4 ANIKA PASTA 100 4 40000
2

16 rows selected.
Name: Bhavya Choubey
Reg No: 21BCE0976

Natural join
A natural join is a type of equi-join where the join predicate arises
implicitly by comparing all columns in both tables that have the same
column-names in the joined tables. The resulting joined table contains
only one column for each pair of equally named columns.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT * FROM LISTS NATURAL JOIN ORDERS;

Output and Status of table (After implementing a SQL query):


CUSTOMER_ID NAME ITEM PRICE ORDER_ID
BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
4 ANIKA PASTA 100 3 30000
2 ANANYA BURGER 60 4 40000
Name: Bhavya Choubey
Reg No: 21BCE0976

Inner join
The INNER JOIN keyword selects records that have matching values in
both tables.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT * FROM LISTS INNER JOIN ORDERS ON
LISTS.CUSTOMER_ID=ORDERS.CUSTOMER_ID;

Output and Status of table (After implementing a SQL query):


CUSTOMER_ID NAME ITEM PRICE ORDER_ID
BATCH_NO
----------- -------------------- ---------- ---------- ---------- ----------
CUSTOMER_ID
-----------
4 ANIKA PASTA 100 3 30000
4

2 ANANYA BURGER 60 4 40000


2
Name: Bhavya Choubey
Reg No: 21BCE0976

Left join
The LEFT JOIN keyword returns all records from the left table (table1),
and the matching records from the right table (table2). The result is 0
records from the right side, if there is no match.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT LISTS.NAME,ORDERS.BATCH_NO FROM LISTS
LEFT JOIN ORDERS ON
LISTS.CUSTOMER_ID=ORDERS.CUSTOMER_ID ORDER BY
LISTS.NAME;

Output and Status of table (After implementing a SQL query):


NAME BATCH_NO
-------------------- ----------
ANANYA 40000
ANIKA 30000
BHAVYA
SAUMYAA
Name: Bhavya Choubey
Reg No: 21BCE0976
Right join
The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1). The result
is 0 records from the left side, if there is no match.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT LISTS.NAME,ORDERS.BATCH_NO FROM LISTS
RIGHT JOIN ORDERS ON
LISTS.CUSTOMER_ID=ORDERS.CUSTOMER_ID ORDER BY
LISTS.NAME;

Output and Status of table (After implementing a SQL query):


NAME BATCH_NO
-------------------- ----------
ANANYA 40000
ANIKA 30000
20000
10000
Name: Bhavya Choubey
Reg No: 21BCE0976

Full Outer join


The FULL OUTER JOIN keyword returns all records when there is a
match in left (table1) or right (table2) table records.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM LISTS;
CUSTOMER_ID NAME ITEM PRICE
----------- -------------------- ---------- ----------
1 BHAVYA PIZZA 50
2 ANANYA BURGER 60
3 SAUMYAA CAKE 70
4 ANIKA PASTA 100
SQL> SELECT * FROM ORDERS;
ORDER_ID BATCH_NO CUSTOMER_ID
---------- ---------- -----------
1 10000 8
2 20000 7
3 30000 4
4 40000 2

SQL Query:
SQL> SELECT LISTS.NAME,ORDERS.BATCH_NO FROM LISTS
FULL OUTER JOIN ORDERS ON
LISTS.CUSTOMER_ID=ORDERS.CUSTOMER_ID ORDER BY
LISTS.NAME;

Output and Status of table (After implementing a SQL query):


NAME BATCH_NO
-------------------- ----------
ANANYA 40000
ANIKA 30000
BHAVYA
SAUMYAA
20000
10000

6 rows selected.
Name: Bhavya Choubey
Reg No: 21BCE0976

DESCRIPTION:
Set Operators
Create Table class_10
The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.

SQL Query:
SQL> CREATE TABLE CLASS_10(ROLL_NO number(5),NAME
varchar2(20),MARKS number(5));

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> DESC CLASS_10;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO NUMBER(5)
NAME VARCHAR2(20)
MARKS NUMBER(5)

Insert in the class_10


The INSERT command is used to add one or more rows of data to a
table in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO NUMBER(5)
NAME VARCHAR2(20)
MARKS NUMBER(5)
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> INSERT INTO CLASS_10 VALUES(21,'BHAVYA',95);

1 row created.

SQL> INSERT INTO CLASS_10 VALUES(22,'SANYA',89);

1 row created.

SQL> INSERT INTO CLASS_10 VALUES(23,'KOMAL',70);

1 row created.

SQL> INSERT INTO CLASS_10 VALUES(24,'PREETI',63);

1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM CLASS_10;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89
23 KOMAL 70
24 PREETI 63

Create Table class_12


The create table command defines each column of the table uniquely.
Each column has minimum of three attributes Name, Data type and
size(column width).

Status of table (Before implementing a SQL query):


No table is created.
Name: Bhavya Choubey
Reg No: 21BCE0976

SQL Query:
SQL> CREATE TABLE CLASS_12(ROLL_NO number(5),NAME
varchar2(20),MARKS number(5));

Output:
Table created.

Status of table (After implementing a SQL query):


SQL> DESC CLASS_12;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO NUMBER(5)
NAME VARCHAR2(20)
MARKS NUMBER(5)

Insert in the class_12


The INSERT command is used to add one or more rows of data to a
table in a database.

Status of table (Before implementing a SQL query):


Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO NUMBER(5)
NAME VARCHAR2(20)
MARKS NUMBER(5)

SQL Query:
SQL> INSERT INTO CLASS_12 VALUES(21,'BHAVYA',95);

1 row created.

SQL> INSERT INTO CLASS_12 VALUES(30,'SRISHTI',67);

1 row created.

SQL> INSERT INTO CLASS_12 VALUES(23,'KOMAL',70);

1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL> INSERT INTO CLASS_12 VALUES(31,'NAMITA',56);

1 row created.

Output:
1 row created.

Status of table (After implementing a SQL query):


SQL> SELECT * FROM CLASS_12;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
30 SRISHTI 67
23 KOMAL 70
31 NAMITA 56

UNION ALL Operator


The UNION ALL command combines the result set of two or more
SELECT statements (allows duplicate values).

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM CLASS_10;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89
23 KOMAL 70
24 PREETI 63

SQL> SELECT * FROM CLASS_12;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
30 SRISHTI 67
23 KOMAL 70
31 NAMITA 56
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT * FROM CLASS_10 UNION ALL SELECT * FROM
CLASS_12;

Output and Status of table (After implementing a SQL query):


ROLL_NO NAME MARKS
---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89
23 KOMAL 70
24 PREETI 63
21 BHAVYA 95
30 SRISHTI 67
23 KOMAL 70
31 NAMITA 56

8 rows selected.

INTERSECT Operator
The SQL INTERSECT operator takes the results of two queries and
returns only rows that appear in both result sets. The INTERSECT
operator removes duplicate rows from the final result set. The
INTERSECT ALL operator does not remove duplicate rows from the
final result set.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM CLASS_10;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89
23 KOMAL 70
24 PREETI 63

SQL> SELECT * FROM CLASS_12;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
30 SRISHTI 67
Name: Bhavya Choubey
Reg No: 21BCE0976
23 KOMAL 70
31 NAMITA 56

SQL Query:
SQL> SELECT * FROM CLASS_10 INTERSECT SELECT * FROM
CLASS_12;

Output and Status of table (After implementing a SQL query):


ROLL_NO NAME MARKS
---------- -------------------- ----------
21 BHAVYA 95
23 KOMAL 70

MINUS(EXCEPT) Operator
It combines the result of two SELECT statements. Minus operator is
used to display the rows which are present in the first query but absent
in the second query. It has no duplicates and data arranged in ascending
order by default.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM CLASS_10;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89
23 KOMAL 70
24 PREETI 63

SQL> SELECT * FROM CLASS_12;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
30 SRISHTI 67
23 KOMAL 70
31 NAMITA 56

SQL Query:
SQL> SELECT * FROM CLASS_10 WHERE MARKS BETWEEN 1
AND 100 MINUS SELECT * FROM CLASS_10 WHERE MARKS
Name: Bhavya Choubey
Reg No: 21BCE0976
BETWEEN 50 AND 75;

Output and Status of table (After implementing a SQL query):


ROLL_NO NAME MARKS
---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89

UNION Operator
The SQL Union operation is used to combine the result of two or more
SQL SELECT queries. In the union operation, all the number of
datatype and 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.

Status of table (Before implementing a SQL query):


SQL> SELECT * FROM CLASS_10;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89
23 KOMAL 70
24 PREETI 63

SQL> SELECT * FROM CLASS_12;

ROLL_NO NAME MARKS


---------- -------------------- ----------
21 BHAVYA 95
30 SRISHTI 67
23 KOMAL 70
31 NAMITA 56

SQL Query:
SQL> SELECT * FROM CLASS_10 UNION SELECT * FROM
CLASS_12;
Name: Bhavya Choubey
Reg No: 21BCE0976

Output and Status of table (After implementing a SQL query):


ROLL_NO NAME MARKS
---------- -------------------- ----------
21 BHAVYA 95
22 SANYA 89
23 KOMAL 70
24 PREETI 63
30 SRISHTI 67
31 NAMITA 56

6 rows selected.

RESULT:
All SQL Queries have been executed successfully and the command
line output has been verified.
Name: Bhavya Choubey
Reg No: 21BCE0976
Name: Bhavya Choubey
Reg No: 21BCE0976

You might also like