You are on page 1of 2

Q. can we have primary key as well a identity key?

Q. When we use auto increment


Q. what is difference between autoincrememt and identity
Q. difference between delete and trucate
-- Delete never reseed the value in Identity column
-- Truncate reseed the identity value to 0
Q> What s the current identity value in a table
dbcc checkident('dbo.CustomerTest')
Q. Rename the newly Added Column to Identity Column you had at first.
EXEC sp_rename 'HR.Customer.TestId','Id','COLUMN'
Q. Never keep computed column inside your table ,(Relational database)

Q. what is Decision Making Statements?


decision making statment is like a first it will run the condition and give the
result and based on that result next statment will execute
the best example of decision making in sql server is IF and ELSE condition
in this way waht will happen
fisrt it will test the condition fisrt and based on result it will execute the next
statement.
like if the condition is true it will go inside IF block and execute the statement
otherwise it will execute the Else condition directly

IF Else condition we can use in limited place like if we have only two condition
either true or false.
But in real world we can have more then two condition , in that time we have an
option called Else If condition, which we can use inside IF Else condition multiple
time

Q. why we should use 1 in place of *?

Question 1: SQL Query to find second highest salary of Employee

select top 1 employee_salary from Employee WHERE employee_salary in


(select top 2 (employee_Salary) from Employee order by employee_salary desc)
order by employee_salary asc

select max(employee_salary) from Employee WHERE employee_salary not in


(select max(employee_Salary) from Employee )

select max(employee_salary) from Employee WHERE employee_salary not in


(select top 2(employee_Salary) from Employee order by employee_salary desc)

Question 2: SQL Query to find Max Salary from each department.


select * from employee
select department_id,max(employee_salary) from employee
group by department_id

Q: How to fins a duplicate records


select column_name,count(*) as row_number from table_name
group by column_name having count(*)>1

Q. how to delete a duplicate records?


Ans: By CTE table
with CTE_Table
As
(select coulmn_name, row_number() over(partition by column_name group by column_id)
as column_name2 from table_name)
delete from cte_table where column_name2>1
Question 13: How do you find all employees which are also manager? .
ANS: Self Join
select e.name, m.name from employee e self join employee m
on e.mgr_d=m.emp_id

Q.What is the difference between Table scan , Index scan and Index Seek ?
Note:Since, we now have a UNIQUE CLUSTERED INDEX on the Id column,
any attempt to duplicate the key values, will throw an error stating
'Violation of PRIMARY KEY constraint 'PK__tblEmplo__3214EC07236943A5'.
Cannot insert duplicate key in object dbo.tblEmployee'

NONCLUSTERED index: Logical read means sql server reads data from buffer memory
-- Physical read means sql server reads data from Disk
-- Larger Physical read means slow performace.

Q. what is the meaning of Set Nocount on?


to improve the perfomance of query we use this ..it doesn't show the message in
message window

1. Partially covering index


2. Fully covering index:
3. Trimmed (non-covering) index:

You might also like