SQL for .NET developers Beginner

Querying data with SELECT

SELECT is how you read data. This lesson covers choosing columns, filtering with WHERE, sorting with ORDER BY and limiting rows, explains the order in which SQL actually processes a query, and shows how to run a safe parameterized query from C#.

The order SQL Server processes a SELECT
  1. StartFROMWhich table (or joined tables) to read
  2. Step 2WHEREKeep only the rows that match the condition
  3. Step 3GROUP BY / HAVINGGroup rows and filter groups (when used)
  4. Step 4SELECTPick and calculate the columns to return
  5. Step 5ORDER BYSort the result
  6. EndTOP / OFFSETReturn only some of the rows

You write SELECT first, but it is evaluated after FROM and WHERE. That is why a column alias from SELECT cannot be used in WHERE.

The idea in plain words

A SELECT statement describes the result you want, not how to fetch it. You name the columns, the table, an optional condition and an optional sort order, and the database works out the fastest way to answer.

Example: everyday queries

SQL
-- Columns, filter and sort
SELECT Name, Email
FROM Customers
WHERE CreatedAt >= '2026-01-01'
ORDER BY Name;

-- The 5 most recent orders over 1000
SELECT TOP (5) OrderId, Total, OrderedAt
FROM Orders
WHERE Total > 1000
ORDER BY OrderedAt DESC;

-- Pattern, list and missing values
SELECT Name FROM Customers WHERE Name LIKE 'Sa%';
SELECT OrderId, Total FROM Orders WHERE CustomerId IN (1, 2, 3);
SELECT Name FROM Customers WHERE Phone IS NULL;   -- not = NULL

-- Paging: rows 21 to 30
SELECT OrderId, Total FROM Orders
ORDER BY OrderId
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;

From C#: always use parameters

Never build SQL by joining strings with user input; that allows SQL injection. Parameters send values separately from the SQL text. Most apps use EF Core instead, which parameterizes for you (see the EF Core course).

C#
await using var conn = new SqlConnection(connectionString);
await conn.OpenAsync();

var cmd = new SqlCommand(
    "SELECT Name, Email FROM Customers WHERE Email = @email", conn);
cmd.Parameters.AddWithValue("@email", emailFromForm);

await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
    Console.WriteLine($"{reader.GetString(0)} <{reader.GetString(1)}>");

Common mistakes

  • Using SELECT * in application code; list the columns you need.
  • Writing = NULL instead of IS NULL.
  • Expecting a stable order without ORDER BY. Without it, the order is not guaranteed.
  • Concatenating user input into SQL strings.

Interview question

What is SQL injection and how do you prevent it in .NET?

SQL injection happens when user input is concatenated into a SQL string, so crafted input can change the query, for example adding OR 1=1 or a DROP statement. Prevent it by always using parameterized queries (SqlParameter, or EF Core LINQ and FromSql with interpolated parameters), never building SQL from raw input, and running the app with a database account that has only the permissions it needs.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.