SQL Practice Logo

SQLPractice Online

SQL Server Features Deep Dive: Concept

Module: Database-Specific Features

SQL Server is Microsoft enterprise database with T-SQL language and rich features. Key features: (1) T-SQL: Extensions to SQL (variables, control flow, stored procedures). (2) Window functions: ROW_NUMBER(), RANK() for pagination and ranking. (3) Columnstore: Columnar storage for analytics (10x faster). (4) Temporal tables: System-versioned tables track history automatically. (5) Always On: High availability with automatic failover. Real-world: Microsoft Dynamics uses T-SQL. Stack Overflow uses window functions. Power BI uses columnstore.

**1. T-SQL Syntax Differences:**

**TOP instead of LIMIT:**

```sql

-- SQL Server: TOP

SELECT TOP 10 * FROM orders

ORDER BY order_date DESC;

-- MySQL/PostgreSQL: LIMIT

-- SELECT * FROM orders ORDER BY order_date DESC LIMIT 10;

```

**IDENTITY instead of AUTO_INCREMENT:**

```sql

-- SQL Server: IDENTITY

CREATE TABLE users (

user_id INT IDENTITY(1,1) PRIMARY KEY,

username VARCHAR(50)

);

-- MySQL: AUTO_INCREMENT

-- user_id INT AUTO_INCREMENT PRIMARY KEY

```

**Variables with @:**

```sql

-- Declare and set variables

DECLARE @user_count INT;

SET @user_count = (SELECT COUNT(*) FROM users);

SELECT @user_count AS total_users;

-- Multiple variables

DECLARE @min_price DECIMAL(10,2), @max_price DECIMAL(10,2);

SELECT @min_price = MIN(price), @max_price = MAX(price)

FROM products;

```

**Control Flow:**

```sql

-- IF statement

DECLARE @count INT = 10;

IF @count > 5

BEGIN

SELECT 'Greater than 5';

END

ELSE