interview-questions

SQL and T-SQL

1. What are the advantages of Normalization versus denormalization?

Normalization is better for OLTP (On-line Transaction Processing) systems:

Denormalization is better for OLAP (On-line Analytical Processing) systems:

2. What is BASE?

3. What does the CAP theorem state?

The CAP theorem, also known as Brewer’s theorem, states that a distributed database system can only simultaneously provide two out of the following three guarantees:

In essence, the theorem posits that in the presence of a network partition, one must choose between consistency and availability. No distributed system can guarantee all three elements at the same time.

4. - What is ACID?

5. What are the differences between clustered and non-clustered indices?

6. What are the differences between primary keys and unique indexes?

7. What are dirty reads, non-repeatable reads and phantom reads?

8. What are the transaction isolation levels?

  Dirty reads Nonrepeatable reads Phantoms
Read uncommitted Yes Yes Yes
Read committed No Yes Yes
Repeatable read No No Yes
Serializable No No No

9. What is a SQL injection?

A type of an injection attack where the attacker executes a malicious batch of SQL inside our database server.

10. How can we protect ourselves from a SQL injection?

11. What are the different types of joins?

alt text

12. What is COALESCE?

COALESCE will take any number of parameters, and return the first value encountered that isn’t NULL.

13. What is the difference between DELETE and TRUNCATE?

DELETE

TRUNCATE

14. What does UNION do? What is the difference between UNION and UNION ALL?

The basic difference between UNION and UNION ALL is union operation eliminates the duplicated rows from the result set but union all returns all rows after joining.

15. What is the difference between the WHERE and HAVING clauses?

HAVING is used to check conditions after the aggregation takes place.

WHERE is used to check conditions before the aggregation takes place.

16. What are the difference between stored procedures and user defined functions?

17. What is the difference between “stored procedure” and “dynamic SQL”?

18. What’s the difference between CREATE PROC and CREATE PROCEDURE?

They are the same.

19. Can we call trigger inside a stored procedure?

No, triggers cannot be called manually.

20. What are CLR stored procedures?

Stored procedures written in a CLR compatible language like C#, VB.NET (How about no.), F#, etc.

21. When to use CLR stored procedures and when T-SQL ones?

22. What is the best approach to check if a query has returned a result?

23. Correlated subqueries vs uncorrelated subqueries?

24. What is parameter sniffing?

25. How can we optimize incorrect parameter sniffing?

-- stored procedure version
CREATE PROCEDURE dbo.sample @ProductID INT
WITH RECOMPILE
AS
/* do something */
-- single statement version
SELECT * FROM Sales.SalesOrderHeader
OPTION (RECOMPILE);
ALTER PROCEDURE Get_OrderID_OrderQty
@ProductID INT
AS
SELECT SalesOrderDetailID, OrderQty
FROM Sales.SalesOrderDetail
WHERE ProductID = @ProductID
OPTION (OPTIMIZE FOR (@ProductID=945));
CREATE PROCEDURE dbo.GetCities
@StateProvinceID int
AS
SELECT DISTINCT City
FROM Person.Address
WHERE StateProvinceID=@StateProvinceID
OPTION (OPTIMIZE FOR UNKNOWN)

26. What the logarithmic complexity if the different query operations?

27. Can you name some optimization tips?

28. What are some alternate ways to store data other than a relational database? Why would you do that, and what are the trade-offs?

29. If your database was under a lot of strain, what are the first few things you might consider to speed it up?