SQL Operators Oracle
SQL Operators Oracle
SQL Operators Oracle
LEVEL PRACTITIONER
Icons Used
Questions
Tools
Hands on Exercise
Coding Standards
Case Study
Demonstration
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?
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;
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;
||
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
BETWEEN Check is the operand value AND/NOT is between a range, this BETWEEN AND includes the lower and higher limits.
10
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
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
12
Time To Reflect
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
15
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
Solution #2:
Solution #3:
Solution #4:
Solution #5:
18
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
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
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
25
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
Select Product_ID, Product_Name from Products INTERSECT Select Oder_Id,Oder_Name from Orders;
Output: Product_Id 2 Product_Name Product 2
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
Select Product_ID, Product_Name from Products MINUS Select Oder_Id,Oder_Name from Orders;
30
Time To Reflect
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
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
1 2 3 4 6
BASE_FEES
10 10 5 10 40
COURSE_FEES_HISTORY
1 2 3 4 6
33
34
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
COURSE_FEES 2 3
36
BASE_FEES 1 4
37