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', 'CHANDRA', 'RAVULAPALLI', 'INDIAN');...

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,DEPARTMENT_ID,DOJ FROM(SELECT ROW_NUMBER()O...

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