You are on page 1of 38

Oracle 10g SQL Oracle SQL Operators

LEVEL PRACTITIONER

About the Author

Created By: Credential Information: Version and Date:

Madhava(t-Madhava)/ Shanmu (105110) Trainer/ Sr Architect 1.0, Jan 30 , 2012

Cognizant Certified Official Curriculum


2

Icons Used

Questions

Tools

Hands on Exercise

Coding Standards

Test Your Understandi ng

Case Study

Demonstration

Best Practices & Industry Standards

Workshop

33

Objectives
After completing this chapter you will be able to understand, What are SQL Operators? What are Arithmetic Operators? What are Character Operators? What are Single Row Comparison Operators? What are Logical Operators? What are Set Operators?

Recap of the Case Study


We will use the same CMS case study for learning how to use operators in DQL and DML statements Case Study Scenario: This case study is to develop a Course Management System (CMS) for ABC University. The following are the two uses case for which the database needs to be designed. Add Course To add the course details into the course management system. Retrieve Course Retrieve the courses stored in the system and display it. The courses to be added will have the following attributes Course Code, Course Name, Number of participants, Course Description, Course Duration, Course start date and Course Type.

SQL Operators
What are SQL Operators ? An SQL Operator used for processing data values (stored in columns of tables) and returns a result. The data values are called operands. SQL Operators are represented by special characters or by keywords. Oracle SQL supports the following operators
1. 2. 3. 4. 5. 6.

Arithmetic Operators Character Operator Single Row Comparison Operators Multiple Row Comparison Operators Logical Operators Set Operators

Arithmetic Operators
Arithmetic operators are used to manipulate numeric operands that is columns storing numeric values. Operator +(unary) -(Unary) / * + Description Makes operand positive Makes operand negative Division(Used with Number and Date) Multiplication Addition (numbers and dates) Example SELECT + EMP_SAL FROM EMPLOYEE; SELECT - EMP_SAL FROM EMPLOYEE; SELECT SAL / 10 FROM EMP; SELECT SAL * 10 FROM EMP; SELECT SAL + 1000 FROM EMP;

Subtraction (numbers and dates) SELECT SAL - 500 FROM EMP;

Character Operators
Character operators are used in to manipulate string operands that is columns storing string values. Operator || Description Concatenates character strings Example SELECT EMP_NAME || || is || || Employee of CTS FROM EMPLOYEE;

||

operator combines the values of more than one column also.

Example: EMP_ID || EMP_NAME || JOIN_DATE

Single Row Comparison Operators


Single Row Comparison operators are used in conditions that compare one operand with another. The result of a comparison can be TRUE (or) FALSE. Operator Description Example = !=, <> > < >= <= Equality Test Inequality Test Greater than test Less than test SELECT EMP_SAL FROM EMPLOYEE WHERE EMP_ID=3; SELECT EMP_PF FROM EMPLOYEE WHERE EMP_ID!=7; SELECT EMP_SAL FROM EMPLOYEE WHERE EMP_ID>3; SELECT EMP_SAL FROM EMPLOYEE WHERE EMP_ID<3;

Greater than or equal to test. SELECT EMP_SAL FROM EMPLOYEE WHERE EMP_ID >= 20; Less than or equal to test. SELECT EMP_SAL FROM EMPLOYEE WHERE EMP_ID <= 150; 9

Multiple Row Comparison Operators


Multiple Row Comparison operators are used in conditions that compares a value to each value in a list . Operator Description Example IN/NOT IN Equivalent to comparing the SELECT EMP_ID FROM EMP WHERE ENAME operand value with a list of IN ('SMITH', 'WARD'); values and if any match happens it returns true. SELECT EMP_ID FROM EMP WHERE ENAME NOT IN ('SMITH', 'WARD'); SELECT ENAME,JOB FROM EMP WHERE SAL BETWEEN 3000 AND 5000; // selects employee whose salary between 3000 and 5000. it also includes the value 3000 and 5000. SELECT ENAME,JOB FROM EMP WHERE SAL NOT BETWEEN 3000 AND 5000; // selects employee whose salary is NOT between 3000 and 5000. it also includes the value 3000 and 5000.

BETWEEN Check is the operand value AND/NOT is between a range, this BETWEEN AND includes the lower and higher limits.

10

Multiple Row Comparison Operators


Operator LIKE/NOT LIKE Description The LIKE operator is used for wild card matching. _ is used for single character. % used for multiple or no character. Example SELECT EMP_NAME FROM EMP WHERE EMP_NAME LIKE %an'; //Select employees whose name ends with AN Example: Conan, Ronan etc. SELECT EMP_NAME FROM EMP WHERE EMP_NAME LIKE Ro_'; //Select employees whose name starts with Ro and ends with one character after it. Example: Ron, Roy etc. SELECT EMP_NAME FROM EMP WHERE EMP_SAL >= ALL (1400, 3000); // Compares if salary is greater than all the values in the list which is 1400 & 3000 if so returns true

ALL

Compares a value with every value in a list or returned by a query. Must be preceded by =, ! =, >, <, <=, or >=. Evaluates to TRUE if the query returns no rows.

11

Multiple Row Comparison Operators


Operator ANY/SOME Description Example

Compares a value to SELECT DEPT_NAME FROM DEPT WHERE each value in a list or LOC = SOME ('NEW YORK','DALLAS'); // returned by a query. Must Compares if location is one of the values either be preceded by =, !=, >, NewYork or Dallas if so returns true. <, <=, or >=. Evaluates to FALSE if the query returns no rows. Tests for nulls. This is the SELECT EMP_NAME FROM EMP WHERE only operator that should EMP_NAME IS NOT NULL AND SAL > 1500; // be used to test for nulls. returns all records which has salary > 1500 and name is not null

IS NULL/ IS NOT NULL

12

Time To Reflect

Associates to reflect the following before proceeding

What is the operator used for concatenating two column values? What is the operator used for checking whether a age falls in the range 10 and 60? What is operator used to check if a name starts with "An"? What is operator used to check if a column values meets all the values in a list or a subquery? How to check if a column is null?
13

Lend A Hand - Prerequisites


Pre-requisite # 1 :Associates should ensure that the tables specified in the document are available in the oracle database with each table followed by the employee id . <Refer to the Resources section of this Session for the attachment CMS tables.doc> Pre-requisite # 2: Load the table with necessary data using the DML statements.
14

Lend a Hand - Operators


Develop the queries for the problems stated below, Problem # 1: Calculate the total fees (base fees + Special fees) for the all the courses and display the course code and total fees. Problem # 2: Calculate the discount fees for all the courses and display the course code and discount fees. Discount fees = discount* (base fees + Special fees)/100 Hint: Use the course_fees _<employee_id> table for this. Problem # 3: Display the names of all the students as follows, Name of the Student is <First Name> <Last Name> Hint: Use the student_info _<employee_id> table for this.

15

Lend a Hand - Operators


Problem # 4: Select all the courses whose base fee > 200. Hint: Use the course_fees _<employee_id> table for this. Problem # 5: Display the students Id , first name whose first name and last name are not equal. Hint: Use the student_info _<employee_id> table for this. Problem # 6: Select all the courses whose base fee is in the range 100 and 3000. Hint: Use the course_fees_<employee_id> table for this. Problem # 7: Display the students Id , first name whose first name starts with A Hint: Use the student_info _<employee_id> table for this.
16

Lend a Hand - Operators

Problem # 8: Display the students Id , first name whose first name has a character o Hint: Use the student_info_<employee id> table for this. Problem # 9: Display the names of all the courses where the course description is Null. Hint: Use the courses_info_<employee id> table for this.

17

Lend a Hand - Solutions


Solution #1:

Solution #2:

Solution #3:

Solution #4:

Solution #5:

18

Lend a Hand - Solutions


Solution #6:

Solution #7:

Solution #8:

Solution #9:

19

Logical Operators
Logical operators is used for manipulating the results of one or more conditions. Example: If age > 45 AND salary < 4000. Here And is the operator used to combine the results of the both the condition and returns a result. Operator Description Example
NOT Returns TRUE if the condition SELECT EMP_NAME FROM EMP WHERE returns FALSE. Returns FALSE if NOT (JOB IS NULL) // Retrieves the the return values is TRUE. employee names who has a job assigned. Used to combine two conditions. Returns TRUE if both condition are met. Returns FALSE if either of it is FALSE. SELECT EMP_NAME FROM EMP WHERE JOB='CLERK AND manager=Tom ; // Retrieves the employee names who has a designation clerk and their manager is Tom.

AND

OR

Returns TRUE if one of the SELECT EMP_NAME FROM EMP WHERE condition returns TRUE. Returns JOB='CLERK OR MGR_ID=10 ; // FALSE if both are FALSE. Retrieves the employee names who has a designation clerk (or) their manager is Tom.

20

Set Operators
Set operators combine the results of two queries into a single result. The two queries can be a select query from a same table or from different tables. The different types of Set Operators are
1.

UNION - Returns all distinct rows selected by both the queries. UNION ALL - Returns all rows selected by either query, including all duplicates. INTERSECT - Returns all distinct rows selected by both queries MINUS - Returns all distinct rows selected by the first query but not the second.

2.

3.

4.

21

Rules of Set Operators


1.

Both queries should select the same number of columns.

2. The columns must be of the same data type. However the length and name of the columns may be different. 3. Column names of first query will be column headings of the retrieved records. Select Name, Salary, Age from person <Set operator> Select EmpName, EmpSalary, EmpAge from Employee The records retrieved will have the columns for the first table
Name Hamilton Salary Lewis Age 32

4. Null values are treated as identical. Normally two null values are not equal. In SET operations if Oracle encounters two null values, one in first table and another in second table then Oracle will treat them as identical. 22

Union Operator

The UNION operator returns all rows selected by the either query UNION operates over all of the columns being selected. By default the output is stored in the ascending order of the first column of the select clause.

Syntax: select column1,column1, .....,columnN from table1 UNION select column1,column1, .....,columnN from table2;

23

Example: Union Operator


Product_ID 1 2 3 Products Product_Name Product 1 Product 2 Product 3 Duplicate records across the table. Order_ID 1 5 4 5 Orders Shop_Name Product 1 Product 5 Product 4 Product 5 Duplicate records within the table.

Select Product_ID, Product_Name from Products UNION Select Oder_Id,Oder_Name from Orders;
Output: Product_Id 1 2 3 4 5 Product_Name Product 1 Product 2 Product 3 Product 4 Product 5

All the unique records from both the tables will be fetched.

24

Union All Operator


The UNION ALL query allows you to combine the result sets of more than one select queries. It returns all rows including duplicate records. Syntax: select column1,column1, .....,columnN from table1 UNION ALL select column1,column1, .....,columnN from table2;

25

Example: Union All Operator


Product_ID 1 2 3 Products Product_Name Product 1 Product 2 Product 3 Orders Order_ID 1 5 4 Shop_Name Product 1 Product 5 Product 4

Select Product_ID, Product_Name from Products UNION ALL Select Oder_Id,Oder_Name from Orders;
Output: Product_Id 1 2 3 4 5 1 Product_Name Product 1 Product 2 Product 3 Product 4 Product 5 Product 1 This also retrieves the duplicate records

26

Intersect Operator
The INTERSECT operator returns all records common in the select queries. The number of columns and data type of the columns being selected must be identical in all the SELECT statements used in the query. Syntax: select column1,column1, .....,columnN from table1 INTERSECT select column1,column1, .....,columnN from table2;

27

Example: Intersect Operator


Products Product_ID 1 2 3 Product_Name Product 1 Product 2 Product 3 Order_ID 2 4 5 Orders Shop_Name Product 2 Product 4 Product 5

Select Product_ID, Product_Name from Products INTERSECT Select Oder_Id,Oder_Name from Orders;
Output: Product_Id 2 Product_Name Product 2

Product 2 record which is common between two tables is retrieved.

28

Minus Operator
The MINUS Operator return the rows of the first query that are not present in the second query. The result of first select statement MINUS The result of second select statement. Syntax: select column1,column1, .....,columnN from table1 MINUS select column1,column1, .....,columnN from table2;

29

Example: Minus Operator


Products Product_ID 1 2 1 Product_Name Product 1 Product 2 Product 3 Order_ID 2 3 4 Orders Shop_Name Product 2 Product 3 Product 4

Select Product_ID, Product_Name from Products MINUS Select Oder_Id,Oder_Name from Orders;

Output: Product_Id 1 1 Product_Name Product 1 Product 3

30

Time To Reflect

Associates to reflect the following before proceeding

What is the operator used for retrieving the common records between two tables? How can one retrieve all the unique records from both the tables? How can one retrieve all the records including the duplicate values from both the tables?

31

Lend a Hand Logical Operators


Develop the queries for the problems stated below, Problem # 1: Display the names of all the courses whose course duration is greater than 10 and number of participants is less than 20. Hint: Use the courses_info _<employee_id> table for this. Solution #1:

Problem # 2: Display the course code whose base fees is greater than 100 or special fees less than 1000. Hint: Use the course_fees _<employee_id> table for this. Solution #2:

32

Lend a Hand Set Operator


Pre- Requisite: Create a table named COURSE_FEES_HISTORY with the following COURSE_FEES columns.
Column Name Data Type
COURSE_CODE BASE_FEES SPECIAL_FEES DISCOUNT

Course_Code Varchar2 Base_fees Number

1 2 3 4 6
BASE_FEES

180 150 160 150 190


SPECIAL_FEES

100 110 170 100 100


CREATED_BY Updated _By

10 10 5 10 40

Special_fees Number Created_By Updated_By Varchar2 Varchar2 COURSE_CODE

COURSE_FEES_HISTORY

1 2 3 4 6

120 150 160 170 190

123 110 170 235 100

Ram Bala Bala Ram Vinod

Ramesh Ram Vinu Ram Vinod

33

Lend a Hand Set Operator


Problem # 1: Display all the unique courses between course_fees_<Employee_id> and course_fees_history _<Employee_id> . Use the following columns to check for uniqueness Course_Code,BASE_FEES and SPECIAL_FEES of the courses in both the COURSE_FEES _<Employee_id> and COURSE_FEES_HISTORY _<Employee_id>. Problem # 2: Display all the common courses between course_fees _<Employee_id> and course_fees_history _<Employee_id> . Use the following columns to check for commonality Course_Code, BASE_FEES and SPECIAL_FEES of the courses in both the COURSE_FEES _<Employee_id> and COURSE_FEES_HISTORY _<Employee_id> Problem #3 : Display all the courses which exists course_fees_history _<Employee_id> but not in course_fees _<Employee_id> . Use the following columns for comparing records Course_Code, BASE_FEES and SPECIAL_FEES of the courses in both the COURSE_FEES _<Employee_id> and COURSE_FEES_HISTORY _<Employee_id>.

34

Lend a Hand Solution


Solution #1:

Sample Output: COURSE_FEES 1 1 2 3 4 4 6 BASE_FEES 120 180 150 160 150 170 190 SPECIAL_FEES 123 100 110 170 100 235 100

35

Lend a Hand Set Operator


Solution # 2:

COURSE_FEES 2 3

BASE_FEES 150 160

SPECIAL_FEES 110 170

36

Lend a Hand Set Operator


Solution #3 :

BASE_FEES 1 4

SPECIAL_FEES 120 170

BASE_FEES 123 235

37

Oracle 10g SQL

You have successfully completed


Oracle SQL Operators

You might also like