Posts

Showing posts from April, 2016

SQL Joins

Image
/*CREATE TABLE tbl_Employee_Data*/ CREATE TABLE tbl_Employee_Data (  EMPID           INT,  FIRST_NAME      VARCHAR(50),  MIDDLE_NAME     VARCHAR(50),  LAST_NAME       VARCHAR(50),  NATIONALITY     VARCHAR(50) ); INSERT INTO dbo.tbl_Employee_Data        (EMPID, FIRST_NAME, MIDDLE_NAME, LAST_NAME,NATIONALITY) VALUES (101, 'SUBAL', 'CHANDRA', 'DAS', 'INDIAN'); INSERT INTO dbo.tbl_Employee_Data        (EMPID, FIRST_NAME, MIDDLE_NAME, LAST_NAME,NATIONALITY) VALUES (103, 'RANGA', 'SELAM', 'REDDY', 'INDIAN'); INSERT INTO dbo.tbl_Employee_Data        (EMPID, FIRST_NAME, MIDDLE_NAME, LAST_NAME,NATIONALITY) VALUES (104, 'SUBHANI', 'SK', 'MAHABOOB', 'INDIAN'); INSERT INTO dbo.tbl_Employee_Data        (EMPID, FIRST_NAME, MIDDLE_NAME, LAST_NAME,NATIONALITY) VALUES (106, 'BHARGHAVI', 'CHAND...

How to fetch alternate records from a table in sql server

Image
CREATE TABLE STUDENTS (       STUDENT_ID int NULL,       [STUDENT_NAME] [varchar](50) NULL,       DOB date NULL,       DEPARTMENT_ID int NULL,       DOJ date NULL ) INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ) VALUES (1, N'BALACHANDAR', '1983-10-28', 2, GETDATE()) INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ) VALUES (2, N'PREMKUMAR', '1986-06-17', 1, GETDATE()) INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ) VALUES (3, N'MADHUSOODHAN', '1988-06-30', 3, GETDATE()) INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ) VALUES (4, N'SAGARBABU', '1995-10-05', 4, GETDATE()) INSERT STUDENTS (STUDENT_ID, STUDENT_NAME, DOB, DEPARTMENT_ID, DOJ) VALUES (5, N'SRAVANTHI', '1988-04-06', 5, GETDATE()) SELECT * FROM STUDENTS SELECT * FROM STUDENTS WHERE STUDENT_ID%2=0 SELECT STUDENT_ID,STUDENT_NAME,DOB,DEPARTMEN...

SQL Pivot Table

Image
create table table_pivot ( EmpID varchar(10), Dept varchar(10) ) insert into table_pivot values('A101','IT') insert into table_pivot values('A102','IT') insert into table_pivot values('B101','Mech') insert into table_pivot values('C101','EEE') insert into table_pivot values('D101','CS') insert into table_pivot values('D102','CS') insert into table_pivot values('E101','Chem') insert into table_pivot values('F101','Aro') select * from table_pivot select EmpID,[IT],[Mech],[EEE],[CS],[Chem],[Aro] from [dbo].[table_pivot] pivot ( count(Dept) for [Dept] in ([IT],[Mech],[EEE],[CS],[Chem],[Aro]) ) as P

How to Remove Duplicate Records From SQL Table

Image
Syntax : set nocount on set rowcount 1 while 1=1 begin delete from Table_Name where Column_Name1 IN ( select Column_Name1 from Table_Name group by Column_Name1 count(*)>1 ) if @@ROWCOUNT=0 break; end set rowcount 0 -- Create a table as Persons CREATE TABLE [dbo].[Persons] ( PersonID int , LastName varchar(25), FirstName varchar(25), Address varchar(25), City varchar(25) ) insert into Persons values(1,'Billa','Ravi','Giddalur','Ongole') insert into Persons values(2,'A','Raju','B pata','Guntur') insert into Persons values(2,'A','Raju','B pata','Guntur') insert into Persons values(3,'B','Ranga','Rachrla','Nellor') insert into Persons values(4,'c','Raky','Komarolu','Cudapa') insert into Persons values(4,'c','Raky','Komarolu...