Showing posts with label sql server. Show all posts
Showing posts with label sql server. Show all posts

Tuesday, June 17, 2014

Difference Between Sql Server VARCHAR and NVARCHAR Data Type


  • varchar : Non-Unicode variable length character type
  • nvarchar : Unicode variable length character type

Note: nvarchar can store both non-Unicode and Unicode characters(Example: Japanese, Korean)

Monday, May 12, 2014

Relationships in SQL Server



One-To-Many
  • One DeviceClass can have many DeviceTypes
  • Key symbol represent one, infinity symbol represent many
  • Note: You don’t have to have many. 


Many-To-Many
  • Not as common as One-To-Many
  • Many-To-Many relationship is expressed with help of junction tables
  • A table name with a joint name of two tables


One-To-One
  • Least commonly used relationship
  • One row in one tables is related to, One row in other table

Wednesday, November 28, 2012

Self join: Employee - Manager


ID   Name        ManagerID

1 Balaji NULL
2 Ashish 1
3 Divya 1
4 Nidhish 2


Write a query to display ManagerName (ManagerID is foreign key to ID)/



SELECT     e.ID, e.Name, mgr.Name AS ManagerName
FROM         Employee AS e LEFT OUTER JOIN
                      Employee AS mgr ON e.ManagerID = mgr.ID

Result:


1 Balaji NULL
2 Ashish Balaji
3 Divya Balaji
4 Nidhish Ashish


SQL Sample question





1.       Find out Name of customer who has maximum number of product?
2.       Find out Name of customer who does not have product.

/*find customer who has maximum product*/
select top 1 c.Name , COUNT(c.ID) as CustomerCount from Customer c left outer join CustomerProduct cp
      on c.ID = cp.CustomerID
            group by c.ID ,c.Name
            order by CustomerCount desc



/*find all customers who does not have any product*/
select c.Name  from Customer c left outer join CustomerProduct cp
      on c.ID = cp.CustomerID
     
            where cp.CustomerID is null

Tuesday, February 14, 2012

#temp table or @table variable


In stored procedures, we often have a need to store data temporarily. There are 3 ways:

1.       local temporary table     -              #temp table
2.       global temporary table -              ##temp table
3.       Table variables                  -              @table_name


Creation:
·         #temp table is created in system database tempdb
·         ##temp table is created in system database tempdb
·         @table variable is created in memory
Scope:
·         Local temporary tables are visible to current session
·         Local temporary tables gets dropped automatically when the current procedure goes out of scope

·         Global temporary tables are visible to all sessions
·         Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them.

·         Table variable are automatically cleared when procedure or function goes out of scope

Advantage:
·         #temp table - No locking is required since only one user is using #temp table

·         table variable is created in memory so performance is slightly better than #temp tables

Friday, December 3, 2010

keys

Primary key
  • an attribute/column which uniquely identifies a record
  • primary key can be column/combination of columns
  • primary key cannot have null values

Candidate key

  • Candidate key is a column which is not a primary key, but it can qualify as a primary key
  • there can be multiple candidate key in one table

alternate key

  • any of the candidate key that is not part of primary key

Thursday, November 25, 2010

correlated sub query

It is a query nested inside another query.

http://en.wikipedia.org/wiki/Correlated_subquery


Scenario: Write a query to select all employees from eastern region. DB diagram follows:




--select all employees from Eastern region
select * from employees where EmployeeID in
(
    -- select all Employees under territory which comes under Eastern region
    select EmployeeID from employeeterritories where TerritoryID in
    (
        --select all territory for Easter region (RegionID = 1)
        select TerritoryID from territories where RegionID = 1
     )
 )

Saturday, February 6, 2010

nth maximum value from table

select Max(empage) from Emp
where empage not in (select top 1 empage from Emp  order by EmpAge desc )