0% found this document useful (0 votes)
19 views

B48 SQL Exp2

This experiment covers restricting and sorting data using conditions like WHERE, BETWEEN, IN, LIKE, logical operators AND and OR, and ORDER BY. It also covers single-row functions to manipulate characters, numbers, dates, and data type conversions. The document provides examples of queries using these concepts and functions and assigns lab work to write queries applying these skills.

Uploaded by

Nikhil Aher
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

B48 SQL Exp2

This experiment covers restricting and sorting data using conditions like WHERE, BETWEEN, IN, LIKE, logical operators AND and OR, and ORDER BY. It also covers single-row functions to manipulate characters, numbers, dates, and data type conversions. The document provides examples of queries using these concepts and functions and assigns lab work to write queries applying these skills.

Uploaded by

Nikhil Aher
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

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‟;
EMPLOYEE_ID NAME JOB_ID LENGTH(LAST_NAME_ Contains’a?
174 EllenAbel SA_REP 4 0
176 JonathonTaylor SA_REP 6 2
178 PatFay MK_REP 5 3

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.
LAB ASSIGNMENT -2

1. Create query to display the last name and salary for all employees whose salary
is not in the range of $5,000 and $12,000.

2. Display employees last name, job ID, and start date of employees hired between
February 20, 1998, and May 1, 1998. Order the query in ascending order by
start date.
3. Display the last name and department number 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 data in descending order of salary and
commissions.

5. Display the last name, job, and salary for all employees whose job is sales
Representative or stock clerk and whose salary is not equal to $2, 500, $3,500, or
$7,000.

6. Write a query to display the current date. Label the column Date.
7. For each employee, display the employee number, last name, salary, and salary
increased by 15% and expressed as a whole number.
8. Write a query that displays the employee‟s last names with the first letter
capitalized and all other letters lowercase, and length of the names, for all
employees whose name starts with J,A, or M. Give each column an appropriate
label. Sort the results by the employees last names.

You might also like