1) It is good to use columns names in SELECT statement instead of than '*'.
For Example: Write the query as
SELECT id, Name, age, Contact, Address FROM Customers;
SELECT * FROM Customers;
2) Minimize the number of sub query block in your query.
For Example: Write the query as
SELECT name, age, salary FROM employee
WHERE (salary, age ) = (SELECT MAX (salary), MAX (age)
FROM employee_details) AND dept = 'Electronics';
SELECT name, age, salary FROM employee
WHERE salary = (SELECT MAX(salary) FROM employee_details)
AND age = (SELECT MAX(age) FROM employee_details)
AND emp_dept = 'Electronics';
3) HAVING clause is used to filter the rows after all the rows are selected. It is just like a filter.
For Example: Write the query as
SELECT Designation, count(Designation) FROM EmployeeWHERE Designation!= 'Manager'
GROUP BY Designation;
SELECT Designation, count(Designation) FROM Employee GROUP BY Designation
HAVING Designation!= 'Manager';
4) Use operator EXISTS, IN and table joins appropriately in your query.
- Usually IN has the slowest performance.
- IN is efficient when most of the filter criteria is in the sub-query.
- EXISTS is efficient when most of the filter criteria is in the main query.
Select p.Name, P.Price, p.Quantity from product p where EXISTS
(select o.Name, o.Price, o.Quantity from orders o
where o.product_id = p.product_id)
Select p.Name, P.Price, p.Quantity from product p where product_id IN
(select product_id from order_items)5) Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship.
For Example: Write the query as
SELECT d.dept_id, d.dept FROM dept d
WHERE EXISTS ( SELECT 'X'
FROM employee e
WHERE e.dept = d.dept);
SELECT DISTINCT d.dept_id, d.dept FROM dept d,employee e
WHERE e.dept = e.dept;
6) Try to use UNION ALL in place of UNION.
For Example: Write the query as
SELECT id, first_name FROM student_details_class10
UNION ALL SELECT id, first_name
FROM sports_team;
SELECT id, first_name, subject
FROM student_details_class10
UNION
SELECT id, first_name
FROM sports_team;
7) Be careful while using conditions in where clause.
For Example: Write the query as
SELECT id, first_name, age FROM student_details WHERE age > 10;
SELECT id, first_name, age FROM student_details WHERE age != 10;
SELECT id, first_name, age FROM student_details
WHERE first_name LIKE 'Chan%';
Instead of:
SELECT id, first_name, age FROM student_details
WHERE SUBSTR(first_name,1,3) = 'Cha';
Write the query as
SELECT id, first_name, age FROM student_details
WHERE first_name LIKE NVL ( :name, '%');
SELECT id, first_name, age FROM student_details
WHERE first_name = NVL ( :name, first_name);
SELECT product_id, product_name FROM product
WHERE unit_price BETWEEN MAX(unit_price) and MIN(unit_price)
SELECT product_id, product_name FROM product
WHERE unit_price >= MAX(unit_price)
and unit_price <= MIN(unit_price)
SELECT id, name, salary FROM employee WHERE dept = 'Electronics'
AND location = 'Bangalore';
SELECT id, name, salary FROM employee
WHERE dept || location= 'ElectronicsBangalore'; For Example: Write the query as
SELECT id
FROM employee WHERE name LIKE 'Ramesh%'
and location = 'Bangalore';
SELECT DECODE(location,'Bangalore',id,NULL) id
FROM employee
WHERE name LIKE 'Ramesh%'; 9) Use of valid Schema for SQL Query.
For Example: Write the query as
SELECT id,Name,salary
FROM [dbo].[employee]
SELECT id,Name,salary
FROM employee
No comments:
Post a Comment