Microsoft SQL Server Basic Queries
Introduction to T-SQL
What Is T-SQL?
T-SQL, or Transact-SQL, is the version of SQL used by Microsoft SQL Server. Think of standard SQL as a basic language that all databases understand. T-SQL speaks that language but adds its own powerful vocabulary and grammar. While you can use standard SQL commands, T-SQL provides extra features for things like declaring variables, handling errors, and managing transactions, which gives developers more control.
Essentially, if you're working with a Microsoft SQL Server database, you'll be writing T-SQL.
Basic Statement Structure
A T-SQL query is a set of instructions sent to the database. The most fundamental instruction is retrieving data, which is done with the SELECT statement. The basic structure is straightforward: you say what you want to SELECT and FROM where.
SELECT ColumnName1, ColumnName2
FROM TableName;
Let's break that down:
SELECTlists the columns you want to see.FROMspecifies the table where those columns live.
While T-SQL is not case-sensitive, it's common practice to write keywords like SELECT and FROM in uppercase to make queries easier to read. Every statement should end with a semicolon (;). Although it's sometimes optional, it's a best practice that clearly marks the end of a command and prevents errors.
Imagine we have a table named Employees. To get a list of just the first and last names of all employees, our query would look like this:
SELECT FirstName, LastName
FROM Employees;
If you want to see every single column in the table, you can use an asterisk (*) as a shortcut.
SELECT *
FROM Employees;
Using * is great for exploring a new table, but for performance, it's always better to specify the exact columns you need. Asking for only what you need reduces the amount of data the database has to process.
Filtering Data with WHERE
Getting all the data from a table is rarely what you want. Usually, you need to find rows that meet certain criteria. This is where the WHERE clause comes in. It acts as a filter, telling the database to only return rows that match a specific condition.
The
WHEREclause always comes after theFROMclause.
Let's say we only want to see employees who work in the Sales department. We can add a WHERE clause to our query to filter the results.
SELECT FirstName, LastName, Department
FROM Employees
WHERE Department = 'Sales';
Notice that 'Sales' is wrapped in single quotes. This is required for text values (also called strings). For numbers, you don't need quotes.
For example, to find all products that cost less than $50, you could write:
SELECT ProductName, Price
FROM Products
WHERE Price < 50;
The WHERE clause can use various comparison operators, such as = (equal to), <> (not equal to), < (less than), and > (greater than). Mastering the combination of SELECT, FROM, and WHERE is the first major step in querying databases with T-SQL.
What is the primary characteristic of T-SQL?
Which query correctly selects the ProductName and Price from a table named Products?
These building blocks are the foundation for nearly every query you'll write. Next, we'll explore how to combine conditions and sort your results.