Elementry MSSQL Commands - Lab 1

1-      Create Table EmployeeInfo

CREATE TABLE EmployeeInfo
(
Empid INT IDENTITY(1001,1) PRIMARY KEY CLUSTERED,
 Empname VARCHAR (20),
 SalaryInLaC DECIMAL (10,2),
  Location VARCHAR (20)
  )


INSERT INTO EmployeeInfo VALUES('AB1001',15.2,'HYD')
INSERT INTO EmployeeInfo VALUES('AB1002',10.5,'PUNE')
INSERT INTO EmployeeInfo VALUES('AB1003',5.2,'BANG')
INSERT INTO EmployeeInfo VALUES('AB1004',20,'DELHI')
INSERT INTO EmployeeInfo VALUES('AB1005',15.2,'HYD')
INSERT INTO EmployeeInfo VALUES('AB1006',10.5,'PUNE')
INSERT INTO EmployeeInfo VALUES('AB1007',5.2,'BANG')
---------------------------------------------------Upto 2000
INSERT INTO EmployeeInfo VALUES('AB2000',10.5,'DELHI')




2. Find out how many of the employees are there in Hyd and Pune
SELECT COUNT(1) FROM EMPLOYEEINFO
WHERE LOCATION LIKE 'HYD'
OR LOCATION LIKE 'PUNE'

3. Find out how many of the employees have the package >10

SELECT COUNT(1) FROM EmployeeInfo
WHERE SALARYINLAC >= 10

4. Find out how many of the employees have the package > 10 and <15
SELECT COUNT(1) FROM EmployeeInfo
WHERE SALARYINLAC >= 10
AND SalaryInLac <=15

5. Create a query to get following data. This should be only one query - hint group by
Location NumberofEmp
Hyd        x
Pune       y
Chennai    z

SELECT Location, COUNT(1) As NumberofEmp
FROM EmployeeInfo
GROUP BY LOCATION



6. Create the clustered index on the empid column and tell me the size of table and index.

sp_spaceused EmployeeInfo






7. Create a nonclustered index on empname column and again tell me the size of table and index.

ALTER TABLE EmployeeInfo
add
CONSTRAINT Emp_id UNIQUE NONCLUSTERED (EMPNAME)





8. Now Pour this all the data into a test table.

SELECT * INTO TestTable
FROM EmployeeInfo

9. Change the column datatype from varchar to char(1800)

ALTER TABLE EmployeeInfo
ALTER COLUMN Empname CHAR(100)





--Then I droped the constraint and then alter the column then again recreated the constraint-

ALTER TABLE EmployeeInfo
--DROP CONSTRAINT EMP_ID
ALTER COLUMN Empname VARCHAR(50)

Again verify the size of table and index-






10. Delete the rows only for Hyd location. Now get the data from back table after you deleted.
delete from employeeInfo
where location like 'HYD'
or location like 'pune'

Now get the data from back table after you deleted.-

INSERT INTO EMPLOYEEINFO (Empid, Empname, SalaryInLaC, Location)
  SELECT DISTINCT Empid, Empname, SalaryInLaC, Location FROM TestTable

Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'EMPLOYEEINFO' can only be specified when a column list is used and IDENTITY_INSERT is ON.

I TRIED-
SET IDENTITY_INSERT EMPLOYEEINFO ON
go
INSERT INTO EMPLOYEEINFO (Empid, Empname, SalaryInLaC, Location)
SELECT DISTINCT Empid, Empname, SalaryInLaC, Location FROM TestTable

Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK__Employee__AF2EBFA115502E78'. Cannot insert duplicate key in object 'dbo.EmployeeInfo'.
The statement has been terminated.


I did below changes and got my task doneJ

SET IDENTITY_INSERT EMPLOYEEINFO ON
GO
INSERT INTO EMPLOYEEINFO (Empid, Empname, SalaryInLaC, Location)
  SELECT DISTINCT Empid, Empname, SalaryInLaC, Location
     FROM TestTable
        WHERE location = 'HYD'

Comments

Popular posts from this blog

MSSQL Interview Question Clustering- Part2

Error 1067: The cluster service is stopped on passive node of my two node sql cluster. It throws following error when I try to start this:

Handle Sexual Harassment