Part 4: Your First SELECT Query
Microsoft SQL Server Tutorial Series: Beginner to Expert
Welcome to Part 4 of our SQL Server tutorial series! Now that you've created your first database and table, it’s time to learn how to retrieve data using the SELECT statement.
In this tutorial, you’ll learn how to:
- Use the
SELECT
command to retrieve data - Select specific columns or all data from a table
- Filter records with
WHERE
- Sort data using
ORDER BY
- Use
TOP
andDISTINCT
keywords
๐ Using the SSMS Query Window
Open SQL Server Management Studio (SSMS):
- Connect to your local server
- Click New Query
- Select the
SchoolDB
database using the dropdown or write:
USE SchoolDB;
GO
๐ SELECT * FROM Table
This is the simplest query to view all data from a table:
SELECT * FROM Students;
GO
๐ก Note: *
selects all columns, but it's better to specify only what you need.
๐ Selecting Specific Columns
To view only first names and last names:
SELECT FirstName, LastName FROM Students;
GO
๐ฏ Filtering Records with WHERE
Use WHERE
to filter specific rows. Example:
SELECT * FROM Students
WHERE IsActive = 1;
GO
This shows only students who are active.
๐ข Sorting Results with ORDER BY
To order students by their birth date:
SELECT * FROM Students
ORDER BY BirthDate ASC;
GO
Use DESC
for descending order.
๐ Using TOP to Limit Rows
To return just the first 3 rows:
SELECT TOP 3 * FROM Students;
GO
๐ Eliminating Duplicates with DISTINCT
If multiple students share the same first name:
SELECT DISTINCT FirstName FROM Students;
GO
๐งพ Quick SQL SELECT Cheat Sheet
-- Select all columns SELECT * FROM TableName; -- Select specific columns SELECT Column1, Column2 FROM TableName; -- Filter rows SELECT * FROM TableName WHERE condition; -- Sort rows SELECT * FROM TableName ORDER BY Column ASC|DESC; -- Return top N rows SELECT TOP N * FROM TableName; -- Remove duplicates SELECT DISTINCT Column FROM TableName;
๐ Real-World Example
Imagine you're a school administrator and want to find all active students born after 2010:
SELECT FirstName, LastName, BirthDate
FROM Students
WHERE IsActive = 1 AND BirthDate > '2010-01-01'
ORDER BY BirthDate;
GO
๐ผ️ Visual Result Example
+-----------+----------+------------+ | FirstName | LastName | BirthDate | +-----------+----------+------------+ | Alice | Johnson | 2012-03-15 | | Brian | Smith | 2011-08-22 | +-----------+----------+------------+
๐ Best Practices for SELECT Queries
Tip | Recommendation |
---|---|
Use Column Names | Avoid SELECT * for performance and clarity |
Alias Columns | Use AS to rename for readability: FirstName AS Name |
Indent and Format | Make code readable, especially in long queries |
Use Comments | Start comments with -- to describe your code |
✅ Summary
In this lesson, you learned:
- How to retrieve data using the
SELECT
statement - Filtering with
WHERE
and sorting withORDER BY
- Using
TOP
andDISTINCT
for cleaner output - Best practices for writing readable SQL queries
๐ Navigation
Have a question? Drop it in the comments — we’d love to help!