Reading data with SELECT
SELECT and FROM
Choose columns from a table and read the resulting rows.
Lesson 6 of 84
Reading: about 3 min
Lesson + practice: about 10 min
Difficulty: Beginner
On this page4 sections
This lesson uses DDI_SQL_Training dataset version 1.0.0 on SQL Server 2019 or later. Imagine you need a customer list for a report. The Customers table contains six columns, but the report only needs an ID, a first name, and a city. SELECT lets you ask for exactly those columns.
Run the example
SELECT CustomerID, FirstName, City
FROM dbo.Customers;Read it step by step
- After SELECT, the column names form the output list. Separate them with commas; their order controls the column order in the result.
- FROM dbo.Customers tells SQL Server which table supplies those columns. dbo is the schema name, and Customers is the table name.
- There is no WHERE clause, so all 20 customer rows qualify.
- There is no ORDER BY clause, so SQL Server does not guarantee the order in which those rows appear.
- The semicolon marks the end of the statement. SQL Server accepts this simple SELECT without it, but using a semicolon is recommended; some T-SQL constructs require one.
Check the result
| CustomerID | FirstName | City |
|---|---|---|
| 4 | Isla | London |
| 11 | Omar | Leicester |
| 17 | Kai | Manchester |
The query returns 20 rows and three columns. Each result row represents one customer. The table shows selected customers 4, 11, and 17, not the first three rows. Your results may appear in another order. Compare the result headings with the column names after SELECT.
Practice
Write a query that returns ProductID and ProductName from dbo.Products. How many rows does the unmodified v1.0.0 dataset return?
Check your answer and expected result
Reveal answer
SELECT ProductID, ProductName
FROM dbo.Products;| ProductID | ProductName |
|---|---|
| 1 | Laptop Stand |
| 2 | USB-C Dock |
| 3 | Webcam |
| 4 | Notebook |
| 5 | Pen Set |
| 6 | Desk Lamp |
| 7 | Headphones |
| 8 | Speaker |
| 9 | Microphone |
| 10 | Wireless Mouse |
| 11 | Keyboard |
| 12 | Cable Pack |
| 13 | Desk Chair |
| 14 | Side Table |
| 15 | Shelf Unit |
The unmodified v1.0.0 dataset returns 15 rows. The table is arranged by ProductID for comparison; the query itself does not request an order.
Your progress
Reveal the check answer or reach the end to complete this lesson.