Thursday, 29 October 2015

Bubble Sorting in C# .Net

Bubble Sorting in C# .Net


  1. Open Visual Studio from Start - All programs - Microsoft Visual Studio.
  2. Then go to to "File" - "New" - "Project..." then select Visual C# - Windows - Console application. 

#_Code
.......................................................................................................................................................................................................................................
static void Main(string[] args)
        {
            int[] a = { 23, 32, 15, 24, 14 };  
            int t;
            for (int p = 0; p <= a.Length - 2; p++)
            {
                for (int i = 0; i <= a.Length - 2; i++)
                {
                    if (a[i] > a[i + 1])
                    {
                        t = a[i + 1];
                        a[i + 1] = a[i];
                        a[i] = t;
                    }
                }
            }
            Console.WriteLine("Simple Bubble Sort in C#");
            Console.WriteLine("The Sorted array");
            foreach (int aa in a)                         
                Console.Write(aa + " ");
            Console.Read();

        }
....................................................................................................................................................................................................................................
Unsorted Elements:-23, 32, 15, 24, 14

Sorted Elements :- 14, 15, 23, 24, 32






Friday, 23 October 2015

Stored Procedures in Sql

Stored Procedures in Sql

"A Stored Procedures is a group of SQL Statements that has been Created and Stored in the database."

............................................................................................................

Advantage :-

  • Better performance
  • Less Traffic
  • Security
  • Integrity
  • Code-Reuseblity

Disadvantage :-

  • Work with Same Database
............................................................................................................
Design a database like this image :-

---------------------------------------------------------------------------------------------------------

Create a Table "Student"

Create table Student         (      RollNo int primary key,      Name nvarchar(50),      MobileNo nvarchar(50),      dob nvarchar(50)       )
-------------------------------------------------------------------------------------------------------

Create a Stored Procedures..........................

A Single Stored Procedures for multiple Query Insert,Update,Delete, & Select .. i.e. SP_Query

-------------------------------------------------------------------------------------------------------------------------
Create proc SP_Query 
      @op nvarchar(50)='g',
      @RollNo int=0,
      @Name nvarchar(50)=null,
      @MobileNo nvarchar(50)=null,
      @dob nvarchar(50)=''
as
begin
      if @op='insert'
begin
      insert into Student values(@RollNo,@Name,@MobileNo,@dob)
end
      if @op='update'
begin
      update Student set 
                 Name=@Name,
                 MobileNo=@MobileNo,
                 dob=@dob
       where RollNo=@RollNo
end
       if @op='delete'
begin
       delete from Student where RollNo=@RollNo
end
       if @op='select'
begin
        select * from Student where RollNo=@RollNo
end
else
begin
        select * from Student
end

end
--------------------------------------------------------------------------------------------------------------------------

Execute a Stored Procedures..........................

1.  executing procedure for insert the data in Student table

exec SP_Query  'insert',3,'Mr. VICKY','09654388184','02-03-1993'


2executing  procedure for updating data in Student table

  exec SP_Query  'update',2,'Mr. Vijay','08826439190','11-Oct-1993'


3. executing  procedure for select data in Student table

    exec SP_Query  'select',1

4. executing  procedure for delete data in Student table  


   exec SP_Query  'delete',4

5. executing  default procedure for  Student table  

      exec SP_Query 


-------------------------------------------------------------------------------

Friday, 9 October 2015

Find 3rd Highest Salary in Sql

Find 3rd Highest Salary in Sql


Step 1:-
Create a table named "Employee" in your database......

CREATE TABLE employee
(
id int,
name char(20),
dept char(10),
age int,
salary int,
location char(10)
)

Step 2:-
Insert some values into the table "Employee"

Insert into employee values(1,'RAM','Soft Eng',24,30000,'Banglore')
Insert into employee values(2,'Shyam','Progmmer',24,40000,'DELHI')
Insert into employee values(3,'Manoj','Admin',24,50000,'MUMBAI')
Insert into employee values(4,'RAja','Progmmer',24,33000,'DELHI')
Insert into employee values(5,'Sunny','Soft Eng',24,32000,'Banglore')
Insert into employee values(6,'Nukul','Soft Eng',24,31000,'MUMBAI')
Insert into employee values(7,'Laxmi','Data Admin',24,36000,'DELHI')
Insert into employee values(8,'RAmesh','Soft Eng',24,80000,'MUMBAI')


Step 3:-
Execute the table "Employee"

SELECT * FROM employee

Step 4:-
3rd Highest  Salary in the table "Employee"

SELECT MIN(salary)
FROM
      (SELECT DISTINCT TOP 3 salary FROM employee ORDER BY salary DESC)
as a




....................................................................

Find nth Highest Salaries in SQL

Find nth Highest Salaries in SQL

Maximum Salary, Minimum Salary 
and 2nd Highest Salary........

1st Step:-

//create table "employee"

CREATE TABLE employee
(
id int,
name char(20),
dept char(10),
age int,
salary int,
location char(10)
)

then execute table "employee"

SELECT * FROM employee

2nd Step:-

then insert values in the table "employee"

Insert into employee values(1,'RAM','Soft Eng',24,30000,'Banglore')
Insert into employee values(2,'Shyam','Progmmer',24,40000,'DELHI')
Insert into employee values(3,'Manoj','Admin',24,50000,'MUMBAI')
Insert into employee values(4,'RAja','Progmmer',24,33000,'DELHI')
Insert into employee values(5,'Sunny','Soft Eng',24,32000,'Banglore')
Insert into employee values(6,'Nukul','Soft Eng',24,31000,'MUMBAI')
Insert into employee values(7,'Laxmi','Data Admin',24,36000,'DELHI')
Insert into employee values(8,'RAmesh','Soft Eng',24,80000,'MUMBAI')

then execute table "employee"

SELECT * FROM employee

Maximum Salary-
Maximum salary in table "employee" 

Select MAX(salary) from employee

Minimum Salary-
Minimum salary in table "employee"

Select MIN(salary) from employee




2nd Highest salary in table "employee"


Select MAX(salary) from employee where Salary <(Select MAX(salary) from employee)




Factorial of a Number

Recently Viewed