Ilovepdf Merged
Ilovepdf Merged
AIM:-
To execute the different commands for ddl,dcl,dml and tcl and show the
outputs accordingly.
DDL Commands
Table created.
REG_NO VARCHAR2(10)
NAME CHAR(30)
DOB DATE
ADDRESS VARCHAR2(50)
The TRUNCATE Command
Table truncated.
REG_NO VARCHAR2(10)
NAME CHAR(30)
DOB DATE
ADDRESS VARCHAR2(50)
Table renamed.
SQL> DESC Student_details;
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.
Table altered.
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.
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.
REG_NO VARCHAR2(10)
NAME VARCHAR2(40)
DOB DATE
AGE NUMBER(2)
MARKS NUMBER(3)
Table dropped.
SQL> DESC
Student_details;
ERROR:
ORA-04043: object Student_details does not exist
DML Commands
Table created.
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.
1 row created.
1 row created.
ADDRESS
ADDRESS
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;
ADDRESS
ADDRESS
ADDRESS
ADDRESS
1 row deleted.
ADDRESS
Table altered.
SQL> SELECT * FROM Student;
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.
Table created.
SQL> INSERT INTO Student VALUES('21BCE0976','Bhavya','15-Nov-03','Bihar');
1 row created.
1 row created.
1 row created.
SQL> INSERT INTO Student VALUES('21BCE3351','Ananya','20-Mar-
03','Gujarat');
1 row created.
SQL> SELECT * FROM Student;
ADDRESS
Bihar
ADDRESS
Grant succeeded.
Grant succeeded.
ADDRESS
ADDRESS
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.
ADDRESS
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.
1 row created.
1 row created.
ADDRESS
Bihar
ADDRESS
ADDRESS
ADDRESS
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
ADDRESS
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.
ADDRESS
ADDRESS
ADDRESS
ADDRESS
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
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).
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
SQL Query:
CREATE TABLE Student_result(Name varchar2(10),Reg_no
varchar2(10) ,Grades varchar2(4),CGPA number(4));
Output:
Table created.
SQL Query:
ALTER TABLE Student_information ADD PRIMARY KEY
(Reg_no);
Output:
Table altered.
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.
Insert
The INSERT command is used to add one or more rows of data to a
table in a database.
SQL Query:
SQL> INSERT INTO Student_information VALUES
('Bhavya','21BCE0976','19','15-nov-2003');
Name: Bhavya Choubey
Reg No: 21BCE0976
1 row created.
Output:
1 row created.
SQL Query:
SQL> INSERT INTO Student_result VALUES
Name: Bhavya Choubey
Reg No: 21BCE0976
('Bhavya','21BCE0976','S','9.3');
1 row created.
Output:
1 row created.
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
Output:
CONSTRAINT_NAME C
------------------------------ -
SYS_C009042 C
SYS_C009043 P
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
SQL Query:
SQL> ALTER TABLE Student_result DISABLE CONSTRAINT
FK_SR;
Output:
Table altered.
Output:
1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> ALTER TABLE Student_result ENABLE CONSTRAINT
FK_SR;
Output:
Table altered.
SQL Query:
SQL> ALTER TABLE Student_result DISABLE CONSTRAINT
FK_SR;
Output:
Table altered.
SQL Query:
SQL> ALTER TABLE Student_information ADD CONSTRAINT
CHK_Age CHECK(Age>=10);
Output:
Table altered.
SQL Query:
SQL> ALTER TABLE Student_information DROP CONSTRAINT
CHK_Age;
Output:
Table altered.
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.
SQL Query:
SQL> ALTER TABLE Student_information ADD CONSTRAINT
UQ_SR UNIQUE(Name);
Output:
Table altered.
SQL Query:
SQL> ALTER TABLE Student_information DROP CONSTRAINT
UQ_SR;
Output:
Table altered.
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.
SQL Query:
SQL> ALTER TABLE Student_information MODIFY AGE
DEFAULT '8';
Output:
Table altered.
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).
SQL Query:
SQL> CREATE TABLE Student_data(Name varchar2(10), Reg_no
number(4), Age number, DOB date);
Output:
Table created.
Insert
The INSERT command is used to add one or more rows of data to a
table in a database.
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.
ASCII
This function Returns the ASCII code value of a keyboard button and
the rest etc (@,R,9,*).
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT ASCII(Name) AS NumCodeOfFirstChar FROM
Student_data;
SPACE
Returns spaces in your SQL query (you can specify the size of space).
SQL Query:
SQL>SELECT (Name)+SPACE(1)+(Reg_no) AS SPACE_1 FROM
Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976
CHARINDEX
Returns the starting position of a character string.
SQL Query:
SQL> SELECT CHARINDEX('N',Name) AS MatchPosition FROM
Student_data;
REPLACE
Replaces all occurrences of the string2 in the string1 with string3.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> UPDATE Student_data SET Reg_no = REPLACE
(Reg_no,'4','0');
Output:
3 rows updated.
QUOTENAME
Returns a Unicode string with the delimiters added to make the input
string a valid Microsoft® SQL Server™ delimited identifier.
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.
SQL Query:
SQL> SELECT STUFF(Name, 3, 2, Reg_no) AS STUFFED FROM
Student-data;
LEFT
Returns left part of a string with the specified number of characters.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT LEFT(Name, 4) AS LEFT_FUNC FROM
Student_data;
RIGHT
Returns right part of a string with the specified number of characters.
SQL Query:
SQL> SELECT LEFT(Name, 3) AS RIGHT_FUNC FROM
Student_data;
VYA
AYA
NCY
REPLICATE
Repeats string for a specified number of times.
SQL Query:
SQL> SELECT REPLICATE(Name,2) AS REP_FUNC FROM
Student_data;
SUBSTRING
Returns part of a string.
SQL Query:
SQL> SELECT SUBSTR(Name,2,4) AS SUBSTR_FUNC FROM
Student_data;
LEN
Returns number of characters in a string.
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
SQL Query:
SQL> SELECT REVERSE(Name) AS REV_FUNC FROM
Student_data;
UNICODE
Returns Unicode standard integer value.
SQL Query:
SQL> SELECT UNICODE(Name) AS UNICODE_FUNC FROM
Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976
LOWER
Convert string to lowercase.
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.
SQL Query:
SQL> SELECT UPPER(Name) AS UPP_FUNC FROM Student_data;
LTRIM
Returns a string after removing leading blanks on Left side.
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.
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.
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.
SQL Query:
SQL> SELECT INSTR(Name,'y') FROM Student_data;
SQL Query:
SQL> SELECT RPAD(Name,2,'_')||LPAD(Name,8,'_') FROM
Student_data;
SQL Query:
SQL> SELECT UPPER(Name), INITCAP(Name), LOWER(Naame)
FROM Student_data;
MAX
To find the maximum value in a column
SQL Query:
SQL> SELECT MAX(Age) FROM Student_data;
MIN
To find the minimum value in a column
SQL Query:
SQL> SELECT MIN(Age) FROM Student_data;
NUMBER
The TRUNC function can reduce the precision of its first numeric,
DATE, or DATETIME argument by returning the truncated value.
SQL Query:
SQL> SELECT TRUNC(Reg_no,-2) FROM Student_data;
SQL Query:
SQL> SELECT (Reg_no + Age) ADD FROM Student_data;
SQL Query:
SQL> SELECT (Reg_no - Age) DIFF FROM Student_data;
CHR
Returns the character that has the ASCII code value that is specified by
the argument.
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.
SQL Query:
SQL> SELECT INSTRB(Name, 'B', 1,2) FROM Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> SELECT INSTRC(Name, 'B', 1,2) FROM Student_data;
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.
SQL Query:
SQL> SELECT INSTR2(Name, 'B', 1,1) FROM Student_data;
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;
LENGTHB
Returns length in bytes
SQL Query:
SQL> SELECT LENGTHB(Reg_no) FROM Student_data;
SUBSTRB
Returns a portion of string, beginning at a specified byte position, and a
specified number of bytes long.
SQL Query:
SQL> SELECT SUBSTRB(NAME,3,2) AS SUB FROM
Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976
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;
COALESCE
Handle the Null values.
SQL Query:
SQL> SELECT COALESCE(NAME,DOB) FROM Student_data;
DESCRIPTION:
Multiple row functions
SUM
Returns the total sum of a numeric column.
SQL Query:
SQL> SELECT SUM(Reg_no) FROM Student_data;
AVG
Returns the average value of a numeric column.
SQL Query:
SQL> SELECT AVG(Reg_no) FROM Student_data;
Name: Bhavya Choubey
Reg No: 21BCE0976
COUNT
Returns the number of records returned by a select query.
SQL Query:
SQL> SELECT COUNT(DOB) FROM Student_data;
MAX
Returns the largest value of the selected column.
SQL Query:
SQL> SELECT MAX(Age) FROM Student_data;
MIN
Returns the smallest value of the selected column.
SQL Query:
SQL> SELECT MIN(Age) FROM Student_data;
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).
SQL Query:
SQL> CREATE TABLE Student_data (Name varchar2(10),Reg_no
varchar2(5),Age number(2),Marks number(2));
Output:
Table created.
Insert
The INSERT command is used to add one or more rows of data to a
table in a database.
SQL Query:
1 row created.
1 row created.
1 row created.
Output:
1 row created.
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;
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;
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;
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.
SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no=7652;
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
SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no<5000;
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
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.
SQL Query:
SQL> SELECT * FROM Student_data WHERE Reg_no<>5211;
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.
SQL Query:
SQL> SELECT NAME FROM Student_data WHERE
Reg_no=ALL(SELECT Reg_no FROM Student_data WHERE
Marks=95);
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.
SQL Query:
SQL> SELECT * FROM Student_data WHERE Name='Bhavya' AND
Marks=95;
SQL Query:
SQL> SELECT * FROM Student_data WHERE Marks>ANY(SELECT
Marks FROM Student_data WHERE Marks>80);
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.
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.
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);
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.
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:
SQL Query:
SQL> SELECT * FROM Student_data WHERE NAME LIKE 'B%';
SQL Query:
SQL> SELECT * FROM Student_data WHERE NAME LIKE '%an%';
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.
SQL Query:
SQL> SELECT * FROM Student_data WHERE NAME NOT LIKE
'B%';
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.
SQL Query:
SQL> SELECT * FROM Student_data WHERE Marks=95 OR
Reg_no=5211;
Name: Bhavya Choubey
Reg No: 21BCE0976
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.
SQL Query:
SQL> SELECT * FROM Student_data WHERE
Marks>SOME(SELECT Marks FROM Student_data WHERE
Marks>70);
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.
SQL Query:
SQL> SELECT AVG(MARKS) FROM Student_data;
COUNT
The COUNT() function returns the number of items in a set.
SQL Query:
SQL> SELECT COUNT(*) FROM Student_data;
SQL Query:
SQL> SELECT MAX(MARKS) FROM Student_data;
MIN
The MIN() function returns the minimum value of a set.
SQL Query:
SQL> SELECT MIN(MARKS) FROM Student_data;
SQL Query:
SQL> SELECT SUM(MARKS) FROM Student_data;
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).
Output:
Table created.
Insert
The INSERT command is used to add one or more rows of data to a
table in a database.
SQL Query:
SQL> INSERT INTO MEM VALUES (1, 'Bhavya', 'Female', '2004');
1 row created.
1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL> INSERT INTO MEM VALUES (3, 'Rohit', 'Male', '2004');
1 row created.
1 row created.
Output:
1 row created.
SQL Query:
SQL> SELECT GEN FROM MEM;
SQL Query:
SQL> SELECT SNO, DOB FROM MEM;
SQL Query:
SQL> SELECT SNO, DOB FROM MEM GROUP BY SNO, DOB;
SQL Query:
SQL> SELECT GEN, COUNT('gender_count') FROM MEM GROUP
BY GEN;
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).
SQL Query:
SQL> CREATE TABLE LISTS(CUSTOMER_ID number(2),NAME
varchar2(20),ITEM varchar2(10),PRICE number(5));
Output:
Table created.
SQL Query:
SQL> INSERT INTO LISTS VALUES(1,'BHAVYA','PIZZA',50);
1 row created.
1 row created.
1 row created.
1 row created.
Output:
1 row created.
Name: Bhavya Choubey
Reg No: 21BCE0976
SQL Query:
SQL> CREATE TABLE ORDERS(ORDER_ID
number(2),BATCH_NO number(5),CUSTOMER_ID number(2));
Output:
Table created.
SQL Query:
SQL> INSERT INTO ORDERS VALUES(1,10000,8);
1 row created.
1 row created.
1 row created.
1 row created.
Output:
1 row created.
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
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
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
SQL Query:
SQL> SELECT CUSTOMER_ID, NAME FROM LISTS WHERE
CUSTOMER_ID = ALL (SELECT CUSTOMER_ID FROM ORDERS
WHERE BATCH_NO=40000);
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.
SQL Query:
SQL> CREATE VIEW DESERT AS SELECT
CUSTOMER_ID,NAME FROM LISTS WHERE ITEM='CAKE';
Output :
View created.
Updating a View
Used to modify the data of a view. A view can be updated with the
CREATE OR REPLACE VIEW statement.
SQL Query:
SQL> CREATE OR REPLACE VIEW DESERT AS SELECT
CUSTOMER_ID,NAME,PRICE FROM LISTS WHERE
ITEM='CAKE';
Output :
View created.
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.
SQL Query:
SQL> DROP VIEW DESERT;
Output :
View dropped.
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.
SQL Query:
SQL> SELECT * FROM LISTS CROSS JOIN ORDERS;
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.
SQL Query:
SQL> SELECT * FROM LISTS NATURAL JOIN ORDERS;
Inner join
The INNER JOIN keyword selects records that have matching values in
both tables.
SQL Query:
SQL> SELECT * FROM LISTS INNER JOIN ORDERS ON
LISTS.CUSTOMER_ID=ORDERS.CUSTOMER_ID;
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.
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;
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;
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;
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).
SQL Query:
SQL> CREATE TABLE CLASS_10(ROLL_NO number(5),NAME
varchar2(20),MARKS number(5));
Output:
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Output:
1 row created.
SQL Query:
SQL> CREATE TABLE CLASS_12(ROLL_NO number(5),NAME
varchar2(20),MARKS number(5));
Output:
Table created.
SQL Query:
SQL> INSERT INTO CLASS_12 VALUES(21,'BHAVYA',95);
1 row created.
1 row created.
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.
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.
SQL Query:
SQL> SELECT * FROM CLASS_10 INTERSECT SELECT * FROM
CLASS_12;
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.
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;
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.
SQL Query:
SQL> SELECT * FROM CLASS_10 UNION SELECT * FROM
CLASS_12;
Name: Bhavya Choubey
Reg No: 21BCE0976
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