SELECT in T-SQL: an overview

A SELECT statement asks the database a question and gets back a table of answers; T-SQL is SQL Server's version of SQL.

Novice · T-SQL · SQL · Updated

After this lesson you can

  • Explain what a SELECT query does and what it returns

Key takeaways

  • SQL is the language of relational databases, and T-SQL is SQL Server's version of it.
  • A SELECT statement, or query, asks the database a question and gets back a result set: a table of rows and columns.
  • In SQL you describe what you want, not how to find it; the database works out how to search.
  • A SELECT only reads: the data in the tables stays exactly as it was.

SQL (Structured Query Language) is the language for working with relational databases. Every relational database understands it: SQL Server, MySQL, PostgreSQL, Oracle. Each adds a few features of its own. SQL Server's version is called T-SQL (Transact-SQL), and it's what you'll write in this course.

Asking questions

You get data out of a database with a SELECT statement. A SELECT is also called a query, because you're querying the database: asking it a question.

  • Which customers are in the UK?
  • What are our ten most expensive products?
  • Which orders were placed last week?

Each question becomes a SELECT statement. The database works out the answer and sends back a result set: a table of rows and columns, just like the tables it stores.

Describe what you want, not how to get it

In C#, you tell the computer how to do something, step by step: loop through the customers, check each one's country, add the matches to a list. SQL is different. You describe what you want, and the database works out how to find it:

SELECT CompanyName, City
FROM Customers
WHERE Country = 'UK';

Read it almost as English: select the company name and city, from the customers, where the country is the UK. There's no loop in sight: the database decides how to search, and it's very good at it.

Think of a report

It helps to think of each query as a small report. You choose which columns go in it, which rows, and in what order. The tables don't change: a SELECT only reads. You can run as many queries as you like, and the data stays exactly as it was.

Check yourself

1 question to try on the lesson page.

So what

SELECT is the statement you'll write most as a developer: every page that shows data, from a product list to an order history, starts with a query.

Key terms

Cheat sheets