GeekInterview.com
Series: Subject: Topic:

SQL Server Interview Questions

Showing Questions 1 - 20 of 113 Questions
First | Prev | | Next | Last Page
Sort by: 
 | 

How to remove duplicate records from a table?

Asked By: Beena | Asked On: Sep 19th, 2005

Answered by: Vijay on: May 10th, 2012

SELECT ID FROM TBLSAMPLE GROUP BY ID OR SELECT DISTINCT (ID) FROM TBLSAMPLE

Code
  1. SELECT ID FROM TBLSAMPLE GROUP BY ID
  2. OR
  3. SELECT DISTINCT (ID) FROM TBLSAMPLE

Answered by: Mally on: Feb 12th, 2012

Good Code...however the code will delete all the duplicates... you may want to add:

delete top(n) clause in your delete statement to exactly delete the required number of records.

Write SQL query for retrieving employee names from employee table who are having salary greater than 5000 without using where clause?

Asked By: Ashok | Asked On: Jul 18th, 2006

Star Read Best Answer

Editorial / Best Answer

Answered by: Parished.D Chennai India

Answered On : Apr 24th, 2007

create table ee (eno int, ename varchar(200), sal int)

insert into ee values(1, 'a', 2000)
insert into ee values(2, 'b', 6000)
insert into ee values(3, 'c', 8000)

select ENO, ENAME, min(sal) AS SAL from ee group by eno,ename having min(sal) > 5000

Answered by: Pushpa on: May 3rd, 2012

If we want to check any values to retrieve the data,case is the best.Case statement is simple and easy to understand.

Code
  1. SELECT case when sal>1000 then ename end
  2. FROM emp

Answered by: r on: Feb 21st, 2012

Using Group By

Code
  1. FROM Employees
  2. GROUP BY eid,ename,salary
  3. HAVING salary>15000

Explain normalization and denormalization with examples?

Asked By: Beena | Asked On: Sep 19th, 2005

Answered by: siva on: Apr 27th, 2012

It is good.I understood if you put some brief explanation with data, is very usable to the persons who are not understanding about normalization like me. ...

Answered by: siva on: Apr 27th, 2012

Your Explnation may be good but it was too dificult to understand with out examples

How to find out duplicate records in SQL server?

Asked By: Srinu | Asked On: Apr 5th, 2006

Star Read Best Answer

Editorial / Best Answer

Answered by: Hanif

Answered On : Apr 12th, 2006

we have to use the group by with having command to get the duplicate values. this query shall show the result of only the users have duplicate values in the employee table.

Syntex:
Select columnName From Table_name
Group By columnName
Having count (*) > 1

Example:
SELECT  UserID FROM employee
GROUP BY userid
HAVING count( * ) > 1

Answered by: nandu on: Apr 26th, 2012

SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

Answered by: Ramakant Sahoo on: Dec 23rd, 2011

Code
  1. SELECT EMAIL, COUNT(*)  "REPETED EMAIL" FROM EMP GROUP BY EMAIL;

What is describe command in SQL server?

Asked By: Priya | Asked On: Aug 30th, 2006

What is its purpose?How to use it?

Star Read Best Answer

Editorial / Best Answer

Answered by: suji

Member Since Sep-2005 | Answered On : Nov 5th, 2011

Here are multiple ways to get the table information. The DESCRIBE command does not exist in MS SQL SERVER. This is an Oracle command used to describe the structure of objects within a given database. To achieve the same task in MSSQL Server, there are a series of stored procedures with the prefix SP_ that can be used. To view the structure of a table within the current database, use the command

Code
  1. sp_help 'TABLE_NAME';

If you would like to see more details, Create your custom procedure

Code
  1. CREATE procedure DESCRIBE
  2. (
  3. @tablename varchar(256)
  4. )
  5. AS
  6. begin
  7. SELECT DISTINCT sCols.colid AS 'order', sCols.name, sTyps.name, sCols.length
  8. FROM [syscolumns] sCols
  9. INNER JOIN [systypes] sTyps ON sCols.xtype = sTyps.xtype
  10. INNER JOIN [sysobjects] sObjs ON sObjs.id = sCols.[id]
  11. AND UPPER(sObjs.name) = UPPER(@tablename)
  12. ORDER BY sCols.colid
  13. end

Here is another alternative way to get the same information

Code
  1. sp_columns 'TableName' (e.g. sp_columns 'Employee')
  2. sp_columns [ @table_name = ] object [ , [ @table_owner = ] owner ]
  3. [ , [ @table_qualifier = ] qualifier ]
  4. [ , [ @column_name = ] COLUMN ]
  5. [ , [ @ODBCVer = ] ODBCVer ]

Here is the usage :

Code
  1. EXEC sp_columns @table_name = 'Department', @table_owner = 'sa';
  2. SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TableName'

Contributors for the editorial answer : Kewlshiva, srilakshmi.b, raaghav,kevaburg

Answered by: gangireddy on: Feb 22nd, 2012

To see the structure (description ) of the table in Sql Server the followed command surely works....
sp_help person

Note:***
to display the structure of the table in Sql Server wer use the followed command ...it has the accurate rating ****
sp_help
sp_help person

Answered by: Nirav on: Dec 29th, 2011

This will give you the best information of your table in SQL Server. Like Name of Table, Column Name, Identity, Row Guid Col, Data Located On Filegroup, Index Name and Constraint Type. EXECUTE sp_help

Code
  1. EXECUTE sp_help <tableName>

Can we replace where clause by having clause. ?

Asked By: bhushan13in | Asked On: Feb 14th, 2012

Interviewer ask.. Can we replace where clause by group by clause let say select * from table_name where some_condition so how we replace where clause by group by clause

Answered by: Mark Haynes on: Feb 16th, 2012

As a basic rule of query building: 1. The WHERE clause should be used to limit the number of rows returned by or affected by the a SQL statement. 2. The GROUP BY clause is used to group a selec...

Can I update views in SQL

Asked By: rahul.solanke | Asked On: Jan 14th, 2012

Can I update views in SQL if yes in what scenarios? if your view is joining multiple tables

Answered by: Jean Daniel Joseph on: Jan 17th, 2012

Yes but you remember views, represents a virtual table but if you delete the table you wont be able to update the view

Answered by: suji on: Jan 15th, 2012

Yes. You can just create view and treat as table. Use update statements like you do for tables.

Is there an import command in SQL 2008 for an excel spreadsheet?

Asked By: merlin1106 | Asked On: Nov 21st, 2011

Answered by: suji on: Jan 15th, 2012

From your SQL Server Management Studio, you open Object Explorer, go to your database where you want to load the data into, right click, then pick Tasks > Import Data. Select the options based on the excel file

How is the error handling in stored proc of T-SQL ?

Asked By: ajay_shu007 | Asked On: Jun 19th, 2008

1 how is the error handling in stored proc of T-SQL2 what is clustered index and non-clustered index? How many clustered indexes and non-clustered indexes can be created in one table? 3-what is disconnected mode?

Answered by: Pradeep Kumar Sharma on: Dec 25th, 2011

Begin Try

--
--
End Try
Begin Catch

Declare @errMsg varchar(1000),@errSev int
select @errMsg=error_message(),@errSev=error_severity()
RaisError(@errMsg,@errSev,1)

End Catch

Answered by: kirangiet on: Aug 16th, 2010

1) CLR is integrated in SQL Server 2005. Hence we can use below code for Exception Handling. BEGIN TRY SELECT * FROM Employees; END TRY BEGIN CATCH SELECT ERROR_NUMBER() ...

Representing SQL server objects

Asked By: polkam.raju | Asked On: Sep 23rd, 2009

How do represent objects in SQL server? Is it possible? If yes, how?If not? Why?

Answered by: Lokesh M on: Dec 12th, 2011

Try using Server Management Objects (SMO) classes

Covering indexes

Asked By: psingla | Asked On: Mar 9th, 2010

List down the advantage and disadvantages of covering indexes.

Answered by: Lokesh M on: Dec 12th, 2011

Covering Indexes are useful for Input / Output intensive workloads.
Covering indexes are smaller in size
Covering Indexes are sorted by values
It is easier to cache date in Covering Indexes

Covering Indexes cannot be used to select all the columns / entire table.

Run PL/SQL block from SQL server

Asked By: vrushali | Asked On: Jan 18th, 2011

How do you run PL/SQL block from SQL server

Answered by: Lokesh M on: Dec 12th, 2011

Try these and see...

Go To Options -> Run SQL
Set "Statement Delimiter" to None

Go To Options -> Technical Parameters
Tick "Use SQLExecDirect"

High water mark

Asked By: hkravipati | Asked On: Jul 9th, 2011

What is high water mark? Give an example where it can be useful ?

Answered by: Lokesh M on: Dec 12th, 2011

High Water Mark (HWM) represents total number of extent blocks used by a table. The value of High Water Mark could sometime be more then the actual size required/used by a table. This is because, some...

How to set GLobal setting for query studio?

Asked By: chandu22.csp | Asked On: Aug 24th, 2011

Answered by: Lokesh M on: Nov 28th, 2011

Steps to set / modify Global Setting for Query Studio: 1. Stop the Cognos 8|ReportNet server. 2. On the Cognos 8|ReportNet Server, go to the directory /c8|crn/templates/ps/async/ 3. Either create a n...

How to store HTML file in SQL server 2008 ?

Asked By: ravi sonaiya | Asked On: Nov 6th, 2011

Star Read Best Answer

Editorial / Best Answer

Answered by: suji

Member Since Sep-2005 | Answered On : Nov 7th, 2011

I would use full-text indexing as you are on the latest version. Create and store the html in varbinary(max) column and set its associated file type to ".html" in a file type column. Then enable full-text index, so indexer will parse the data and search only the text content while ignoring the HTML tags. We had been using blob to store the documents, with the new features have come out in the later versions, this seems to be best option. We are in the process of migrating one of the app to use varbinary and to take advantage of the search feature. There is another way of storing using filestrem. Here is a good read-up from MS . FILESTREAM enables SQL Server-based applications to store unstructured data, such as documents and images, on the file system. Applications can leverage the rich streaming APIs and performance of the file system and at the same time maintain transactional consistency between the unstructured data and corresponding structured data http://blogs.msdn.com/b/manisblog/archive/2007/10/21/filestream-data-type-sql-server-2008.aspx Here is another old report from MS back in 2006 explains about using oldway of BLOG http://research.microsoft.com/apps/pubs/default.aspx?id=64525 There are multiple options for you, pick the best approach that matches your needs Thanks, Suji

Answered by: suji on: Nov 7th, 2011

I would use full-text indexing as you are on the latest version. Create and store the html in varbinary(max) column and set its associated file type to ".html" in a file type column. Then enable fu...

Give example for binary datatype non binary datatype?

Asked By: Gajalakshmi | Asked On: Oct 3rd, 2006

Star Read Best Answer

Editorial / Best Answer

Answered by: suji

Member Since Sep-2005 | Answered On : Nov 6th, 2011

Here are the Binary data types in sql server - bit, binary, varbinary, image. Bit variables store a single bit with a value of 0, 1 or NULL. binary(n) variables store n bytes of fixed-size binary data. They may store a maximum of 8,000 bytes. varbinary(n) variables store variable-length binary data of approximately n bytes. They may store a maximum of 8,000 bytes. image variables store up to 2 gigabytes of data and are commonly used to store any type of data file. Rest of the data types are non-binary

Answered by: suji on: Nov 6th, 2011

Here are the Binary data types in sql server - bit, binary, varbinary, image. Bit variables store a single bit with a value of 0, 1 or NULL. binary(n) variables store n bytes of fixed-size binary da...

Answered by: krishnakumar on: Nov 2nd, 2006

The binary datatype is used for storing the images in the database.the non binary datatypes are integer,varchar etc

How to use roll back query in SQL server 2008

Asked By: plresponse | Asked On: Apr 25th, 2011

Answered by: suji on: Nov 6th, 2011

Here is how you can use rollback in any of the SQL Server versions. works in both SQL Server 2000 and later versions. "sql Begin tran insert into table values or updates rollb...

What is normolization in SQL server ?

Asked By: cmannulal | Asked On: Aug 25th, 2011

Star Read Best Answer

Editorial / Best Answer

Answered by: Steven

Answered On : Oct 4th, 2011

Normalizatiopn is the process of splitting tables up to reduce record redundancy. For instance if you have a table of addresses you may want to create a table of cities and link the addresses to the cities table in a foreign key. Since the city names would be identical on several records this would save a lot of space. In some cases if tables are not normalized the amount of space they would take up would be so large that it would not be possible to record the data. The amount of normalization that you choose would depend upon how often field data is repeated throughout the table.

Answered by: rayudu on: Oct 11th, 2011

normalizatilon is the process of organizing the table to minimize the redundency

Answered by: Steven on: Oct 4th, 2011

Normalizatiopn is the process of splitting tables up to reduce record redundancy. For instance if you have a table of addresses you may want to create a table of cities and link the addresses to the ...

Difference between a "where" clause and a "having" clause?

Asked By: Beena | Asked On: Sep 19th, 2005

Star Read Best Answer

Editorial / Best Answer

Answered by: Ankush Sharma

Answered On : Sep 13th, 2011

Though the HAVING clause specifies a condition that is similar to the purpose of a WHERE clause, the two clauses are not interchangeable. Listed below are some differences to help distinguish between the two:

1. The WHERE clause specifies the criteria which individual records must meet to be selcted by a query. It can be used without the GROUP BY clause. The HAVING clause cannot be used without the GROUP BY clause.
2. The WHERE clause selects rows before grouping. The HAVING clause selects rows after grouping.
3. The WHERE clause cannot contain aggregate functions. The HAVING clause can contain aggregate functions.

for Example: if for an "Select" statement we use the "where" clause then the the result based on the "where" condition results and then we can use "group by" clause to arrange in some order, Now if we want to impose the condition on that group then we use "having" clause.

The main reason for using WHERE clause is to select rows that are to be included in the query. For example, assume table Test.Suppose I want the names, account numbers, and balance due of all customers from California and Los Angles. Since STATE is one of the fields in the record format, I can use WHERE to select those customers.

Using the code

Code
  1. SELECT cusnum, lstnam, init
  2. FROM Test
  3. WHERE state IN ('CA', 'LA')
  4.  
  5. CUSNUM LSTNAM INIT BALDUE
  6. ====== ============ ==== ========
  7. 938472 John G K 37.00
  8. 938485 Mark J A 3987.50
  9. 593029 Lily E D 25.00

Suppose I want the total amount due from customers by state. In that case, I would need to use the GROUP BY clause to build an aggregate query.

Code
  1. SELECT state,SUM(baldue)
  2. FROM Test
  3. GROUP BY state
  4. ORDER BY state
  5.  
  6. State Sum(Baldue)
  7. ===== ===========
  8. CA 250.00
  9. CO 58.75
  10. GA 3987.50
  11. MN 510.00
  12. NY 589.50
  13. TX 62.00
  14. VT 439.00
  15. WY .00

Using Having

Code
  1. SELECT state,SUM(baldue)
  2. FROM Test
  3. GROUP BY state
  4. HAVING SUM(baldue) > 250
  5.  
  6.  
  7. State Sum(Baldue)
  8. ===== ===========
  9. GA 3987.50
  10. MN 510.00
  11. NY 589.50
  12. VT 439.00

Answered by: Ankush Sharma on: Sep 13th, 2011

Though the HAVING clause specifies a condition that is similar to the purpose of a WHERE clause, the two clauses are not interchangeable. Listed below are some differences to help distinguish between ...

Answered by: Ankush Sharma on: Sep 13th, 2011

Though the HAVING clause specifies a condition that is similar to the purpose of a WHERE clause, the two clauses are not interchangeable. Listed below are some differences to help distinguish between ...

First | Prev | | Next | Last Page

 

 

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Career Counselling

 Have Career Question?

 Ask Chandra

 Ask Only Career questions.

Follow us:
 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, click "Subscribe".