Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Monday, November 30, 2009

Cross Join in SQL

SQL CROSS JOIN will return all records where each row from the first table is combined with each row from the second table. Which also mean CROSS JOIN returns the Cartesian product of the sets of rows from the joined tables.
in other meaning Cross Joins produce results that consist of every combination of rows from two or more tables That means if table A has 3 rows and table B has 2 rows, a CROSS JOIN will result in 6 rows.  There is no relationship established between the two tables – you literally just produce every possible combination.


it is useful if you do lots of report writing in SQL, a CROSS JOIN can be your best friend.
you can create Cross join in two ways 
1. using join keyword 
2. list all the tables in the from clause separated by comma without using where 







Saturday, November 21, 2009

Have Look at Data Type of SQL Server 2008

Character Types

  • The text and ntext types are both deprecated in SQL server 2008 so avoid using them, they have been replaced by varchar(max) and nvarchar(max).


  • The difference between char and varchar is that char is fixed length and varchar is variable length .


  • The advantage of using char over varchar is that updates made to a char column never require moving the row because the data that is entered always fits in the allocated space 


  • What about nchar and nvarchar? both of these data types store characters using the Unicode Universal Code Page (UCS-2). this means that if you use nchar or nvarchar, you can store any type of character regardless of the collation you choose because two bytes are always used to store each character. contrast with varchar and char, which store characters using one or two bytes depending on the collation. 
  • Note that: if you want to use different collation than the one specified on a column when making te comparsion

............  where Name='Mohamed' COLLATE Finnish_Swedish_CI_AS

  • To get all the collations of the SQL Server 2008 by using table valued function fn_helpcollations

Select  *
                        From  fn_helpcollations()

Exact Numbers 

  • Difference between Decimal Types & numeric: 
    • when using the decimal data type, you can specify the precision and scale of values stored using the data type. the precision defines the total number of digits that the data type holds, supporting a maximum precision of 38 and the scale defines how many of  the digits defined by the precision are used as decimals. 


Approximate Numeric Types 

  • the float data type accepts a parameter. the parameter supplied to the float data type defines the number of bits that are used to store the mantissa of the floating point number 

Monday, October 5, 2009

Difference Between Count(*) and Count(1) in SQL

Both will be same in terms of performance, as the execution is to retrieve information from the table. You are free to use any constant expression instead of * - you can use COUNT(0), COUNT('a') and even COUNT(NULL) - each with the same results. You can also use a column name as the parameter, but this means that rows with a NULL on that column will not be counted The 'normal' parameter for COUNT is * - in most cases there is no reason to use anything else