0% found this document useful (0 votes)
10 views8 pages

10992exp 2

Uploaded by

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

10992exp 2

Uploaded by

cat3eow9503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB MANUAL

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment no: - 2

Experiment Name: - Ret rival Commands-I

Aim: - Performing practical by using restricting, sorting conditions and single-


row functions.

Resource required: - Oracle 9i - iSQLplus

Theory: -

 RESTRICTING AND SORTING DATA:


Limiting the Rows
Selected: Syntax:-
SELECT *| {[DISTINCT] column / expression [alias],….}
FROM table
[WHERE condition(s)];
-Restrict the rows returned by using the WHERE
clause Example:
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE depertment_id = 90;
EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID
100 King AD_PRES 90
101 Kochhar AD_VP 90
102 De Haan AD_VP 90
- Comparison Conditions
1) SELECT last_name, salary
FROM employees
WHERE salary<= 3000;
LAST_NAME SALARY
Matos 2600
Vargas 2500
2) BETWEEN Condition: display row based on a range of
values SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
LAST_NAME SALARY
Rajs 3600
Devies 3200
Matos 2600

3) IN Condition: test for values in a list


SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100,101,201);
EMPLOYEE_ID LAST_NAME SALARY MANAGER_ID
202 Fay 6000 201
200 Whalen 4400 101
205 Higgins 120000 100
4) LIKE Condition: Combine pattern-matching character
SELECT last_name
FROM employees
WHERE last_name LIKE „_o%‟;
LAST_NAME
Kochhars
Mourgas
- Logical Conditions
5) AND & OR operator:
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE (job_id =„SA_REP‟
OR job_id = „AD_PRES‟)
AND salary> 15000;
LAST_NAME JOB_ID SALARY
King AD_PRES 2400
6) NOT operator:
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN („IT_PROG‟, „ST_CLERK‟, „SA_REP‟);
LAST_NAME JOB_ID
King AD_PRES
Kichhar AD_VP
Morgus AD_VP
Ziofkey ST_MAN
- ORDER BY Clause: Sort rows
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal;
EMPLOYEE_ID LAST_NAME ANNSAL
144 Vargas 30000
143 Matos 31200
141 Davies 37200
107 Rajas 50400
 SINGAL-ROW FUNCTION:
1. Character Function
2. Number Function
3. Date Function
4. Conversion Function
5. General Function
Character-Manipulation Functions
SELECT employee_id, CONCAT (first_name, last_name) NAME,
Job_id, LENGTH (last_name),
INSTR (last_name, „a‟) “Contains „a‟?”
FROM employees
WHERE SUBSTR (job_id, 4) = „REP‟;
NAME JOB_ID LEN
G
T
H(
L
AS
T_
N
A
M
E_
EllenAbel
17 4

JonathonTaylor
17 6

PatFay
17 MK_REP 5

Number Functions
- ROUND: rounds value to specified decimal
SELECT ROUND (45.923, 2), ROUND (45.923, 0)
,
ROUND (45.923, -
1) FROM DUAL;
ROUND(45.923,2) ROUND (45.923, 0) ROUND (45.923, -1)
45.92 46 50
DUAL is a dummy table
- TRUNC: truncates value to specified decimal
SELECT TRUNC (45.923, 2), TRUNC
(45.923),
TRUNC (45.923, -
2) FROM DUAL;
TRUNC(45.923 , 2) TRUNC (45.923) TRUNC(45.923,-2)
45.92 45 0
- MOD: returns remainder of division
SELECT last-name, salary, MOD (salary, 5000) FROM
employees
WHERE job_id = „SA_REP‟;
LAST_NAME SALARY MOD(SLARY, 5000)
Abel 1100 1000
Taylor 6600 3600
Grant 7000 2000
Date Functions
- MONTHS_BETWEEN: Number of months between two
dates MONTHS_BETWEEN („01-SEM-95‟, ‟11-JAN-94‟)
ANS: 19.6774194
-
- ADD_MONTHS: Add calendar months to
date ADD_MONTHS (‟11-JAN-94‟, 6)
ANS: ’11-Jul-94’
- NEXT_DAY: Next day of month
NEXT_DAY („01-SEP-95‟, „FRIDAY’)
ANS: ’08-SEP-95’
- LAST_DAY: Last day of the month
LAST_DAY (‟01-FEB-95‟)
ANS: ’28-FEB-95’

- ROUND: Round date


Assume SYSDATE= ‟25-JUL-95‟:
ROUND (SYSDATE, MONTH‟)
ANS: 01-AUG-95

- TRUNC: Truncate date


TRUNC (SYSDATE, „YEAR‟)
ANS: 01-JAN-95

Conversion Functions
Data type conversion
- from number to character
TO_CHAR (number, „format_model‟)
- from character to number
TO_NUMBER (char [, „format_model‟])
- from character to date
TO_DATE (char, „format_model‟])
- from date to character
TO_CHAR (date, „format_model‟)

e.g.: SELECT last_name, TO_CHAR (hire date, „DD-Mon-YYYY‟)


FROM employees
WHERE hire_date < TO_DATE (‟01-Jan-90‟, „DD-Mon-RR‟);
LAST_NAME TO_CHAR(HIR
King 17-Jan-1987
Kochhar 21-Sep-1989
Whalen 17-Sep-1987

General Functions
These functions work with any data type and pertain to using nulls.
- NVL (expr1, expr2)
- NVL2 (expr1, expr2, expr3)
- NULLIF (expr1, expr2)
- COALESCE ( expr1, expr2, ….,
exprn) Others are Conditional Expressions:
- Use of IF-THEN-ELSE logic within a SQL statement
- Use two methods: CASE expression and DECODE function

Conclusion:
This practical covers topics:
- Selecting, restricting, and sorting data.
- Perform calculations on data using various functions.
PART B
(PART B: TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical. The soft copy
must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties at the end of the
practical in case the there is no Black board access available)

Roll. No. Name:


Class Batch:
Date of Experiment: Date of Submission:
Grade:
B.1: Read the given question carefully and write a SQL statement and check the output.
(Put the query and its output after each question.)
1. Create a query to display the last_name, department_id of all employees whose salary
is greater than $7000.
2. Create a query to display the last name and salary for all employees whose salary is
not in the range of $5,000 and $12,000.(make use of BETWEEN ….AND clause)
3. Display the last name and department number, job_id, salary of all employees in
departments 20 and 50 in alphabetical order by name.
4. Display the last name, salary, and commission for all employees who earn
commissions. Sort the data in descending order of commissions.
5. Display the last name, job, and salary for all employees whose job is Sales
Representative(SA_REP) or Stock Clerk (ST_CLERK) and whose salary is not equal to $2,
500,
$3,500, or $7,000. (Make use of the IN clause.)
6. Write a query that displays the last_names of employees with last_name starting with
‘J’ or ‘A’ or ‘M’.

B.2: Write an observation according to the query execution.

B.3: Write an answer of the following questions


1. Explain the difference between BETWEEN and using >= and <= conditions in the WHERE
clause.
2. Can the BETWEEN operator be used with text or date values? Provide an example.
3. What happens if the lower bound of a BETWEEN condition is greater than the upper bound?
4. Compare the performance of the IN operator with multiple OR conditions in a WHERE
clause.
5. What are the limitations of the LIKE operator when performing pattern matching?
6. A database has a table of employees with columns for name, age, and department. Explain
how you would use the WHERE, BETWEEN, and LIKE operators to find employees aged 30–40
in departments starting with 'HR'.

You might also like