Best ways to write SQL query


Many of us never give attention how to write sql query,once we get work we simply start with writing it without consider some of the facts ,we need to consider some of rules and guidelines while writing query that will make query effective.Those are as follows..

1.Do not use '*' sign in SQL queries rather use column name in select statement. E.g. Select [column name] from [Table name] so that it would easy to identify query colums you are searching for.

2.Use square bracket for each Column name and table name in sql queries avoid naming conflicts.Use uppercase for all predefined types. E.g.

SELECT [userID] FROM [Users]
- This is standard way of writing query.

3.Aggregate functions ignore null values so use column name instead of '*'. E.g.
SELECT COUNT(*) from [Employee]
use -
SELECT COUNT(empID) [Employee]


4.Use WITH NOLOCK option in inner join, In order to remove deadlock while using transaction. used for query optimization

5.Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship. For Example: Write the query as
SELECT [EmployeeID], [Title] FROM [Employee] AS Parent WHERE EXISTS ( SELECT EmployeeID FROM [EmployeePayHistory] Child WHERE Parent.EmployeeID = Child.EmployeeID)
instead of
SELECT DISTINCT [Parent.EmployeeID], [Parent.title] FROM [AdventureWorks].[HumanResources].[Employee] Parent,[EmployeePayHistory] child WHERE Parent.EmployeeID = Child.EmployeeID 


6.Having Clause is used to filter rows after all rows selected. Use having clause whenever it is necessary.

7.Try to minimize use of sub queries in your block,because it may affect on performance.

8.Use Index for columns that frequently you are searching for from User Interface other than primary column because by default for whenever you create primary key clustered Index get created and table can have only 1 clusterd index but you can create maximum 249 non clustered index.

9.In Left outer Join always use ISNULL to get appropriate results.

10.It is good to use BETWEEN whenever you are seaching for any range rather than or /In operator

Thanks & Regards
Shweta Kulkarni


Comments

Author: Ranajoy28 Jul 2013 Member Level: Silver   Points : 10


The best ways of writing sql queries are given below which must be maintained :-

First of all, I agree with that using COUNT(COLUMNNAME) instead of using COUNT(*).

1.Use views and stored procedures instead of heavy-duty queries. This can reduce network traffic, because your client will send to server only stored procedure or view name (perhaps with some parameters) instead of large heavy-duty queries text. This can be used to facilitate permission management also, because you can restrict user access to table columns they should not see.

2. Try to use constraints instead of triggers, whenever possible. Constraints are much more efficient than triggers and can boost performance. So, you should use constraints instead of triggers, whenever possible.

3. Use table variables instead of temporary tables. Table variables require less locking and logging resources than temporary tables, so table variables should be used whenever possible. The table variables are available in SQL Server 2000 only.

4. Try to use UNION ALL statement instead of UNION, whenever possible. The UNION ALL statement is much faster than UNION, because UNION ALL statement does not look for duplicate rows, and UNION statement does look for duplicate rows, whether or not they exist.

5. Try to avoid using the DISTINCT clause, whenever possible. Because using the DISTINCT clause will result in some performance degradation, you should use this clause only when it is necessary.

6. Try to avoid using SQL Server cursors, whenever possible. SQL Server cursors can result in some performance degradation in comparison with select statements. Try to use correlated sub-query or derived tables, if you need to perform row-by-row operations.

7. Try to avoid the HAVING clause, whenever possible. The HAVING clause is used to restrict the result set returned by the GROUP BY clause. When you use GROUP BY with the HAVING clause, the GROUP BY clause divides the rows into sets of grouped rows and aggregates their values, and then the HAVING clause eliminates undesired aggregated groups. In many cases, you can write your select statement so, that it will contain only WHERE and GROUP BY clauses without HAVING clause. This can improve the performance of your query.

8. If you need to return the total table’s row count, you can use alternative way instead of SELECT COUNT(*) statement. Because SELECT COUNT(*) statement make a full table scan to return the total table’s row count, it can take very many time for the large table. There is another way to determine the total row count in a table. You can use sysindexes system table, in this case. There is ROWS column in the sysindexes table. This column contains the total row count for each table in your database. So, you can use the following select statement instead of SELECT COUNT(*): SELECT rows FROM sysindexes WHERE id = OBJECT_ID(‘table_name’) AND indid < 2 So, you can improve the speed of such queries in several times.

9. Try to restrict the queries result set by using the WHERE clause. This can results in good performance benefits, because SQL Server will return to client only particular rows, not all rows from the table(s). This can reduce network traffic and boost the overall performance of the query.

10. Use the select statements with TOP keyword or the SET ROWCOUNT statement, if you need to return only the first n rows. This can improve performance of your queries, because the smaller result set will be returned. This can also reduce the traffic between the server and the clients.

11. Try to restrict the queries result set by returning only the particular columns from the table, not all table’s columns. This can results in good performance benefits, because SQL Server will return to client only particular columns, not all table’s columns. This can reduce network traffic and boost the overall performance of the query. 1.Indexes 2.avoid more number of triggers on the table 3.unnecessary complicated joins 4.correct use of Group by clause with the select list 5 In worst cases Denormalization Index Optimization tips

12. Every index increases the time in takes to perform INSERTS, UPDATES and DELETES, so the number of indexes should not be very much. Try to use maximum 4-5 indexes on one table, not more. If you have read-only table, then the number of indexes may be increased.

13.Keep your indexes as narrow as possible. This reduces the size of the index and reduces the number of reads required to read the index.

14. Try to create indexes on columns that have integer values rather than character values.

15. If you create a composite (multi-column) index, the order of the columns in the key are very important. Try to order the columns in the key as to enhance selectivity, with the most selective columns to the leftmost of the key.

16. If you want to join several tables, try to create surrogate integer keys for this purpose and create indexes on their columns.

17. Create surrogate integer primary key (identity for example) if your table will not have many insert operations.

18. Clustered indexes are more preferable than nonclustered, if you need to select by a range of values or you need to sort results set with GROUP BY or ORDER BY.

19. If your application will be performing the same query over and over on the same table, consider creating a covering index on the table.

20. Include SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a T-SQL statement. This can reduce network traffic, because your client will not receive the message indicating the number of rows affected by a T-SQL statement.


The above are maximum points of best ways to write sql queries.

Note :- Here, in the point there is an use of < it just denotes the less than sign (since it's not supporting special character).

Author: devanand30 Jul 2013 Member Level: Silver   Points : 6

Step 1

First step is to identify the tables from where we can find users and posts. We can solve above problem from two wordpress tables (wp_posts and wp_users).

We need to INNER JOIN both tables ON the basis of columns wp_users.ID and wp_posts.post_author. Both of them contains the user id so we can join them as shown below:

FROM wp_users u INNER JOIN wp_posts p
ON u.ID = p.post_author

You can place SELECT * before FROM clause to test the results returned from this query. Also remember to test each clause throughly before moving to next one.
Step 2
Remeber we need to COUNT only those posts whose status is ‘publish’. Actually in WordPress posts can have different states like published, draft, revision, and pending review. We need to filter Individual Rows using WHERE to get only those whose post_status is ‘publish’

SELECT *
FROM wp_users u INNER JOIN wp_posts p
ON u.ID = p.post_author
WHERE p.post_status = 'publish'

Step 3

Now we need to GROUP individual rows on the basis of wp_users.user_login (used to login WordPress admin panel and it’s unique) column because we want to make a single (Group) row for each user. Also by making a Group Row by user_login, we can COUNT the total number of posts (in SELECT clause later) added by each user.

SELECT *
FROM wp_users u INNER JOIN wp_posts p
ON u.ID = p.post_author
WHERE p.post_status = 'publish'
GROUP BY u.user_login

Step 4

We can skip HAVING clause because we don’t need any filtration of Group Rows.
Step 5

According to our requirement, we need to display only two columns using SELECT clause. First is the user_login and second is the COUNT of posts added by the user.

SELECT u.user_login, COUNT(p.ID) AS total_posts
FROM wp_users u INNER JOIN wp_posts p
ON u.ID = p.post_author
WHERE p.post_status = 'publish'
GROUP BY u.user_login

COUNT() is an aggregate function which is used to count the rows and in our case it counts the rows in each group, not the total rows fetched from the table. Remember that each group is created on the basis of user_login.
Step 6

Finally we need to sort our results in ascending order using ORDER BY

SELECT u.user_login, COUNT(p.ID) AS total_posts
FROM wp_users u INNER JOIN wp_posts p
ON u.ID = p.post_author
WHERE p.post_status = 'publish'
GROUP BY u.user_login
ORDER BY u.user_login

Sub Queries

If your query invole subqueries then try writing them when writing a clause in which it will go. For example, if the subquery will go in FROM clause then try writing it in Step 1. Similarly if your subquery will go in SELECT clause, then try writing it in Step 5.

I want to remind you again that this article is not about learning SQL basics. It’s about the steps to write complex queries. Your problem will get much simplified if you follow these steps to write complex queries. Try writing more queries using these steps.

Regards
Gadhvi Devanand



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: