Você está na página 1de 3

http://oraclegeneration.blogspot.in/2014/07/oracle-sqlqueries.

html
http://www.deepinopensource.com/informatica-tutorialsbeginners/
Perfromance Tuning in SQL Server 2008
Query Optimization
1.
2.
3.

Remove * from SELECT and use columns which are only necessary in code
Remove any unnecessary joins from table

Always use WHERE Clause in SELECT Queries while we dont need all the rows to be
returned.
4. Avoid SELECT * FROM OrderTable WHERE LOWER(UserName)='telsa' , because case
insensitive.
5. The operators shown below are in their decreasing order of their performance.
=
>,>=,<, <=
LIKE
<>
ie, better to go for '=' rather than 'LIKE' or '<>'
6.

NOT IN, then this is going to offer poor performance, this can be avoided by using
EXISTS or NOT EXISTS.
7. When there is a choice to use IN or EXIST, we should go with EXIST clause for better
performance.
8. SELECT * FROM OrderTable WHERE Status = 1 AND OrderID IN (222,444,111,555)
Takes more time than
SELECT * FROM OrderTable with (INDEX=IX_OrderID) WHERE Status = 1 AND OrderID IN
(222,444,111,555 6)
9. While there is case to use IN or BETWEEN clauses in the query, it is always advisable to
use BETWEEN for better result.
10. Always avoid the use of SUBSTRING function in the query.
11. Provide the least likely true expressions first in the AND. Because if the first condition is
false the result also false, ie it never want to check the second condition of AND
operation .
12. It is sometimes better to combine queries using UNION ALL instead of using many OR
clauses.

13. SELECT * FROM CustomerTable WHERE City = 'Wichita' OPTION(FAST n). In some
scenario's we have to display only 10-15 result set in the web page . But in database it
having more than Ten Thousand row. In this time the data base will give the 'n' result set
very fast, and rest of the data will update soon.
14. Avoid Expensive Operators Such as NOT LIKE
15. WITH NOLOCK and WITH READUNCOMMITTED
16. Avoid Long-Running Transactions. it has to do by the sys admin.
17. Use Set NOCOUNT ON in stored procedures.
18. You can rewrite a subquery to use JOIN and achieve better performance.

19. Performance increase by adding a unique key for table. (it can index the table by unique
key)

10 Complex Sql Query on basis of


Sql Interview Question between 1 to
5 year work experience.
1. Sql Query to find second highest salary of Employee.
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from
Employee );

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


SELECT DeptID, MAX(Salary) FROM Employee

GROUP BY DeptID.

3.Write a SQL Query to print the name of distinct employee


whose DOB is between 01/01/1960 to 31/12/1975.
SELECT DISTINCT EmpName FROM Employees WHERE DOB
31/12/1975;

BETWEEN 01/01/1960 AND

[adsenseyu1]
4. Write an SQL Query find number of employees according to
gender whose DOB is between 01/01/1960 to 31/12/1975.
SELECT COUNT(*), sex from Employees
31/12/1975 GROUP BY sex

WHERE

DOB BETWEEN 01/01/1960 ' AND

5.Write an SQL Query to find employee whose Salary is equal or


greater than 10000.
SELECT EmpName FROM

Employees WHERE

Salary>=10000;

6. Write an SQL Query to find name of employee whose name


Start with M
SELECT * FROM Employees WHERE EmpName like 'M%';

7. Select all record from emp table where deptno =10 or 40.
select * from emp where deptno=30 or deptno=10;

8. How to create a clone of another table


Create table emp1 as select * from emp where 1=2;

9.How can I retrive all records of emp1 those should not present
in emp2?
Select * from emp) Minus (Select * from emp1

10.How to fetch only common records from two tables emp and
emp1?
(Select * from emp) Intersect (Select * from emp1)

Você também pode gostar