Questions
: 1 What are different types of joins
in SQL?
Answers
: 1
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
Questions
: 2 What is “CROSS JOIN”? or What is
Cartesian product?
Answers
: 2
“CROSS
JOIN” or “CARTESIAN PRODUCT” combines all rows from both tables. Number of rows
will be product of the number of rows in each table. In real life scenario I
can not imagine where we will want to use a Cartesian product. But there are
scenarios where we would like permutation and combination probably Cartesian
would be the easiest way to achieve it.
Questions
: 3 How to select the first record in a
given set of rows?
Answers
: 3
Select
top 1 * from sales.salesperson
Questions
: 4 What is the default “-SORT ” order
for a SQL?
Answers
: 4
ASCENDING
Questions
: 5 What is a self-join?
Answers
: 5
If
we want to join two instances of the same table we can use self-join.
Questions
: 6 What’s the difference between
DELETE and TRUNCATE ?
Answers
: 6
Following
are difference between them:
=>>DELETE
TABLE syntax logs the deletes thus making the delete operations low. TRUNCATE
table does not log any information but it logs information about deallocation
of data page of the table. So TRUNCATE table is faster as compared to delete
table.
=>>DELETE
table can have criteria while TRUNCATE can not.
=>>
TRUNCATE table can not have triggers.
Questions
: 7 What’s the difference between
“UNION” and “UNION ALL” ?
Answers
: 7
UNION
SQL syntax is used to select information from two tables. But it selects only
distinct records from both the table. , while UNION ALL selects all records
from both the tables.
Questions
: 8 What are cursors and what are the
situations you will use them?
Answers
: 8
SQL
statements are good for set at a time operation. So it is good at handling set
of data. But there are scenarios where we want to update row depending on
certain criteria. we will loop through all rows and update data accordingly.
There’s where cursors come in to picture.
Questions
: 9 What is " Group by "
clause?
Answers
: 9
“Group
by” clause group similar data so that aggregate values can be derived.
Questions
: 10 What is the difference between
“HAVING” and “WHERE” clause?
Answers
:10
“HAVING”
clause is used to specify filtering criteria for “GROUP BY”, while “WHERE”
clause applies on normal SQL.
Questions
: 11 What is a Sub-Query?
Answers
: 11
A
query nested inside a SELECT statement is known as a subquery and is an
alternative to complex join statements. A subquery combines data from multiple
tables and returns results that are inserted into the WHERE condition of the
main query. A subquery is always enclosed within parentheses and returns a
column. A subquery can also be referred to as an inner query and the main query
as an outer query. JOIN gives better performance than a subquery when you have
to check for the existence of records.
For
example, to retrieve all EmployeeID and CustomerID records from the ORDERS
table that have the EmployeeID greater than the average of the EmployeeID
field, you can create a nested query, as shown:
SELECT
DISTINCT EmployeeID, CustomerID FROM ORDERS WHERE EmployeeID > (SELECT
AVG(EmployeeID) FROM ORDERS)
Questions
: 12 What are Aggregate and Scalar
Functions?
Answers
: 12
Aggregate
and Scalar functions are in built function for counting and calculations.
Aggregate
functions operate against a group of values but returns only one value.
AVG(column)
:- Returns the average value of a column
COUNT(column)
:- Returns the number of rows (without a NULL value) of a column
COUNT(*)
:- Returns the number of selected rows
MAX(column)
:- Returns the highest value of a column
MIN(column)
:- Returns the lowest value of a column
Scalar
functions operate against a single value and return value on basis of the
single value.
UCASE(c)
:- Converts a field to upper case
LCASE(c)
:- Converts a field to lower case
MID(c,start[,end])
:- Extract characters from a text field
LEN(c)
:- Returns the length of a text
Questions
:13 Can you explain the SELECT INTO
Statement?
Answers
:13
SELECT
INTO statement is used mostly to create backups. The below SQL backsup the
Employee table in to the EmployeeBackUp table. One point to be noted is that
the structure of pcdsEmployeeBackup and pcdsEmployee table should be same.
SELECT * INTO pcdsEmployeeBackup FROM pcdsEmployee
Questions
:14 What is a View?
Answers
: 14
View
is a virtual table which is created on the basis of the result set returned by the
select statement.
CREATE
VIEW [MyView] AS SELECT * from pcdsEmployee where LastName = 'singh'
In
order to query the view
SELECT
* FROM [MyView]
Questions
:15 What is SQl injection ?
Answers
:15
It
is a Form of attack on a database-driven Web site in which the attacker
executes unauthorized SQL commands by taking advantage of insecure code on a
system connected to the Internet, bypassing the firewall. SQL injection attacks
are used to steal information from a database from which the data would normally
not be available and/or to gain access to an organization’s host computers
through the computer that is hosting the database.
SQL
injection attacks typically are easy to avoid by ensuring that a system has
strong input validation.
As
name suggest we inject SQL which can be relatively dangerous for the database.
Example this is a simple SQL
SELECT
email, passwd, login_id, full_name
FROM
members WHERE email = 'x'
Now
somebody does not put “x” as the input but puts “x ; DROP TABLE members;”.
So
the actual SQL which will execute is :-
SELECT
email, passwd, login_id, full_name FROM members WHERE email = 'x' ; DROP TABLE
members;
Think
what will happen to your database.
Questions
:16 What is Data Warehousing ?
Answers
:16
Data
Warehousing is a process in which the data is stored and accessed from central
location and is meant to support some strategic decisions. Data Warehousing is
not a requirement for Data mining. But just makes your Data mining process more
efficient.
Data
warehouse is a collection of integrated, subject-oriented databases designed to
support the decision-support functions (DSF), where each unit of data is
relevant to some moment in time.
Questions
:17 What are Data Marts?
Answers
:17
Data
Marts are smaller section of Data Warehouses. They help data warehouses collect
data. For example your company has lot of branches which are spanned across the
globe. Head-office of the company decides to collect data from all these
branches for anticipating market. So to achieve this IT department can setup
data mart in all branch offices and a central data warehouse where all data
will finally reside.
Questions
:18 What are Fact tables and Dimension
Tables ? What is Dimensional Modeling and Star Schema Design
Answers
:18
When
we design transactional database we always think in terms of normalizing design
to its least form. But when it comes to designing for Data warehouse we think
more in terms of denormalizing the database. Data warehousing databases are
designed using Dimensional Modeling. Dimensional Modeling uses the existing
relational database structure and builds on that.
There
are two basic tables in dimensional modeling:-
Fact
Tables.
Dimension
Tables.
Fact
tables are central tables in data warehousing. Fact tables have the actual
aggregate values which will be needed in a business process. While dimension
tables revolve around fact tables. They describe the attributes of the fact
tables.
Questions
:19 What is Snow Flake Schema design in
database? What’s the difference between Star and Snow flake schema?
Answers
:19
Star
schema is good when you do not have big tables in data warehousing. But when
tables start becoming really huge it is better to denormalize. When you
denormalize star schema it is nothing but snow flake design. For instance below
customeraddress table is been normalized and is a child table of Customer
table. Same holds true for Salesperson table.
Questions
:20 What is ETL process in Data
warehousing? What are the different stages in “Data warehousing”?
Answers
:20
ETL
(Extraction, Transformation and Loading) are different stages in Data
warehousing. Like when we do software development we follow different stages
like requirement gathering, designing, coding and testing. In the similar
fashion we have for data warehousing.
Extraction:-
In
this process we extract data from the source. In actual scenarios data source
can be in many forms EXCEL, ACCESS, Delimited text, CSV (Comma Separated Files)
etc. So extraction process handle’s the complexity of understanding the data
source and loading it in a structure of data warehouse.
Transformation:-
This
process can also be called as cleaning up process. It’s not necessary that
after the extraction process data is clean and valid. For instance all the
financial figures have NULL values but you want it to be ZERO for better
analysis. So you can have some kind of stored procedure which runs through all
extracted records and sets the value to zero.
Loading:-
After
transformation you are ready to load the information in to your final data
warehouse database.
Questions
:21 What is Data mining ?
Answers
:21
Data
mining is a concept by which we can analyze the current data from different
perspectives and summarize the information in more useful manner. It’s mostly
used either to derive some valuable information from the existing data or to
predict sales to increase customer market.
There
are two basic aims of Data mining:-
Prediction:
-
From
the given data we can focus on how the customer or market will perform. For
instance we are having a sale of 40000 $ per month in India, if the same
product is to be sold with a discount how much sales can the company expect.
Summarization:
-
To
derive important information to analyze the current business scenario. For
example a weekly sales report will give a picture to the top management how we
are performing on a weekly basis?
Questions
:22 Compare Data mining and Data
Warehousing ?
Answers
:22
“Data
Warehousing” is technical process where we are making our data centralized
while “Data mining” is more of business activity which will analyze how good
your business is doing or predict how it will do in the future coming times
using the current data. As said before “Data Warehousing” is not a need for
“Data mining”. It’s good if you are doing “Data mining” on a “Data Warehouse”
rather than on an actual production database. “Data Warehousing” is essential
when we want to consolidate data from different sources, so it’s like a cleaner
and matured data which sits in between the various data sources and brings then
in to one format. “Data Warehouses” are normally physical entities which are
meant to improve accuracy of “Data mining” process. For example you have 10
companies sending data in different format, so you create one physical database
for consolidating all the data from different company sources, while “Data
mining” can be a physical model or logical model. You can create a database in
“Data mining” which gives you reports of net sales for this year for all
companies. This need not be a physical database as such but a simple query.
Questions
:23 What are indexes? What are B-Trees?
Answers
:23
Index
makes your search faster. So defining indexes to your database will make your
search faster.Most of the indexing fundamentals use “B-Tree” or “Balanced-Tree”
principle. It’s not a principle that is something is created by SQL Server or
ORACLE but is a mathematical derived fundamental.In order that “B-tree”
fundamental work properly both of the sides should be balanced.
Questions
: 24 I have a table which has lot of
inserts, is it a good database design to create indexes on that table?
Insert’s
are slower on tables which have indexes, justify it?or Why do page splitting
happen?
Answers
: 24
All
indexing fundamentals in database use “B-tree” fundamental. Now whenever there
is new data inserted or deleted the tree tries to become unbalance.
Creates
a new page to balance the tree.
Shuffle
and move the data to pages.
So
if your table is having heavy inserts that means it’s transactional, then you
can visualize the amount of splits it will be doing. This will not only
increase insert time but will also upset the end-user who is sitting on the
screen. So when you forecast that a table has lot of inserts it’s not a good
idea to create indexes.
Questions : 25 What
are the two types of indexes and explain them in detail? or What’s the
difference between clustered and non-clustered indexes?
Answers
:25
There
are basically two types of indexes:-
Clustered
Indexes.
Non-Clustered Indexes.
In
clustered index the non-leaf level actually points to the actual data.In
Non-Clustered index the leaf nodes point to pointers (they are rowid’s) which
then point to actual data.
No comments:
Post a Comment