Wednesday, December 8, 2010

DataBase Q

Q Get Table result with Total and SubTotal.
CC         1    40
DD         1    5
EE         2    35
SubTOTAL   :::  35
SubTOTAL   :::  45
TOTAL      :::  80

      ------------------ Or ---------------------
with ct as  
( select pname, pvalue ,ggroup from Ta1 
 union all 
 select 'Sub Total',SUM(cast(pvalue as int)),'-' from Ta1 group by ggroup  
union all 
select pname, pvalue ,ggroup from Ta1 where ggroup = 2 
union all 
select 'Grand TOTAL' as Name,SUM(cast(pvalue as int)) as pvalue,'-' as ggroup from Ta1 )
 select


Q: select the duplicate records from table
select* from ta1 where ggroup in(select ggroup from Ta1 group by ggroup having count(*) > 1)
pname, pvalue,ggroup from ct where ggroup is not null and pvalue is not null
     ------------------ Or ---------------------
select
         case when pname = (select top 1 pname from Ta1 where ggroup = O.ggroup order by pname desc)         then (select cast(sum(cast(pvalue as int)) as char(10)) from Ta1
where pname <= O.pname and ggroup = O.ggroup)    else ' ' end as 'Sub Total',             case when pname = (select top 1 pname from Ta1 order by ggroup desc)             then (select sum(cast(pvalue as int)) from Ta1)
    else ' ' end as 'Grand Total'  from Ta1 O where pvalue is not null order by pname
O.pname,O.pvalue,
pname,ggroup,pvalue from CTE where pvalue is not null
'TOTAL' as Name,':::' as ggroup,SUM(cast(pvalue as int)) as pvalue from Ta1
'SubTOTAL' as Name,':::' as ggroup,SUM(cast(pvalue as int)) as pvalue from Ta1 group by ggroup
pname,ggroup,pvalue from Ta1
pvalue is not null  group by pname with rollup order by pname desc
Ta1
  isnull(convert(VARCHAR,pname),'GRAND TOTAL') AS ggroup,  sum(cast(pvalue as int)) AS 'Total In Inventory'
;
Q Get value  by ','
-- Check Table ColumnSELECT Name    FROM HumanResources.Shift
GO
-- Get CSV values  SELECT SUBSTRING(
         (SELECT ',' + s.Name      FROM HumanResources.Shift s      ORDER BY s.Name     FOR XML PATH('')),2,200000) AS CSV
GO


Q find the 3rd highest value from table
 selectcast(pvalue as int)  from ta1 e1  where 2= ( count(distinct pvalue) from ta1 where cast(pvalue as int) > cast(e1.pvalue as int)) )
--------------OR---------------
SELECTTOP 1 pvalue    FROM     (
      SELECT distinct TOP 3             cast(pvalue as int) as pvalue         FROM ta1 group by cast(pvalue as int)         ORDER BY cast(pvalue as int
        TmpTable
        ) DESC )     ORDER BY cast(pvalue as int) ASC

--------------OR---------------
 select max(id)as id
       from student
    where id in
  (select top 3 id from student1 )
    select count(distinct pvalue) from ta1    where    cast(pvalue as int) > cast(e1.pvalue as int)

19 comments:

  1. http://www.dotnetfunda.com/interview/showcatquestion.aspx?start=108&page=10&category=38

    ReplyDelete
  2. Q- http://itfunda.com/net-interview-preparation-kit-online/Show/4

    ReplyDelete
  3. Why we use Unicode In Sql server
    Unicode data is stored using the nchar, nvarchar,and ntext data types in SQL Server. Use these data types for columns that store characters from more than one character set. The SQL Server Unicode data types are based on the National Character data types in the SQL-92 standard.

    ReplyDelete
  4. Q - Diffrence between temp table and table variable
    (1)Temp Tables are created in the SQL Server TEMPDB database and therefore require more IO resources and locking. Table Variables and Derived Tables are created in memory.

    (2)Temp Tables will generally perform better for large amounts of data that can be worked on using parallelism whereas Table Variables are best used for small amounts of data (I use a rule of thumb of 100 or less rows) where parallelism would not provide a significant performance improvement.

    (3)You cannot use a stored procedure to insert data into a Table Variable or Derived Table. For example, the following will work: INSERT INTO #MyTempTable EXEC dbo.GetPolicies_sp whereas the following will generate an error: INSERT INTO @MyTableVariable EXEC dbo.GetPolicies_sp.

    ReplyDelete
  5. Q- How many types of local table in sql define with syntax

    There are 2 types of temporary tables, local and global. Local temporary tables are created using a single pound (#) sign and are visible to a single connection and automatically dropped when that connection ends. Global temporary tables are created using a double pound (##) sign and are visible across multiple connections and users and are automatically dropped when all SQL sessions stop referencing the global temporary table.

    CREATE TABLE #MyTempTable
    ( PolicyId INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
    LastName VARCHAR(50) NOT NULL
    )

    ReplyDelete
  6. Q- What is SQL injection
    SQL injection is a security vulnerability that occurs in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is in fact an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another.

    ReplyDelete
  7. Q- What is heap table in SQL SERVER
    A table that doesnot contains cluster and non cluster index is heap table because there is no indexing there on to the table.

    ReplyDelete
  8. Q- What are Checkpoint in SQL Server
    When we done operation on SQL SERVER that is not commited directly to the database.All operation must be logged in to Transaction Log files after that they should be done on to the main database.CheckPoint are the point which alert Sql Server to save all the data to main database if no Check point is there then log files get full we can use Checkpoint command to commit all data in the SQL SERVER.When we stop the SQL Server it will take long time because Checkpoint is also fired.

    ReplyDelete
  9. Q- What is the difference between UNION ALL Statement and UNION
    The main difference between UNION ALL statement and UNION is UNION All statement is much faster than UNION,the reason behind this is that because UNION ALL statement does not look for duplicate rows, but on the other hand UNION statement does look for duplicate rows, whether or not they exist.

    ReplyDelete
  10. Q- Write some disadvantage of Cursor
    Cursor plays there row quite nicely but although there are some disadvantage of Cursor .
    Because we know cursor doing roundtrip it will make network line busy and also make time consuming methods.

    First of all select query gernate output and after that cursor goes one by one so roundtrip happen.

    Another disadvange of cursor are ther are too costly because they require lot of resources and temporary storage so network is quite busy.

    ReplyDelete
  11. Q- Write a Role of Sql Server 2005 in XML Web Services
    SQL Server 2005 create a standard method for getting the database engine using SOAP via HTTP. By this method, we can send SOAP/HTTP requests to SQL Server for executing T-SQL batch statements, stored procedures, extended stored procedures, and scalar-valued user-defined functions may be with or without parameters.

    ReplyDelete
  12. Q- What is COMMIT and ROLLBACK statement in SQL
    Commit statement helps in termination of the current transaction and do all the changes that occur in transaction persistent and this also commits all the changes to the database.COMMIT we can also use in store procedure. ROLLBACK do the same thing just terminate the currenct transaction but one another thing is that the changes made to database are ROLLBACK to the database.

    ReplyDelete
  13. Q- When do you use SQL Profiler
    SQL Profiler utility allows us to basically track Connections to the SQL Server and also determine activities such as which SQL Scripts are running, failed jobs etc.

    ReplyDelete
  14. Q- What are the Global Temporary Tables
    We can create global temporary tables but these are not using much in sql an the name of these table start with two pound signs. For example, ##interviewqsn is a global temporary table.As the name suggest these table is Global temporary tables and visible to all SQL Server connections. When we create any one of these all users can see it.

    ReplyDelete
  15. http://www.questpond.com/demo.html#dp

    ReplyDelete
  16. Q- What is Cursor
    Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time. For example, you can use cursor to include a list of all user databases and make multiple operations against each database by passing each database name as a variable

    ReplyDelete
  17. http://www.dotnetspider.com/resources/1241-Frequently-Asked-SQL-Server-Interview-Question.aspx

    ReplyDelete
  18. All Database systems which include transaction support implement ACID properties to ensure the integrity of the database. ACID stands for Atomicity, Consistency, Isolation and Durability
    • Atomicity: Each transaction is said to be “atomic.” If one part of the transaction fails, the entire transaction fails. Modifications on the data in the database either fail or succeed.
    • Consistency: This property ensures that only valid data will be written to the database. If, for some reason, a transaction is executed that violates the database’s consistency rules, the entire transaction will be rolled back and the database will be restored to a state consistent with those rules.
    • Isolation: It requires that multiple transactions occurring at the same time not impact each other’s execution.
    • Durability: It ensures that any transaction committed to the database will not be lost.

    ReplyDelete