Skip to main content

Posts

Showing posts with the label sql-tips

SQL Joins

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys. A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table FULL OUTER JOIN A JOIN is made matching a column on a table to a column on the other table. After a FULL OUTER JOIN, for a given value (red), for a given row with this value on one table ([ red | 9999 ]), one row is created for each row that matches on the other table ([ red | OOOOOO ] and [ red | LLLLLL ]). If a value exists in only one table, then a row is created and is completed with NULL columns. FROM table_1 FULL OUTER JOIN table_2 ON table_1 . common_value = table_2 . common_value ...

Tips to increase your Transact-SQL efficiency Part 2

First Part :  http://www.developerscloud.org/2013/09/tips-to-increase-your-transact-sql.html 11. Use 'BETWEEN' operator instead of >= and <= operators to select data in range. 12. Wisely use the EXISTS, IN clauses in sub query select statement. - IN has the slowest performance as data is filtered between the range. - 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. 13. Avoid 'NOT IN' in select clause. Because when we use “NOT IN” in SQL queries, the query optimizer uses 'Nested table scan' technique  to perform the activity 14. Use Stored Procedure, functions(UDF) and views instead of heavy-duty queries. - The application must first convert the binary value into a character string (which doubles its size, thus increasing network traffic and taking more time) before it can be sent to the server. And when the  server receives the charac...

Tips to increase your Transact-SQL efficiency Part 1.

Given below are little known tips that you can use to ensure your Transact-SQL queries are performing in the  most efficient manner possible. 1. Avoid '*' in select query.      Restrict the queries result set by returning only the particular columns from the table and not all the  table's columns. The sql query becomes faster if you use the actual column names in SELECT  statement instead of than '*'. 2. Avoid COUNT(*) in select statement to check the existence of records in table.       Instead use IF EXISTS() to check records. - Write the query as: IF EXISTS (SELECT * FROM table_name WHERE column_name = ‘xxx’) - Instead of : SELECT COUNT(*) FROM table_name WHERE column_name = ‘xxx’ 3. Use alternate of SELECT COUNT(*).      Use an alternative way instead of the SELECT COUNT(*) statement to count the number     of records in  table.         - SELECT CO...