Beruflich Dokumente
Kultur Dokumente
To find duplicate values in the same field, you need to use an alias with the same table twice.
Perhaps seeing this is easier than explaining it. Notice in the following example that the customer
table is added twice to the "from" clause in the SQL statement. Then in the where clause both c1
and c2 aliases are used to refer to the same table. First we use both aliases to mach up the City
column. Then we use both aliases to exclude the primary key.
Finding Duplicates in a single column:
select c1.*
from "customer.db" c1, "customer.db" c2
where c1."City" = c2."City"
and c1."Customer No" <> c2."Customer No"
Finding Duplicates in Multiple Fields:
select c1.*
from "customer.db" c1, "customer.db" c2
where c1."City" = c2."City"
and c1."State" = c2."State"
and c1."Customer No" <> c2."Customer No"
For multiple column primary key fields, you would add a line similar to the last
line for each column.
DECLARE @i int
SELECT @i = 1
WHILE @i < 1000
BEGIN
INSERT INTO Table2 VALUES (@i, @i, LTRIM(str(@i)))
SELECT @i = @i + 1
END
GO
SET SHOWPLAN_TEXT ON
GO
SELECT a.Table1_id, b.Table1_id FROM Table1 a INNER JOIN Table2 b
ON a.Table1_id = b.Table1_id
GO
SET SHOWPLAN_TEXT OFF
GO
These are the results:
StmtText
-------------------------------------------------------------------------------------------------------------------
|--Merge Join(Inner Join, MERGE:([a].[Table1_id])=([b].[Table1_id]),
RESIDUAL:([a].[Table1_id]=[b].[Table1_id]))
|--Clustered Index Scan(OBJECT:([Test].[dbo].[Table1].[PK__Table1__1F4E99FE] AS [a]),
ORDERED)
|--Clustered Index Scan(OBJECT:([Test].[dbo].[Table2].[indTable2] AS [b]), ORDERED)
This is a algorithm of the Merge join (description of its work in general case, for many-to-many
relationship):
while (not Table1.eof) and (not Table2.eof) do
begin
while Table2.Table1_id > Table1.Table1_id do Table1.MoveToNextRecord();
value = Table1.Table1_id;
while Table2.Table1_id < value do Table2.MoveToNextRecord();
RID = Table1.RowID();
while Table2.Table1_id = value do
begin
while Table1.Table1_id = value do
begin
< SELECT Table1.Table1_id, Table2.Table1_id >
Table1.MoveToNextRecord();
end
Table1.MoveTo(RID);
Table2.MoveToNextRecord();
end
end
NOTE. If the joined tables are small (contain only one data page for example), and at least one of
the joined tables have index on the column that joins the tables, then SQL Server will use Nested-
Loop join instead of Merge join or Hash join (usually).
Because the query optimizer usually selects the best execution plan for a given select statement, it
is not necessary to change the kind of join, but sometimes it can be useful. You can enforce the
desirable join type with OPTION clause.
This is the example to enforce Merge join:
USE pubs
GO
SET SHOWPLAN_TEXT ON
GO
SELECT a.au_id FROM authors a JOIN titleauthor b
ON a.au_id = b.au_id OPTION (MERGE JOIN)
GO
SET SHOWPLAN_TEXT OFF
GO
These are the results:
StmtText
------------------------------------------------------------------------------------------------
SELECT a.au_id FROM authors a JOIN titleauthor b
ON a.au_id = b.au_id OPTION (MERGE JOIN)
(1 row(s) affected)
StmtText
---------------------------------------------------------------------------------------------------
|--Merge Join(Inner Join, MERGE:([a].[au_id])=([b].[au_id]),
RESIDUAL:([a].[au_id]=[b].[au_id]))
|--Clustered Index Scan(OBJECT:([pubs].[dbo].[authors].[UPKCL_auidind] AS [a]),
ORDERED)
|--Index Scan(OBJECT:([pubs].[dbo].[titleauthor].[auidind] AS [b]), ORDERED)
(3 row(s) affected)
JOIN Examples
OUTPUT: (8 rows)
1 Davolio Nancy 2 Fuller Andrew
3 Leverling Janet 2 Fuller Andrew
4 Peacock Margaret 2 Fuller Andrew
5 Buchanan Steven 2 Fuller Andrew
6 Suyama Michael 5 Buchanan Steven
7 King Robert 5 Buchanan Steven
8 Callahan Laura 2 Fuller Andrew
9 Dodsworth Anne 5 Buchanan Steven
SELECT Emp.EmployeeID AS "Employee ID",
Emp.lastname + ' ' + Emp.FirstName AS "Employee Name",
Mgr.EmployeeID As "Manager ID" ,
Mgr.lastname + ' ' + Mgr.FirstName AS "Manager Name"
FROM Employees Emp LEFT JOIN Employees Mgr
ON Emp.ReportsTo = Mgr.EmployeeID
OUTPUT:
1 Davolio Nancy 2 Fuller Andrew
2 Fuller Andrew NULL NULL
3 Leverling Janet 2 Fuller Andrew
4 Peacock Margaret 2 Fuller Andrew
5 Buchanan Steven 2 Fuller Andrew
6 Suyama Michael 5 Buchanan Steven
7 King Robert 5 Buchanan Steven
8 Callahan Laura 2 Fuller Andrew
9 Dodsworth Anne 5 Buchanan Steven
1)whats the difference between Delete and Truncate?Under what conditions Truncate won't
work?
Answer : Delete is logged operation
truncate is non-logged operation. To remove entire data from table, we use this. Not for
partial data delete.
2) How do you update the rows which are divisble by 10,given a set of numbers in column
Ans: Update emp set sal = sal * 5 where (sal%10) = 0
select * from emp a where (n-1) = ( select count(distinct b.sal) from emp b where b.sal> a.sal)
9)How will you test the stored procedure taking two parameters namely first name and last name
returning full name?
Ans: Check the syntax by using sp_helptext of sp. It will show the proc defination.
10)How do you find the error,How can u know the number of rows effected by last SQL
statement,
Ans: Select @@error
Select @@rowcount
11)How can you get @@error and @@rowcount at the same time
Ans: Run the following query: Select @@error,@@rowcount.
If above one won't works:
15)what are subqueries?Give example.In which case suqueries are not feasible
Ans: Inner query executes first and gives the values to outer query to evaluate.
Subquery is not possible if returning multi values and outer query is not using in or or operators
to evaluate.
If subquery hits performnace then that will het query performance.
16)How many types of joins are there?when outer and self joins are used
Ans: Inner join: If condition is true
Outer JOin: even condition is not satisified.
1) left outer, 2) right outer ,3) Full outer (c the BOL for complete info)
Cross join: If no common column exists and requires cartieasan product values as output.
17)Questions on identity
21)If there is failure during updation of certain rows,what will be the state
Ans: If the transactions are set to autocommit then few rows will get effected else
all rows will be rolled back.
Implementation of relationships
1-1 Relationship: It is the most uncommon relationship. You might use one-one relationship to
divide a table with many fields, to isolate a part of table for security reasons can be implemented
by referencing a primary key in one table to a primary key in other table.
1-m Relationship: It is the most common type of relationship, a record in Table A can have
multiple matching records in B, and where as record in B should have only one matching record
in A. It can be implemented by Primary-foreign key relationships.
M-m Relationship: Database doesn’t support m-m relationships, the same can be implemented
by introducing a third (Intermediate) table, which connects both, and it is equivalent to having 2
1-m relationships with junction table. The Intermediate table contains a foreign key which is
made up of Primary keys in I & II table. For Example the orders and products table have many-
many relationship as defined, by creating 2 1-m relationships with Order details table.
If the table should be in 3rd normal form, the minimum no. of tables should be one.
Q. Why one should not prefix user stored procedures with sp_?
A. If the stored procedure starts with sp_, sql server first searches that sp in master database and
comes to current database. The stored procedures in master database is global, It is available to all
DB’s. It becomes a performance issue.
Query Optimizer: It is a component which analysis the query and determines the most efficient
way to request the data and thus optimizing the sql statement. The process of choosing an
execution plan out of several plans is called optimizing and this is done by query optimizer.
SQL Server Query Optimizer is a cost based optimizer. Each possible execution plan has an
associated cost in terms of computing resources. The query optimizer chooses that plan which has
lowest estimated cost.
Query Execution plan deals with the sequence in which the source tables are accessed, and the
methods used to extract the data from the table.
Optimizer Hints are used to override the Query Optimizer’s choice of execution. The five
categories of hints are Table hints, Join hints, Query hints, View hints, Lock hints.
You can specify multiple after triggering actions (Insert, Update, Delete).It can be applied to only
tables.
Instead of Triggers executes instead of triggering action. It can be applied to views and tables.
The primary advantage of this trigger is one can update a view which is made up of multiple
tables.
The two virtual tables that will be used by trigger are Inserted and Deleted tables.
Local variables are user-defined identified by @.The value can change during batch or stored
procedure in which it is used.
and Global variables are system-supplied and predefined identified by @@.
Local Temporary table is used by single user and will be deleted when user disconnects
where as global temporary tables can be used by multiple users and the tables will remain until
all users disconnect.
A derived table is a select statement that is used as a table; it is always enclosed in parenthesis
and follows from or join. Here is an example of a derived table.
The SELECT statement that gets data from the Customers table is the derived table.
A view is a virtual table that is made up of one or more tables. It can be used for security purpose,
hiding complex queries.
An Indexed view is a view that has unique clustered index created on it. This takes physical
storage i.e. it stores data. It will be used in OLAP, decision support where inserts/updates are low.
It can be used in the following scenarios
1) Joins and aggregation of big tables.
2) Repeated joins of the same tables on the same keys.
Replication is a set of technologies for copying and distributing the data and database objects
from one database to other and synchronizing between databases for consistency.
It allows multiple sites to keep the copies of same data .This is useful when multiple sites need to
read the same data, or need multiple servers for reporting applications.
It separates OLTP from OLAP.
Snapshot replication:
It distributes data exactly as it appears at a specific moment in time and doesn’t monitor for
updates. It can be used when data changes are infrequent. It is often used for browsing data such
as price lists, online catalog, or data for decision support where the current data is not required
and data is used as read only.
Transactional replication:
With this an initial snapshot of data is applied, and whenever data modifications are made at the
publisher, the individual transactions are captured and propagated to the subscribers.
Merge Replication:
It is the process of distributing the data between publisher and subscriber, it allows the publisher
and subscriber to update the data while connected or disconnected, and then merging the updates
between the sites when they are connected.
The distributor is the server that is responsible for synchronizing the data between publishers
and subscribers.
The where clause applies to individual rows and having clause applies only to groups.
Q.)Write a query to fetch all the managers names with the employee names in the order of
manager name.
Table: EMP
Columns
EmpId (Primary Key)
EmpName (varchar)
MgrId
Q) Write a query to update all the names that has spaces in front of their names (above table).
A) Update emp set empname='updated' where empname in (select empname from emp where
empname like ' %')
Set NoCount stops the message displaying the number of rows affected.
Temporary tables are like permanent table but they are created in tempdb, and they are deleted
automatically when no longer in use.
Collation specifies bit patterns that represent each character, and rules by which characters are
compared and sorted.
1) Select count (*) from table--- It will do full table scan, performance factor
2) Select rows from sysindexes where id=object_id ('table_name') and indid<2
Use constraints instead of triggers, rules and defaults, because constraints are more efficient and
boost performance.
OSQL utility allows Transact-SQL statement, stored procedures, and script files. This utility uses
ODBC to communicate with the server and ISQL uses DBLibrary to communicate with SQL
server.
BULK INSERT command is better when there is a text file import operation. Bulk Insert can’t
copy bulk data from Sql server to a data file. BCP utility is widely used for export operations
from SQL server to data file.
Two types of BCP – Logged and Non-logged BCP, Logged BCP will log the modifications and
can be used for recovery, whenever there is a failure and it is slow,
Non-logged is fast.
DBCC statements check the physical and logical consistency of the data base. It can fix the
detected problems. Ex DBCC SHOWCONTIG, DBCCREINDEX.
The database can be detached from one server and can be attached to another server/instance or
to the same server leaving the data intact, while drop statement removes the database
permanently.
1) XML support
2) Table data types
3) Instead of Triggers
4) Cascading
5) Indexed views
Extended stored Procedure’s are DLL’s that can be loaded and executed in the address space of
SQL Server.
Distributed Query is the query which accesses the data from multiple heterogeneous data.
The purpose of with no check option in alter table is Check and foreign key constraints are not
checked during table modification.
If a view consists of few columns in the table as opposed to all, then view can be modified only
when the left out columns are Nullable.
If a view is created using with check option, the data modification statements are validated
against the view’s select statement.
If a view is made up of one or more tables than view can’t be modified except through indexed
views.
Using constraints is preferred to using triggers, rules, and defaults. The query optimizer also uses
constraint definitions to build high-performance query execution plans.
Testing questions:
1. 1. What is regression testing?
2. 2. What are the SDLC followed?
3. 3. How you go about to test a refrigerator?
4. 4. How will u report your progress to your supervisor?
5. 5. How will u do weekly status report/daily report?
6. 6. How will u validate a test case? What are the documents to be taken care…?
SQL Questions:
1. 1. Inner merge join
2. 2. types of joins
3. 3. self join
4. 4. Difference between inner and outer join.
5. 5. difference between varchar and Nvarchar
6. 6. What are stored procedures?
7. 7. Difference between stored procedures and normal SQL statements?
8. 8. Reasons why we go for stored procedures
9. 9. what is identity
10. 10. how will u find out a dup in a column
11. 11. How will u find out dup in multiple columns? Whether it is possible to find out?
12. 12. The UI of your software is displayed with junk boxes…what will the cause for
these junk boxes? All the connection and system is running properly. Whether this is a
bug?
SQL Server:
1. Use of CASE statement.
2. I have 5 tables, I need to join these 5 tables every time and use this Output with some other
tables. What will you do in this case?
3. How can we get top 10 rows from a table?
6. How do we get, database user name, server name, version and database name.
7. Use of SET statement inside a procedure.
8. What is denormalization and on what circumstances we do that.
9. We have 1 table with a PK and 4 tables having FK referencing the PK of the first table. What
will happen if I try to delete a row from a table among the other 4 table mentioned.
10. How do we get the list of tables in a database?
11. How do we get the list of indexes and the type of the index for a table?
12. Syntax of bcp utility.
13. How do you execute a sql file from command prompt?
14. How do you update a column of a table with value in another column of the same table?
15. How do we import data from an excel sheet.
Testing:
1. You are given 5 modules to test with some deadline. First module itself you took a lot of time.
What shall you do in this situation?
2. You are given 10 modules to test and you are done. At that time your ML is absent. So what
will do in that situation?
3. You have code drop 3 running in your machine. Developer gave you drop 4, to test. Will you
test in the same machine or you will take a fresh machine to test.
4. Developer gave a drop to you to test. You got an error such that you can't proceed. The error
was due to a small mistake and so the developer came to your machine and start changing the
code in your machine. What will you do in this situation?
5. How do you report bugs in your testing.
6. How will you test a computer monitor, give that it has 4 buttons, with 1 for menu, 1 for
browsing up, 1 for browsing down, and 1 for applying the change?