SQL (Structured Query Language)
SQL is the standardized language for relational databases. With it, you describe which data you want, and the database decides by itself how to fetch it. Microsoft's dialect is called Transact-SQL.
SQL (Structured Query Language) is the language with which you query and change data in relational databases and describe their structure. It is declarative: you write which result you want, not the steps in which it is computed.
SQL for .NET and Azure developers
SQL rests on the international standard ISO/IEC 9075, but every database speaks its own dialect. Microsoft's dialect is called Transact-SQL (T-SQL). According to Microsoft's own documentation, it supports and extends the ANSI SQL standard. T-SQL runs on SQL Server, in Azure SQL Database, and in Azure SQL Managed Instance. Whoever copies an example from the web therefore checks which dialect it was written for.
A typical query:
SELECT c.Name, COUNT(a.Id) AS Appointments
FROM Companies AS c
LEFT JOIN Appointments AS a ON a.CompanyId = c.Id
GROUP BY c.Name
ORDER BY Appointments DESC;
The LEFT JOIN also keeps companies without an appointment, and COUNT(a.Id) counts only appointments that exist. With an INNER JOIN, companies without an appointment would be missing. Whoever does not keep this in mind reads wrong numbers.
SQL and C#
In .NET, you rarely write SQL by hand. EF Core translates LINQ queries into SQL and sends the values along as parameters. Whoever wants to understand what a query costs reads the generated SQL, for example with ToQueryString():
var query = db.Contacts
.Where(c => c.Email == null)
.OrderBy(c => c.Name);
Console.WriteLine(query.ToQueryString());
Without knowing SQL, this remains flying blind: whether an index is missing, or whether a query runs once per row instead of once in total, is only shown by the SQL and the execution plan.
Common misconceptions
- SQL is not a database. SQL is the language; SQL Server or PostgreSQL are databases that speak it. The term database is the larger one.
NULLis not a value but "unknown".WHERE x = NULLfinds nothing; you writeIS NULL.NOT INwith a list that containsNULLalso returns, surprisingly, nothing.- Never paste input into the command text. Values belong in parameters; otherwise SQL injection threatens.
- LINQ does not replace SQL. It describes the same way of thinking, and not every expression can be translated into SQL.
An example from the Kladde
The Kladde stores contacts, companies, and appointments first in a JSON file, later in SQL Server, and in Azure as Azure SQL. There, the value of constraints shows: the Kladde stores the start and end of an appointment and additionally protects the rule "the end is after the start" with a CHECK constraint. The database then rejects rows that violate it, even if an error in the C# code let them through.
In the course
You learn SQL in K2, module 2.4 (data), in three steps: SQL Server in a container, SQL: querying, and SQL: changing data, data types, constraints. Indexes, EF Core, and migrations follow. In K3, module 3.6, Azure SQL comes in as a service.