SQL Server · Querying

SELECT

SELECT is the statement you will write more than any other. It reads data and returns it as a result set.

Beginner 5 min Lesson 5 of 31Examples run on SQL Server 2022 and Azure SQL Database.
0/31 in SQL Server
On this page · 5 min · Beginner

In simple words#

SELECT is asking the office clerk: “Show me the name and phone columns from the Students register.” You choose which columns to see.

SELECT
Choose columns to read.
FROM
Which table to read from.
Alias (AS)
A temporary nickname for a column or table.

Learning objectives#

  • Select specific columns
  • Rename columns with aliases
  • Return unique values with DISTINCT
  • Limit rows with TOP

Explanation#

SELECT lists the columns (or expressions) you want; FROM says which table to read.

AS gives a column a friendly name in the result — useful for calculated values.

DISTINCT removes duplicate rows from the result. TOP (n) returns only the first n rows — always pair it with ORDER BY, otherwise “first” is not defined.

Syntax#

SQL Illustrative
SELECT [DISTINCT] [TOP (n)] column1, expression AS Alias
FROM TableName
[ORDER BY column];

Code example: The three most expensive products#

SQLThe three most expensive products Illustrative
SELECT TOP (3) Name AS Product, Category, Price
FROM Products
ORDER BY Price DESC;


SELECT DISTINCT Category
FROM Products
ORDER BY Category;
Expected output
Product  Category     Price
Laptop   Electronics  55000.00
Desk     Furniture    12000.00
Chair    Furniture    4500.00

Category
Electronics
Furniture
Stationery

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The first query sorts all products by price, highest first, and keeps the top three.
  2. Name AS Product renames the column in the output only — the table is unchanged.
  3. The second query returns each category once, even though each appears twice in the table.

Real-world use case#

“Top 10 best-selling products” on a shop homepage is a SELECT TOP with ORDER BY.

Common mistakes#

SELECT * in application code

It returns columns you do not need, breaks when columns are added, and prevents covering indexes. List the columns.

TOP without ORDER BY

Without ORDER BY, SQL Server may return any rows, and the result can change between runs.

Best practices#

  • Name the columns you need
  • Use clear aliases for calculated columns
  • Always use ORDER BY with TOP
  • Format queries one clause per line

Remember this#

SELECT columns FROM table.

Avoid SELECT * in applications.

AS gives a column a friendly name.

Practice exercise#

Your task: Return the two cheapest products with their price, using an alias Cost for Price.

Show solution
SQL Illustrative
SELECT TOP (2) Name, Price AS Cost FROM Products ORDER BY Price;  -- Pen 20.00, Notebook 60.00

Mini quiz#

SELECT quizMultiple choice Question 1 of 2

What does DISTINCT do?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.