0% found this document useful (0 votes)
26 views9 pages

Lab 4

lab 4

Uploaded by

umair malik
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)
26 views9 pages

Lab 4

lab 4

Uploaded by

umair malik
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/ 9

Riphah International University

Faculty of Computing
Database Systems
Lab 4: Introduction to Oracle and SQL

Learning Objective
After completing this lab the student should be able to:
 Discuss Logical operator and their use.
 Difference between single row function and multiple row function.
 Describe various types of functions that are available in SQL.
Tools and Technologies
 Oracle 11g Express edition / enterprise edition
HR Schema for Reference

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


Logical operators

A logical operator combines the result of two component conditions to produce a


single result based on them or to invert the result of a single condition. Three logical
operators are available in SQL.

 AND Returns TRUE if both component conditions are TRUE


 OR Returns TRUE if either component condition is TRUE
 NOT Returns TRUE if the following condition is FALSE

You can use several conditions in one WHERE clause using the AND and OR
operators.

Syntax
Select column_name, all
From table Name
Where column_name=value and column_name=value;

Example:

Write a query to display employee number, first name, job id ,salary form those
employees whose salary is greater than equal to 1100 and job_id is ‘IT_PROG’

SELECT employee_id,first_name, job_id ,salary from employees where


salary>=1100 and job_id =’IT_PROG’;

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


Exercise:
Write a query to display job_id and min salary from those jobs whose job
title ‘President’ and maximum salary is 40000

Write a query to show job start date and job end date of those employees
whose department id is 110 and job title is ‘AC_ACCOUNT’

Write a query to display first_name , last_name of those employees whose


job_id is' ST_CLERK and employee number is 131;

OR operator
Example:
Write a query to display employee number, first name, job title ,salary form
those employees whose salary is greater than equal to 1100 or job_id is
‘IT_PROG’
SELECT employee_id,first_name, job_id ,salary from employees where
salary>=1100
or job_id =’IT_PROG’;
Lab Task 1:
 Write a query to display salary and job tile of those employee whose salary is
greater than 4000 or job title IT_PROG
 Write a query to show country code, country name of those employees
whose country name ‘Argentina’ or region id 4

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


Not IN
Example:
Write a query to display first_name,job title form those employees whose job
title not in st_clerk and programmer.
Select first_name,job_id
From employees
Where job_id not in (‘ST_CLERK’, ‘PROGRAMMER’)
Lab Task 2:
 Write a query to display employee_id,salary from those employees where
department_id not 50 and 80.
 Write a query to show all records in job_history except those records whose
Job_id (‘IT_PROG’, ‘AC_ACCOUNT’)

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


Rules of Precedence

Order Evaluated Operator


1 All comparison operators
2 NOT
3 AND
4 OR

Override rules of precedence by using parentheses

Check these two examples:


SELECT first_name, job_id ,salary from employees where job_id='ST_CLERK'
or job_id ='IT_PROG' and salary >6000;
SELECT first_name, job_id ,salary from employees where ( job_id=’ST_CLERK’
or job_id =’IT_PROG’) and salary >6000;

Order by clause
The order by clause can be used to sort the rows. If it is used then it must be
placed in last. The default sort order is ascending. To reverse the order you
specify the keyword DESC after column name in ORDER BY clause.
e.g
Syntax
Select comlumn_name
From Table_name
Order by column_name

SELECT first_name, job_id ,department_id,hire_date from employees


order by hire_date asc ,first_name asc;

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


SELECT first_name, job_id ,department_id,hire_date from employees order by
hire_date desc;

You can use a column alias in the order by clause.


e.g
SELECT employee_id,first_name,salary *12 annualsalary from employees
order by annualsalary ;

Lab Task 3:

Write a query to show all country name is descending order Sorting by multiple
columns
You can sort query results by more than one column. The sort limit is the
number of columns in the given table.
e.g
SELECT first_name,department_id,salary from employees order by
department_id , salary desc;

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


SQL Functions

Functions are a very powerful feature of SQL and can be used to do the following:
 Perform calculation on data
 Modify individual data items
 Manipulate output for groups of rows
 Format dates and numbers for display
 Convert column data types

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


Two Types of Function

Character Manipulation Function


CONCAT, SUBSTR, LENGTH, INSTR, and LPAD are the five character manipulation
functions
 CONCAT: Joins values together
 SUBSTR: Extracts a string of determined length
 LENGTH: Shows the length of a string as a numeric value
 INSTR: Finds numeric position of a named character
 LPAD: Pads the character value right-justified
 RPAD: Pads the character value left-justified
Functions Result
CONCAT (‘Good’,’Sring’) GoodString
SUBSTR (‘String’,1,3) Str
LENGTH (‘String’) 6
INSTR (‘String’, ‘r’) 3
LPAD (salary , 10, ‘*’) ******5000

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah


Example
To display employee name and job_id joined together, length of the employee
name, and the numeric position of the letter A in the employee name, for all
employees who are in department 110
SELECT first_name, CONCAT (first_name, job_id ), LENGTH (first_name), INSTR
(first_name,’A’) FROM employees
WHERE department_id=110;

Lab Task 4:

Write a query to display full name of that employees whose first name contains ‘a’
and salary is 6000, 8400, 8600 and 6100.

Display the full name of each employee, and please note each name is right
justified.

Database Systems (Lab 4) Lab Instructor: Syed Hassaan Ali Shah

You might also like