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.
Watch lesson 1 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.
Contents
- 1. The table we will use
- 2. SELECT: read data
- 3. WHERE: filter rows
- 4. AND and OR
- 5. ORDER BY
- 6. Putting it together
- 7. Common mistakes
- 8. Practice
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 = Salesmakes SQL Server look for a column named Sales. - Using double quotes for text. Use single quotes for values.
- Writing
= NULLto find empty values. UseIS NULLinstead (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
- Lesson 1: SELECT, WHERE and ORDER BY (this article)
- Lesson 2: JOINs
- Lesson 3: GROUP BY, HAVING and Subqueries
- Lesson 4: INSERT, UPDATE, DELETE and Transactions
- Lesson 5: CREATE TABLE, Data Types and Constraints
- Lesson 6: Indexes, Views and Stored Procedures
- Lesson 7: CTEs and Window Functions
- Lesson 8: Built-in Functions
- Lesson 9: Functions, Temp Tables and Table Variables
- Lesson 10: Triggers and Audit Tables
- Lesson 11: Security: Logins, Users and Roles
- Lesson 12: Backup, Restore and Recovery Models
- Lesson 13: Query Performance Tuning
- Lesson 14: SQL Server Agent Jobs and Maintenance
- Lesson 15: Encryption and Auditing Basics
- Lesson 16: Working with JSON
- Lesson 17: Temporal Tables and Row History
- Lesson 18: Partitioning and Large Tables
- lesson 19: Full Text Search
- lesson 20: Query Store and Slow Queries
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.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE
Search words inside text columns fast: create a full text catalog and index, use CONTAINS, word forms, phrases and rank the best matches with…
