0% found this document useful (0 votes)
117 views50 pages

Class Copy of SQL OPERATORS (Chap 5)

The document discusses various SQL operators including arithmetic, comparison, logical, and concatenation operators. It also discusses SQL functions that are categorized as group functions (aggregate functions) and scalar functions (single row functions). Some key points include: - Arithmetic operators include addition, subtraction, multiplication, and division operators. - Comparison operators compare two values and return true, false, or null. Some examples given are equal to, not equal to, less than, greater than, between. - Logical operators combine comparison results and include NOT, AND, and OR. - Functions are used to manipulate data and return results. Group functions act on sets of values while scalar functions act on single values.

Uploaded by

meenadegree
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views50 pages

Class Copy of SQL OPERATORS (Chap 5)

The document discusses various SQL operators including arithmetic, comparison, logical, and concatenation operators. It also discusses SQL functions that are categorized as group functions (aggregate functions) and scalar functions (single row functions). Some key points include: - Arithmetic operators include addition, subtraction, multiplication, and division operators. - Comparison operators compare two values and return true, false, or null. Some examples given are equal to, not equal to, less than, greater than, between. - Logical operators combine comparison results and include NOT, AND, and OR. - Functions are used to manipulate data and return results. Group functions act on sets of values while scalar functions act on single values.

Uploaded by

meenadegree
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

SQL OPERATORS

A)Arithmetic Operators
Arithmetic operators operate on numeric datatypes. Following are the various arithmetic operators in Oracle and how to use them. i)+ - Unary operators: Use to represent positive or negative data item. For positive items, the + is optional. Example: 1)create table num(no number(4)); 2)insert into num values(17); 3)select -no from num; 4)update num set no=-no; 5)insert into num values(-17); 6)update num set no=-no; ii)+ Addition: Use to add two data items or expressions. Example: 1)select sal+comm from emp; 2)Select (sal+100)+(comm+5) from emp;

iii)- Subtraction: Use to find the difference between two data items or expressions. Example: 1)select sal-comm from emp; iv)* Multiplication: Use to multiply two data items or expressions. Example: 1)select sal*comm from emp; v)/ Division: Use to divide a data item or expression with another. Example: 1)select sal/comm from emp;

Renaming Columns Used with Expression list Rename the default output column names with an alias, when required. Syntax: Select <Columnname> <Aliasname>, <Columnname> <Aliasname> from <Tablename> Example: 1)select sal+comm Net Salary from emp; Renaming Existing Columns Syntax: Alter table <Tablename> Rename Column <Oldcolumnname> To <Newcolumnname> Example Alter table Emp rename column sal to salary;

Concatenation Operator
The concatenation operator is used to concatenate or join two character (text) strings. The result of concatenation is another character string. If one of the string is NULL, the result is another string. Concatenating a zero-length string ('') with another string results in another string. Two vertical bars (||) are used as the concatenation operator. Examples: 1)'Oracle10g' || 'Database' results in 'Oracle10gDatabase 2)SELECT first_name || ' ' || last_name FROM emp; gives the name of the employees in the emp table.

B)Comparison Operators Comparison operators compare two values or expressions and give a Boolean result of TRUE, FALSE or NULL. i)= Equality test Example: SELECT * FROM emp WHERE last_name = 'SCOTT'; ii)!=,<>,^= Inequality test Any of the three operators may be used. Example: SELECT * FROM emp WHERE first_name != 'TIGER'; iii)< Less than test Example: SELECT last_name FROM emp WHERE salary < 2000;

iv)> Greater than test Example: SELECT first_name, salary FROM emp WHERE salary > 10000; v)<= Less than or equal to test. Example: SELECT last_name FROM emp WHERE salary <= 2000; vi)>= Greater than or equal to test. Example:SELECT first_name, salary FROM emp WHERE salary >= 10000; vii)[NOT] IN Equal to any member of test. If NOT is used,evaluates to TRUE if the value is not in the member list. Example: i)SELECT first_name, salary FROM emp WHERE first_name IN ('JOHN , 'SAM', MARY', 'LEEZA'); ii)SELECT last_name FROM emp WHERE last_name NOT IN (SELECT last_name FROM emp WHERE dept = 10);

viii)[NOT] BETWEEN a AND b TRUE if value greater than or equal to a and less than or equal to b . If NOT is used, the result is the reverse. Example: 1)SELECT last_name FROM emp WHERE salary BETWEEN 5000 and 10000; 2)SELECT last_name FROM emp WHERE salary NOT BETWEEN 5000 and 10000;

ix)[NOT] EXISTS
TRUE if a subquery returns at least one row. FALSE if the value does not exist.

1)create table branch(bno number(2) primary key,name varchar2(20)); 2)select * from branch; bno name 1 mumbai 2 solapur 3 sangli 4 satara

3) create table emp(eno number(2) primary key,ename varchar2(20),city varchar2(20)); 4)select * from emp; eno ename city 11 shaha mumbai 12 patil pune 13 joshi sangli 14 shinde satara
5)SELECT name FROM branch B WHERE NOT EXISTS (SELECT city FROM emp WHERE city = B.name); o/p-solapur 6)SELECT name FROM branch B WHERE EXISTS (SELECT city FROM emp WHERE city=B.name); o/p-mumbai,sangli, satara

x)a [NOT] LIKE b Used for pattern matching; TRUE if pattern a matches to pattern b . Wild character % is used to match any string of length zero or more characters. Wild character _ is used to match any single character. Example i)SELECT last_name FROM emp WHERE last_name NOT LIKE 'THO%'; ii)SELECT last_name FROM emp WHERE last_name LIKE '_HOR%'; xi)IS [NOT] NULL The only operator used to test for NULL values. Example: SELECT last_name FROM emp WHERE salary IS NULL;

C)Logical Operators
Logical operators are used to combine the results of two comparison conditions to produce a single result or to reverse the result of a single comparison. i)NOT Used to reverse the result. Evaluates to TRUE if the operand is FALSE. Evaluates to FALSE if the operand is TRUE. Returns NULL if the operand is NULL.

Example:
SELECT * FROM emp WHERE NOT (salary < 1000); ii)AND Evaluates to TRUE if both operands are TRUE. Evaluates to FALSE if either operand is FALSE. Otherwise returns NULL.

Example:
SELECT * FROM emp WHERE last_name = 'JACOB' AND sal > 5000;

iii)OR
Evaluates to TRUE if either operand is TRUE. Evaluates to FALSE if both operands are FALSE. Otherwise returns NULL.

Example:
SELECT * FROM emp WHERE last_name = 'JACOB' OR last_name = 'THOMAS';

SQL FUNCTIONS

Oracle functions serve the purpose of manipulating data items & returning a result. Functions accepts variables or constants & operating on them. Such variables or constants are called Arguments. Any no. of arguments(or no arguments at all) can be passed to a function in the following Syntax: Function_Name(argument1,argument2,-----) Oracle functions can be clubbed together depending upon whether they operate on a single row or a group of rows retrieved from a table. Accordingly functions are classified as follows:

A)Group Functions(Aggregate functions) Functions that act on a set of values are called as Group functions. E.g.,AVG, is a function ,which calculates the average of set of numbers. A group function returns a single result row for a group of queried rows. B)Scalar Functions(Single Row Functions) Functions that act on only one value at a time are called Scalar Functions. E.g.Length, is a function,which calculates the length of one particular string value. A single row function returns one result for every row of a table.

Single row functions can be further grouped together by the data type of their arguments & return values. E.g.Length relates to String Data type. Functions can be classified corresponding to different data types as: a)String Functions:For String Data type b)Numeric Functions:For Number Data type c)Conversion Functions:For Conversion of one data type to another. d)Date Functions:For Date Data type.

A)Group Functions(Aggregate functions) i)Avg:Returns an average value of n ,ignoring null values in a column. Syntax: Avg([<Distinct>|<All>] <n>) Example: Select Avg(balance) Average Balance from acct_mstr; ii)Min:Returns a minimum value of expression. Syntax: Min([<Distinct>|<All>] <expr>) Example: Select Min(balance) Minimum Balance from acct_mstr;

iii)Max:Returns a maximum value of expression. Syntax: Max([<Distinct>|<All>] <expr>) Example: Select Max(balance) Maximum Balance from acct_mstr; iv)Count(expr):Returns the no. of rows where expr is not null. Syntax: Count([<Distinct>|<All>] <expr>) Example: Select Count(Acc_no) No. of Accounts from acct_mstr;

v)Count(*):Returns the no. of rows in the table, including duplicates & those with nulls. Syntax: Count(*) Example: Select Count(*) No. of records from acct_mstr; vi)Sum:Returns the sum of values of n . Syntax: Sum([<Distinct>|<All>] <n>) Example: Select Sum(balance) Total Balance from acct_mstr;

B)Scalar Functions (Single Row Functions) a)Numeric Functions i)ABS:Returns an absolute value of n . Syntax: ABS(n) Example: Select Abs(-7) Absolute numbers from Num; o/p 7 ii)Power(m,n):Returns m raised to the nth power.n must be an integer,else an error is returned. Syntax: Power(m,n) Example: 1)Select power(3,2) Raised from Dual; o/p-9 2)Select power(0,9) "Raised" from Dual; o/p-0 3)Select power(9,0) "Raised" from Dual; o/p-1 4)Select power(3,-2) "Raised" from Dual;o/p-0.1111111111--------

Note:
DUAL is a dummy table available to all users in the database. The DUAL table is used to select system variables or to evaluate an expression. SELECT SYSDATE, USER FROM DUAL; will show the current system date and the connected username. Dual consists of exactly one column whose name is dummy and one record. The value of that record is X. The dual table is used in Oracle when you need to run SQL that does not logically have a table name.

iii)Round:Returns n,rounded to m places to the right of a decimal point.If m is omitted,n is rounded to 0 places. m can be negative to round off digits to the left of the decimal point. m must be an integer. Syntax: Round(n,[m]) Example: 1)Select Round(15.19,1) round from dual; o/p-15.2 2)Select Round(15.19,3) "round" from dual; o/p-15.19 3)Select Round(1846.19,-2) "round" from dual; o/p-1800 4)Select Round(1856.19,-2) "round" from dual; o/p-1900 5)Select Round(1846.19,-3) "round" from dual; o/p-2000 6)Select Round(1846.19,-4) "round" from dual; o/p-0

iv)SQRT:Returns square root of n. error.SQRT returns a real result.If n<0 i.e. if n is negative then Oracle gives error. Syntax: SQRT(n) Example: 1)Select sqrt(9) square root from dual; o/p-3

v)Extract:Returns a value extracted from a date or an interval


value.A DATE can be used only to extract YEAR,MONTH & DAY while TIMESTAMP can be used to extract HOUR, MINUTE & SECOND. Syntax: Extract{YEAR|MONTH|DAY|HOUR|MINUTE|SECOND} FROM {DATE_VALUE|INTERVAL_VALUE} Example:
1) SELECT EXTRACT(YEAR FROM TO_DATE('01-JAN-2005')) AS YEAR FROM dual; 2) SELECT EXTRACT(MONTH FROM TO_DATE('01-JAN-2005')) AS MONTH FROM dual; 3) SELECT EXTRACT(DAY FROM TO_DATE('01-JAN-2005')) AS DAY FROM dual; 4) SELECT EXTRACT(HOUR FROM TO_TIMESTAMP('01-JAN-2005 19:15:26 ,'DDMON-YYYY HH24:MI:SS')) AS HOUR FROM dual 5) SELECT EXTRACT(MINUTE FROM TO_TIMESTAMP('01-JAN-2005 19:15:26', 'DD- MON-YYYY HH24:MI:SS')) AS MINUTE FROM dual; 6) SELECT EXTRACT(SECOND FROM TO_TIMESTAMP('01-JAN-2005 19:15:26', 'DD-MON-YYYY HH24:MI:SS')) AS SECOND FROM dual;

vi)Greatest:Returns the greatest value in a list of expressions. Syntax: Greatest(expr1,expr2,-------,expr_n) where,expr1,expr2,----expr_n are expressions that are evaluated by the Greatest function.
Example: SELECT Greatest(3,6,67) greatest num1 , Greatest(34,6,1) greatest num2 from dual;

vii)Least:Returns the least value in a list of expressions.


Syntax:
Least(expr1,expr2,-------,expr_n) where,expr1,expr2,----expr_n are expressions that are evaluated by the Least function.

Example:
SELECT Least(3,6,67) Smallest num1 , Least(34,6,1) Smallest Num2 from dual;

Note:
In the Least() & Greatest() function if the datatype of the expressions are different,all expressions will be converted to datatype of first expression.If the comparison is based on character comparison,one character is considered greater than another based on Ascii value of first character in the expression. Example: 1) SELECT GREATEST('gh','DFG','hj',789) from dual; O/P-hj [comparing(103,68,104,55)] 2) SELECT GREATEST(789 ,'gh','DFG','hj') from dual; o/p-error Because we cannot convert Character into Number 3) SELECT LEAST('ORACLE','SQL Server','DB2') as Least_Value FROM dual; o/p-DB2 [comparing(79,83,68) 4) SELECT LEAST('ORACLE',123,345) as Least_Value FROM dual; O/P-123 [comparing(79,49,51)] 5) SELECT LEAST('a',1,null) as Least_Value FROM dual; o/p-null [comparing(97,49,null)] Ascii Chart:

viii)Mod:Returns the remainder of a first number divided by second number passed a parameter.If the second number is zero, the result is the same as the first number. Syntax: Mod(m,n) Example:
1)SELECT Mod(15,7) Mod1 , Mod(15.7,7) Mod2 from dual; o/p-1,1.7 2)SELECT Mod(0,7) "Mod1", Mod(15.7,0) "Mod2" from dual; o/p-0,15.7

ix)Trunc:Returns a number truncated to a certain number of decimal places.The decimal place value must be an integer.If this parameter is omitted,the Trunc function will truncate the number to 0 decimal places. Syntax: Trunc(number,[decimal_places]) Example:
1)SELECT Trunc(125.815,1) Trunc1 , Trunc(125.815,-2) Trunc2 from dual; o/p-125.8, 100 2) SELECT Trunc(125.815,4) "Trunc1", Trunc(125.815,-3) "Trunc2" from dual; o/p-125.815, 0

x)Ceil:Returns the smallest integer value that is greater than or


equal to number. Syntax: Ceil(n) Example:
1)SELECT Ceil(15.7) Ceil1 , Ceil(13.15) Ceil2 , Ceil(-1.23) Ceil3 from dual; o/p-16 , 14 ,-1 2)SQL> select * from myTable ID VALUE ---------- ---------1 12.12 2 -12.12 3 22.1 4 2.1 SQL> SELECT id, value,Ceil(value) FROM myTable ID VALUE Ceil(VALUE) ---------- ---------- ------------------------1 12.12 13 2 -12.12 -12 3 22.1 23 4 2.1 3

xi)Floor:Returns the largest integer value that is less than or equal to a number. Syntax: Floor(n) Example:
1)SELECT Floor(125.8) Floor1 , Floor(13.15) Floor2 from dual; o/p-125, 13 2)SQL> select * from myTable ID VALUE ---------- ---------1 9 2 2.11 3 3.44 4 -4.21 SQL> SELECT id, value,FLOOR(value) FROM myTable ID VALUE FLOOR(VALUE) ---------- ---------- ------------------------1 9 9 2 2.11 2 3 3.44 3 4 -4.21 -5

b)String Functions i)Lower:Returns char,with all letters in lowercase. Syntax: Lower(char) Example: 1)Select Lower( ORACLE ) Lower from dual; o/p-oracle 2)Select Lower( oracle ) Lower from dual; o/p-oracle ii)Upper:Returns char,with all letters in uppercase. Syntax: Upper(char) Example: 1)Select Upper( oracle ) Upper from dual; o/p- ORACLE 2)Select Upper( ORACLE ) Upper from dual; o/p-ORACLE

iii)Initcap:Returns a string first letter of each word in Upper case. Syntax: Initcap(char) Example: 1)Select Initcap( ORACLE DATABASE ) Title Case from dual; o/p-Oracle Database iv)Substr:Returns a portion of characters,beginning at character m & going upto character n.If n is omitted,the result returned is upto last character in the string.The first position of char is 1. Syntax: Substr(<string>,<start_position>,[<length>]) where,string is the source string. start_position is the position for extraction.The first position in the string is always 1. length is the number of characters to extract. Example: 1)Select Substr( oracle ,3,4) substring from dual; o/p- acle 2)Select Substr( oracledatabase ,-5,4) substring from dual; o/p-abas

v)Ascii:Returns the NUMBER code that represents the


specified character.If more than one character is entered,the function will return the value for the first character & ignore all of the characters after the first.

Syntax:
Ascii(<single_character>) where, single_character is the specified character to retrieve the NUMBER code for.

Example:
1)Select Ascii( a ) Ascii1 , Ascii( A ) Ascii2 from dual; o/p-97,65

vi)Instr:Returns a location of a substring in a string.


Syntax:
Instr(<string1>,<string2>,[<start_position>],[<nth_appearance>]) where,string1 is the string to search. string2 is the substring to search for in string1 start_position is the position in string1 where the search will start. If omitted,it defaults to 1.The first position in the string is 1.If the start_position is negative, the function counts back start_position number of characters from the end of string1 & then searches towards the beginning of string1. nth_appearance is the nth_appearance of string2.If omitted,it defaults to 1 .

Example:
1)Select Instr( Functions accepts variables , t ) Instr1 , Instr( Functions accepts variables , t ,1,2) Instr2 from dual; o/p- 5,16 2) Select Instr('Functions accepts variables','s',-10,2) "Instr2" from dual; o/p-9

vii)Translate:Replaces a sequence of characters in a string,with


another set of characters.However,it replaces a single character at a time.e.g.,it will replace the 1st character in the string_to_replace with the 1st character in the replacement_string.Then it will replace the 2nd character in the replacement_string & so on. Syntax: Translate(<string1>,<string_to_replace>,<replacement _string>) where,string1 is the string to replace a sequence of characters with another set of characters. string_to_replace is the string that will be searched for in string1. All characters in the string_to_replace will be replaced with the corresponding character in the replacement_string. Example: 1)Select Translate( Functions , uts , 123 ) Change from dual; o/p- F1nc2ion3

viii)Length:Returns a length of a word. Syntax: Length(word) Example: 1)Select Length( oracle ) Length from dual; o/p- 6 ix)Ltrim:Removes a characters from the left of char with initial
characters removed upto the first character not in set. Syntax: Ltrim(char[,set])

Example:
1)Select Ltrim( oracle , o ) Ltrim from dual; o/p-racle 2)Select Ltrim('ooracle','ora') "Ltrim" from dual; o/p- cle 3)Select Ltrim('ooracle','orc') "Ltrim" from dual; o/p- acle 4)Select Ltrim('ooracle','ra') "Ltrim" from dual; o/p- error

x)Rtrim:Returns char, with final characters removed after

the last

character not in set. Syntax: Rtrim(char[,set]) Example:1)Select Rtrim( oracle , el ) Rtrim from dual; o/p-orac 2)Select Rtrim('oracle', lc') Rtrim" from dual; o/p- error

xi)Trim:Removes all specified characters either from beginning or the ending of string. Syntax:
Trim([leading | trailing | both [<trim_character> from ]] <string1>)

Where, leading-remove trim_string from the front of string1. trailing-remove trim_string from the end of string1.
both-remove trim_string from the front & end of string1.

If none of the above option is chosen,the Trim function will remove trim_string from both the front & end of string1. Trim_character is the character that will be removed from string1.If this parameter is omitted,the trim function will remove all leading & trailing spaces from string1. string1 is the string to trim.

Example: 1)Select Trim( oracle ) Trim both sides from dual; o/p-oracle 2)Select Trim(leading x from xxxoraclexxx') Remove prefixes" from dual; o/p-oraclexxx 3)Select Trim(trailing x from xxxoraclexxx') Remove suffixes" from dual; o/p-xxxoracle 4)Select Trim(both x from xxxoraclexxx') Remove prefixes & suffixes" from dual; o/p-oracle 5)Select Trim(both 1 from 123oracle12111') Remove string" from dual; o/p-23oracle12

xii)Lpad:Returns char1,left-padded to length n with the sequence of


characters specified in char2.If char2 is not specified Oracle uses blanks by default.

Syntax: Example:

Lpad(char1,n ,[,char2])

1)Select Lpad( oracle ,10, * ) Lpad from dual; o/p-****oracle (total length is 10) 2)select Lpad('oracle',2) "Lpad" from dual; o/p- or

xiii)Rpad:Returns char1,right-padded to length n with the


characters specified in char2.If char2 is not specified Oracle uses blanks by default.

Syntax: Rpad(char1,n ,[,char2]) Example:


1)Select Rpad( oracle ,10, * ) Rpad from dual; o/p-oracle**** (total length is 10) 2)select Rpad('oracle',2) Rpad" from dual; o/p- or

c)Conversion Functions
i)To_number:converts char,a CHARACTER value expressing a number, to a NUMBER datatype. Syntax: To_number(char) Example: 1)Select to_number( 123 ) from dual; o/p-123 2)Select curbal+to_number( 1000 ) from customer; ii)To_char(Number Conversion): Converts a value of a NUMBER datatype to a character Datatype,using the optional format string.To_char() accepts a number(n) & a numeric format(fmt ) in which the number has to appear.If fmt is omitted,n is converted to a char value exactly long enough to hold all significant digits. Syntax: To_char(n [,fmt]) Example: 1)Select to_char(17689, $099,999 ) char from dual; o/p- $017,689

iii)To_char(Date Conversion): Converts a value of a DATE datatype to a CHAR value.To_char() accepts a date, as well as the format ( fmt ) in which the date has to appear. fmt must be in date format .If fmt is omitted, the date is converted to a char value using the default date format i.e. DD-MON-YY . Syntax: To_char(date [,fmt]) Example: 1)Select to_char(jdate, Month DD,YYYY ) New date format from employee where eno=100;

Date Conversion function: iv)To_date Converts a char field to a DATE field. Syntax: To_date(char [,fmt]) Example: 1)Insert into emp(eno,ename,salary,jdate) values (100, satish ,20000,to_date( 06-NOV-1999 10:55 :06 A.M. , DD-MON-YYYY HH:MI:SS A.M. ));

d)Date Functions
To manipulate & extract values from the date column of a table Oracle provides some date functions as follows: i)Add_months:Returns date after adding the number of months specified in the function. Syntax: Add_months(d,n) Example: 1)Select add_months(sysdate,3) from dual; ii)Last_day: Returns the last date of the month specified with the function. Syntax: Last_day(d) Example: 1)Select sysdate,last_day(sysdate) from dual;

iii)Months_between:Returns the number of months between d1 & d2. Syntax: Months_between(d1,d2) Example: 1)Select Months_between( 02-nov-1999 , 02-sep-1992 ) from dual; o/p-86 iv)Next_day: Returns the date of the first weekday named by char that is after the date named by date.char must be a day of the week. Syntax: Next_day(date,char) Example: 1)Select next_day( 25-jan-2012 , Wednesday ) from dual; o/p- 01-feb-2012 2)Select next_day( 25-jan-2012 , monday ) from dual; o/p- 30-jan-2012 3)Select next_day( 21-jan-2012 , saturday ) from dual; o/p- 28-jan-2012

Special Date Formats Using To_char Function Sometimes the date value is required to be displayed in special formats. e.g.instead of 03-JAN-81,displays the date as 03rd January-1981. For this.Oracle provides Special attributes, which can be used in the format specified with the To_char function.

1)Use of TH in the To_char() function DDTH places TH,RD,ND for the date (DD). e.g. 2ND,3RD,8TH etc. Example: i)select cust_no,name,To_char(dob, ddth-mon-yy ) dob_ddth from customer where cust_no=10; o/p- 10 shaha 03rd-jan-99 ii)select cust_no,name,To_char(dob, ddth-monthyyyy ) dob_ddth from customer where cust_no =11; o/p- 11 patil 2nd -October-1990 iii)select cust_no,name,To_char(dob, ddth ) dob_ddth from customer where cust_no =12; o/p- 12 mane 2nd

2)Use of SP in the To_char() function


DDSP indicates that the date (DD) must be displayed by spelling the date,e.g.ONE,TWELVE etc.

Example: i)select cust_no,name,To_char(dob, ddsp-mon-yy ) dob_ddsp from customer where cust_no=10; o/p- 10 shaha three-jan-99 ii)select cust_no,name,To_char(dob, ddsp-monthyyyy ) dob_ddsp from customer where cust_no =11; o/p- 11 patil twenty-two -October-1990
iii)select cust_no,name,To_char(dob, ddsp ) dob_ ddsp from customer where cust_no =12;

o/p- 12 mane nineteen

3)Use of SPTH in the To_char() function


SPTH displays the date (DD) with th added to the spelling fourteenth,twelfth,thirtieth etc.

Example: i)select cust_no,name,To_char(dob, ddspth-mon-yy ) dob_ddspth from customer where cust_no=10; o/p- 10 shaha third-jan-99
ii)select cust_no,name,To_char(dob, ddspth-month-yyyy ) dob_ddspth from customer where cust_no =11;

o/p- 11 patil twenty-second-October-1990 iii)select cust_no,name,To_char(dob, ddspth ) dob_ ddsp th from customer where cust_no =12; o/p- 12 mane nineteenth

Group By Clause
The Group By clause creates a data set,containing several sets of records grouped together based on a condition.
Syntax:

Select <Columnname1>,<Columnname2>,----, <ColumnnameN>, Aggregate_Function(Expression) from Tablename where <Condition> Group bY <Columnname1>,<Columnname2>,------, <ColumnnameN>;
Example: EMPNO 1 2 3 4 5 6 NAME BRANCHNO SHAHA 10 SHUKLA 12 MANE 10 RANE 15 SANE 11 DESHPANDE 10

1)Find out how many employees are there in each branch.

Ans.-Select BRANCHNO BRANCH NO ,Count(EMPNO) NO. OF EMPLOYEES FROM EmpBranch Group By BRANCHNO; O/P BRANCH NO NO. OF EMPLOYEES 11 1 10 3 12 1 15 1

Having Clause
The Having clause can be used in conjunction with the GROUP BY clause. Having imposes a condition on the Group by clause, which further filters the groups created by the Group by clause. Each column specification specified in the Having clause must occur within a statistical function or must occur in the list of columns named in the Group By Clause. Example: Q.Find out the branches which has more than 2 employees. Ans.-Select Branchno "BRANCH NO",Count(Empno)"NO. OF EMPLOYEES" FROM EmpBranch Group By Branchno having count(empno)>2; o/p BRANCHNO NO. OF EMPLOYEES 10 3

Rules for Group By & Having Clause Columns listed in the select statement have to be listed in the Group By clause. Only group functions can be used in the Having clause.

You might also like