Questions tagged [sql-server]

Microsoft SQL Server is a relational database management system (RDBMS). Use this tag for all Microsoft SQL Server editions including Compact, Express, Azure, Fast-track, APS (formerly PDW) and Azure SQL DW. Do not use this tag for other types of DBMS (MySQL, PostgreSQL, Oracle, etc.). Do not use this tag for issues on software and mobile development, unless it is directly related to the database.

How to write a good SQL Server question

There are six ingredients to this recipe:

  1. Provide a clear textual explanation of the problem at hand.
  2. Provide proper sample data, as DDL (Create table statement(s)) and DML (Insert statement(s)). The sample data should be enough to demonstrate the problem, including edge cases. Usually, a few relevant rows are enough. (You can use SQLFiddle or db-fiddle as an online tool to generate DDL and DML statements for you from tabular data using their Text To DDL button.)
  3. Provide expected output for the sample data you've posted.
  4. Show your attempts to solve the problem.
  5. Tag properly. In addition to , also provide the tag for the lowest version you need the solution for. (I.e., if you're developing on SQL Server 2016, but the production server is SQL Server 2012, use .)
  6. Do not include images of data or code!

A link to an online SQL test environment such as SQL Fiddle or DB Fiddle might help, but it is not a replacement for having all the data inside the question.

For more information, read Why should I provide an MCVE for what seems to me to be a very simple SQL query? and Help me write this query in SQL.

Questions about performance tuning should include, along with all of the above, the execution plan of the query. (How do I obtain a Query Execution Plan?). You can paste the execution plan on Brent Ozar's Paste The Plan and share a link to it in the question.

General Information:

Microsoft's SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality. It originated from the Sybase SQL Server 4.x codebase and Transact-SQL dialect (), but it has forked significantly since then.

SQL Server is available in multiple versions (typically identified by release year), each of which are subdivided into editions to distinguish between product functionality. The latest released version is SQL Server 2019 which was released on November 4th, 2019.

The SQL Server product range is split broadly into six categories:

  1. SQL Server () is the main suite of enterprise and developer server products. The primary differences are licensing costs, capacities, and components included in the product, with some minor differences supported language features. Standard components include database language and storage server, developer tools, ETL tools (), schedulers, and replication. Other components include OLAP (), reporting (), and parallel computation. Components runs as NT Services.

  2. SQL Server Express () is free for use and distribution, but it has reduced engine performance, functionality and capacity than found in its other server siblings. It is focused on small deployments and runs as an NT service.

  3. SQL Server Compact Edition () is an embeddable subset of SQL Server. Like the Express edition it has a reduced language, functionality and capacity, but it is free to distribute. It's focused on small installations and desktop applications where its small footprint and no-management-required features are a great advantage.

Note: SQL Server Compact Edition is deprecated. Customers should use SQL Server Express and possibly LocalDB).

  1. Azure SQL Database () is a completely managed, hosted, high-availability product with similar features to SQL Server, operated in Microsoft Azure data centers. Notable differences are language syntax support for federated queries, and lack of support for CLR integration.

  2. SQL Server Analytics Platform System (or APS), formerly known as SQL Server Parallel Data Warehouse (PDW), is a pre-built data warehouse appliance that offers massively parallel processing for SQL Server, allowing support for many hundreds of terabytes.

  3. Azure SQL Data Warehouse is an enterprise-class distributed database in the Azure Cloud capable of processing up to petabyte volumes of relational and non-relational data. It is the industry's first cloud data warehouse with grow, shrink, and pause in seconds.

Microsoft offers a comparison table of the different SQL Server editions.

SQL Server Release History

|   Version   | Year |       Release Name        |       Codename       |
| 15.0        | 2019 | SQL Server 2019           | Seattle              |
| 14.0        | 2017 | SQL Server 2017           | Helsinki             |
| 13.0        | 2016 | SQL Server 2016           | SQL16                |
| 12.0        | 2014 | SQL Server 2014           | Hekaton              |
| 11.0        | 2012 | SQL Server 2012           | Denali               |
| 10.5        | 2010 | SQL Server 2008 R2        | Kilimanjaro (aka KJ) |
| 10.25       | 2010 | SQL Azure DB              | CloudDatabase        |
| 10.0        | 2008 | SQL Server 2008           | Katmai               |
| 9.0         | 2005 | SQL Server 2005           | Yukon                |
| 8.0         | 2003 | SQL Server 2000 x64       | Liberty              |
| 8.0         | 2000 | SQL Server 2000           | Shiloh               |
| -           | 1999 | SQL Server 7.0 OLAP Tools | Palato mania         |
| 7.0         | 1998 | SQL Server 7.0            | Sphinx               |
| 6.5         | 1996 | SQL Server 6.5            | Hydra                |
| 6.0         | 1995 | SQL Server 6.0            | SQL95                |
| 4.21(WinNT) | 1993 | SQL Server 4.21           | SQLNT                |
| 1.1 (OS/2)  | 1991 | SQL Server 1.1            | - (16-bit)           |
| 1.0 (OS/2)  | 1989 | SQL Server 1.0            | Ashton-Tate (16-bit) |


Tagging Recommendation

There are several version- and edition-specific tags. It is recommended to use the tag together with the version- and/or edition-specific tag; for example, or . Do not use this tag for other types of DBMS (, , , etc.).

331896 questions
38 answers

How do I UPDATE from a SELECT in SQL Server?

In SQL Server, it is possible to insert rows into a table with an INSERT.. SELECT statement: INSERT INTO Table (col1, col2, col3) SELECT col1, col2, col3 FROM other_table WHERE sql = 'cool' Is it also possible to update a table with SELECT? I…
  • 44,484
  • 11
  • 36
  • 49
44 answers

Add a column with a default value to an existing table in SQL Server

How can I add a column with a default value to an existing table in SQL Server 2000 / SQL Server 2005?
  • 33,351
  • 7
  • 26
  • 33
47 answers

How to concatenate text from multiple rows into a single text string in SQL Server

Consider a database table holding names, with three rows: Peter Paul Mary Is there an easy way to turn this into a single string of Peter, Paul, Mary?
  • 28,758
  • 10
  • 38
  • 37
32 answers

How to check if a column exists in a SQL Server table

I need to add a specific column if it does not exist. I have something like the following, but it always returns false: IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'myTableName' AND…
  • 21,919
  • 3
  • 20
  • 23
13 answers


What is the difference between LEFT JOIN and LEFT OUTER JOIN?
KG Sosa
  • 21,565
  • 6
  • 26
  • 27
4 answers

Inserting multiple rows in a single SQL query?

I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and Office. INSERT INTO MyTable VALUES ("John", 123, "Lloyds Office"); INSERT INTO MyTable VALUES ("Jane", 124, "Lloyds Office"); INSERT INTO MyTable…
34 answers

Insert results of a stored procedure into a temporary table

How do I do a SELECT * INTO [temp table] FROM [stored procedure]? Not FROM [Table] and without defining [temp table]? Select all data from BusinessLine into tmpBusLine works fine. select * into tmpBusLine from BusinessLine I am trying the same, but…
  • 21,332
  • 6
  • 29
  • 31
41 answers

Table Naming Dilemma: Singular vs. Plural Names

Academia has it that table names should be the singular of the entity that they store attributes of. I dislike any T-SQL that requires square brackets around names, but I have renamed a Users table to the singular, forever sentencing those using…
  • 49,207
  • 121
  • 399
  • 775
29 answers

How do I perform an IF...THEN in an SQL SELECT?

How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
Eric Labashosky
  • 29,484
  • 14
  • 39
  • 32
14 answers

How can I delete using INNER JOIN with SQL Server?

I want to delete using INNER JOIN in SQL Server 2008. But I get this error: Msg 156, Level 15, State 1, Line 15 Incorrect syntax near the keyword 'INNER'. My code: DELETE FROM WorkRecord2 INNER JOIN Employee ON…
  • 17,733
  • 7
  • 30
  • 45
36 answers

Find all tables containing column with specified name - MS SQL Server

Is it possible to query for table names which contain columns being LIKE '%myName%' ?
  • 28,739
  • 35
  • 124
  • 216
21 answers

What is the difference between varchar and nvarchar?

Is it just that nvarchar supports multibyte characters? If that is the case, is there really any point, other than storage concerns, to using varchars?
  • 42,945
  • 30
  • 96
  • 149
28 answers

Get size of all tables in database

I have inherited a fairly large SQL Server database. It seems to take up more space than I would expect, given the data it contains. Is there an easy way to determine how much space on disk each table is consuming?
  • 18,512
  • 4
  • 29
  • 34
18 answers

How can I do an UPDATE statement with JOIN in SQL Server?

I need to update this table in SQL Server with data from its 'parent' table, see below: Table: sale id (int) udid (int) assid (int) Table: ud id (int) assid (int) sale.assid contains the correct value to update ud.assid. What query will do…
Ant Swift
  • 20,089
  • 10
  • 38
  • 55
30 answers

Check if table exists in SQL Server

I would like this to be the ultimate discussion on how to check if a table exists in SQL Server 2000/2005 using SQL Statements. Here are two possible ways of doing it. Which one is the standard/best way of doing it? First way: IF EXISTS (SELECT 1 …
  • 22,366
  • 18
  • 58
  • 61
2 3
99 100