Q: What is denormalization and when would you go for it?
Ans: As the name indicates, denormalization is the reverse process of
normalization. It's the controlled introduction of redundancy in to the database design. It helps improve the
query performance as the number of joins could be reduced.
Q: What are the different types of joins? What is the difference between them?
Ans: Inner join
Inner join shows matches only when they exist in both tables. Example, in the below SQL there
are two tables Customers and Orders and the inner join in made on Customers Customerid and
Orders Customerid.So this SQL will only give you result with customers who have orders. If the
customer does not have order, it will not display that record.
SELECT Customers.*, Orders.* FROM Customers INNER JOIN Orders ON Customers.CustomerID =Orders.CustomerID
|
|
|
LEFT OUTER JOIN
Left join will display all records in left table of the SQL statement. In SQL below customers with
or without orders will be displayed. Order data for customers without orders appears as NULL
values. For example, you want to determine the amount ordered by each customer and you need
to see who has not ordered anything as well. You can also see the LEFT OUTER JOIN as a
mirror image of the RIGHT OUTER JOIN (Is covered in the next section) if you switch the side of each table.
SELECT Customers.*, Orders.* FROM Customers LEFT OUTER JOIN Orders ON Customers.CustomerID =Orders.CustomerID
RIGHT OUTER JOIN
Right join will display all records in right table of the SQL statement. In SQL below all orders
with or without matching customer records will be displayed. Customer data for orders without
customers appears as NULL values. For example, you want to determine if there are any orders in
the data with undefined CustomerID values (say, after a conversion or something like it). You can
also see the RIGHT OUTER JOIN as a mirror image of the LEFT OUTER JOIN if you switch
the side of each table.
SELECT Customers.*, Orders.* FROM Customers
RIGHT OUTER JOIN Orders ON Customers.CustomerID =Orders.CustomerID
Q: How do you implement one-to-one, one-to-many and many-to-many relationships while designing tables?
Ans: One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships.
One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships. Many-to-Many relationships are implemented using a junction table with
the keys from both the tables forming the composite primary key of the junction table.
Q: What's the difference between a primary key and a unique key?
Ans: Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index
on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs,
but unique key allows one NULL only.
Q: What are user defined datatypes and when you should go for them?
Ans: User defined datatypes let you extend the base SQL Server datatypes by providing a descriptive name, and format to the database. Take for example, in your database, there is a column called Flight_Num which
appears in many tables. In all these tables it should be varchar(8). In this case you could create a user defined datatype called Flight_num_type of varchar(8) and use it across all your tables.
Q: What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
Ans: You can use Having Clause with the GROUP BY function in a query and WHERE Clause is applied to each row before, they are part of the GROUP BY function in a query.
|
|
|
<< back
next >>
|
|
1
2
3
4
5
|
|
|