Monday, October 6, 2025

⚙️ SQL vs T-SQL vs PostgreSQL — Practical Code Comparison & EF Core Integration

 

🧩 1. Side-by-Side Query Comparison

OperationSQL (Standard)T-SQL (SQL Server)PostgreSQL
Select Datasql SELECT * FROM Employees; sql SELECT * FROM Employees; sql SELECT * FROM Employees;
Insert Recordsql INSERT INTO Employees (Name, Dept) VALUES ('Ravi', 'IT'); sql INSERT INTO Employees (Name, Dept) VALUES ('Ravi', 'IT'); sql INSERT INTO Employees (Name, Dept) VALUES ('Ravi', 'IT');
Declare Variable❌ Not supportedsql DECLARE @Dept NVARCHAR(20) = 'HR'; SELECT * FROM Employees WHERE Dept = @Dept; sql DO $$ DECLARE dept TEXT := 'HR'; BEGIN SELECT * FROM Employees WHERE Dept = dept; END $$;
String Concatenation```sql SELECT FirstName' '
Current Datesql SELECT CURRENT_DATE; sql SELECT GETDATE(); sql SELECT NOW();
Top / Limitsql SELECT * FROM Employees FETCH FIRST 5 ROWS ONLY; sql SELECT TOP 5 * FROM Employees; sql SELECT * FROM Employees LIMIT 5;
Stored ProcedureLimited / Varies by DBsql CREATE PROCEDURE GetEmployees AS BEGIN SELECT * FROM Employees; END; sql CREATE FUNCTION GetEmployees() RETURNS TABLE(...) AS $$ SELECT * FROM Employees; $$ LANGUAGE sql;

Takeaway:

  • T-SQL adds variables, flow control, and procedural logic.

  • PostgreSQL has its own procedural language PL/pgSQL with advanced functions and JSON operations.

  • Standard SQL works across all, but lacks procedural capabilities.


🛠 2. Entity Framework Core Setup in .NET Core Web API

Now let’s see how to connect .NET Core Web API with each database using Entity Framework Core (EF Core).


🔹 A. SQL Server (T-SQL)

🧩 Install NuGet Package

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

⚙️ appsettings.json

{ "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=EmployeeDB;User Id=sa;Password=Your@123;" } }

⚙️ Program.cs

builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

🧱 Example Entity

public class Employee { public int Id { get; set; } public string Name { get; set; } public string Department { get; set; } }

✅ SQL Server uses T-SQL syntax internally for all queries EF Core generates.


🔹 B. PostgreSQL

🧩 Install NuGet Package

dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL

⚙️ appsettings.json

{ "ConnectionStrings": { "PostgresConnection": "Host=localhost;Database=EmployeeDB;Username=postgres;Password=admin123" } }

⚙️ Program.cs

builder.Services.AddDbContext<AppDbContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("PostgresConnection")));

✅ PostgreSQL queries generated by EF Core use PostgreSQL syntax, including features like LIMIT, RETURNING, and ILIKE.


🔹 C. Generic SQL (Database Independent Example)

If you’re writing SQL logic that can work on both SQL Server and PostgreSQL, use standard SQL commands in EF Core’s LINQ or raw SQL:

var employees = await _context.Employees .Where(e => e.Department == "Finance") .OrderByDescending(e => e.Id) .Take(5) .ToListAsync();

✅ EF Core translates this query automatically to T-SQL (if SQL Server) or PostgreSQL syntax — making your code database-independent.


⚡ 3. Real-Time Example: Hybrid Database Microservice

In real-world microservices:

  • OrderService (SQL Server / T-SQL) → Handles transactional operations

  • AnalyticsService (PostgreSQL) → Stores analytical data, JSON reports, or logs

Example Architecture:

Angular UI ↓ API Gateway (Ocelot) ↓ OrderService (.NET Core → EF Core → SQL Server) ↓ AnalyticsService (.NET Core → EF Core → PostgreSQL)

This architecture ensures high scalability and polyglot persistence, allowing each microservice to choose the most efficient database engine.


🧠 Interview Q&A (EF + DB Context)

Q1. How does EF Core handle different databases?
👉 EF Core uses different providers (SqlServer, Npgsql, MySql, etc.) that translate LINQ queries into DB-specific SQL.

Q2. Can I switch from SQL Server to PostgreSQL easily?
👉 Yes, if you write queries using LINQ and avoid DB-specific T-SQL code.

Q3. What’s the main difference between EF Core SQL Server and EF Core PostgreSQL providers?
👉 SQL Server uses TOP, GETDATE(), IDENTITY; PostgreSQL uses LIMIT, NOW(), and SERIAL.

Q4. Is PostgreSQL good for microservices?
👉 Absolutely. It supports JSON, schema evolution, and strong concurrency — ideal for microservices and cloud-native apps.


🏁 Conclusion

Use CaseDatabaseQuery TypeEF Core Provider
Enterprise apps on AzureSQL ServerT-SQLMicrosoft.EntityFrameworkCore.SqlServer
Open-source, cloud-based systemsPostgreSQLSQL / PLpgSQLNpgsql.EntityFrameworkCore.PostgreSQL
Database-agnostic logicAny RDBMSStandard SQLGeneric EF Core LINQ

SQL = The language
T-SQL = Microsoft’s dialect
PostgreSQL = The database engine

Mastering all three gives you the flexibility to design robust, cloud-ready microservices that run anywhere — from Azure to AWS to on-premises.

No comments:

Blog Archive

Don't Copy

Protected by Copyscape Online Plagiarism Checker

Pages