SQL Server for Beginners: SELECT, WHERE and ORDER BY Explained with Examples

2026-12-03来源 dev.toEnglish

This tutorial was originally published on biglandnetwork.com. It is part of the free SQL Server for Beginners series, with a 10-minute video for every lesson.

If you are new to databases, the first skill to learn is how to read data. In SQL Server that means three clauses: SELECT to choose columns, WHERE to filter rows and ORDER BY to sort. With only these you can already answer many real questions. This guide builds them step by step on a tiny table, and every result is shown so you can check your own.

Part of the SQL Server for Beginners series. This article is the written version of lesson 1 of the Bigland Network video course, with the same sample data. The results shown were worked out by hand from that data, so run every example on a test database, not on production data.

1. The table we will use

A database stores data in tables. A table has columns (the kinds of information) and rows (one record each). In this lesson we use a small table called Employees:

Id FirstName Department Salary
1 Amina Sales 4200
2 Youssef IT 5200
3 Salma Sales 3600
4 Karim HR 3100
5 Nadia IT 4800

To follow along, create the table in a test database using SQL Server Management Studio (SSMS) or Azure Data Studio:

CREATE TABLE Employees ( Id INT PRIMARY KEY, FirstName VARCHAR(50), Department VARCHAR(50), Salary INT ); INSERT INTO Employees VALUES (1,'Amina','Sales',4200),(2,'Youssef','IT',5200),(3,'Salma','Sales',3600), (4,'Karim','HR',3100),(5,'Nadia','IT',4800); 

2. SELECT: read data

SELECT chooses what to show and FROM says which table to read. The star means all columns:

SELECT * FROM Employees; 

This returns the whole table. It is fine for exploring, but in real work you should name the columns you need. It is faster, clearer, and it keeps working if somebody adds a column later.

SELECT FirstName, Department FROM Employees; 
FirstName Department
Amina Sales
Youssef IT
Salma Sales
Karim HR
Nadia IT

3. WHERE: filter rows

WHERE keeps only the rows that match a condition. Text values go inside single quotes:

SELECT FirstName, Department FROM Employees WHERE Department = 'Sales'; 
FirstName Department
Amina Sales
Salma Sales

For numbers, use comparison operators: =, <> (not equal), >, <, >=, <=. Numbers do not need quotes.

SELECT FirstName, Salary FROM Employees WHERE Salary > 4000; 
FirstName Salary
Amina 4200
Youssef 5200
Nadia 4800

4. Combine conditions with AND and OR

AND needs both conditions to be true. OR needs at least one. When you mix them, add parentheses so the meaning is clear.

SELECT FirstName, Department, Salary FROM Employees WHERE Department = 'IT' AND Salary > 5000; 
FirstName Department Salary
Youssef IT 5200
SELECT FirstName, Department FROM Employees WHERE Department = 'HR' OR Department = 'Sales'; 
FirstName Department
Amina Sales
Salma Sales
Karim HR

Other useful filters are IN ('Sales','HR'), BETWEEN 3000 AND 4000 and LIKE 'A%' (names that start with A).

5. ORDER BY: sort the results

Without ORDER BY SQL Server gives you no guaranteed order. Add it whenever the order matters. The default is ascending; add DESC for highest first.

SELECT FirstName, Salary FROM Employees ORDER BY Salary DESC; 
FirstName Salary
Youssef 5200
Nadia 4800
Amina 4200
Salma 3600
Karim 3100

6. Putting it all together

SELECT FirstName, Salary FROM Employees WHERE Department = 'IT' ORDER BY Salary DESC; 
FirstName Salary
Youssef 5200
Nadia 4800

Always write the clauses in this order: SELECT, FROM, WHERE, ORDER BY. (SQL Server reads them in a different order, which lesson 3 explains.)

7. Common mistakes

  • Forgetting the quotes around text: WHERE Department = Sales makes SQL Server look for a column named Sales.
  • Using double quotes for text. Use single quotes for values.
  • Writing = NULL to find empty values. Use IS NULL instead (lesson 8 explains NULL).
  • Relying on SELECT * in applications and reports.
  • Assuming a table comes back in a fixed order without ORDER BY.

8. Practice

  • Show the names of the employees in HR.
  • Show everyone who earns less than 4000, lowest salary first.
  • Show Sales employees who earn more than 4000.

Answers: SELECT FirstName FROM Employees WHERE Department = 'HR'; returns Karim. SELECT FirstName, Salary FROM Employees WHERE Salary < 4000 ORDER BY Salary; returns Karim (3100) then Salma (3600). The third returns Amina (4200).

Frequently asked questions

Do I need to install SQL Server to practise?

Yes, or use a cloud database. The free Developer or Express edition of SQL Server plus SQL Server Management Studio (SSMS) or Azure Data Studio is enough for every lesson of this series.

Is SQL case sensitive?

Keywords such as SELECT and WHERE are not case sensitive. Text values depend on the database collation, and the default collation is usually case insensitive, so ‘sales’ matches ‘Sales’.

What is the difference between SQL and T-SQL?

SQL is the standard language. T-SQL (Transact-SQL) is the version used by SQL Server, with extra features such as variables, TRY…CATCH and TOP.

What to learn next

Next, learn how to combine several tables with JOINs in Lesson 2: JOINs. For a quick reference of useful queries, see the SQL Server DBA Cheat Sheet.

The whole series

Sources and further reading

  • Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for every statement used here.
  • Microsoft Learn, SQL Server documentation, for SQL Server Management Studio (SSMS) and the sample concepts.

Watch the video lesson on YouTube: https://www.youtube.com/watch?v=PSoLHL9DJPA

Read the original article (always up to date): https://biglandnetwork.com/sql-server-select-where-order-by-beginners/

All the lessons: https://biglandnetwork.com/lessons/ · Subscribe on YouTube: https://www.youtube.com/@biglandnetwork

原文:https://www.a8q9.cn/en/article/sql-server-for-beginners-select-where-and-order-by-explained-with-examples-816f3b.html

相关文章