Friday 15 January 2016

Operator Precedence in Sql.

In this article, we will see Operator Precedence in Sql .

When a complex expression has multiple operators, operator precedence determines the sequence in which the operations are performed. The order of execution can significantly affect the resulting value.


Operators have the precedence levels shown in the following table. An operator on higher levels is evaluated before an operator on a lower level.


 +-------+----------------------------------------------------------------------------+
  | Level  |                                Operators                                                                 |
 +-------+----------------------------------------------------------------------------+
  | 1        |  ~ (Bitwise NOT)                                                                                      |
  | 2        |  * (Multiply), / (Division), % (Modulo)                                                      |
  | 3        |  + , - , & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR)             |
  | 4        |  =, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)                         |
  | 5        |  NOT                                                                                                        |
  | 6        |  AND                                                                                                        |
  | 7        |  ALL, ANY, BETWEEN, IN, LIKE, OR, SOME                                               | 
  | 8        |  = (Assignment)                                                                                       |
 +------+-----------------------------------------------------------------------------+

Let's see the program for this.


































Thanks.

ORDER BY clause

In this article, we will see the  ORDER BY clause Sql.

The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. Some database sorts query results in ascending order by default.


Syntax

SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC

Let's see the program for this.




































Thanks.

Friday 13 November 2015

pl sql program for 'palindrome'

Write a program in PL/SQL to check whether a number is a palindrome or not.

In this article, we will see the program for Palindrome in Pl/Sql.

palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers.

Let's see the program for this.


sql> set serveroutput on
sql>declare
2  g varchar2(20);
3  r varchar2(20);
4  i number(4);
5  begin
6  g:='&g';
7  for i in reverse 1.. length(g) loop
8  r:=r || substr(g,i,1);      
9  end loop;
10  dbms_output.put_line('reverse string is ' || r);
11  if r=g then
12  dbms_output.put_line('String is Palindrome');
13  else
14  dbms_output.put_line('String is not Palindrome');
15  end if;      
16  end;
17  /


Thanks.

Saturday 7 November 2015

Reverse Of a Number

Write a program in PL/SQL to print the reverse of a number.

According to the problem if a number  is 677 then it should print 776.

Program Code:

SQL> set serveroutput on
SQL>Declare
2 num number:=&n;
3 rev number:=0;
4 Begin
5 while(num>0)
6 Loop
7 rev:=rev*10+mod(num,10);
8 num:=num/10;
9 End loop;
10 Dbms_Output.Put_Line('Reverse of number is '|| rev);
11 End;
12 /



Thanks.

PL/SQL Fibonacci Sequence

Write a PL/SQL code to get the Fibonacci Sequence.

First, I will explain what is Fibonacci Sequence and how to get this series.
So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,13,21.............

In Fibonacci Sequence, first and second elements are 0 and 1 and to get the next elements we will add the previous elements and it will generate the next element.
So, first element=0
      second element =1
      third element=sum of last 2 elements (first element + second element)
                           =0+1
                           =1
      fourth element=second element + third element
                             =1+1
                             =2
      fifth element=third element + fourth element
                           =1+2
                           =3

So in this way we can generate a Fibonacci Sequence.

Program Code:

SQL> declare
  2  a number(5);
  3  b number(5);
  4  c number(5);
  5  n number(5);
  6  i number(5);
  7  begin
  8  n:=6;
  9  a:=0;
 10  b:=1;
 11  for i in 1..n
 12  loop
 13  c:=a+b;
 14  a:=b;
 15  b:=c;
 16   dbms_output.put_line(c);
 17  end loop;
 18  end;

 19  /

Thanks.

PL/SQL factorial program

Write a program in PL/SQL to print the factorial of a number.



In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find the factorial of a number.
To get the factorial of any given number we have to multiply that number to every number less than that number till 1.
Suppose we have to find the factorial of 5. Then,
factorial(5)=5*4*3*2*1
                  =120


Program Code:

SQL> set serveroutput on
SQL> declare
  2  i number(5);
  3  j number (5);
  4  f number(10);
  5  begin
  6  i:=&i;
  7  f:=1;
  8  for j in 1..i
  9  loop
 10  f:=f*j;
 11  end loop;
 12  dbms_output.put_line(f);
 13  end;
 14  /

Thanks

Wednesday 28 October 2015

Difference function

Given a City table, whose fields are described as

+-------------+----------+
| Field | Type |
+-------------+----------+
| ID | int(11) |
| Name | char(35) |
| CountryCode | char(3) |
| District | char(20) |
| Population | int(11) |
+-------------+----------+

print the difference between the maximum and minimum city populations.

>>select (max(population)-min(population)) from city;

Thanks.