You are on page 1of 6

1

DBMS PRACTICAL-2
AIM: To perform various SQL Queries using between, in and like operators

THEORY:

ARITHMETIC OPERATORS

Oracle allows arithmetic operators to be used while viewing record from table or
while performing Data Manipulation operations such as Insert, Update and
Delete. These are:

+ Addition

- Subtraction

/ Division

* Multiplication

** Exponentiation ()

Enclosed Operation Example:

SELECT EMPNAME, ROUND(SALARY+(SALARY*1.5)) FROM EMPLOYEE WHERE


DEPTNO=10;

RENAMING COLUMN IN OUTPUT

To rename the default output column names with an alias, use following syntax:

Syntax:

SELECT <COLUMNNAME><ALIASNAME><COLUMNNAME><ALIASNAME> FROM


<TABLENAME>; Example:

SELECT EMPNAME, ROUND(SALARY+(SALARY*1.5)) UPDATED_SALARY FROM


EMPLOYEE WHERE DEPTNO=10;

OR

SELECT EMPNAME, ROUND(SALARY+(SALARY*1.5)) “UPDATED_SALARY” FROM


EMPLOYEE WHERE DEPTNO=10;

LOGICAL OPERATORS

The AND, OR and NOT operators can be used with SQL statements.

AND – Allows creating SQL statement based on two or more conditions being
met.

OR - Allows to create SQL statement where records are returned when any one
of the conditions are met.
Patel Dhruti Enrollment no. : 22C21058
2

NOT – Used to display only those records that do not satisfy the condition
specified. Example:

SELECT * FROM EMP WHERE FNAME = ‘HARRY’ AND DEPTNO=20;

SELECT * FROM EMP WHERE FNAME = ‘HARRY’ OR DEPTNO=20;

SELECT * FROM EMP WHERE NOT(DEPTNO=20);

COMPARISON OPERATORS
Comparison Description
Operator
= Equal
<> Not Equal
!= Not Equal
> Greater Than
>= Greater Than or Equal
< Less Than
<= Less Than or Equal
IN ( ) Matches a value in a list
NOT Negates a condition
BETWEEN Within a range (inclusive)
IS NULL NULL value
IS NOT NULL Non-NULL value
LIKE Pattern matching with % and _
EXISTS Condition is met if subquery returns at least one
row
BETWEEN

In order to select data that is within range of values, the BETWEEN operator is
used. The BETWEEN operator allows the selection of rows that contains values
within a specified lower and upper limit. The range coded after the word
BETWEEN is inclusive.

The lower value must be coded first. The two values in between the range must
be linked with keyword AND. The BETWEEN operator can be used with both
character and numeric data types. However, data types cannot be mixed.

EXAMPLE: SELECT * FROM EMP WHERE DEPTNO BETWEEN 10 AND 30;

PATTERN MATCHING

The LIKE predicate allows comparison of one string value with another string
value, which is not identical. This is achieved using Wildcard characters. The two
wildcard characters available are:

% allows to match any string of any length.

Patel Dhruti Enrollment no. : 22C21058


3

_ allows to match on a single character

Example:

SELECT ENAME FROM EMP WHERE ENAME LIKE ‘HA%’; SELECT ENAME FROM
EMP WHERE ENAME LIKE ‘HA_ _ Y’;

IN PREDICATE

The arithmetic operator (=) compares a single value to another single value. In
case a value needs to be compared to a list of values then the IN predicate is
used. The IN predicate helps reduce the need to use multiple OR conditions.

Example:

SELECT * FROM EMP WHERE DEPNO IN (10, 30, 40);

The above example displays all records where dept. no value is 10, 30 or 40;

EXISTS

The SQL EXISTS condition is used in combination with a subquery and is


considered to be met, if the subquery returns at least one row. It can be used in
a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax:

SELECT <COLUMNNAME> FROM <TABLENAME> WHERE EXISTS (subquery);


Parameters or Arguments: subquery

The subquery is a SELECT statement. If the subquery returns at least one record
in its result set, the EXISTS clause will evaluate to true and the EXISTS condition
will be met. If the subquery does not return any records, the EXISTS clause will
evaluate to false and the EXISTS condition will not be met.

Note:

SQL statements that use the EXISTS condition are very inefficient since the
subquery is rerun for EVERY row in the outer query's table. There are more
efficient ways to write most queries, that do not use the EXISTS condition.

Example

SELECT * FROM EMP WHERE EXISTS (SELECT DEPTNO FROM DEPT WHERE
DEPT=10);

ORACLE TABLE DUAL

DUAL is a table owned by SYS. SYS owns Data Dictionary and the Dual is part of
Data Dictionary. Dual is a small Oracle worktable, which consists of only one row
and one column, and contains the value x in that column. Besides, arithmetic
calculation, it also supports date retrieval and it’s formatting.

Patel Dhruti Enrollment no. : 22C21058


4

Often a simple calculation needs to be done, for example 2*2. The only SQL verb
to cause an output to be written to screen is SELECT. However, a Select must
have a table name in it’s FROM clause. Otherwise the select fails.

Example:

SELECT * FROM DUAL;

SELECT 2*2 FROM DUAL;

SELECT SYSDATE FROM DUAL;

SYSDATE IS A PSEUDOCODE THAT CONTAINS CURRENT DATE AND TIME.

PRACTICAL EXECUTION:

1. Give details of account no. and deposited rupees of customers having


account opened between dates 01-01-06 and 25-07-06.

2. Display all jobs with minimum salary is greater than 4000.

3. Display name and salary of employee whose department no is 20. Give


alias name to name of employee.

Patel Dhruti Enrollment no. : 22C21058


5

4. Display employee no, name and department details of those employee


whose department lies in (10, 20).

To study various options of LIKE predicate:

5. Display all employee whose name start with ‘A’ and third character is ‘A’.

6. Display name, number and salary of those employees whose name is 5


characters long and first three characters are ‘ANI’.

7. Display the non-null values of employees and also employee name second
character should be ‘N’ and string should be 5 character long.

Patel Dhruti Enrollment no. : 22C21058


6

8. Display the null values of employee and also employee name’s third
character should be ‘A’.

9. What will be output if you are giving LIKE predicate as ‘%\_%’ ESCAPE ‘\’

VIVA QUESTIONS:

1) Can you sort a column using a column alias?


YES, by the means of the ORDER BY command

2) Is a NULL value same as zero or a blank space? If not, then what is the
difference?
NO NULL VALUE is not same as the zero or blank space as the zero is
a integer and blank space is consider in the string type

3) Say True or False. Give explanation if false: If a column value taking part in
an arithmetic expression is NULL, then the result obtained would be NULL?

TRUE

4) If a table contains duplicate rows, does a query result display the


duplicate values by default? How can you eliminate duplicate rows from a
query result?
THE RESULT WILL DISPLAY ALL THE DUPLICATE ROWS
TO ELIMINATE THOSE ROWS, WE HAVE TO USE DISTINCT KEYWORD

5) Say True or False. Give explanation if false: The DISTINCT keyword allows
a function consider only non-duplicate values.
TRUE

Patel Dhruti Enrollment no. : 22C21058

You might also like