Answered by: Parished.D Chennai India
Answered On : Apr 24th, 2007create 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
If we want to check any values to retrieve the data,case is the best.Case statement is simple and easy to understand.
Code
SELECT case when sal>1000 then ename end FROM emp
Using Group By
Code
FROM Employees GROUP BY eid,ename,salary HAVING salary>15000
Explain normalization and denormalization with examples?
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. ...
Your Explnation may be good but it was too dificult to understand with out examples
How to find out duplicate records in SQL server?
Answered by: Hanif
Answered On : Apr 12th, 2006we 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
SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
Code
SELECT EMAIL, COUNT(*) "REPETED EMAIL" FROM EMP GROUP BY EMAIL;
What is describe command in SQL server?
What is its purpose?How to use it?
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
If you would like to see more details, Create your custom procedureCode
sp_help 'TABLE_NAME';
Here is another alternative way to get the same informationCode
CREATE procedure DESCRIBE ( @tablename varchar(256) ) AS begin SELECT DISTINCT sCols.colid AS 'order', sCols.name, sTyps.name, sCols.length FROM [syscolumns] sCols INNER JOIN [systypes] sTyps ON sCols.xtype = sTyps.xtype INNER JOIN [sysobjects] sObjs ON sObjs.id = sCols.[id] AND UPPER(sObjs.name) = UPPER(@tablename) ORDER BY sCols.colid end
Here is the usage :Code
sp_columns 'TableName' (e.g. sp_columns 'Employee') sp_columns [ @table_name = ] object [ , [ @table_owner = ] owner ] [ , [ @table_qualifier = ] qualifier ] [ , [ @column_name = ] COLUMN ] [ , [ @ODBCVer = ] ODBCVer ]
Contributors for the editorial answer : Kewlshiva, srilakshmi.b, raaghav,kevaburgCode
EXEC sp_columns @table_name = 'Department', @table_owner = 'sa'; SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TableName'
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
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
How to configure ssis package in SQL server 2008 r2?
Can we replace where clause by having clause. ?
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
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 if yes in what scenarios? if your view is joining multiple tables
Yes but you remember views, represents a virtual table but if you delete the table you wont be able to update the view
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?
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 ?
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?
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
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
How do represent objects in SQL server? Is it possible? If yes, how?If not? Why?
Try using Server Management Objects (SMO) classes
List down the advantage and disadvantages of covering indexes.
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
How do you run PL/SQL block from SQL server
Try these and see...
Go To Options -> Run SQL
Set "Statement Delimiter" to None
Go To Options -> Technical Parameters
Tick "Use SQLExecDirect"
What is high water mark? Give an example where it can be useful ?
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?
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 ?
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
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?
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
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...
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
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 ?
Answered by: Steven
Answered On : Oct 4th, 2011Normalizatiopn 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.
normalizatilon is the process of organizing the table to minimize the redundency
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?
Answered by: Ankush Sharma
Answered On : Sep 13th, 2011Though 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
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
SELECT cusnum, lstnam, init FROM Test WHERE state IN ('CA', 'LA') CUSNUM LSTNAM INIT BALDUE ====== ============ ==== ======== 938472 John G K 37.00 938485 Mark J A 3987.50 593029 Lily E D 25.00
Using HavingCode
SELECT state,SUM(baldue) FROM Test GROUP BY state ORDER BY state State Sum(Baldue) ===== =========== CA 250.00 CO 58.75 GA 3987.50 MN 510.00 NY 589.50 TX 62.00 VT 439.00 WY .00
Code
SELECT state,SUM(baldue) FROM Test GROUP BY state HAVING SUM(baldue) > 250 State Sum(Baldue) ===== =========== GA 3987.50 MN 510.00 NY 589.50 VT 439.00
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 ...
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 ...
SELECT ID FROM TBLSAMPLE GROUP BY ID OR SELECT DISTINCT (ID) FROM TBLSAMPLE
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.